diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java index 47246aaa35c..75266c1de53 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java @@ -158,15 +158,19 @@ public final class TerserUtil { }); } - public static boolean equals(IBase theItem1, IBase theItem2){ + private static Method getMethod(IBase item){ Method method = null; - for (Method m : theItem1.getClass().getDeclaredMethods()) { + for (Method m : item.getClass().getDeclaredMethods()) { if (m.getName().equals("equalsDeep")) { method = m; break; } } - final Method m = method; + return method; + } + + public static boolean equals(IBase theItem1, IBase theItem2){ + final Method m = getMethod(theItem1); return equals(theItem1, theItem2, m); } @@ -183,18 +187,8 @@ public final class TerserUtil { } private static boolean contains(IBase theItem, List 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); + return theItems.stream().anyMatch(i -> equals(i, theItem, m)); } /** diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcher.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcher.java index 8fc246568cc..a3b4f014636 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcher.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcher.java @@ -11,25 +11,22 @@ import java.util.List; public class ExtensionMatcher implements IMdmFieldMatcher{ @Override public boolean matches(FhirContext theFhirContext, IBase theLeftBase, IBase theRightBase, boolean theExact, String theIdentifierSystem) { - List> leftExtension = null; - List> 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> leftExtension = ((IBaseHasExtensions) theLeftBase).getExtension(); + List> 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); - } - } + for (IBaseExtension rightExtensionValue : rightExtension) { + match |= ExtensionUtil.equals(leftExtensionValue, rightExtensionValue); } } return match; diff --git a/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcherR4Test.java b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcherR4Test.java index 21a4420cf87..ee586d9b734 100644 --- a/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcherR4Test.java +++ b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/rules/matcher/ExtensionMatcherR4Test.java @@ -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)); + } } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java index 52826cfc893..df0a9de8b06 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java @@ -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; @@ -237,4 +238,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)); + } }