Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mosip 19313 allow norm for name id attribs #789

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.mosip.authentication.common.service.builder;
import static io.mosip.authentication.core.constant.IdAuthCommonConstants.ID_NAME;
import static io.mosip.authentication.core.constant.IdAuthCommonConstants.MAPPING_CONFIG;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -15,7 +18,6 @@
import io.mosip.authentication.common.service.impl.match.IdaIdMapping;
import io.mosip.authentication.common.service.util.EnvUtil;
import io.mosip.authentication.core.constant.IdAuthCommonConstants;
import io.mosip.authentication.core.constant.IdAuthConfigKeyConstants;
import io.mosip.authentication.core.constant.IdAuthenticationErrorConstants;
import io.mosip.authentication.core.exception.IdAuthUncheckedException;
import io.mosip.authentication.core.indauth.dto.AuthRequestDTO;
Expand Down Expand Up @@ -317,7 +319,9 @@ private MatchInput contstructMatchInput(AuthRequestDTO authRequestDTO, String id
matchValue = matchThresholdOpt.orElseGet(() -> EnvUtil.getDefaultMatchValue());
}
}
Map<String, Object> matchProperties = authType.getMatchProperties(authRequestDTO, idInfoFetcher, language);
Map<String, Object> matchProperties = new HashMap<>(authType.getMatchProperties(authRequestDTO, idInfoFetcher, language));
matchProperties.put(ID_NAME, idName);
matchProperties.put(MAPPING_CONFIG, idInfoFetcher.getMappingConfig());
return new MatchInput(authType, idName, matchType, matchingStrategy, matchValue, matchProperties, language);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.mosip.authentication.common.service.impl.match;
import static io.mosip.authentication.core.constant.IdAuthCommonConstants.ID_NAME;
import static io.mosip.authentication.core.constant.IdAuthCommonConstants.MAPPING_CONFIG;

import java.util.Map;

import io.mosip.authentication.core.spi.indauth.match.MappingConfig;
import io.mosip.authentication.core.spi.indauth.match.MatchFunction;
import io.mosip.authentication.core.spi.indauth.match.MatchingStrategyType;
import io.mosip.authentication.core.spi.indauth.match.TextMatchingStrategy;
Expand All @@ -18,12 +21,33 @@ public enum DynamicDemoAttributeMatchingStrategy implements TextMatchingStrategy

EXACT(MatchingStrategyType.EXACT, (Object reqInfo, Object entityInfo, Map<String, Object> props) -> {
if (reqInfo instanceof String && entityInfo instanceof String) {
return getDemoMatcherUtilObject(props).doExactMatch((String) reqInfo, (String) entityInfo);
} if (reqInfo.equals(entityInfo)) {
DemoMatcherUtil demoMatcherUtilObject = getDemoMatcherUtilObject(props);
Object idNameObj = props.get(ID_NAME);
Object mappingConfigObj = props.get(MAPPING_CONFIG);
if(idNameObj instanceof String && mappingConfigObj instanceof MappingConfig) {
MappingConfig mappingConfig = (MappingConfig) mappingConfigObj;
String idName = (String) idNameObj;
if(isNameAttribute(idName, mappingConfig)) {
return TextMatchingStrategy.normalizeAndMatch(reqInfo,
entityInfo,
props,
NameMatchingStrategy::normalizeText,
demoMatcherUtilObject::doExactMatch);
} else if(isFullAddressAttribute(idName, mappingConfig)) {
return TextMatchingStrategy.normalizeAndMatch(reqInfo,
entityInfo,
props,
FullAddressMatchingStrategy::normalizeText,
demoMatcherUtilObject::doExactMatch);
}
}

return demoMatcherUtilObject.doExactMatch((String)reqInfo, (String)entityInfo);
} else if (reqInfo.equals(entityInfo)) {
return DemoMatcherUtil.EXACT_MATCH_VALUE;
} else {
return 0;
}

return 0;
});

/** The match function. */
Expand All @@ -43,6 +67,20 @@ private DynamicDemoAttributeMatchingStrategy(MatchingStrategyType matchStrategyT
this.matchStrategyType = matchStrategyType;
}

private static boolean isFullAddressAttribute(String idName, MappingConfig mappingConfig) {
return IdaIdMapping.FULLADDRESS
.getMappingFunction()
.apply(mappingConfig, DemoMatchType.ADDR)
.contains(idName);
}

private static boolean isNameAttribute(String idName, MappingConfig mappingConfig) {
return IdaIdMapping.NAME
.getMappingFunction()
.apply(mappingConfig, DemoMatchType.NAME)
.contains(idName);
}

/* (non-Javadoc)
* @see io.mosip.authentication.core.spi.indauth.match.MatchingStrategy#getType()
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package io.mosip.authentication.common.service.impl.match;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.mockito.Mockito;

import io.mosip.authentication.core.spi.indauth.match.MappingConfig;
import io.mosip.authentication.core.spi.indauth.match.MatchingStrategyType;
import io.mosip.authentication.core.util.DemoMatcherUtil;

public class DynamicDemoAttributeMatchingStrategyTest {


@Test
public void testMatch_EmptyValues() throws Exception {
Map<String, Object> properties = Map.of("demoMatcherUtil", Mockito.mock(DemoMatcherUtil.class));
Map<String, String> reqValues = Map.of();
Map<String, String> entityValues = Map.of();
int res = DynamicDemoAttributeMatchingStrategy.EXACT.match(reqValues, entityValues, properties);
assertEquals(0, res);
}

@Test
public void testMatchFunction_NonStringValues_positive() throws Exception {
Map<String, Object> properties = Map.of("demoMatcherUtil", Mockito.mock(DemoMatcherUtil.class));
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(1, 1, properties);
assertEquals(100, res);
}

@Test
public void testMatchFunction_NonStringValues_negative() throws Exception {
Map<String, Object> properties = Map.of("demoMatcherUtil", Mockito.mock(DemoMatcherUtil.class));
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(1, 2, properties);
assertEquals(0, res);
}

@Test
public void testMatchFunction_StringValues_positive() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil);

String reqInfo = "abc";
String entityInfo = "abc";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(100);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);

assertEquals(100, res);
}

@Test
public void testMatchFunction_StringValues_negative() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil);

String reqInfo = "abc";
String entityInfo = "xyz";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(0);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);
assertEquals(0, res);
}

@Test
public void testMatchFunction_StringValues_NotMapped_checkIdName_positive() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
String idName = "residenceStatus";
MappingConfig mappingConfig = Mockito.mock(MappingConfig.class);
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil
,"idName",idName,"mappingConfig", mappingConfig);

String reqInfo = "abc";
String entityInfo = "abc";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(100);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);

assertEquals(100, res);
}

@Test
public void testMatchFunction_StringValues_NotMapped_checkIdName_negative() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
String idName = "residenceStatus";
MappingConfig mappingConfig = Mockito.mock(MappingConfig.class);
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil
,"idName",idName,"mappingConfig", mappingConfig);

String reqInfo = "abc";
String entityInfo = "xyz";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(0);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);

assertEquals(0, res);
}

@Test
public void testMatchFunction_Name_Mapped_checkIdName_positive() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
String idName = "residenceStatus";
MappingConfig mappingConfig = Mockito.mock(MappingConfig.class);
Mockito.when(mappingConfig.getName()).thenReturn(List.of(idName));
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil
,"idName",idName,"mappingConfig", mappingConfig);

String reqInfo = "abc";
String entityInfo = "abc";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(100);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);

assertEquals(100, res);
}

@Test
public void testMatchFunction_Name_Mapped_checkIdName_negative() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
String idName = "residenceStatus";
MappingConfig mappingConfig = Mockito.mock(MappingConfig.class);
Mockito.when(mappingConfig.getName()).thenReturn(List.of(idName));
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil
,"idName",idName,"mappingConfig", mappingConfig);

String reqInfo = "abc";
String entityInfo = "xyz";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(0);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);

assertEquals(0, res);
}

@Test
public void testMatchFunction_Addr_Mapped_checkIdName_positive() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
String idName = "fullName";
MappingConfig mappingConfig = Mockito.mock(MappingConfig.class);
Mockito.when(mappingConfig.getFullAddress()).thenReturn(List.of(idName));
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil
,"idName",idName,"mappingConfig", mappingConfig);

String reqInfo = "abc";
String entityInfo = "abc";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(100);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);

assertEquals(100, res);
}

@Test
public void testMatchFunction_Addr_Mapped_checkIdName_negative() throws Exception {
DemoMatcherUtil demoMatcherUtil = Mockito.mock(DemoMatcherUtil.class);
String idName = "zone";
MappingConfig mappingConfig = Mockito.mock(MappingConfig.class);
Mockito.when(mappingConfig.getFullAddress()).thenReturn(List.of(idName));
Map<String, Object> properties = Map.of("demoMatcherUtil", demoMatcherUtil
,"idName",idName,"mappingConfig", mappingConfig);

String reqInfo = "abc";
String entityInfo = "xyz";
Mockito.when(demoMatcherUtil.doExactMatch(reqInfo, entityInfo)).thenReturn(0);
int res = DynamicDemoAttributeMatchingStrategy.EXACT
.getMatchFunction().match(reqInfo, entityInfo, properties);

assertEquals(0, res);
}

@Test
public void testGetType() {
assertEquals(MatchingStrategyType.EXACT, DynamicDemoAttributeMatchingStrategy.EXACT.getType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ public final class IdAuthCommonConstants {
public static final String BIO_TYPE_SEPARATOR="_";

public static final String LANG_CODE_SEPARATOR="_";

public static final String MAPPING_CONFIG = "mappingConfig";

public static final String ID_NAME = "idName";


private IdAuthCommonConstants() {
Expand Down