diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BinaryUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BinaryUtil.java index b6c4411c8f3..cfa4323d718 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BinaryUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BinaryUtil.java @@ -1,5 +1,25 @@ package ca.uhn.fhir.util; +/*- + * #%L + * HAPI FHIR - Core Library + * %% + * Copyright (C) 2014 - 2017 University Health Network + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.BaseRuntimeElementDefinition; import ca.uhn.fhir.context.FhirContext; diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java index 5be2e8e7977..9be7c175f0c 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java @@ -2,6 +2,7 @@ package ca.uhn.fhir.parser; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.util.TestUtil; +import com.google.common.collect.Sets; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Patient; import org.junit.AfterClass; @@ -20,6 +21,19 @@ public class XmlParserR4Test { private static final Logger ourLog = LoggerFactory.getLogger(XmlParserR4Test.class); private static FhirContext ourCtx = FhirContext.forR4(); + private Bundle createBundleWithPatient() { + Bundle b = new Bundle(); + b.setId("BUNDLEID"); + b.getMeta().addProfile("http://FOO"); + + Patient p = new Patient(); + p.setId("PATIENTID"); + p.getMeta().addProfile("http://BAR"); + p.addName().addGiven("GIVEN"); + b.addEntry().setResource(p); + return b; + } + @Test public void testExcludeNothing() { IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); @@ -45,6 +59,32 @@ public class XmlParserR4Test { assertEquals("GIVEN", ((Patient) b.getEntry().get(0).getResource()).getNameFirstRep().getGivenAsSingleString()); } + @Test + public void testExcludeRootStuff() { + IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); + Set excludes = new HashSet<>(); + excludes.add("id"); + excludes.add("meta"); + parser.setDontEncodeElements(excludes); + + Bundle b = createBundleWithPatient(); + + String encoded = parser.encodeResourceToString(b); + ourLog.info(encoded); + + assertThat(encoded, not(containsString("BUNDLEID"))); + assertThat(encoded, not(containsString("http://FOO"))); + assertThat(encoded, (containsString("PATIENTID"))); + assertThat(encoded, (containsString("http://BAR"))); + assertThat(encoded, containsString("GIVEN")); + + b = parser.parseResource(Bundle.class, encoded); + + assertNotEquals("BUNDLEID", b.getIdElement().getIdPart()); + assertEquals("Patient/PATIENTID", ((Patient) b.getEntry().get(0).getResource()).getId()); + assertEquals("GIVEN", ((Patient) b.getEntry().get(0).getResource()).getNameFirstRep().getGivenAsSingleString()); + } + @Test public void testExcludeStarDotStuff() { IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); @@ -71,45 +111,6 @@ public class XmlParserR4Test { assertEquals("GIVEN", ((Patient) b.getEntry().get(0).getResource()).getNameFirstRep().getGivenAsSingleString()); } - @Test - public void testExcludeRootStuff() { - IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); - Set excludes = new HashSet<>(); - excludes.add("id"); - excludes.add("meta"); - parser.setDontEncodeElements(excludes); - - Bundle b = createBundleWithPatient(); - - String encoded = parser.encodeResourceToString(b); - ourLog.info(encoded); - - assertThat(encoded, not(containsString("BUNDLEID"))); - assertThat(encoded, not(containsString("http://FOO"))); - assertThat(encoded, (containsString("PATIENTID"))); - assertThat(encoded, (containsString("http://BAR"))); - assertThat(encoded, containsString("GIVEN")); - - b = parser.parseResource(Bundle.class, encoded); - - assertNotEquals("BUNDLEID", b.getIdElement().getIdPart()); - assertEquals("Patient/PATIENTID", ((Patient) b.getEntry().get(0).getResource()).getId()); - assertEquals("GIVEN", ((Patient) b.getEntry().get(0).getResource()).getNameFirstRep().getGivenAsSingleString()); - } - - private Bundle createBundleWithPatient() { - Bundle b = new Bundle(); - b.setId("BUNDLEID"); - b.getMeta().addProfile("http://FOO"); - - Patient p = new Patient(); - p.setId("PATIENTID"); - p.getMeta().addProfile("http://BAR"); - p.addName().addGiven("GIVEN"); - b.addEntry().setResource(p); - return b; - } - @Test public void testParseAndEncodeExtensionWithValueWithExtension() { String input = "\n" + @@ -131,11 +132,16 @@ public class XmlParserR4Test { " \n" + ""; - Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, input); + IParser xmlParser = ourCtx.newXmlParser(); + IParser jsonParser = ourCtx.newJsonParser(); + jsonParser.setDontEncodeElements(Sets.newHashSet("id", "meta")); + xmlParser.setDontEncodeElements(Sets.newHashSet("id", "meta")); - ourLog.info(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed)); - assertThat(ourCtx.newXmlParser().encodeResourceToString(parsed), containsString("Underweight")); - assertThat(ourCtx.newJsonParser().encodeResourceToString(parsed), containsString("Underweight")); + Patient parsed = xmlParser.parseResource(Patient.class, input); + + ourLog.info(jsonParser.setPrettyPrint(true).encodeResourceToString(parsed)); + assertThat(xmlParser.encodeResourceToString(parsed), containsString("Underweight")); + assertThat(jsonParser.encodeResourceToString(parsed), containsString("Underweight")); }