Skip to content

Commit

Permalink
MOSIP-35260 Fix claims issue (#1353)
Browse files Browse the repository at this point in the history
* MOSIP-35260 Fix claims issue

Signed-off-by: kameshsr <[email protected]>

* MOSIP-35260 Refactor code

Signed-off-by: kameshsr <[email protected]>

* MOSIP-35260 Fixed claims issue

Signed-off-by: kameshsr <[email protected]>

* MOSIP-35260 Fixed claims issue

Signed-off-by: kameshsr <[email protected]>

* MOSIP-35260 Fixed get full name

Signed-off-by: kameshsr <[email protected]>

* Fixed postconstruct method

Signed-off-by: kameshsr <[email protected]>

* MOSIP-35260 Corrected method

Signed-off-by: kameshsr <[email protected]>

---------

Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
kameshsr authored Nov 6, 2024
1 parent f021c0e commit 9bdd2b5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ public interface ResidentConfigService {
*/
Map<String, Map<String, Map<String, Object>>> getUISchemaCacheableData(String schemaType);

Map<String, Object> getIdentityMappingMap() throws ResidentServiceCheckedException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ else if(identityMap.containsKey(attribute.getAttributeName())) {
}

public Map<String, Object> getIdentityMappingMap()
throws ResidentServiceCheckedException, IOException, JsonParseException, JsonMappingException {
throws ResidentServiceCheckedException, IOException {
String identityMapping = getIdentityMapping();
Map<String, Object> identityMappingMap = objectMapper
.readValue(identityMapping.getBytes(StandardCharsets.UTF_8), Map.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@ public ResponseWrapper<UserInfoDto> getUserinfo(String idaToken, String langCode
try {
Map<String, Object> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -32,6 +34,7 @@
@Component
public class IdentityUtil {

private static final int SIZE = 1;
private final Logger logger = LoggerConfiguration.logConfig(IdentityUtil.class);

@Autowired
Expand Down Expand Up @@ -71,6 +74,9 @@ public class IdentityUtil {
@Autowired
private PerpetualVidUtil perpetualVidUtil;


private List<String> nameValueList;

public Map<String, Object> getIdentityAttributes(String id, String schemaType) throws ResidentServiceCheckedException, IOException {
return getIdentityAttributes(id, schemaType, List.of(
Objects.requireNonNull(env.getProperty(ResidentConstants.ADDITIONAL_ATTRIBUTE_TO_FETCH))
Expand Down Expand Up @@ -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<? extends String, ? extends Object>) identity.get(IDENTITY));

if(fetchFace) {
Expand All @@ -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<String, Object> 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<String, Object> identity, String langCode) {
if (nameString == null || identity == null || langCode == null) {
return ""; // Return early if any input is null
}

// Retrieve the identity value map
Map<String, Object> identityValueMap = (Map<String, Object>) identity.get(IDENTITY);
if (identityValueMap == null) {
return ""; // Return early if identity map is null
}

// Retrieve the list of nameValueMap
List<Map<String, Object>> nameValueMap = (List<Map<String, Object>>) 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<String> getNameValueFromIdentityMapping() throws ResidentServiceCheckedException {
if (Objects.isNull(nameValueList)) {
try {
Map<String, Object> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<String, Object> identityMappingMap = new HashMap<>();
Map<String, String> valueMap = new HashMap<>();
valueMap.put("value", "fullName");
identityMappingMap.put("name", valueMap);
when(residentConfigService.getIdentityMappingMap()).thenReturn(identityMappingMap);
Optional<String> perpVid = Optional.of("8251649601");
when(perpetualVidUtil.getPerpatualVid(anyString())).thenReturn(perpVid);
}
Expand Down

0 comments on commit 9bdd2b5

Please sign in to comment.