Add tests for #275

This commit is contained in:
jamesagnew 2016-03-01 07:34:11 -05:00
parent c44b23f493
commit 4573b86972
3 changed files with 266 additions and 156 deletions

View File

@ -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("<valueMarkdown value=\"THIS IS MARKDOWN\"/>"));
}
@Test
public void testChoiceTypeWithProfiledType2() {
Parameters par = new Parameters();
@ -945,7 +945,6 @@ public class XmlParserDstu2Test {
}
@Test
public void testEncodeNarrativeSuppressed() {
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("<given value=\"Shmoe\"><extension url=\"http://examples.com#givenext_parent\"><extension url=\"http://examples.com#givenext_child\"><valueString value=\"CHILD\"/></extension></extension></given>"));
}
@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"));
@ -1603,6 +1601,51 @@ public class XmlParserDstu2Test {
assertEquals(input, output);
}
@Test
public void testParseAndEncodeNestedExtensions() {
//@formatter:off
String input = "<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <birthDate value=\"2005-03-04\">\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <valueString value=\"myNestedValue\"/>\n" +
" </extension>\n" +
" </extension>\n" +
" </birthDate>\n" +
"</Patient>";
//@formatter:on
Patient p = ourCtx.newXmlParser().parseResource(Patient.class, input);
DateDt bd = p.getBirthDateElement();
assertEquals("2005-03-04", bd.getValueAsString());
List<ExtensionDt> 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(
"<Patient xmlns=\"http://hl7.org/fhir\">",
"<birthDate value=\"2005-03-04\">",
"<extension url=\"http://my.fancy.extension.url\">",
"<extension url=\"http://my.fancy.extension.url\">",
"<valueString value=\"myNestedValue\"/>",
"</extension>",
"</extension>",
"</birthDate>",
"</Patient>"));
//@formatter:on
}
@Test
public void testParseBundleNewWithPlaceholderIds() {
@ -1625,6 +1668,7 @@ public class XmlParserDstu2Test {
}
@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 = "<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <birthDate value=\"2005-03-04\">\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <valueString value=\"myvalue\"/>\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <valueString value=\"myNestedValue\"/>\n" +
" </extension>\n" +
" </extension>\n" +
" </birthDate>\n" +
"</Patient>";
//@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
*/

View File

@ -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 =
"<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <!-- comment 1 -->\n" +
" <id value=\"someid\"/>\n" +
" <!-- comment 2 -->\n" +
" <extension url=\"urn:patientext:att\">\n" +
" <!-- comment 3 -->\n" +
" <valueAttachment>\n" +
" <!-- comment 4 -->\n" +
" <contentType value=\"aaaa\"/>\n" +
" <data value=\"AAAA\"/>\n" +
" <!-- comment 5 -->\n" +
" </valueAttachment>\n" +
" <!-- comment 6 -->\n" +
" </extension>\n" +
" <!-- comment 7 -->\n" +
"</Patient>";
Patient pat = ourCtx.newXmlParser().parseResource(Patient.class, input);
String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pat);
ourLog.info(output);
assertThat(output, stringContainsInOrder(
"<Patient xmlns=\"http://hl7.org/fhir\">",
" <!-- comment 1 -->",
" <id value=\"someid\"/>",
" <!-- comment 2 -->",
" <extension url=\"urn:patientext:att\">",
" <!-- comment 3 -->",
" <valueAttachment>",
" <!-- comment 4 -->",
" <contentType value=\"aaaa\"/>",
" <data value=\"AAAA\"/>",
" <!-- comment 5 -->",
" </valueAttachment>",
" <!-- comment 6 -->",
" </extension>",
" <!-- comment 7 -->",
"</Patient>"
));
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() {
@ -243,6 +155,7 @@ public class XmlParserDstu3Test {
assertEquals("ORG", o.getName());
}
@Test
public void testDuration() {
Encounter enc = new Encounter();
@ -257,6 +170,7 @@ public class XmlParserDstu3Test {
assertThat(str, containsString("<length><value value=\"123\"/><unit value=\"day\"/></length>"));
}
@Test
public void testEncodeAndParseContained() {
IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
@ -345,7 +259,6 @@ public class XmlParserDstu3Test {
}
@Test
public void testEncodeAndParseExtensionOnReference() {
DataElement de = new DataElement();
@ -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();
@ -557,6 +470,7 @@ public class XmlParserDstu3Test {
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
*/
@ -987,7 +901,6 @@ public class XmlParserDstu3Test {
assertThat(encoded, containsString("maritalStatus"));
}
@Test
public void testEncodeNonContained() {
// Create an organization
@ -1065,7 +978,6 @@ public class XmlParserDstu3Test {
assertThat(str, containsString("<reference value=\"Observation/phitcc_obs_bp_dia\"/>"));
}
@Test
public void testEncodeSummary() {
Patient patient = new Patient();
@ -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();
@ -1307,6 +1218,7 @@ public class XmlParserDstu3Test {
}
@Test
public void testParseAndEncodeComments() throws IOException {
//@formatter:off
@ -1413,6 +1325,95 @@ public class XmlParserDstu3Test {
}
@Test
public void testParseAndEncodeCommentsOnExtensions() {
//@formatter:off
String input =
"<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <!-- comment 1 -->\n" +
" <id value=\"someid\"/>\n" +
" <!-- comment 2 -->\n" +
" <extension url=\"urn:patientext:att\">\n" +
" <!-- comment 3 -->\n" +
" <valueAttachment>\n" +
" <!-- comment 4 -->\n" +
" <contentType value=\"aaaa\"/>\n" +
" <data value=\"AAAA\"/>\n" +
" <!-- comment 5 -->\n" +
" </valueAttachment>\n" +
" <!-- comment 6 -->\n" +
" </extension>\n" +
" <!-- comment 7 -->\n" +
"</Patient>";
Patient pat = ourCtx.newXmlParser().parseResource(Patient.class, input);
String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pat);
ourLog.info(output);
assertThat(output, stringContainsInOrder(
"<Patient xmlns=\"http://hl7.org/fhir\">",
" <!-- comment 1 -->",
" <id value=\"someid\"/>",
" <!-- comment 2 -->",
" <extension url=\"urn:patientext:att\">",
" <!-- comment 3 -->",
" <valueAttachment>",
" <!-- comment 4 -->",
" <contentType value=\"aaaa\"/>",
" <data value=\"AAAA\"/>",
" <!-- comment 5 -->",
" </valueAttachment>",
" <!-- comment 6 -->",
" </extension>",
" <!-- comment 7 -->",
"</Patient>"
));
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,6 +1650,51 @@ public class XmlParserDstu3Test {
assertEquals(input, output);
}
@Test
public void testParseAndEncodeNestedExtensions() {
//@formatter:off
String input = "<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <birthDate value=\"2005-03-04\">\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <valueString value=\"myNestedValue\"/>\n" +
" </extension>\n" +
" </extension>\n" +
" </birthDate>\n" +
"</Patient>";
//@formatter:on
Patient p = ourCtx.newXmlParser().parseResource(Patient.class, input);
DateType bd = p.getBirthDateElement();
assertEquals("2005-03-04", bd.getValueAsString());
List<Extension> 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(
"<Patient xmlns=\"http://hl7.org/fhir\">",
"<birthDate value=\"2005-03-04\">",
"<extension url=\"http://my.fancy.extension.url\">",
"<extension url=\"http://my.fancy.extension.url\">",
"<valueString value=\"myNestedValue\"/>",
"</extension>",
"</extension>",
"</birthDate>",
"</Patient>"));
//@formatter:on
}
@Test
public void testParseBundleNewWithPlaceholderIds() {
@ -1671,6 +1717,7 @@ public class XmlParserDstu3Test {
}
@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 = "<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <birthDate value=\"2005-03-04\">\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <valueString value=\"myvalue\"/>\n" +
" <extension url=\"http://my.fancy.extension.url\">\n" +
" <valueString value=\"myNestedValue\"/>\n" +
" </extension>\n" +
" </extension>\n" +
" </birthDate>\n" +
"</Patient>";
//@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
*/

View File

@ -1,27 +0,0 @@
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ./xsd/web-app_3_0.xsd">
<!-- Servlets -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>