diff --git a/registration-processor/core-processor/registration-processor-uin-generator-stage/src/main/java/io/mosip/registration/processor/stages/uingenerator/stage/UinGeneratorStage.java b/registration-processor/core-processor/registration-processor-uin-generator-stage/src/main/java/io/mosip/registration/processor/stages/uingenerator/stage/UinGeneratorStage.java index e35a66c5354..4dcb76ee57c 100644 --- a/registration-processor/core-processor/registration-processor-uin-generator-stage/src/main/java/io/mosip/registration/processor/stages/uingenerator/stage/UinGeneratorStage.java +++ b/registration-processor/core-processor/registration-processor-uin-generator-stage/src/main/java/io/mosip/registration/processor/stages/uingenerator/stage/UinGeneratorStage.java @@ -164,6 +164,9 @@ public class UinGeneratorStage extends MosipVerticleAPIManager { @Value("${mosip.regproc.uin.generator.idrepo-max-retry-count}") Integer maxRetrycount; + @Value("${mosip.regproc.uin.generator.trim-whitespaces.simpleType-value:false}") + private boolean trimWhitespaces; + /** The core audit request builder. */ @Autowired private AuditLogRequestBuilder auditLogRequestBuilder; @@ -471,6 +474,9 @@ else if (json instanceof JSONArray) { for (int i = 0; i < jsonArray.length(); i++) { Object obj = jsonArray.get(i); HashMap hashMap = new ObjectMapper().readValue(obj.toString(), HashMap.class); + if(trimWhitespaces && hashMap.get("value") instanceof String) { + hashMap.put("value",((String)hashMap.get("value")).trim()); + } jsonList.add(hashMap); } demographicIdentity.putIfAbsent(e.getKey(), jsonList); diff --git a/registration-processor/core-processor/registration-processor-uin-generator-stage/src/test/java/io/mosip/registration/processor/stages/uigenerator/UinGeneratorStageTest.java b/registration-processor/core-processor/registration-processor-uin-generator-stage/src/test/java/io/mosip/registration/processor/stages/uigenerator/UinGeneratorStageTest.java index b8a655d8ec9..3a88b5a3f24 100644 --- a/registration-processor/core-processor/registration-processor-uin-generator-stage/src/test/java/io/mosip/registration/processor/stages/uigenerator/UinGeneratorStageTest.java +++ b/registration-processor/core-processor/registration-processor-uin-generator-stage/src/test/java/io/mosip/registration/processor/stages/uigenerator/UinGeneratorStageTest.java @@ -1,19 +1,13 @@ package io.mosip.registration.processor.stages.uigenerator; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; @@ -25,9 +19,14 @@ import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.google.gson.JsonObject; import io.mosip.registration.processor.core.status.util.StatusUtil; +import io.mosip.registration.processor.stages.uingenerator.idrepo.dto.IdRequestDto; import org.apache.commons.io.IOUtils; import org.assertj.core.util.Lists; +import org.json.JSONArray; import org.json.JSONException; import org.json.simple.JSONObject; import org.junit.Before; @@ -407,7 +406,7 @@ public void testUinGenerationResponseNull() throws Exception { IdResponseDTO idResponseDTO = new IdResponseDTO(); ResponseDTO responseDTO = new ResponseDTO(); - + ResponseWrapper responseVid = new ResponseWrapper(); List errors = new ArrayList<>(); responseVid.setErrors(errors); @@ -524,7 +523,7 @@ public void testUinGenerationSuccessWithoutUINAndUinUnused() throws Exception { assertFalse(result.getInternalError()); } - + @Test public void testUinReActivationifAlreadyActivatedSuccess() throws Exception { @@ -576,7 +575,7 @@ public void testUinReActivationifAlreadyActivatedSuccess() throws Exception { assertFalse(result.getIsValid()); } - + @Test public void testUinReActivationResponseStatusAsActivated() throws Exception { Map fieldMap = new HashMap<>(); @@ -765,7 +764,7 @@ public void testUinReActivationWithStatusAsAny() throws Exception { } - + @Test public void testUinReActivationIfNotActivatedSuccess() throws Exception { @@ -1077,7 +1076,7 @@ public void deactivateTestSuccess() throws ApisResourceAccessException, IOExcept MessageDTO result = uinGeneratorStage.process(messageDTO); assertTrue(result.getIsValid()); } - + @Test public void checkIsUinDeactivatedSuccess() throws ApisResourceAccessException, IOException, JSONException, JsonProcessingException, PacketManagerException { @@ -1170,7 +1169,7 @@ public void deactivateTestWithDeactivate() throws ApisResourceAccessException, I MessageDTO result = uinGeneratorStage.process(messageDTO); //assertTrue(result.getIsValid()); } - + @Test public void deactivateTestWithNullResponseDTO() throws ApisResourceAccessException, PacketManagerException, IOException, JsonProcessingException, JSONException { Map fieldMap = new HashMap<>(); @@ -1217,7 +1216,7 @@ public void deactivateTestWithNullResponseDTO() throws ApisResourceAccessExcepti MessageDTO result = uinGeneratorStage.process(messageDTO); //assertTrue(result.getIsValid()); } - + @Test public void deactivateTestForExistingUinTestSuccess() throws ApisResourceAccessException, PacketManagerException, IOException, JsonProcessingException, JSONException { @@ -1351,8 +1350,8 @@ public void apisResourceAccessExceptionTest() throws ApisResourceAccessException .thenThrow(apisResourceAccessException); uinGeneratorStage.process(messageDTO); } - - + + @Test public void testHttpServerErrorException() throws Exception { @@ -1372,7 +1371,7 @@ public void testHttpServerErrorException() throws Exception { when(registrationProcessorRestClientService.getApi(any(), any(), anyString(), any(), any())) .thenThrow(apisResourceAccessException); - + MessageDTO result = uinGeneratorStage.process(messageDTO); //assertTrue(result.getIsValid()); @@ -1394,7 +1393,7 @@ public void testHttpClientErrorException() throws Exception { when(registrationProcessorRestClientService.getApi(any(), any(), anyString(), any(), any())) .thenThrow(apisResourceAccessException); - + MessageDTO result = uinGeneratorStage.process(messageDTO); //assertTrue(result.getIsValid()); @@ -1413,7 +1412,7 @@ public void testUinGenerationHttpClientErrorException() throws Exception { when(apisResourceAccessException.getCause()).thenReturn(httpClientErrorException); when(registrationProcessorRestClientService.getApi(any(), any(), anyString(), any(), any())).thenReturn(str); - + when(registrationProcessorRestClientService.postApi(any(), any(), any(), any(), any())).thenThrow(apisResourceAccessException); messageDTO.setReg_type(RegistrationType.NEW); @@ -1435,7 +1434,7 @@ public void testUinGenerationHttpServerErrorException() throws Exception { when(apisResourceAccessException.getCause()).thenReturn(httpServerErrorException); when(registrationProcessorRestClientService.getApi(any(), any(), anyString(), any(), any())).thenReturn(str); - + when(registrationProcessorRestClientService.postApi(any(), any(), any(), any(), any())).thenThrow(apisResourceAccessException); messageDTO.setReg_type(RegistrationType.NEW); @@ -1488,7 +1487,7 @@ public void getApiExceptionTest() throws ApisResourceAccessException { .thenThrow(apisResourceAccessException); uinGeneratorStage.process(messageDTO); } - + @Test public void testIOException() { IOException exception = new IOException("File not found"); @@ -1924,7 +1923,7 @@ public void testUpdateSuccess() throws Exception { assertFalse(result.getInternalError()); } - + @Test public void testUpdateWithoutIdResponseDto() throws Exception { Map fieldMap = new HashMap<>(); @@ -1954,7 +1953,7 @@ public void testUpdateWithoutIdResponseDto() throws Exception { .thenReturn(responsedto); IdResponseDTO idResponseDTO1 = new IdResponseDTO(); - + when(registrationProcessorRestClientService.patchApi(any(), any(), any(), any(), any(), any())) .thenReturn(idResponseDTO1); @@ -2013,7 +2012,7 @@ public void testUpdateunsuccess() throws Exception { assertFalse(result.getIsValid()); } - @Ignore + @Ignore @Test public void testUinAlreadyExists() throws Exception { Map fieldMap = new HashMap<>(); @@ -2168,7 +2167,7 @@ public void testJsonProcessingException() throws ApisResourceAccessException, IO MessageDTO result = uinGeneratorStage.process(messageDTO); assertTrue(result.getInternalError()); } - + @Test public void testIdRecordExistTrueFlagWithErrorResponse() throws Exception { @@ -2664,4 +2663,73 @@ public void testUinGenerationSuccessWithoutUINwithErrorAsEmpty() throws Exceptio MessageDTO result = uinGeneratorStage.process(messageDTO); assertFalse(result.getInternalError()); } -} \ No newline at end of file + + @Test + public void testUinGenerationSuccessWithEmptyName() throws Exception { + ReflectionTestUtils.setField(uinGeneratorStage,"trimWhitespaces",true); + MessageDTO messageDTO = new MessageDTO(); + messageDTO.setRid("27847657360002520181210094052"); + String str = "{\"id\":\"mosip.id.read\",\"version\":\"1.0\",\"responsetime\":\"2019-04-05\",\"metadata\":null,\"response\":{\"uin\":\"2812936908\"},\"errors\":[{\"errorCode\":null,\"errorMessage\":null}]}"; + String response = "{\"uin\":\"6517036426\",\"status\":\"ASSIGNED\"}"; + + when(registrationProcessorRestClientService.getApi(any(), any(), anyString(), any(), any())).thenReturn(str); + when(registrationProcessorRestClientService.putApi(any(), any(), any(), any(), any(), any(), any())) + .thenReturn(response); + messageDTO.setReg_type(RegistrationType.NEW); + + IdResponseDTO idResponseDTO = new IdResponseDTO(); + ResponseDTO responseDTO = new ResponseDTO(); + responseDTO.setEntity("https://dev.mosip.io/idrepo/v1.0/identity/203560486746"); + responseDTO.setStatus("ACTIVATED"); + idResponseDTO.setErrors(null); + idResponseDTO.setId("mosip.id.create"); + idResponseDTO.setResponse(responseDTO); + idResponseDTO.setResponsetime("2019-01-17T06:29:01.940Z"); + idResponseDTO.setVersion("1.0"); + + ResponseWrapper responseVid = new ResponseWrapper(); + List errors = new ArrayList<>(); + responseVid.setErrors(errors); + responseVid.setVersion("v1"); + responseVid.setMetadata(null); + DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + LocalDateTime localdatetime = LocalDateTime + .parse(DateUtils.getUTCCurrentDateTimeString("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"), format); + responseVid.setResponsetime(localdatetime); + VidResponseDto vidResponseDto = new VidResponseDto(); + vidResponseDto.setVID("123456"); + vidResponseDto.setVidStatus("ACTIVE"); + responseVid.setResponse(vidResponseDto); + + when(registrationProcessorRestClientService.postApi(any(), any(), any(), any(), any(Class.class))) + .thenReturn(idResponseDTO).thenReturn(responseVid).thenReturn(response); + + Map fieldMap = new HashMap<>(); + fieldMap.put("firstName","[ {\n" + + " \"language\" : \"eng\",\n" + + " \"value\" : \" \"\n" + + "} ]"); + fieldMap.put("email", "mono@mono.com"); + fieldMap.put("phone", "23456"); + fieldMap.put("dob", "11/11/2011"); + + List defaultFields = new ArrayList<>(); + defaultFields.add("name"); + defaultFields.add("dob"); + defaultFields.add("gender"); + + when(packetManagerService.getFields(any(),any(),any(),any())).thenReturn(fieldMap); + when(idSchemaUtil.getDefaultFields(anyDouble())).thenReturn(defaultFields); + ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(IdRequestDto.class); + + MessageDTO result = uinGeneratorStage.process(messageDTO); + verify(registrationProcessorRestClientService,Mockito.times(2)).postApi(any(), any(), any(),argumentCaptor.capture(),any()); + ObjectMapper objectMapper = new ObjectMapper(); + String jsonobject=objectMapper.writeValueAsString(argumentCaptor.getAllValues().get(0).getRequest().getIdentity()); + JsonNode jsonNode=objectMapper.readTree(jsonobject); + + assertEquals("",jsonNode.get("firstName").asText()); + assertFalse(result.getInternalError()); + assertTrue(result.getIsValid()); + } +} diff --git a/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/mapper/PacketInfoMapper.java b/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/mapper/PacketInfoMapper.java index 11feaa842d6..65149a04de6 100644 --- a/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/mapper/PacketInfoMapper.java +++ b/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/mapper/PacketInfoMapper.java @@ -99,16 +99,21 @@ public static RegAbisRefEntity convertRegAbisRefToEntity(RegAbisRefDto regAbisRe * the language * @return the json values */ - private static String getJsonValues(JsonValue[] jsonNode, String language) { + + private static String getJsonValues(JsonValue[] jsonNode, String language,boolean istrim) { String value = null; if (jsonNode != null) { for (int i = 0; i < jsonNode.length; i++) { if (jsonNode[i].getLanguage().equals(language)) { - value = jsonNode[i].getValue().trim(); + if(istrim) + { + value = jsonNode[i].getValue().trim(); + }else { + value = jsonNode[i].getValue(); + } } } } - return value; } @@ -140,8 +145,14 @@ private static String[] getLanguages(JsonValue[] jsonNode, StringBuilder languag * the reg id * @return the list */ + public static List converDemographicDedupeDtoToEntity( IndividualDemographicDedupe demoDto, String regId) throws NoSuchAlgorithmException { + + return converDemographicDedupeDtoToEntity(demoDto,regId,false); + } + public static List converDemographicDedupeDtoToEntity( + IndividualDemographicDedupe demoDto, String regId,Boolean istrim) throws NoSuchAlgorithmException { regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.USERID.toString(), regId, "PacketInfoMapper::converDemographicDedupeDtoToEntity()::entry"); IndividualDemographicDedupeEntity entity; @@ -168,7 +179,7 @@ public static List converDemographicDedupeDto if (demoDto.getName()!=null &&!demoDto.getName().isEmpty()) { for (JsonValue[] jsonValue : demoDto.getName()) { - applicantFullName.append(getJsonValues(jsonValue, languageArray[i])); + applicantFullName.append(getJsonValues(jsonValue, languageArray[i],istrim)); } entity.setName(!applicantFullName.toString().isEmpty() ? getHMACHashCode(applicantFullName.toString().trim().toUpperCase()) @@ -187,7 +198,7 @@ public static List converDemographicDedupeDto throw new DateParseException(PlatformErrorMessages.RPR_SYS_PARSING_DATE_EXCEPTION.getMessage(), e); } } - entity.setGender(getHMACHashCode(getJsonValues(demoDto.getGender(), languageArray[i]))); + entity.setGender(getHMACHashCode(getJsonValues(demoDto.getGender(), languageArray[i],istrim))); entity.setPhone(getHMACHashCode(demoDto.getPhone())); entity.setEmail(getHMACHashCode(demoDto.getEmail())); demogrphicDedupeEntities.add(entity); diff --git a/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/service/impl/PacketInfoManagerImpl.java b/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/service/impl/PacketInfoManagerImpl.java index 3b997efd9d9..3ba5c04cb2f 100644 --- a/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/service/impl/PacketInfoManagerImpl.java +++ b/registration-processor/registration-processor-info-storage-service/src/main/java/io/mosip/registration/processor/packet/storage/service/impl/PacketInfoManagerImpl.java @@ -145,6 +145,9 @@ public class PacketInfoManagerImpl implements PacketInfoManager applicantDemographicEntities = PacketInfoMapper - .converDemographicDedupeDtoToEntity(demographicData, regId); + .converDemographicDedupeDtoToEntity(demographicData, regId,trimWhitespace); for (IndividualDemographicDedupeEntity applicantDemographicEntity : applicantDemographicEntities) { demographicDedupeRepository.save(applicantDemographicEntity); @@ -343,7 +346,8 @@ public void saveIndividualDemographicDedupeUpdatePacket(IndividualDemographicDed try { List applicantDemographicEntities = PacketInfoMapper - .converDemographicDedupeDtoToEntity(demographicData, registrationId); + .converDemographicDedupeDtoToEntity(demographicData, registrationId, trimWhitespace); + for (IndividualDemographicDedupeEntity applicantDemographicEntity : applicantDemographicEntities) { demographicDedupeRepository.save(applicantDemographicEntity); diff --git a/registration-processor/registration-processor-info-storage-service/src/test/java/io/mosip/registration/processor/packet/storage/PacketInfoManagerImplTest.java b/registration-processor/registration-processor-info-storage-service/src/test/java/io/mosip/registration/processor/packet/storage/PacketInfoManagerImplTest.java index 5f0293f809d..19f8eac8499 100644 --- a/registration-processor/registration-processor-info-storage-service/src/test/java/io/mosip/registration/processor/packet/storage/PacketInfoManagerImplTest.java +++ b/registration-processor/registration-processor-info-storage-service/src/test/java/io/mosip/registration/processor/packet/storage/PacketInfoManagerImplTest.java @@ -85,6 +85,7 @@ import io.mosip.registration.processor.packet.storage.service.impl.PacketInfoManagerImpl; import io.mosip.registration.processor.packet.storage.utils.Utilities; import io.mosip.registration.processor.rest.client.audit.builder.AuditLogRequestBuilder; +import org.springframework.test.util.ReflectionTestUtils; /** * The Class PacketInfoManagerImplTest. @@ -1011,7 +1012,7 @@ public void testsaveIndividualDemographicDedupe() throws NoSuchAlgorithmExceptio entity.setDob("2019-03-02T06:29:41.011Z"); applicantDemographicEntities.add(entity); PowerMockito.mockStatic(PacketInfoMapper.class); - Mockito.when(PacketInfoMapper.converDemographicDedupeDtoToEntity(any(), any())) + Mockito.when(PacketInfoMapper.converDemographicDedupeDtoToEntity(any(),any())) .thenReturn(applicantDemographicEntities); Mockito.when(demographicDedupeRepository.save(any())).thenReturn(entity); packetInfoManagerImpl.saveIndividualDemographicDedupeUpdatePacket(demographicData, "1001", "", ""); @@ -1028,11 +1029,10 @@ public void testsaveIndividualDemographicDedupeException() throws NoSuchAlgorith entity.setDob("2019-03-02T06:29:41.011Z"); applicantDemographicEntities.add(entity); PowerMockito.mockStatic(PacketInfoMapper.class); - Mockito.when(PacketInfoMapper.converDemographicDedupeDtoToEntity(any(), any())) + Mockito.when(PacketInfoMapper.converDemographicDedupeDtoToEntity(any(),any(),any())) .thenReturn(applicantDemographicEntities); Mockito.when(demographicDedupeRepository.save(any())).thenThrow(exp); packetInfoManagerImpl.saveIndividualDemographicDedupeUpdatePacket(demographicData, "1001", "", ""); - } @Test @@ -1105,4 +1105,22 @@ public void dataAccessLayerExceptionTest() { } + + @Test + public void testsaveIndividualDemographicDedupeWithTrim() throws NoSuchAlgorithmException { + ReflectionTestUtils.setField(packetInfoManagerImpl,"trimWhitespace",true); + IndividualDemographicDedupe demographicData = new IndividualDemographicDedupe(); + demographicData.setDateOfBirth("2019-03-02T06:29:41.011Z"); + List applicantDemographicEntities = new ArrayList<>(); + IndividualDemographicDedupeEntity entity = new IndividualDemographicDedupeEntity(); + entity.setDob("2019-03-02T06:29:41.011Z"); + applicantDemographicEntities.add(entity); + PowerMockito.mockStatic(PacketInfoMapper.class); + Mockito.when(PacketInfoMapper.converDemographicDedupeDtoToEntity(any(),any(),any())) + .thenReturn(applicantDemographicEntities); + Mockito.when(demographicDedupeRepository.save(any())).thenReturn(entity); + packetInfoManagerImpl.saveIndividualDemographicDedupeUpdatePacket(demographicData, "1001", "", ""); + + } + }