From 69525144f3814b0887ab793c4d167bc620de83ff Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 18 Sep 2024 14:07:09 +1000 Subject: [PATCH] #1723 fix R3 Quantity.copyValues() --- .../org/hl7/fhir/dstu3/model/Quantity.java | 7 +++- .../MedicationAdministrationCopyTest.java | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/MedicationAdministrationCopyTest.java diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Quantity.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Quantity.java index fd3912594..1fe0a11e0 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Quantity.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Quantity.java @@ -1,6 +1,7 @@ package org.hl7.fhir.dstu3.model; import java.math.BigDecimal; +import java.util.ArrayList; /* Copyright (c) 2011+, HL7, Inc. @@ -668,12 +669,16 @@ public class Quantity extends Type implements ICompositeType { public Quantity copy() { Quantity dst = new Quantity(); copyValues(dst); + return dst; + } + + public void copyValues(Quantity dst) { + super.copyValues(dst); dst.value = value == null ? null : value.copy(); dst.comparator = comparator == null ? null : comparator.copy(); dst.unit = unit == null ? null : unit.copy(); dst.system = system == null ? null : system.copy(); dst.code = code == null ? null : code.copy(); - return dst; } protected Quantity typedCopy() { diff --git a/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/MedicationAdministrationCopyTest.java b/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/MedicationAdministrationCopyTest.java new file mode 100644 index 000000000..a1adeb216 --- /dev/null +++ b/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/MedicationAdministrationCopyTest.java @@ -0,0 +1,40 @@ +package org.hl7.fhir.dstu3.model; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.hl7.fhir.dstu3.formats.JsonParser; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class MedicationAdministrationCopyTest { + + @DisplayName("Test MedicationAdministration copy") + @Test + public void test() throws IOException { + MedicationAdministration beforeCopy = createMedAdminWithDosageDurationExtension(); + MedicationAdministration afterCopy = beforeCopy.copy(); + + System.out.println("---- BEFORE COPY (BEGIN)"); + System.out.println(new JsonParser().composeString(beforeCopy)); + System.out.println("---- BEFORE COPY (END)"); + System.out.println(); + System.out.println("---- AFTER COPY (BEGIN)"); + System.out.println(new JsonParser().composeString(afterCopy)); + System.out.println("---- AFTER COPY (END)"); + assertTrue(beforeCopy.equalsDeep(afterCopy)); + } + + private static MedicationAdministration createMedAdminWithDosageDurationExtension() { + MedicationAdministration resource = new MedicationAdministration(); + resource.setId("12345"); + var dosage = new MedicationAdministration.MedicationAdministrationDosageComponent(); + dosage.setDose((SimpleQuantity) new SimpleQuantity().setValue(40)) + .addExtension(new Extension() + .setUrl("http://duration") + .setValue(new Duration().setValue(5340000))); + resource.setDosage(dosage); + return resource; + } +}