From 9629aceff06ddea00bb1d5f25063b6176f60481b Mon Sep 17 00:00:00 2001 From: Nick Goupinets Date: Thu, 1 Apr 2021 15:52:52 -0400 Subject: [PATCH] Removed obsolete test and added more control to replace fields in TU --- .../java/ca/uhn/fhir/util/TerserUtil.java | 41 +++++++++++++++---- .../fhir/jpa/mdm/svc/MdmMatchLinkSvcTest.java | 22 ---------- .../java/ca/uhn/fhir/util/TerserUtilTest.java | 17 ++++++++ 3 files changed, 51 insertions(+), 29 deletions(-) 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 fc7e8fd0ced..126b2dfca3b 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 @@ -25,6 +25,7 @@ import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition; import ca.uhn.fhir.context.BaseRuntimeElementDefinition; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; +import org.apache.commons.lang3.tuple.Triple; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseResource; import org.slf4j.Logger; @@ -58,6 +59,17 @@ public final class TerserUtil { } }; + public static final Predicate> EXCLUDE_IDS_META_AND_EMPTY = new Predicate>() { + @Override + public boolean test(Triple theTriple) { + if (!EXCLUDE_IDS_AND_META.test(theTriple.getLeft().getElementName())) { + return false; + } + + return theTriple.getLeft().getAccessor().getValues(theTriple.getRight()).isEmpty(); + } + }; + public static final Predicate INCLUDE_ALL = new Predicate() { @Override public boolean test(String s) { @@ -235,20 +247,35 @@ public final class TerserUtil { } /** - * Replaces all fields that test positive by the given inclusion strategy. theTo will contain a copy of the + * Replaces all fields that have matching field names by the given inclusion strategy. theTo will contain a copy of the * values from theFrom instance. * - * @param theFhirContext Context holding resource definition - * @param theFrom The resource to merge the fields from - * @param theTo The resource to merge the fields into - * @param inclusionStrategy Inclusion strategy that checks if a given field should be replaced by checking {@link Predicate#test(Object)} + * @param theFhirContext Context holding resource definition + * @param theFrom The resource to merge the fields from + * @param theTo The resource to merge the fields into + * @param theFieldNameInclusion Inclusion strategy that checks if a given field should be replaced */ - public static void replaceFields(FhirContext theFhirContext, IBaseResource theFrom, IBaseResource theTo, Predicate inclusionStrategy) { + public static void replaceFields(FhirContext theFhirContext, IBaseResource theFrom, IBaseResource theTo, Predicate theFieldNameInclusion) { + Predicate> predicate + = (t) -> theFieldNameInclusion.test(t.getLeft().getElementName()); + replaceFieldsByPredicate(theFhirContext, theFrom, theTo, predicate); + } + + /** + * Replaces empty fields on theTo resource that test positive by the given predicate. theTo will contain a copy of the + * values from theFrom for which predicate tests positive. + * + * @param theFhirContext Context holding resource definition + * @param theFrom The resource to merge the fields from + * @param theTo The resource to merge the fields into + * @param thePredicate Predicate that checks if a given field should be replaced + */ + public static void replaceFieldsByPredicate(FhirContext theFhirContext, IBaseResource theFrom, IBaseResource theTo, Predicate> thePredicate) { FhirTerser terser = theFhirContext.newTerser(); RuntimeResourceDefinition definition = theFhirContext.getResourceDefinition(theFrom); for (BaseRuntimeChildDefinition childDefinition : definition.getChildrenAndExtension()) { - if (!inclusionStrategy.test(childDefinition.getElementName())) { + if (!thePredicate.test(Triple.of(childDefinition, theFrom, theTo))) { continue; } diff --git a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmMatchLinkSvcTest.java b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmMatchLinkSvcTest.java index 06eb9ebe9d4..b3236ccaa39 100644 --- a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmMatchLinkSvcTest.java +++ b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmMatchLinkSvcTest.java @@ -476,28 +476,6 @@ public class MdmMatchLinkSvcTest extends BaseMdmR4Test { assertThat(nameFirstRep.getGivenAsSingleString(), is(equalToIgnoringCase("paul"))); } - @Test - public void testPatientCreateDoesNotOverwriteGoldenResourceAttributesThatAreInvolvedInLinking() { - Patient paul = buildPaulPatient(); - paul.setGender(Enumerations.AdministrativeGender.MALE); - paul = createPatientAndUpdateLinks(paul); - - Patient sourcePatientFromTarget = (Patient) getGoldenResourceFromTargetResource(paul); - - assertThat(sourcePatientFromTarget.getGender(), is(equalTo(Enumerations.AdministrativeGender.MALE))); - - Patient paul2 = buildPaulPatient(); - paul2.setGender(Enumerations.AdministrativeGender.FEMALE); - paul2 = createPatientAndUpdateLinks(paul2); - - assertThat(paul2, is(sameGoldenResourceAs(paul))); - - //Newly matched patients aren't allowed to overwrite GoldenResource Attributes unless they are empty, - // so gender should still be set to male. - Patient paul2GoldenResource = (Patient) getGoldenResourceFromTargetResource(paul2); - assertThat(paul2GoldenResource.getGender(), is(equalTo(Enumerations.AdministrativeGender.MALE))); - } - @Test //Test Case #1 public void testPatientUpdatesOverwriteGoldenResourceData() { 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 d4af78f0162..a94efbfb845 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 @@ -77,6 +77,7 @@ class TerserUtilTest { assertEquals(check.getValue(), p.getBirthDate()); } + @Test void testFieldExists() { assertTrue(TerserUtil.fieldExists(ourFhirContext, "identifier", TerserUtil.newResource(ourFhirContext, "Patient"))); @@ -294,6 +295,22 @@ class TerserUtilTest { assertEquals("Doe", p2.getName().get(0).getFamily()); } + @Test + public void testReplaceFieldsByPredicate() { + Patient p1 = new Patient(); + p1.addName().setFamily("Doe"); + p1.setGender(Enumerations.AdministrativeGender.MALE); + + Patient p2 = new Patient(); + p2.addName().setFamily("Smith"); + + TerserUtil.replaceFieldsByPredicate(ourFhirContext, p1, p2, TerserUtil.EXCLUDE_IDS_META_AND_EMPTY); + + assertEquals(1, p2.getName().size()); + assertEquals("Smith", p2.getName().get(0).getFamily()); + assertEquals(Enumerations.AdministrativeGender.MALE, p2.getGender()); + } + @Test public void testClearFields() { Patient p1 = new Patient();