From 84dcd51091e944fd1e29b2837cd808dec0935125 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 12 Feb 2018 09:25:59 -0500 Subject: [PATCH] Add tests for #814 --- .../ca/uhn/fhir/parser/JsonParserR4Test.java | 57 +++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java index 0d51f6c9535..ef437be5b74 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java @@ -5,10 +5,7 @@ import ca.uhn.fhir.util.TestUtil; import com.google.common.collect.Sets; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.hl7.fhir.r4.model.Bundle; -import org.hl7.fhir.r4.model.Extension; -import org.hl7.fhir.r4.model.Patient; -import org.hl7.fhir.r4.model.StringType; +import org.hl7.fhir.r4.model.*; import org.junit.AfterClass; import org.junit.Test; import org.slf4j.Logger; @@ -39,6 +36,58 @@ public class JsonParserR4Test { return b; } + /** + * See #814 + */ + @Test + public void testDuplicateContainedResourcesNotOutputtedTwice() { + MedicationDispense md = new MedicationDispense(); + + MedicationRequest mr = new MedicationRequest(); + md.addAuthorizingPrescription().setResource(mr); + + Medication med = new Medication(); + md.setMedication(new Reference(med)); + mr.setMedication(new Reference(med)); + + String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(md); + ourLog.info(encoded); + + int idx = encoded.indexOf("\"Medication\""); + assertNotEquals(-1, idx); + + idx = encoded.indexOf("\"Medication\"", idx + 1); + assertEquals(-1, idx); + + } + + /** + * See #814 + */ + @Test + public void testDuplicateContainedResourcesNotOutputtedTwiceWithManualIds() { + MedicationDispense md = new MedicationDispense(); + + MedicationRequest mr = new MedicationRequest(); + mr.setId("#MR"); + md.addAuthorizingPrescription().setResource(mr); + + Medication med = new Medication(); + med.setId("#MED"); + md.setMedication(new Reference(med)); + mr.setMedication(new Reference(med)); + + String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(md); + ourLog.info(encoded); + + int idx = encoded.indexOf("\"Medication\""); + assertNotEquals(-1, idx); + + idx = encoded.indexOf("\"Medication\"", idx + 1); + assertEquals(-1, idx); + + } + @Test public void testExcludeNothing() { IParser parser = ourCtx.newJsonParser().setPrettyPrint(true);