From 9bdd2b56f5f0c3b5a74e08f721eff760f6845c34 Mon Sep 17 00:00:00 2001 From: kameshsr <47484458+kameshsr@users.noreply.github.com> Date: Wed, 6 Nov 2024 12:42:48 +0530 Subject: [PATCH] MOSIP-35260 Fix claims issue (#1353) * MOSIP-35260 Fix claims issue Signed-off-by: kameshsr * MOSIP-35260 Refactor code Signed-off-by: kameshsr * MOSIP-35260 Fixed claims issue Signed-off-by: kameshsr * MOSIP-35260 Fixed claims issue Signed-off-by: kameshsr * MOSIP-35260 Fixed get full name Signed-off-by: kameshsr * Fixed postconstruct method Signed-off-by: kameshsr * MOSIP-35260 Corrected method Signed-off-by: kameshsr --------- Signed-off-by: kameshsr --- .../service/ResidentConfigService.java | 1 + .../impl/ResidentConfigServiceImpl.java | 2 +- .../service/impl/ResidentServiceImpl.java | 2 +- .../io/mosip/resident/util/IdentityUtil.java | 70 ++++++++++++++++++- .../mosip/resident/util/IdentityUtilTest.java | 12 ++-- 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/handler/service/ResidentConfigService.java b/resident/resident-service/src/main/java/io/mosip/resident/handler/service/ResidentConfigService.java index 5a30fdb0903..9c9c76d2160 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/handler/service/ResidentConfigService.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/handler/service/ResidentConfigService.java @@ -57,4 +57,5 @@ public interface ResidentConfigService { */ Map>> getUISchemaCacheableData(String schemaType); + Map getIdentityMappingMap() throws ResidentServiceCheckedException, IOException; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentConfigServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentConfigServiceImpl.java index 87b39b5b62d..bcf3d2fa071 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentConfigServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentConfigServiceImpl.java @@ -195,7 +195,7 @@ else if(identityMap.containsKey(attribute.getAttributeName())) { } public Map getIdentityMappingMap() - throws ResidentServiceCheckedException, IOException, JsonParseException, JsonMappingException { + throws ResidentServiceCheckedException, IOException { String identityMapping = getIdentityMapping(); Map identityMappingMap = objectMapper .readValue(identityMapping.getBytes(StandardCharsets.UTF_8), Map.class); diff --git a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java index 9d8987a14a9..6c3f7dc7f64 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/service/impl/ResidentServiceImpl.java @@ -2067,7 +2067,7 @@ public ResponseWrapper getUserinfo(String idaToken, String langCode try { Map identity = identityUtil .getIdentityAttributes(availableClaimUtility.getResidentIndvidualIdFromSession(), null); - name = utility.getMappingValue(identity, ResidentConstants.NAME, langCode); + name = identityUtil.getFullName(identity, langCode); } catch (IOException e) { logger.error("Error occured in accessing identity data %s", e.getMessage()); throw new ResidentServiceCheckedException(ResidentErrorCode.IO_EXCEPTION.getErrorCode(), diff --git a/resident/resident-service/src/main/java/io/mosip/resident/util/IdentityUtil.java b/resident/resident-service/src/main/java/io/mosip/resident/util/IdentityUtil.java index f91196d162f..895fb9ae297 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/util/IdentityUtil.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/util/IdentityUtil.java @@ -3,6 +3,7 @@ import io.mosip.kernel.core.logger.spi.Logger; import io.mosip.resident.config.LoggerConfiguration; import io.mosip.resident.constant.IdType; +import io.mosip.resident.constant.LoggerFileConstant; import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.constant.ResidentErrorCode; import io.mosip.resident.dto.IdResponseDTO1; @@ -11,6 +12,7 @@ import io.mosip.resident.exception.ResidentServiceCheckedException; import io.mosip.resident.exception.ResidentServiceException; import io.mosip.resident.handler.service.ResidentConfigService; +import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; @@ -32,6 +34,7 @@ @Component public class IdentityUtil { + private static final int SIZE = 1; private final Logger logger = LoggerConfiguration.logConfig(IdentityUtil.class); @Autowired @@ -71,6 +74,9 @@ public class IdentityUtil { @Autowired private PerpetualVidUtil perpetualVidUtil; + + private List nameValueList; + public Map getIdentityAttributes(String id, String schemaType) throws ResidentServiceCheckedException, IOException { return getIdentityAttributes(id, schemaType, List.of( Objects.requireNonNull(env.getProperty(ResidentConstants.ADDITIONAL_ATTRIBUTE_TO_FETCH)) @@ -178,8 +184,7 @@ public IdentityDTO getIdentity(String id, boolean fetchFace, String langCode) th LocalDate localDate=LocalDate.parse(dateOfBirth, formatter); identityDTO.setYearOfBirth(Integer.toString(localDate.getYear())); } - String name = utility.getMappingValue(identity, ResidentConstants.NAME, langCode); - identityDTO.setFullName(name); + identityDTO.setFullName(getFullName(identity, langCode)); identityDTO.putAll((Map) identity.get(IDENTITY)); if(fetchFace) { @@ -199,4 +204,65 @@ public IdentityDTO getIdentity(String id, boolean fetchFace, String langCode) th logger.debug("IdentityServiceImpl::getIdentity()::exit"); return identityDTO; } + + public String getFullName(Map identity, String langCode) throws ResidentServiceCheckedException, IOException { + if(nameValueList==null){ + nameValueList= getNameValueFromIdentityMapping(); + } + StringBuilder nameValue = new StringBuilder(); + for (String nameString : nameValueList) { + nameValue.append(getValueFromIdentityMapping(nameString, identity, langCode)); + } + return String.valueOf(nameValue); + } + + private String getValueFromIdentityMapping(String nameString, Map identity, String langCode) { + if (nameString == null || identity == null || langCode == null) { + return ""; // Return early if any input is null + } + + // Retrieve the identity value map + Map identityValueMap = (Map) identity.get(IDENTITY); + if (identityValueMap == null) { + return ""; // Return early if identity map is null + } + + // Retrieve the list of nameValueMap + List> nameValueMap = (List>) identityValueMap.get(nameString); + if (nameValueMap == null) { + return ""; // Return early if the nameValueMap is null + } + + // Use stream to find the matching language and return the corresponding value + return nameValueMap.stream() + .filter(nameMap -> langCode.equalsIgnoreCase((String) nameMap.get(ResidentConstants.LANGUAGE))) + .map(nameMap -> (String) nameMap.get(ResidentConstants.VALUE)) + .findFirst() // Get the first matching value + .orElse(""); // Return an empty string if no match is found + } + + + @PostConstruct + public List getNameValueFromIdentityMapping() throws ResidentServiceCheckedException { + if (Objects.isNull(nameValueList)) { + try { + Map identityMappingMap = residentConfigService.getIdentityMappingMap(); + Map nameMap = (Map) identityMappingMap.get(ResidentConstants.NAME); + String nameValue = (String) nameMap.get(ResidentConstants.VALUE); + + if(nameValue.contains(ResidentConstants.COMMA)){ + nameValueList = List.of(nameValue.split(ResidentConstants.COMMA)); + } else{ + nameValueList = List.of(nameValue); + } + } catch (IOException e) { + logger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + "getNameValueFromIdentityMapping", + ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorCode() + org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e)); + throw new ResidentServiceCheckedException(ResidentErrorCode.POLICY_EXCEPTION.getErrorCode(), + ResidentErrorCode.POLICY_EXCEPTION.getErrorMessage(), e); + } + } + return nameValueList; + } } 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 998439c1bfe..82cf5cde622 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 @@ -19,10 +19,7 @@ import org.springframework.test.util.ReflectionTestUtils; import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import static io.mosip.resident.service.impl.IdentityServiceTest.getAuthUserDetailsFromAuthentication; import static org.junit.Assert.assertEquals; @@ -70,7 +67,7 @@ public class IdentityUtilTest { private String uin; @Before - public void setup() throws ApisResourceAccessException, ResidentServiceCheckedException { + public void setup() throws ApisResourceAccessException, ResidentServiceCheckedException, IOException { Map identityMap = new LinkedHashMap(); uin = "8251649601"; identityMap.put("UIN", uin); @@ -88,6 +85,11 @@ public void setup() throws ApisResourceAccessException, ResidentServiceCheckedEx 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")); + Map identityMappingMap = new HashMap<>(); + Map valueMap = new HashMap<>(); + valueMap.put("value", "fullName"); + identityMappingMap.put("name", valueMap); + when(residentConfigService.getIdentityMappingMap()).thenReturn(identityMappingMap); Optional perpVid = Optional.of("8251649601"); when(perpetualVidUtil.getPerpatualVid(anyString())).thenReturn(perpVid); }