From fa62d5ee96633fcc1d9e68767923ed39c7af0934 Mon Sep 17 00:00:00 2001 From: kameshsr Date: Mon, 29 Jul 2024 17:38:11 +0530 Subject: [PATCH 01/10] MOSIP-34803 Added IdentityUtil test Signed-off-by: kameshsr --- .../mosip/resident/util/IdentityUtilTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java new file mode 100644 index 00000000000..6060cb8d46b --- /dev/null +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java @@ -0,0 +1,50 @@ +package io.mosip.resident.util; + +import io.mosip.resident.exception.ApisResourceAccessException; +import io.mosip.resident.exception.ResidentServiceCheckedException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.core.env.Environment; + +import static io.mosip.resident.service.impl.IdentityServiceTest.getAuthUserDetailsFromAuthentication; +import static org.mockito.Mockito.when; + +/** + * @author Kamesh Shekhar Prasad + */ + +@RunWith(MockitoJUnitRunner.class) +public class IdentityUtilTest { + + @InjectMocks + private IdentityUtil identityUtil = new IdentityUtil(); + + @Mock + private CachedIdentityDataUtil cachedIdentityDataUtil; + + @Mock + private Environment environment; + + @Mock + private AccessTokenUtility accessTokenUtility; + + @Before + public void setup(){ + when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) + .thenReturn("UIN,email,phone,dateOfBirth,fullName"); + when(accessTokenUtility.getAccessToken()).thenReturn("token"); + } + + @Test(expected = ResidentServiceCheckedException.class) + public void testGetIdentityAttributesFailure() throws Exception { + getAuthUserDetailsFromAuthentication(); + when(cachedIdentityDataUtil.getCachedIdentityData(Mockito.anyString(), Mockito.anyString(), Mockito.any())) + .thenThrow(new ApisResourceAccessException()); + identityUtil.getIdentityAttributes("6", "personalized-card"); + } +} From bfe1e2ed31a3ff0acb98b63d3989d874f0752479 Mon Sep 17 00:00:00 2001 From: kameshsr Date: Tue, 30 Jul 2024 11:58:42 +0530 Subject: [PATCH 02/10] MOSIP-34803 Increased sonar coverage Signed-off-by: kameshsr --- .../mosip/resident/util/IdentityUtilTest.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java index 6060cb8d46b..add4f17cb44 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java @@ -1,5 +1,7 @@ package io.mosip.resident.util; +import io.mosip.resident.dto.IdResponseDTO1; +import io.mosip.resident.dto.ResponseDTO1; import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.ResidentServiceCheckedException; import org.junit.Before; @@ -11,6 +13,10 @@ import org.mockito.junit.MockitoJUnitRunner; import org.springframework.core.env.Environment; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + import static io.mosip.resident.service.impl.IdentityServiceTest.getAuthUserDetailsFromAuthentication; import static org.mockito.Mockito.when; @@ -32,12 +38,23 @@ public class IdentityUtilTest { @Mock private AccessTokenUtility accessTokenUtility; + private IdResponseDTO1 idResponseDTO1; @Before - public void setup(){ + public void setup() throws ApisResourceAccessException { + Map identityMap = new LinkedHashMap(); + identityMap.put("UIN", "8251649601"); + identityMap.put("email", "manojvsp12@gmail.com"); + identityMap.put("phone", "9395910872"); + identityMap.put("dateOfBirth", "1970/11/16"); + idResponseDTO1 = new IdResponseDTO1(); + ResponseDTO1 responseDTO1 = new ResponseDTO1(); + responseDTO1.setIdentity(identityMap); + idResponseDTO1.setResponse(responseDTO1); when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) .thenReturn("UIN,email,phone,dateOfBirth,fullName"); when(accessTokenUtility.getAccessToken()).thenReturn("token"); + when(cachedIdentityDataUtil.getCachedIdentityData(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(idResponseDTO1); } @Test(expected = ResidentServiceCheckedException.class) @@ -47,4 +64,10 @@ public void testGetIdentityAttributesFailure() throws Exception { .thenThrow(new ApisResourceAccessException()); identityUtil.getIdentityAttributes("6", "personalized-card"); } + + @Test + public void testGetIdentityAttributesCachedIdentityData() throws ApisResourceAccessException, ResidentServiceCheckedException, IOException { + getAuthUserDetailsFromAuthentication(); + identityUtil.getIdentityAttributes("6", "personalized-card"); + } } From 3d5f9bf4a5a384d25c387ff5545bcbc08e66a5c7 Mon Sep 17 00:00:00 2001 From: kameshsr Date: Tue, 30 Jul 2024 16:02:55 +0530 Subject: [PATCH 03/10] MOSIP-34803 Increased code coverage Signed-off-by: kameshsr --- .../mosip/resident/util/IdentityUtilTest.java | 63 ++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java index add4f17cb44..725986c6b04 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java @@ -1,9 +1,13 @@ package io.mosip.resident.util; +import io.mosip.kernel.core.exception.ServiceError; +import io.mosip.resident.constant.ResidentErrorCode; import io.mosip.resident.dto.IdResponseDTO1; import io.mosip.resident.dto.ResponseDTO1; import io.mosip.resident.exception.ApisResourceAccessException; import io.mosip.resident.exception.ResidentServiceCheckedException; +import io.mosip.resident.exception.ResidentServiceException; +import io.mosip.resident.handler.service.ResidentConfigService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -15,9 +19,12 @@ import java.io.IOException; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; +import java.util.Optional; import static io.mosip.resident.service.impl.IdentityServiceTest.getAuthUserDetailsFromAuthentication; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; /** @@ -38,10 +45,20 @@ public class IdentityUtilTest { @Mock private AccessTokenUtility accessTokenUtility; + + @Mock + private ResidentConfigService residentConfigService; + + @Mock + private PerpetualVidUtil perpetualVidUtil; + + @Mock + private AvailableClaimValueUtility availableClaimValueUtility; + private IdResponseDTO1 idResponseDTO1; @Before - public void setup() throws ApisResourceAccessException { + public void setup() throws ApisResourceAccessException, ResidentServiceCheckedException { Map identityMap = new LinkedHashMap(); identityMap.put("UIN", "8251649601"); identityMap.put("email", "manojvsp12@gmail.com"); @@ -55,6 +72,10 @@ public void setup() throws ApisResourceAccessException { .thenReturn("UIN,email,phone,dateOfBirth,fullName"); when(accessTokenUtility.getAccessToken()).thenReturn("token"); when(cachedIdentityDataUtil.getCachedIdentityData(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(idResponseDTO1); + when(residentConfigService.getUiSchemaFilteredInputAttributes(anyString())) + .thenReturn(List.of("UIN", "email", "phone", "dateOfBirth", "firstName", "middleName", "lastName", "perpetualVID")); + Optional perpVid = Optional.of("8251649601"); + when(perpetualVidUtil.getPerpatualVid(anyString())).thenReturn(perpVid); } @Test(expected = ResidentServiceCheckedException.class) @@ -66,8 +87,46 @@ public void testGetIdentityAttributesFailure() throws Exception { } @Test - public void testGetIdentityAttributesCachedIdentityData() throws ApisResourceAccessException, ResidentServiceCheckedException, IOException { + public void testGetIdentityAttributesCachedIdentityDataSuccessSecureSession() throws ResidentServiceCheckedException, IOException { getAuthUserDetailsFromAuthentication(); identityUtil.getIdentityAttributes("6", "personalized-card"); } + + @Test + public void testGetIdentityAttributesCachedIdentityDataSuccess() throws ResidentServiceCheckedException, IOException { + identityUtil.getIdentityAttributes("6", "personalized-card"); + } + + @Test(expected = ResidentServiceCheckedException.class) + public void testGetIdentityAttributesCachedIdentityDataFailure() throws ResidentServiceCheckedException, IOException, ApisResourceAccessException { + idResponseDTO1.setErrors(List.of(new ServiceError(ResidentErrorCode.API_RESOURCE_ACCESS_EXCEPTION.getErrorCode() + , ResidentErrorCode.API_RESOURCE_ACCESS_EXCEPTION.getErrorMessage()))); + identityUtil.getIdentityAttributes("6", "personalized-card"); + } + + @Test + public void testGetIdentityAttributesCachedIdentityDataSuccessSchemaTypeNull() throws ResidentServiceCheckedException, IOException { + when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) + .thenReturn("UIN,email,phone,dateOfBirth,fullName,perpetualVID"); + identityUtil.getIdentityAttributes("6", null); + } + + @Test(expected = ResidentServiceException.class) + public void testGetIdentityAttributesCachedIdentityDataFailurePerpetualVid() throws ResidentServiceCheckedException, IOException, ApisResourceAccessException { + when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) + .thenReturn("UIN,email,phone,dateOfBirth,fullName,perpetualVID"); + when(perpetualVidUtil.getPerpatualVid(anyString())).thenThrow(new ApisResourceAccessException()); + identityUtil.getIdentityAttributes("6", null); + } + + @Test + public void testGetIdentityAttributesCachedIdentityDataSuccessWithPhoto() throws ResidentServiceCheckedException, IOException, ApisResourceAccessException { + when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) + .thenReturn("UIN,email,phone,dateOfBirth,fullName,perpetualVID,photo"); + when(environment.getProperty("mosip.resident.photo.attribute.name")).thenReturn("photo"); + when(environment.getProperty("mosip.resident.photo.token.claim-photo")).thenReturn("picture"); + when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenReturn("photo"); + getAuthUserDetailsFromAuthentication(); + identityUtil.getIdentityAttributes("6", null); + } } From fcd1838b5e9410c99290c883bb369a187fe26fad Mon Sep 17 00:00:00 2001 From: kameshsr Date: Wed, 31 Jul 2024 15:48:01 +0530 Subject: [PATCH 04/10] MOSIP-34803 Added Junit test case for Identity util class Signed-off-by: kameshsr --- .../mosip/resident/util/IdentityUtilTest.java | 73 +++++++++++++++++-- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java index 725986c6b04..998439c1bfe 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/IdentityUtilTest.java @@ -16,6 +16,7 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.core.env.Environment; +import org.springframework.test.util.ReflectionTestUtils; import java.io.IOException; import java.util.LinkedHashMap; @@ -24,6 +25,7 @@ import java.util.Optional; import static io.mosip.resident.service.impl.IdentityServiceTest.getAuthUserDetailsFromAuthentication; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -55,15 +57,27 @@ public class IdentityUtilTest { @Mock private AvailableClaimValueUtility availableClaimValueUtility; + @Mock + private MaskDataUtility maskDataUtility; + + @Mock + private Utility utility; + + @Mock + private ClaimValueUtility claimValueUtility; + private IdResponseDTO1 idResponseDTO1; + private String uin; @Before public void setup() throws ApisResourceAccessException, ResidentServiceCheckedException { Map identityMap = new LinkedHashMap(); - identityMap.put("UIN", "8251649601"); + uin = "8251649601"; + identityMap.put("UIN", uin); identityMap.put("email", "manojvsp12@gmail.com"); identityMap.put("phone", "9395910872"); identityMap.put("dateOfBirth", "1970/11/16"); + identityMap.put("masked", "1970/11/16"); idResponseDTO1 = new IdResponseDTO1(); ResponseDTO1 responseDTO1 = new ResponseDTO1(); responseDTO1.setIdentity(identityMap); @@ -89,12 +103,12 @@ public void testGetIdentityAttributesFailure() throws Exception { @Test public void testGetIdentityAttributesCachedIdentityDataSuccessSecureSession() throws ResidentServiceCheckedException, IOException { getAuthUserDetailsFromAuthentication(); - identityUtil.getIdentityAttributes("6", "personalized-card"); + assertEquals(uin, identityUtil.getIdentityAttributes("6", "personalized-card").get("UIN")); } @Test public void testGetIdentityAttributesCachedIdentityDataSuccess() throws ResidentServiceCheckedException, IOException { - identityUtil.getIdentityAttributes("6", "personalized-card"); + assertEquals(uin, identityUtil.getIdentityAttributes("6", "personalized-card").get("UIN")); } @Test(expected = ResidentServiceCheckedException.class) @@ -108,7 +122,7 @@ public void testGetIdentityAttributesCachedIdentityDataFailure() throws Resident public void testGetIdentityAttributesCachedIdentityDataSuccessSchemaTypeNull() throws ResidentServiceCheckedException, IOException { when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) .thenReturn("UIN,email,phone,dateOfBirth,fullName,perpetualVID"); - identityUtil.getIdentityAttributes("6", null); + assertEquals(uin, identityUtil.getIdentityAttributes("6", null).get("UIN")); } @Test(expected = ResidentServiceException.class) @@ -127,6 +141,55 @@ public void testGetIdentityAttributesCachedIdentityDataSuccessWithPhoto() throws when(environment.getProperty("mosip.resident.photo.token.claim-photo")).thenReturn("picture"); when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenReturn("photo"); getAuthUserDetailsFromAuthentication(); - identityUtil.getIdentityAttributes("6", null); + assertEquals(uin, identityUtil.getIdentityAttributes("6", null).get("UIN")); + } + + @Test(expected = ResidentServiceException.class) + public void testGetIdentityAttributesCachedIdentityDataFailureWithPhoto() throws ResidentServiceCheckedException, IOException, ApisResourceAccessException { + when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) + .thenReturn("UIN,email,phone,dateOfBirth,fullName,perpetualVID,photo"); + when(environment.getProperty("mosip.resident.photo.attribute.name")).thenReturn("photo"); + when(environment.getProperty("mosip.resident.photo.token.claim-photo")).thenReturn("picture"); + when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenThrow(new ApisResourceAccessException()); + getAuthUserDetailsFromAuthentication(); + identityUtil.getIdentityAttributes("6", null).get("UIN"); + } + + @Test + public void testGetIdentityAttributesCachedIdentityDataSuccessWithMaskedAttribute() throws ResidentServiceCheckedException, IOException, ApisResourceAccessException { + when(environment.getProperty("resident.additional.identity.attribute.to.fetch")) + .thenReturn("UIN,email,phone,dateOfBirth,fullName,perpetualVID,photo,masked_email"); + when(maskDataUtility.convertToMaskData(Mockito.anyString())).thenReturn("8**3"); + getAuthUserDetailsFromAuthentication(); + assertEquals(uin, identityUtil.getIdentityAttributes("6", null).get("UIN")); + } + + @Test + public void testGetIdentity() throws ResidentServiceCheckedException, IOException { + when(utility.getMappingValue(Mockito.anyMap(), Mockito.anyString())).thenReturn("2016/02/02"); + ReflectionTestUtils.setField(identityUtil, "dateFormat", "yyyy/MM/dd"); + assertEquals("1970/11/16", identityUtil.getIdentity(uin).getDateOfBirth()); + } + + @Test(expected = ResidentServiceCheckedException.class) + public void testGetIdentityFailure() throws ResidentServiceCheckedException, IOException { + when(utility.getMappingValue(Mockito.anyMap(), Mockito.anyString())).thenThrow(new IOException()); + identityUtil.getIdentity(uin).getDateOfBirth(); + } + + @Test + public void testGetIdentityWithFetchFace() throws ResidentServiceCheckedException, IOException { + when(utility.getMappingValue(Mockito.anyMap(), Mockito.anyString())).thenReturn("2016/02/02"); + ReflectionTestUtils.setField(identityUtil, "dateFormat", "yyyy/MM/dd"); + assertEquals("1970/11/16", identityUtil.getIdentity(uin, true, "eng").getDateOfBirth()); + } + + @Test(expected = ResidentServiceCheckedException.class) + public void testGetIdentityWithFetchFaceFailure() throws ResidentServiceCheckedException, IOException, ApisResourceAccessException { + when(utility.getMappingValue(Mockito.anyMap(), Mockito.anyString())).thenReturn("2016/02/02"); + when(environment.getProperty("mosip.resident.photo.token.claim-photo")).thenReturn("photo"); + ReflectionTestUtils.setField(identityUtil, "dateFormat", "yyyy/MM/dd"); + when(claimValueUtility.getClaimValue(Mockito.anyString())).thenThrow(new ApisResourceAccessException()); + assertEquals("1970/11/16", identityUtil.getIdentity(uin, true, "eng").getDateOfBirth()); } } From 2011d1fa36313fb47964cfd96fcf15d83bf7ae33 Mon Sep 17 00:00:00 2001 From: kameshsr Date: Wed, 31 Jul 2024 18:34:29 +0530 Subject: [PATCH 05/10] MOSIP-34803 Added AttributeDisplayTextTest class Signed-off-by: kameshsr --- .../util/AttributeDisplayTextTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java new file mode 100644 index 00000000000..70d333a16d1 --- /dev/null +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java @@ -0,0 +1,42 @@ +package io.mosip.resident.util; + +import io.mosip.resident.constant.RequestType; +import io.mosip.resident.handler.service.ResidentConfigService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +/** + * @author Kamesh + */ + +@RunWith(MockitoJUnitRunner.class) +public class AttributeDisplayTextTest { + + @InjectMocks + private AttributesDisplayText attributesDisplayText = new AttributesDisplayText(); + + @Mock + ResidentConfigService residentConfigService; + + @Test + public void testGetAttributesDisplayText(){ + Map>> keyMap = new HashMap<>(); + Map> attributeMap = new HashMap<>(); + Map stringObjectMap = new HashMap<>(); + stringObjectMap.put("testKey", "testobj"); + attributeMap.put("key", stringObjectMap); + keyMap.put("eng", attributeMap); + Mockito.when(residentConfigService.getUISchemaCacheableData(Mockito.anyString())).thenReturn(keyMap); + assertEquals("test", + attributesDisplayText.getAttributesDisplayText("test", "eng", RequestType.UPDATE_MY_UIN)); + } +} From a828dd91f2d05af7a03bed7dabe9cee5d1c76bdb Mon Sep 17 00:00:00 2001 From: kameshsr Date: Wed, 31 Jul 2024 18:57:58 +0530 Subject: [PATCH 06/10] MOSIP-34803 Added unit test case Signed-off-by: kameshsr --- .../util/AttributeDisplayTextTest.java | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java index 70d333a16d1..3a51f29d784 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java @@ -1,7 +1,9 @@ package io.mosip.resident.util; import io.mosip.resident.constant.RequestType; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.handler.service.ResidentConfigService; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -13,6 +15,7 @@ import java.util.Map; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; /** * @author Kamesh @@ -27,6 +30,72 @@ public class AttributeDisplayTextTest { @Mock ResidentConfigService residentConfigService; + private Map> mockUISchemaDataMap; + + @Before + public void setUp() { + // Mock UI Schema Data + mockUISchemaDataMap = new HashMap<>(); + + Map attributeMap = new HashMap<>(); + attributeMap.put(ResidentConstants.LABEL, "Mocked Label"); + + Map formatOptionMap = new HashMap<>(); + formatOptionMap.put("format1", "Formatted 1"); + formatOptionMap.put("format2", "Formatted 2"); + + attributeMap.put(ResidentConstants.FORMAT_OPTION, formatOptionMap); + + mockUISchemaDataMap.put("attr1", attributeMap); + + // Mock ResidentConfigService response + when(residentConfigService.getUISchemaCacheableData(Mockito.anyString())) + .thenReturn(Map.of("en", mockUISchemaDataMap)); + } + + @Test + public void testGetAttributesDisplayText_withSchemaType() { + String attributesFromDB = "en:format1,format2"; + String languageCode = "en"; + RequestType requestType = RequestType.UPDATE_MY_UIN; + + String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType); + + assertEquals("en", result); + } + + @Test + public void testGetAttributesDisplayText_withoutSchemaType() { + String attributesFromDB = "attr1"; + String languageCode = "en"; + RequestType requestType = RequestType.UPDATE_MY_UIN; + String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType); + + assertEquals("Mocked Label", result); + } + + @Test + public void testGetAttributesDisplayText_withNullAttributes() { + String attributesFromDB = null; + String languageCode = "en"; + RequestType requestType = RequestType.UPDATE_MY_UIN; + + String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType); + + assertEquals("", result); + } + + @Test + public void testGetAttributesDisplayText_withEmptyAttributes() { + String attributesFromDB = ""; + String languageCode = "en"; + RequestType requestType = RequestType.UPDATE_MY_UIN; + + String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType); + + assertEquals("", result); + } + @Test public void testGetAttributesDisplayText(){ Map>> keyMap = new HashMap<>(); @@ -35,7 +104,7 @@ public void testGetAttributesDisplayText(){ stringObjectMap.put("testKey", "testobj"); attributeMap.put("key", stringObjectMap); keyMap.put("eng", attributeMap); - Mockito.when(residentConfigService.getUISchemaCacheableData(Mockito.anyString())).thenReturn(keyMap); + when(residentConfigService.getUISchemaCacheableData(Mockito.anyString())).thenReturn(keyMap); assertEquals("test", attributesDisplayText.getAttributesDisplayText("test", "eng", RequestType.UPDATE_MY_UIN)); } From 81df0f7ded433dd3f39179a1008d100af1fb7dc8 Mon Sep 17 00:00:00 2001 From: kameshsr Date: Thu, 1 Aug 2024 12:44:46 +0530 Subject: [PATCH 07/10] MOSIP-34803 Added test case Signed-off-by: kameshsr --- .../java/io/mosip/resident/util/AttributeDisplayTextTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java index 3a51f29d784..be381d9415a 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/AttributeDisplayTextTest.java @@ -39,6 +39,7 @@ public void setUp() { Map attributeMap = new HashMap<>(); attributeMap.put(ResidentConstants.LABEL, "Mocked Label"); + attributeMap.put("label1", "Mocked Label2"); Map formatOptionMap = new HashMap<>(); formatOptionMap.put("format1", "Formatted 1"); @@ -66,7 +67,7 @@ public void testGetAttributesDisplayText_withSchemaType() { @Test public void testGetAttributesDisplayText_withoutSchemaType() { - String attributesFromDB = "attr1"; + String attributesFromDB = "attr1:attr2"; String languageCode = "en"; RequestType requestType = RequestType.UPDATE_MY_UIN; String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType); From c55b8d8be19b52a749a9f43ecd4380b8c710f15c Mon Sep 17 00:00:00 2001 From: kameshsr Date: Thu, 1 Aug 2024 13:10:10 +0530 Subject: [PATCH 08/10] MOSIP-34803 Added PerpetualVidUtilTest Signed-off-by: kameshsr --- .../resident/util/PerpetualVidUtilTest.java | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java new file mode 100644 index 00000000000..7d1092349f0 --- /dev/null +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java @@ -0,0 +1,131 @@ +package io.mosip.resident.util; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.mosip.resident.dto.ResponseWrapper; +import io.mosip.resident.exception.ApisResourceAccessException; +import io.mosip.resident.exception.ResidentServiceCheckedException; +import io.mosip.resident.repository.ResidentTransactionRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.core.env.Environment; +import org.springframework.test.util.ReflectionTestUtils; + +import java.security.NoSuchAlgorithmException; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class PerpetualVidUtilTest { + + @InjectMocks + private PerpetualVidUtil perpetualVidUtil; + + @Mock + private Environment env; + + @Mock + private ResidentServiceRestClient residentServiceRestClient; + + @Mock + private MaskDataUtility maskDataUtility; + + @Mock + private Utility utility; + + @Mock + private ResidentTransactionRepository residentTransactionRepository; + + @Mock + private ObjectMapper mapper; + + @Before + public void setUp() { + ReflectionTestUtils.setField(perpetualVidUtil, "perpatualVidType", "PERPETUAL"); + } + + @Test + public void testGetPerpetualVid_Success() throws ResidentServiceCheckedException, ApisResourceAccessException { + String uin = "123456789012"; + String vid = "1234567890123456"; + String vidType = "PERPETUAL"; + + Map vidMap = new HashMap<>(); + vidMap.put("vid", vid); + vidMap.put("vidType", vidType); + + List> vidList = Collections.singletonList(vidMap); + + ResponseWrapper>> responseWrapper = new ResponseWrapper<>(); + responseWrapper.setResponse(vidList); + + when(residentServiceRestClient.getApi(anyString(), eq(ResponseWrapper.class))).thenReturn(responseWrapper); + + Optional result = perpetualVidUtil.getPerpatualVid(uin); + + assertTrue(result.isPresent()); + assertEquals(vid, result.get()); + } + + @Test + public void testGetPerpetualVid_Empty() throws ResidentServiceCheckedException, ApisResourceAccessException { + String uin = "123456789012"; + + ResponseWrapper>> responseWrapper = new ResponseWrapper<>(); + responseWrapper.setResponse(Collections.emptyList()); + + when(residentServiceRestClient.getApi(anyString(), eq(ResponseWrapper.class))).thenReturn(responseWrapper); + + Optional result = perpetualVidUtil.getPerpatualVid(uin); + + assertFalse(result.isPresent()); + } + + @Test + public void testRetrieveVidsfromUin_Success() throws ResidentServiceCheckedException, ApisResourceAccessException, NoSuchAlgorithmException { + String uin = "123456789012"; + int timeZoneOffset = 0; + String locale = "en"; + + Map vidMap = new HashMap<>(); + vidMap.put("vid", "1234567890123456"); + vidMap.put("transactionLimit", 5); + + List> vidList = Collections.singletonList(vidMap); + + ResponseWrapper>> responseWrapper = new ResponseWrapper<>(); + responseWrapper.setResponse(vidList); + + when(residentServiceRestClient.getApi(anyString(), eq(ResponseWrapper.class))).thenReturn(responseWrapper); + when(utility.getRefIdHash(anyString())).thenReturn("hashedVid"); + when(residentTransactionRepository.findByRefIdAndAuthTypeCodeLike(anyString(), anyString())).thenReturn(2); + when(maskDataUtility.convertToMaskData(anyString())).thenReturn("maskedVid"); + + ResponseWrapper>> result = perpetualVidUtil.retrieveVidsfromUin(uin, timeZoneOffset, locale); + + assertNotNull(result); + assertEquals(1, result.getResponse().size()); + assertEquals("maskedVid", result.getResponse().get(0).get("maskedVid")); + } + + @Test + public void testRetrieveVidsfromUin_Exception() throws ResidentServiceCheckedException, ApisResourceAccessException { + String uin = "123456789012"; + int timeZoneOffset = 0; + String locale = "en"; + + when(residentServiceRestClient.getApi(anyString(), eq(ResponseWrapper.class))) + .thenThrow(new RuntimeException("API error")); + + assertThrows(ApisResourceAccessException.class, () -> { + perpetualVidUtil.retrieveVidsfromUin(uin, timeZoneOffset, locale); + }); + } +} + From 3d3fac0d1da797993eb3e464d475aaa16b91676e Mon Sep 17 00:00:00 2001 From: kameshsr Date: Thu, 1 Aug 2024 16:16:33 +0530 Subject: [PATCH 09/10] MOSIP-34803 Increased code coverage Signed-off-by: kameshsr --- .../resident/util/PerpetualVidUtilTest.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java index 7d1092349f0..143a07624ac 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/PerpetualVidUtilTest.java @@ -15,11 +15,25 @@ import org.springframework.test.util.ReflectionTestUtils; import java.security.NoSuchAlgorithmException; -import java.util.*; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +/** + * @author Kamesh Shekhar Prasad + */ @RunWith(MockitoJUnitRunner.class) public class PerpetualVidUtilTest { From 52381eb3d7e3a85f95d0e238b94260a059570bd2 Mon Sep 17 00:00:00 2001 From: kameshsr Date: Thu, 1 Aug 2024 17:41:51 +0530 Subject: [PATCH 10/10] MOSIP-34803 Corrected test case failure for AuditUtil Signed-off-by: kameshsr --- .../io/mosip/resident/util/AuditUtilTest.java | 35 +++++-------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/resident/resident-service/src/test/java/io/mosip/resident/util/AuditUtilTest.java b/resident/resident-service/src/test/java/io/mosip/resident/util/AuditUtilTest.java index 6d9fbb1b9d9..43bec21ce21 100644 --- a/resident/resident-service/src/test/java/io/mosip/resident/util/AuditUtilTest.java +++ b/resident/resident-service/src/test/java/io/mosip/resident/util/AuditUtilTest.java @@ -1,5 +1,6 @@ package io.mosip.resident.util; +import static io.mosip.resident.service.impl.IdentityServiceTest.getAuthUserDetailsFromAuthentication; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.times; @@ -11,7 +12,6 @@ import java.time.LocalDateTime; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -19,16 +19,10 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.core.env.Environment; import org.springframework.http.HttpEntity; import org.springframework.http.ResponseEntity; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.context.SecurityContextImpl; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.client.RestTemplate; @@ -51,10 +45,7 @@ /** * @author Abubacker Siddik */ -@RunWith(PowerMockRunner.class) -@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"}) -@PrepareForTest({SecurityContextHolder.class, InetAddress.class, DateUtils.class}) -@Ignore +@RunWith(MockitoJUnitRunner.class) public class AuditUtilTest { @InjectMocks @@ -103,24 +94,16 @@ public void setUp() throws Exception { ReflectionTestUtils.setField(auditUtil, "auditUrl", auditUrl); ReflectionTestUtils.setField(auditUtil, "asyncUtil", asyncUtil); - PowerMockito.mockStatic(SecurityContextHolder.class); - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.mockStatic(DateUtils.class); - - UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user1", "password", null); - when(SecurityContextHolder.getContext()).thenReturn(new SecurityContextImpl(token)); - host = InetAddress.getLocalHost(); - when(InetAddress.getLocalHost()).thenReturn(host); - localDateTime = DateUtils.getUTCCurrentDateTime(); - when(DateUtils.getUTCCurrentDateTime()).thenReturn(localDateTime); + ReflectionTestUtils.setField(auditUtil, "hostIpAddress","MOSIP"); when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenReturn("user1"); when(environment.getProperty(Mockito.anyString())).thenReturn("user1"); } @Test public void setAuditRequestDtoTest() throws Exception { + getAuthUserDetailsFromAuthentication(); AuditEvent auditEvent = AuditEnum.getAuditEventWithValue(AuditEnum.VALIDATE_REQUEST, "get Rid status API"); AuditResponseDto auditResponseDto = new AuditResponseDto(); auditResponseDto.setStatus(true); @@ -161,20 +144,18 @@ public void setAuditRequestDtoTest() throws Exception { httpEntity.getBody().getRequest().getId()); assertEquals(IdType.UIN.name(), httpEntity.getBody().getRequest().getIdType()); - assertEquals(host.getHostName(), httpEntity.getBody().getRequest().getHostName()); - assertEquals(host.getHostAddress(), httpEntity.getBody().getRequest().getHostIp()); - assertEquals("user1", httpEntity.getBody().getRequest().getSessionUserId()); assertEquals("user1", httpEntity.getBody().getRequest().getSessionUserName()); assertEquals("user1", httpEntity.getBody().getRequest().getCreatedBy()); - assertEquals(localDateTime, httpEntity.getBody().getRequest().getActionTimeStamp()); + assertEquals(localDateTime.getYear(), httpEntity.getBody().getRequest().getActionTimeStamp().getYear()); assertEquals(auditUrlInput, auditUrl); } @Test(expected = RuntimeException.class) public void testSetAuditRequestDtoWithApisResourceAccessException() throws Exception { + getAuthUserDetailsFromAuthentication(); AuditEvent auditEvent = AuditEnum.getAuditEventWithValue(AuditEnum.VALIDATE_REQUEST, "get Rid status API"); when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenThrow(ApisResourceAccessException.class); auditUtil.setAuditRequestDto(auditEvent); @@ -182,6 +163,7 @@ public void testSetAuditRequestDtoWithApisResourceAccessException() throws Excep @Test(expected = ResidentServiceException.class) public void testGetRefIdHashAndTypeWithApisResourceAccessException() throws Exception { + getAuthUserDetailsFromAuthentication(); AuditEvent auditEvent = AuditEnum.getAuditEventWithValue(AuditEnum.VALIDATE_REQUEST, "get Rid status API"); when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenReturn(null); Mockito.when(availableClaimUtility.getResidentIndvidualIdFromSession()).thenThrow(ApisResourceAccessException.class); @@ -196,6 +178,7 @@ public void testGetRefIdandTypeNoID() { @Test public void testGetRefIdandType() throws ApisResourceAccessException, NoSuchAlgorithmException { + getAuthUserDetailsFromAuthentication(); String individualId = "9054257143"; Mockito.when(availableClaimUtility.getResidentIndvidualIdFromSession()).thenReturn(individualId); Mockito.when(uinVidValidator.getIndividualIdType(individualId)).thenReturn(IdType.UIN);