From bd4209c5cf9d4a95937b40e6c5b5f752a68f7dec Mon Sep 17 00:00:00 2001 From: khuddus shariff Date: Wed, 24 Jan 2024 10:41:20 +0530 Subject: [PATCH] [Mosip-30241]adding trim functionality based on property (#1837) Signed-off-by: khuddus shariff Signed-off-by: Sowmya Ujjappa Banakar --- .../uingenerator/stage/UinGeneratorStage.java | 7 ++- .../uigenerator/UinGeneratorStageTest.java | 59 ++++++++++++++++--- 2 files changed, 57 insertions(+), 9 deletions(-) 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 5d5e8ac4421..63c05ac64ac 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 @@ -152,6 +152,9 @@ public class UinGeneratorStage extends MosipVerticleAPIManager { @Value("${uingenerator.lost.packet.allowed.update.fields:null}") private String updateInfo; + @Value("${mosip.regproc.uin.generator.trim-whitespaces.simpleType-value:false}") + private boolean trimWhitespaces; + /** The core audit request builder. */ @Autowired private AuditLogRequestBuilder auditLogRequestBuilder; @@ -323,7 +326,6 @@ public MessageDTO process(MessageDTO object) { description); } } - } regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), registrationId, description.getMessage()); @@ -477,6 +479,9 @@ else if (json instanceof JSONArray) { for (int i = 0; i < jsonArray.length(); i++) { Object obj = jsonArray.get(i); HashMap hashMap = 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 f3328aafd64..a94473058a9 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,12 +1,11 @@ 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.when; +import static org.mockito.Mockito.*; import java.io.ByteArrayInputStream; import java.io.File; @@ -15,6 +14,8 @@ import java.io.InputStream; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -22,6 +23,10 @@ import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.JsonNode; +import io.mosip.kernel.core.util.DateUtils; +import io.mosip.registration.processor.packet.manager.dto.IdRequestDto; +import io.mosip.registration.processor.stages.uingenerator.dto.VidResponseDto; import org.apache.commons.io.IOUtils; import org.assertj.core.util.Lists; import org.json.JSONException; @@ -30,11 +35,7 @@ import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; +import org.mockito.*; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -2432,4 +2433,46 @@ public void testUinGenerationIDRepoDraftReprocessableException() throws Exceptio assertTrue(result.getInternalError()); assertTrue(result.getIsValid()); } + + @Test + public void testUinGenerationSuccessWithEmptyName() throws Exception { + ReflectionTestUtils.setField(uinGeneratorStage,"trimWhitespaces",true); + 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"); + when(packetManagerService.getFields(any(),any(),any(),any())).thenReturn(fieldMap); + ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(IdRequestDto.class); + + MessageDTO messageDTO = new MessageDTO(); + messageDTO.setRid("27847657360002520181210094052"); + messageDTO.setReg_type(RegistrationType.NEW.name()); + + IdResponseDTO idResponseDTO = new IdResponseDTO(); + ResponseDTO responseDTO = new ResponseDTO(); + responseDTO.setStatus("ACTIVATED"); + idResponseDTO.setErrors(null); + idResponseDTO.setId("mosip.id.update"); + idResponseDTO.setResponse(responseDTO); + idResponseDTO.setResponsetime("2019-01-17T06:29:01.940Z"); + idResponseDTO.setVersion("1.0"); + + when(idrepoDraftService.idrepoUpdateDraft(anyString(), any(), any())).thenReturn(idResponseDTO); + when(utility.getRegistrationProcessorMappingJson(MappingJsonConstants.IDENTITY)).thenReturn(identityObj); + when(utility.getRegistrationProcessorMappingJson(MappingJsonConstants.DOCUMENT)).thenReturn(documentObj); + + MessageDTO result = uinGeneratorStage.process(messageDTO); + verify(idrepoDraftService).idrepoUpdateDraft(any(), any(), argumentCaptor.capture()); + 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()); + } } \ No newline at end of file