Merge branch '2478_add_extension_mdm_matcher' of github.com:hapifhir/hapi-fhir into 2478_add_extension_mdm_matcher

This commit is contained in:
Nick Goupinets 2021-03-19 18:15:43 -04:00
commit 5f2c0a55b7
4 changed files with 54 additions and 29 deletions

View File

@ -47,6 +47,8 @@ public final class TerserUtil {
public static final String FIELD_NAME_IDENTIFIER = "identifier";
private static final String EQUALS_DEEP="equalsDeep";
public static final Collection<String> IDS_AND_META_EXCLUDES =
Collections.unmodifiableSet(Stream.of("id", "identifier", "meta").collect(Collectors.toSet()));
@ -175,15 +177,19 @@ public final class TerserUtil {
});
}
public static boolean equals(IBase theItem1, IBase theItem2){
private static Method getMethod(IBase item, String methodName){
Method method = null;
for (Method m : theItem1.getClass().getDeclaredMethods()) {
if (m.getName().equals("equalsDeep")) {
for (Method m : item.getClass().getDeclaredMethods()) {
if (m.getName().equals(methodName)) {
method = m;
break;
}
}
final Method m = method;
return method;
}
public static boolean equals(IBase theItem1, IBase theItem2){
final Method m = getMethod(theItem1, EQUALS_DEEP);
return equals(theItem1, theItem2, m);
}
@ -200,18 +206,8 @@ public final class TerserUtil {
}
private static boolean contains(IBase theItem, List<IBase> theItems) {
Method method = null;
for (Method m : theItem.getClass().getDeclaredMethods()) {
if (m.getName().equals("equalsDeep")) {
method = m;
break;
}
}
final Method m = method;
return theItems.stream().anyMatch(i -> {
return equals(i, theItem, m);
});
final Method m = getMethod(theItem, EQUALS_DEEP);
return theItems.stream().anyMatch(i -> equals(i, theItem, m));
}
/**

View File

@ -11,27 +11,24 @@ import java.util.List;
public class ExtensionMatcher implements IMdmFieldMatcher{
@Override
public boolean matches(FhirContext theFhirContext, IBase theLeftBase, IBase theRightBase, boolean theExact, String theIdentifierSystem) {
List<? extends IBaseExtension<?, ?>> leftExtension = null;
List<? extends IBaseExtension<?, ?>> rightExtension = null;
if (theLeftBase instanceof IBaseHasExtensions && theRightBase instanceof IBaseHasExtensions){
leftExtension = ((IBaseHasExtensions) theLeftBase).getExtension();
rightExtension = ((IBaseHasExtensions) theRightBase).getExtension();
}
else{
if (!(theLeftBase instanceof IBaseHasExtensions && theRightBase instanceof IBaseHasExtensions)){
return false;
}
List<? extends IBaseExtension<?, ?>> leftExtension = ((IBaseHasExtensions) theLeftBase).getExtension();
List<? extends IBaseExtension<?, ?>> rightExtension = ((IBaseHasExtensions) theRightBase).getExtension();
boolean match = false;
if (theIdentifierSystem != null) {
leftExtension.removeIf(iBaseExtension -> !iBaseExtension.getUrl().equals(theIdentifierSystem));
rightExtension.removeIf(iBaseExtension -> !iBaseExtension.getUrl().equals(theIdentifierSystem));
}
for (IBaseExtension leftExtensionValue : leftExtension) {
if (leftExtensionValue.getUrl().equals(theIdentifierSystem) || theIdentifierSystem == null) {
for (IBaseExtension rightExtensionValue : rightExtension) {
if (rightExtensionValue.getUrl().equals(theIdentifierSystem) || theIdentifierSystem == null) {
match |= ExtensionUtil.equals(leftExtensionValue, rightExtensionValue);
}
}
}
}
return match;
}
}

View File

@ -2,6 +2,7 @@ package ca.uhn.fhir.mdm.rules.matcher;
import org.hl7.fhir.r4.model.IntegerType;
import org.hl7.fhir.r4.model.Organization;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.StringType;
import org.junit.jupiter.api.Test;
@ -83,5 +84,12 @@ public class ExtensionMatcherR4Test extends BaseMatcherR4Test {
assertTrue(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
@Test
public void testPatientWithNoExtension(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
assertFalse(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
}

View File

@ -6,6 +6,7 @@ import org.hl7.fhir.r4.model.DateTimeType;
import org.hl7.fhir.r4.model.DateType;
import org.hl7.fhir.r4.model.Enumerations;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.HumanName;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.Test;
@ -239,4 +240,27 @@ class TerserUtilTest {
assertThat(p2.getName(), hasSize(1));
assertThat(p2.getName().get(0).getGiven(), hasSize(2));
}
@Test
void testEqualsFunction(){
Patient p1 = new Patient();
Patient p2 = new Patient();
p1.addName(new HumanName().setFamily("family").addGiven("asd"));
p2.addName(new HumanName().setFamily("family").addGiven("asd"));
assertTrue(TerserUtil.equals(p1, p2));
}
@Test
void testEqualsFunctionNotEqual(){
Patient p1 = new Patient();
Patient p2 = new Patient();
p1.addName(new HumanName().setFamily("family").addGiven("asd"));
p2.addName(new HumanName().setFamily("family").addGiven("asd1"));
assertFalse(TerserUtil.equals(p1, p2));
}
}