From e41a17a21d8bcba0e453ecd6fc7386869b0a6b4c Mon Sep 17 00:00:00 2001 From: Tejash Date: Thu, 3 Nov 2022 14:51:14 +0530 Subject: [PATCH] removed shared id in async (transaction id) --- .../registry/helper/RegistryHelper.java | 5 +-- .../registry/helper/RegistryHelperTest.java | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java index 691aa25ad..38a9da9e5 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java @@ -238,7 +238,7 @@ private void sendNotificationToOwners(JsonNode inputJson, String operation, Stri } private String addEntity(JsonNode inputJson, String userId, String entityType, boolean skipSignature) throws Exception { - RecordIdentifier recordId = null; + RecordIdentifier recordId; try { logger.info("Add api: entity type: {} and shard propery: {}", entityType, shardManager.getShardProperty()); Shard shard = shardManager.getShard(inputJson.get(entityType).get(shardManager.getShardProperty())); @@ -246,10 +246,11 @@ private String addEntity(JsonNode inputJson, String userId, String entityType, b String resultId; if (asyncRequest.isEnabled()) { resultId = registryAsyncService.addEntity(shard, userId, inputJson, skipSignature); + recordId = new RecordIdentifier(null, resultId); } else { resultId = registryService.addEntity(shard, userId, inputJson, skipSignature); + recordId = new RecordIdentifier(shard.getShardLabel(), resultId); } - recordId = new RecordIdentifier(shard.getShardLabel(), resultId); watch.stop("RegistryController.addToExistingEntity"); logger.info("AddEntity,{}", recordId.toString()); } catch (Exception e) { diff --git a/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java b/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java index aba5041de..8b1295b53 100644 --- a/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java +++ b/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java @@ -714,4 +714,36 @@ public void shouldReturnFalseIfSignedDataIsNotRevoked() throws Exception { when(searchService.search(any())).thenReturn(searchResponse); assertFalse(registryHelper.checkIfCredentialIsRevoked("signedData")); } + + @Test + public void shouldNotContainShardIdInAsyncMode() throws Exception { + JsonNode inviteJson = new ObjectMapper().readTree("{\"Institute\":{\"email\":\"gecasu.ihises@tovinit.com\",\"instituteName\":\"gecasu\"}}"); + Shard shard = mock(Shard.class); + when(shard.getShardLabel()).thenReturn("1"); + when(shardManager.getShard(any())).thenReturn(shard); + + when(registryService.addEntity(any(), any(), any(), anyBoolean())).thenReturn(UUID.randomUUID().toString()); + when(registryAsyncService.addEntity(any(), any(), any(), anyBoolean())).thenReturn(UUID.randomUUID().toString()); + when(asyncRequest.isEnabled()).thenReturn(Boolean.TRUE); + String entity = registryHelper.addEntity(inviteJson, ""); + verify(registryService, never()).addEntity(any(), anyString(), any(), anyBoolean()); + verify(registryAsyncService, atLeastOnce()).addEntity(any(), anyString(), any(), anyBoolean()); + assertFalse(entity.startsWith("1-")); + } + + @Test + public void shouldContainShardIdInSyncMode() throws Exception { + JsonNode inviteJson = new ObjectMapper().readTree("{\"Institute\":{\"email\":\"gecasu.ihises@tovinit.com\",\"instituteName\":\"gecasu\"}}"); + Shard shard = mock(Shard.class); + when(shard.getShardLabel()).thenReturn("1"); + when(shardManager.getShard(any())).thenReturn(shard); + + when(registryService.addEntity(any(), any(), any(), anyBoolean())).thenReturn(UUID.randomUUID().toString()); + when(registryAsyncService.addEntity(any(), any(), any(), anyBoolean())).thenReturn(UUID.randomUUID().toString()); + when(asyncRequest.isEnabled()).thenReturn(Boolean.FALSE); + String entity = registryHelper.addEntity(inviteJson, ""); + verify(registryService, atLeastOnce()).addEntity(any(), anyString(), any(), anyBoolean()); + verify(registryAsyncService, never()).addEntity(any(), anyString(), any(), anyBoolean()); + assertTrue(entity.startsWith("1-")); + } } \ No newline at end of file