From 10382c49150cc4b4d86d4abb020908997a961207 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 22 Jan 2021 16:46:18 -0500 Subject: [PATCH] Added a few more tests --- .../java/ca/uhn/fhir/mdm/util/TerserUtil.java | 17 +++ .../ca/uhn/fhir/mdm/util/TerserUtilTest.java | 102 +++++++++++++++++- 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java index 88c75228246..d6515f7e159 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java @@ -190,6 +190,10 @@ public final class TerserUtil { return definition.getChildByName(theFieldName) != null; } + public static void replaceField(FhirContext theFhirContext, String theFieldName, IBaseResource theFrom, IBaseResource theTo) { + replaceField(theFhirContext, theFhirContext.newTerser(), theFieldName, theFrom, theTo); + } + public static void replaceField(FhirContext theFhirContext, FhirTerser theTerser, String theFieldName, IBaseResource theFrom, IBaseResource theTo) { replaceField(theFrom, theTo, getBaseRuntimeChildDefinition(theFhirContext, theFieldName, theFrom)); } @@ -221,6 +225,19 @@ public final class TerserUtil { } } + /** + * Merges value of the specified field from theFrom resource to theTo resource. Fields values are compared via + * the equalsDeep method, or via object identity if this method is not available. + * + * @param theFhirContext + * @param theFieldName + * @param theFrom + * @param theTo + */ + public static void mergeField(FhirContext theFhirContext, String theFieldName, IBaseResource theFrom, IBaseResource theTo) { + mergeField(theFhirContext, theFhirContext.newTerser(), theFieldName, theFrom, theTo); + } + /** * Merges value of the specified field from theFrom resource to theTo resource. Fields values are compared via * the equalsDeep method, or via object identity if this method is not available. diff --git a/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java index c0eb2a2b09a..338fe934024 100644 --- a/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java +++ b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java @@ -2,13 +2,18 @@ package ca.uhn.fhir.mdm.util; import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.mdm.BaseR4Test; +import org.hl7.fhir.r4.model.DateTimeType; +import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Patient; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; class TerserUtilTest extends BaseR4Test { @@ -69,6 +74,101 @@ class TerserUtilTest extends BaseR4Test { assertThat(p2.getName().get(1).getGiven(), hasSize(2)); } + @Test + void testMergeForAddressWithExtensions() { + Extension ext = new Extension(); + ext.setUrl("http://hapifhir.io/extensions/address#create-timestamp"); + ext.setValue(new DateTimeType("2021-01-02T11:13:15")); + + Patient p1 = new Patient(); + p1.addAddress() + .addLine("10 Main Street") + .setCity("Hamilton") + .setState("ON") + .setPostalCode("Z0Z0Z0") + .setCountry("Canada") + .addExtension(ext); + + Patient p2 = new Patient(); + p2.addAddress().addLine("10 Lenin Street").setCity("Severodvinsk").setCountry("Russia"); + + TerserUtil.mergeField(ourFhirContext,"address", p1, p2); + + assertEquals(2, p2.getAddress().size()); + assertEquals("[10 Lenin Street]", p2.getAddress().get(0).getLine().toString()); + assertEquals("[10 Main Street]", p2.getAddress().get(1).getLine().toString()); + assertTrue(p2.getAddress().get(1).hasExtension()); + + p1 = new Patient(); + p1.addAddress().addLine("10 Main Street").addExtension(ext); + p2 = new Patient(); + p2.addAddress().addLine("10 Main Street").addExtension(new Extension("demo", new DateTimeType("2021-01-02"))); + + TerserUtil.mergeField(ourFhirContext,"address", p1, p2); + assertEquals(2, p2.getAddress().size()); + assertTrue(p2.getAddress().get(0).hasExtension()); + assertTrue(p2.getAddress().get(1).hasExtension()); + + } + + @Test + void testReplaceForAddressWithExtensions() { + Extension ext = new Extension(); + ext.setUrl("http://hapifhir.io/extensions/address#create-timestamp"); + ext.setValue(new DateTimeType("2021-01-02T11:13:15")); + + Patient p1 = new Patient(); + p1.addAddress() + .addLine("10 Main Street") + .setCity("Hamilton") + .setState("ON") + .setPostalCode("Z0Z0Z0") + .setCountry("Canada") + .addExtension(ext); + + Patient p2 = new Patient(); + p2.addAddress().addLine("10 Lenin Street").setCity("Severodvinsk").setCountry("Russia"); + + TerserUtil.replaceField(ourFhirContext,"address", p1, p2); + + assertEquals(1, p2.getAddress().size()); + assertEquals("[10 Main Street]", p2.getAddress().get(0).getLine().toString()); + assertTrue(p2.getAddress().get(0).hasExtension()); + } + + @Test + void testMergeForSimilarAddresses() { + Extension ext = new Extension(); + ext.setUrl("http://hapifhir.io/extensions/address#create-timestamp"); + ext.setValue(new DateTimeType("2021-01-02T11:13:15")); + + Patient p1 = new Patient(); + p1.addAddress() + .addLine("10 Main Street") + .setCity("Hamilton") + .setState("ON") + .setPostalCode("Z0Z0Z0") + .setCountry("Canada") + .addExtension(ext); + + Patient p2 = new Patient(); + p2.addAddress() + .addLine("10 Main Street") + .setCity("Hamilton") + .setState("ON") + .setPostalCode("Z0Z0Z1") + .setCountry("Canada") + .addExtension(ext); + + TerserUtil.mergeField(ourFhirContext,"address", p1, p2); + + assertEquals(2, p2.getAddress().size()); + assertEquals("[10 Main Street]", p2.getAddress().get(0).getLine().toString()); + assertEquals("[10 Main Street]", p2.getAddress().get(1).getLine().toString()); + assertTrue(p2.getAddress().get(1).hasExtension()); + } + + @Test void testCloneWithDuplicateNonPrimitives() { Patient p1 = new Patient();