From 4573b86972ff42df0331ef5aae92f076ed6796ff Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Tue, 1 Mar 2016 07:34:11 -0500 Subject: [PATCH] Add tests for #275 --- .../uhn/fhir/parser/XmlParserDstu2Test.java | 91 +++++- .../uhn/fhir/parser/XmlParserDstu3Test.java | 304 +++++++++++------- .../src/test/resources/web_.xml | 27 -- 3 files changed, 266 insertions(+), 156 deletions(-) delete mode 100644 hapi-fhir-testpage-overlay/src/test/resources/web_.xml diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java index ec3fbc4765c..1ed60605ead 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java @@ -12,6 +12,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -33,7 +34,6 @@ import org.hamcrest.core.StringContains; import org.hamcrest.text.StringContainsInOrder; import org.hl7.fhir.instance.model.api.IBaseResource; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -97,7 +97,6 @@ public class XmlParserDstu2Test { private static final FhirContext ourCtx = FhirContext.forDstu2(); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserDstu2Test.class); - @Test public void testBundleWithBinary() { //@formatter:off @@ -150,6 +149,7 @@ public class XmlParserDstu2Test { assertThat(encoded, containsString("")); } + @Test public void testChoiceTypeWithProfiledType2() { Parameters par = new Parameters(); @@ -923,7 +923,7 @@ public class XmlParserDstu2Test { assertThat(encoded, containsString("scheme")); assertThat(encoded, not(containsString("Label"))); } - + @Test public void testEncodeExtensionWithResourceContent() { IParser parser = ourCtx.newXmlParser(); @@ -945,7 +945,6 @@ public class XmlParserDstu2Test { } - @Test public void testEncodeNarrativeSuppressed() { Patient patient = new Patient(); @@ -964,7 +963,7 @@ public class XmlParserDstu2Test { assertThat(encoded, containsString("family")); assertThat(encoded, containsString("maritalStatus")); } - + @Test public void testEncodeNonContained() { // Create an organization @@ -1061,7 +1060,7 @@ public class XmlParserDstu2Test { assertThat(encoded, not(containsString("maritalStatus"))); } - + @Test public void testEncodeSummary2() { Patient patient = new Patient(); @@ -1087,8 +1086,6 @@ public class XmlParserDstu2Test { @Test public void testEncodeWithEncodeElements() throws Exception { - String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml")); - Patient patient = new Patient(); patient.addName().addFamily("FAMILY"); patient.addAddress().addLine("LINE1"); @@ -1135,6 +1132,7 @@ public class XmlParserDstu2Test { } + @Test public void testMoreExtensions() throws Exception { @@ -1189,7 +1187,6 @@ public class XmlParserDstu2Test { assertThat(enc, containsString("")); } - @Test public void testOmitResourceId() { Patient p = new Patient(); @@ -1236,6 +1233,7 @@ public class XmlParserDstu2Test { } + @Test public void testParseAndEncodeBundleNewStyle() throws Exception { String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml")); @@ -1272,7 +1270,7 @@ public class XmlParserDstu2Test { assertTrue(d.toString(), d.identical()); } - + @Test public void testParseAndEncodeComments() throws IOException { //@formatter:off @@ -1603,7 +1601,52 @@ public class XmlParserDstu2Test { assertEquals(input, output); } - + @Test + public void testParseAndEncodeNestedExtensions() { + //@formatter:off + String input = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + + Patient p = ourCtx.newXmlParser().parseResource(Patient.class, input); + DateDt bd = p.getBirthDateElement(); + assertEquals("2005-03-04", bd.getValueAsString()); + + List exts = bd.getUndeclaredExtensionsByUrl("http://my.fancy.extension.url"); + assertEquals(1, exts.size()); + ExtensionDt ext = exts.get(0); + assertEquals(null, ext.getValue()); + + exts = ext.getUndeclaredExtensionsByUrl("http://my.fancy.extension.url"); + assertEquals(1, exts.size()); + ext = exts.get(0); + assertEquals("myNestedValue", ((StringDt)ext.getValue()).getValue()); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(p); + ourLog.info(encoded); + + //@formatter:off + assertThat(encoded, stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "")); + //@formatter:on + + } + @Test public void testParseBundleNewWithPlaceholderIds() { //@formatter:off @@ -1624,7 +1667,8 @@ public class XmlParserDstu2Test { assertEquals("urn:oid:0.1.2.3", parsed.getEntry().get(0).getResource().getId().getValue()); } - + + @Test public void testParseBundleNewWithPlaceholderIdsInBase1() { //@formatter:off @@ -2005,6 +2049,29 @@ public class XmlParserDstu2Test { assertEquals(htmlNs, p.getText().getDiv().getValueAsString()); } + @Test + public void testParseNestedExtensionsInvalid() { + //@formatter:off + String input = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + + try { + ourCtx.newXmlParser().parseResource(Patient.class, input); + fail(); + } catch (DataFormatException e) { + assertThat(e.getMessage(), containsString("Extension (URL='http://my.fancy.extension.url') must not have both a value and other contained extensions")); + } + } + /** * See #163 */ diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java index afa269c898e..f7ac1cf4238 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java @@ -12,6 +12,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -31,15 +32,20 @@ import org.custommonkey.xmlunit.XMLUnit; import org.hamcrest.collection.IsEmptyCollection; import org.hamcrest.core.StringContains; import org.hamcrest.text.StringContainsInOrder; +import org.hl7.fhir.dstu3.model.Address.AddressUse; import org.hl7.fhir.dstu3.model.AllergyIntolerance; import org.hl7.fhir.dstu3.model.Annotation; import org.hl7.fhir.dstu3.model.Binary; import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; +import org.hl7.fhir.dstu3.model.Bundle.BundleLinkComponent; +import org.hl7.fhir.dstu3.model.Bundle.BundleType; import org.hl7.fhir.dstu3.model.CodeType; import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.Composition; import org.hl7.fhir.dstu3.model.ConceptMap; +import org.hl7.fhir.dstu3.model.ContactPoint.ContactPointSystem; import org.hl7.fhir.dstu3.model.DataElement; import org.hl7.fhir.dstu3.model.DateTimeType; import org.hl7.fhir.dstu3.model.DateType; @@ -47,16 +53,23 @@ import org.hl7.fhir.dstu3.model.DiagnosticReport; import org.hl7.fhir.dstu3.model.DocumentManifest; import org.hl7.fhir.dstu3.model.Duration; import org.hl7.fhir.dstu3.model.ElementDefinition; +import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionBindingComponent; import org.hl7.fhir.dstu3.model.Encounter; +import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender; +import org.hl7.fhir.dstu3.model.Enumerations.DocumentReferenceStatus; import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.HumanName; +import org.hl7.fhir.dstu3.model.HumanName.NameUse; import org.hl7.fhir.dstu3.model.IdType; import org.hl7.fhir.dstu3.model.Identifier; +import org.hl7.fhir.dstu3.model.Identifier.IdentifierUse; import org.hl7.fhir.dstu3.model.InstantType; import org.hl7.fhir.dstu3.model.Medication; import org.hl7.fhir.dstu3.model.MedicationOrder; import org.hl7.fhir.dstu3.model.MedicationStatement; import org.hl7.fhir.dstu3.model.Observation; +import org.hl7.fhir.dstu3.model.Observation.ObservationRelationshipType; +import org.hl7.fhir.dstu3.model.Observation.ObservationStatus; import org.hl7.fhir.dstu3.model.Organization; import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.dstu3.model.Quantity; @@ -65,18 +78,6 @@ import org.hl7.fhir.dstu3.model.SimpleQuantity; import org.hl7.fhir.dstu3.model.StringType; import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.dstu3.model.ValueSet; -import org.hl7.fhir.dstu3.model.Address.AddressUse; -import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; -import org.hl7.fhir.dstu3.model.Bundle.BundleLinkComponent; -import org.hl7.fhir.dstu3.model.Bundle.BundleType; -import org.hl7.fhir.dstu3.model.ContactPoint.ContactPointSystem; -import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionBindingComponent; -import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender; -import org.hl7.fhir.dstu3.model.Enumerations.DocumentReferenceStatus; -import org.hl7.fhir.dstu3.model.HumanName.NameUse; -import org.hl7.fhir.dstu3.model.Identifier.IdentifierUse; -import org.hl7.fhir.dstu3.model.Observation.ObservationRelationshipType; -import org.hl7.fhir.dstu3.model.Observation.ObservationStatus; import org.hl7.fhir.instance.model.api.IBaseResource; import org.junit.After; import org.junit.BeforeClass; @@ -99,95 +100,6 @@ public class XmlParserDstu3Test { ourCtx.setNarrativeGenerator(null); } - @Test - public void testParseAndEncodeCommentsOnExtensions() { - //@formatter:off - String input = - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - ""; - - Patient pat = ourCtx.newXmlParser().parseResource(Patient.class, input); - String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pat); - ourLog.info(output); - - assertThat(output, stringContainsInOrder( - "", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - "" - )); - - output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(pat); - ourLog.info(output); - - assertThat(output, stringContainsInOrder( - "{", - " \"resourceType\":\"Patient\",", - " \"id\":\"someid\",", - " \"_id\":{", - " \"fhir_comments\":[", - " \" comment 1 \"", - " ]", - " },", - " \"extension\":[", - " {", - " \"fhir_comments\":[", - " \" comment 2 \",", - " \" comment 7 \"", - " ],", - " \"url\":\"urn:patientext:att\",", - " \"valueAttachment\":{", - " \"fhir_comments\":[", - " \" comment 3 \",", - " \" comment 6 \"", - " ],", - " \"contentType\":\"aaaa\",", - " \"_contentType\":{", - " \"fhir_comments\":[", - " \" comment 4 \"", - " ]", - " },", - " \"data\":\"AAAA\",", - " \"_data\":{", - " \"fhir_comments\":[", - " \" comment 5 \"", - " ]", - " }", - " }", - " }", - " ]", - "}" - )); - - //@formatter:on - } - @Test public void testBundleWithBinary() { @@ -220,7 +132,7 @@ public class XmlParserDstu3Test { assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); } - + @Test public void testContainedResourceInExtensionUndeclared() { Patient p = new Patient(); @@ -242,6 +154,7 @@ public class XmlParserDstu3Test { o = (Organization) rr.getResource(); assertEquals("ORG", o.getName()); } + @Test public void testDuration() { @@ -257,6 +170,7 @@ public class XmlParserDstu3Test { assertThat(str, containsString("")); } + @Test public void testEncodeAndParseContained() { IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); @@ -325,7 +239,7 @@ public class XmlParserDstu3Test { assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "")))); } - + @Test public void testEncodeAndParseExtensionOnCode() { Organization o = new Organization(); @@ -344,7 +258,6 @@ public class XmlParserDstu3Test { assertEquals("acode", code.getValue()); } - @Test public void testEncodeAndParseExtensionOnReference() { @@ -371,7 +284,6 @@ public class XmlParserDstu3Test { } - @Test public void testEncodeAndParseExtensions() throws Exception { @@ -489,6 +401,7 @@ public class XmlParserDstu3Test { assertEquals("MR", patient.getIdentifier().get(0).getType().getCoding().get(0).getCode()); } + @Test public void testEncodeAndParseMetaProfileAndTags() { Patient p = new Patient(); @@ -556,7 +469,8 @@ public class XmlParserDstu3Test { assertEquals("sec_term2", tagList.get(1).getCode()); assertEquals("sec_label2", tagList.get(1).getDisplay()); } - + + @Test public void testEncodeAndParseMetaProfiles() { Patient p = new Patient(); @@ -894,8 +808,6 @@ public class XmlParserDstu3Test { } - - @Test public void testEncodeDoesntIncludeUuidId() { Patient p = new Patient(); @@ -928,6 +840,8 @@ public class XmlParserDstu3Test { assertThat(encoded, not(containsString("tag"))); } + + /** * #158 */ @@ -966,7 +880,7 @@ public class XmlParserDstu3Test { assertEquals("Organization/123", ref.getReference()); } - + @Test public void testEncodeNarrativeSuppressed() { Patient patient = new Patient(); @@ -987,7 +901,6 @@ public class XmlParserDstu3Test { assertThat(encoded, containsString("maritalStatus")); } - @Test public void testEncodeNonContained() { // Create an organization @@ -1064,7 +977,6 @@ public class XmlParserDstu3Test { assertThat(str, containsString("")); assertThat(str, containsString("")); } - @Test public void testEncodeSummary() { @@ -1084,6 +996,7 @@ public class XmlParserDstu3Test { assertThat(encoded, not(containsString("maritalStatus"))); } + @Test public void testEncodeSummary2() { Patient patient = new Patient(); @@ -1105,11 +1018,8 @@ public class XmlParserDstu3Test { assertThat(encoded, not(containsString("maritalStatus"))); } - @Test @Ignore public void testEncodeWithEncodeElements() throws Exception { - String content = IOUtils.toString(XmlParserDstu3Test.class.getResourceAsStream("/bundle-example.xml")); - Patient patient = new Patient(); patient.addName().addFamily("FAMILY"); patient.addAddress().addLine("LINE1"); @@ -1156,6 +1066,7 @@ public class XmlParserDstu3Test { } + @Test public void testEncodeWithNarrative() { Patient p = new Patient(); @@ -1223,7 +1134,7 @@ public class XmlParserDstu3Test { assertThat(enc, containsString("")); } - + @Test public void testOmitResourceId() { Patient p = new Patient(); @@ -1306,6 +1217,7 @@ public class XmlParserDstu3Test { assertTrue(d.toString(), d.identical()); } + @Test public void testParseAndEncodeComments() throws IOException { @@ -1413,6 +1325,95 @@ public class XmlParserDstu3Test { } + @Test + public void testParseAndEncodeCommentsOnExtensions() { + //@formatter:off + String input = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + Patient pat = ourCtx.newXmlParser().parseResource(Patient.class, input); + String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pat); + ourLog.info(output); + + assertThat(output, stringContainsInOrder( + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + "" + )); + + output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(pat); + ourLog.info(output); + + assertThat(output, stringContainsInOrder( + "{", + " \"resourceType\":\"Patient\",", + " \"id\":\"someid\",", + " \"_id\":{", + " \"fhir_comments\":[", + " \" comment 1 \"", + " ]", + " },", + " \"extension\":[", + " {", + " \"fhir_comments\":[", + " \" comment 2 \",", + " \" comment 7 \"", + " ],", + " \"url\":\"urn:patientext:att\",", + " \"valueAttachment\":{", + " \"fhir_comments\":[", + " \" comment 3 \",", + " \" comment 6 \"", + " ],", + " \"contentType\":\"aaaa\",", + " \"_contentType\":{", + " \"fhir_comments\":[", + " \" comment 4 \"", + " ]", + " },", + " \"data\":\"AAAA\",", + " \"_data\":{", + " \"fhir_comments\":[", + " \" comment 5 \"", + " ]", + " }", + " }", + " }", + " ]", + "}" + )); + + //@formatter:on + } + @Test public void testParseAndEncodeExtensionOnReference() { //@formatter:off @@ -1649,7 +1650,52 @@ public class XmlParserDstu3Test { assertEquals(input, output); } - + @Test + public void testParseAndEncodeNestedExtensions() { + //@formatter:off + String input = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + + Patient p = ourCtx.newXmlParser().parseResource(Patient.class, input); + DateType bd = p.getBirthDateElement(); + assertEquals("2005-03-04", bd.getValueAsString()); + + List exts = bd.getExtensionsByUrl("http://my.fancy.extension.url"); + assertEquals(1, exts.size()); + Extension ext = exts.get(0); + assertEquals(null, ext.getValue()); + + exts = ext.getExtensionsByUrl("http://my.fancy.extension.url"); + assertEquals(1, exts.size()); + ext = exts.get(0); + assertEquals("myNestedValue", ((StringType)ext.getValue()).getValue()); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(p); + ourLog.info(encoded); + + //@formatter:off + assertThat(encoded, stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "")); + //@formatter:on + + } + @Test public void testParseBundleNewWithPlaceholderIds() { //@formatter:off @@ -1670,7 +1716,8 @@ public class XmlParserDstu3Test { assertEquals("urn:oid:0.1.2.3", parsed.getEntry().get(0).getResource().getIdElement().getValue()); } - + + @Test public void testParseBundleNewWithPlaceholderIdsInBase1() { //@formatter:off @@ -2034,6 +2081,29 @@ public class XmlParserDstu3Test { assertEquals(htmlNs, p.getText().getDiv().getValueAsString()); } + @Test + public void testParseNestedExtensionsInvalid() { + //@formatter:off + String input = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + + try { + ourCtx.newXmlParser().parseResource(Patient.class, input); + fail(); + } catch (DataFormatException e) { + assertThat(e.getMessage(), containsString("Extension (URL='http://my.fancy.extension.url') must not have both a value and other contained extensions")); + } + } + /** * See #163 */ diff --git a/hapi-fhir-testpage-overlay/src/test/resources/web_.xml b/hapi-fhir-testpage-overlay/src/test/resources/web_.xml deleted file mode 100644 index db9662dabec..00000000000 --- a/hapi-fhir-testpage-overlay/src/test/resources/web_.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - spring - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - - - - 2 - - - - - spring - /* - - - - - -