Add unit tests for #144
This commit is contained in:
parent
e63323ca5c
commit
0ca1b9feab
|
@ -6,6 +6,7 @@ import static org.junit.Assert.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Observable;
|
||||||
|
|
||||||
import net.sf.json.JSON;
|
import net.sf.json.JSON;
|
||||||
import net.sf.json.JSONSerializer;
|
import net.sf.json.JSONSerializer;
|
||||||
|
@ -26,9 +27,9 @@ import ca.uhn.fhir.model.api.TagList;
|
||||||
import ca.uhn.fhir.model.base.composite.BaseCodingDt;
|
import ca.uhn.fhir.model.base.composite.BaseCodingDt;
|
||||||
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
|
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
|
||||||
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
|
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
|
||||||
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
|
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Binary;
|
import ca.uhn.fhir.model.dstu2.resource.Binary;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.MedicationPrescription;
|
import ca.uhn.fhir.model.dstu2.resource.MedicationPrescription;
|
||||||
|
import ca.uhn.fhir.model.dstu2.resource.Observation;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Patient;
|
import ca.uhn.fhir.model.dstu2.resource.Patient;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.QuestionnaireAnswers;
|
import ca.uhn.fhir.model.dstu2.resource.QuestionnaireAnswers;
|
||||||
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
|
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
|
||||||
|
@ -53,6 +54,43 @@ public class JsonParserTest {
|
||||||
assertEquals("{\"resourceType\":\"Binary\",\"id\":\"11\",\"meta\":{\"versionId\":\"22\"},\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", val);
|
assertEquals("{\"resourceType\":\"Binary\",\"id\":\"11\",\"meta\":{\"versionId\":\"22\"},\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* see #144 and #146
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testParseContained() {
|
||||||
|
|
||||||
|
FhirContext c = FhirContext.forDstu2();
|
||||||
|
IParser parser = c.newJsonParser().setPrettyPrint(true);
|
||||||
|
|
||||||
|
Observation o = new Observation();
|
||||||
|
o.getCode().setText("obs text");
|
||||||
|
|
||||||
|
Patient p = new Patient();
|
||||||
|
p.addName().addFamily("patient family");
|
||||||
|
o.getSubject().setResource(p);
|
||||||
|
|
||||||
|
String enc = parser.encodeResourceToString(o);
|
||||||
|
ourLog.info(enc);
|
||||||
|
|
||||||
|
//@formatter:off
|
||||||
|
assertThat(enc, stringContainsInOrder(
|
||||||
|
"\"resourceType\":\"Observation\"",
|
||||||
|
"\"contained\":[",
|
||||||
|
"\"resourceType\":\"Patient\",",
|
||||||
|
"\"id\":\"1\"",
|
||||||
|
"\"reference\":\"#1\""
|
||||||
|
));
|
||||||
|
//@formatter:on
|
||||||
|
|
||||||
|
o = parser.parseResource(Observation.class, enc);
|
||||||
|
assertEquals("obs text", o.getCode().getText());
|
||||||
|
|
||||||
|
assertNotNull(o.getSubject().getResource());
|
||||||
|
p = (Patient) o.getSubject().getResource();
|
||||||
|
assertEquals("patient family", p.getNameFirstRep().getFamilyAsSingleString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEncodeAndParseMetaProfileAndTags() {
|
public void testEncodeAndParseMetaProfileAndTags() {
|
||||||
Patient p = new Patient();
|
Patient p = new Patient();
|
||||||
|
@ -95,7 +133,7 @@ public class JsonParserTest {
|
||||||
Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc);
|
Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc);
|
||||||
List<IdDt> gotLabels = ResourceMetadataKeyEnum.PROFILES.get(parsed);
|
List<IdDt> gotLabels = ResourceMetadataKeyEnum.PROFILES.get(parsed);
|
||||||
|
|
||||||
assertEquals(2,gotLabels.size());
|
assertEquals(2, gotLabels.size());
|
||||||
|
|
||||||
IdDt label = (IdDt) gotLabels.get(0);
|
IdDt label = (IdDt) gotLabels.get(0);
|
||||||
assertEquals("http://foo/Profile1", label.getValue());
|
assertEquals("http://foo/Profile1", label.getValue());
|
||||||
|
@ -109,7 +147,6 @@ public class JsonParserTest {
|
||||||
assertEquals(new Tag("scheme2", "term2", "label2"), tagList.get(1));
|
assertEquals(new Tag("scheme2", "term2", "label2"), tagList.get(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEncodeAndParseSecurityLabels() {
|
public void testEncodeAndParseSecurityLabels() {
|
||||||
Patient p = new Patient();
|
Patient p = new Patient();
|
||||||
|
@ -158,7 +195,7 @@ public class JsonParserTest {
|
||||||
Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc);
|
Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc);
|
||||||
List<BaseCodingDt> gotLabels = ResourceMetadataKeyEnum.SECURITY_LABELS.get(parsed);
|
List<BaseCodingDt> gotLabels = ResourceMetadataKeyEnum.SECURITY_LABELS.get(parsed);
|
||||||
|
|
||||||
assertEquals(2,gotLabels.size());
|
assertEquals(2, gotLabels.size());
|
||||||
|
|
||||||
CodingDt label = (CodingDt) gotLabels.get(0);
|
CodingDt label = (CodingDt) gotLabels.get(0);
|
||||||
assertEquals("SYSTEM1", label.getSystem());
|
assertEquals("SYSTEM1", label.getSystem());
|
||||||
|
@ -175,7 +212,6 @@ public class JsonParserTest {
|
||||||
assertEquals("VERSION2", label.getVersion());
|
assertEquals("VERSION2", label.getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fixing #89
|
* Fixing #89
|
||||||
*/
|
*/
|
||||||
|
@ -236,38 +272,11 @@ public class JsonParserTest {
|
||||||
ourLog.info(output);
|
ourLog.info(output);
|
||||||
|
|
||||||
String enc = ourCtx.newJsonParser().encodeResourceToString(patient);
|
String enc = ourCtx.newJsonParser().encodeResourceToString(patient);
|
||||||
assertThat(enc, Matchers.stringContainsInOrder("{\"resourceType\":\"Patient\",",
|
assertThat(enc, Matchers.stringContainsInOrder("{\"resourceType\":\"Patient\",", "\"extension\":[{\"url\":\"http://example.com/extensions#someext\",\"valueDateTime\":\"2011-01-02T11:13:15\"}",
|
||||||
"\"extension\":[{\"url\":\"http://example.com/extensions#someext\",\"valueDateTime\":\"2011-01-02T11:13:15\"}",
|
"{\"url\":\"http://example.com#parent\",\"extension\":[{\"url\":\"http://example.com#child\",\"valueString\":\"value1\"},{\"url\":\"http://example.com#child\",\"valueString\":\"value2\"}]}"));
|
||||||
"{\"url\":\"http://example.com#parent\",\"extension\":[{\"url\":\"http://example.com#child\",\"valueString\":\"value1\"},{\"url\":\"http://example.com#child\",\"valueString\":\"value2\"}]}"
|
assertThat(enc, Matchers.stringContainsInOrder("\"modifierExtension\":[" + "{" + "\"url\":\"http://example.com/extensions#modext\"," + "\"valueDate\":\"1995-01-02\"" + "}" + "],"));
|
||||||
));
|
assertThat(enc, containsString("\"_given\":[" + "{" + "\"extension\":[" + "{" + "\"url\":\"http://examples.com#givenext\"," + "\"valueString\":\"given\"" + "}" + "]" + "}," + "{" + "\"extension\":[" + "{" + "\"url\":\"http://examples.com#givenext_parent\"," + "\"extension\":[" + "{"
|
||||||
assertThat(enc, Matchers.stringContainsInOrder("\"modifierExtension\":[" +
|
+ "\"url\":\"http://examples.com#givenext_child\"," + "\"valueString\":\"CHILD\"" + "}" + "]" + "}" + "]" + "}"));
|
||||||
"{" +
|
|
||||||
"\"url\":\"http://example.com/extensions#modext\"," +
|
|
||||||
"\"valueDate\":\"1995-01-02\"" +
|
|
||||||
"}" +
|
|
||||||
"],"));
|
|
||||||
assertThat(enc, containsString("\"_given\":[" +
|
|
||||||
"{" +
|
|
||||||
"\"extension\":[" +
|
|
||||||
"{" +
|
|
||||||
"\"url\":\"http://examples.com#givenext\"," +
|
|
||||||
"\"valueString\":\"given\"" +
|
|
||||||
"}" +
|
|
||||||
"]" +
|
|
||||||
"}," +
|
|
||||||
"{" +
|
|
||||||
"\"extension\":[" +
|
|
||||||
"{" +
|
|
||||||
"\"url\":\"http://examples.com#givenext_parent\"," +
|
|
||||||
"\"extension\":[" +
|
|
||||||
"{" +
|
|
||||||
"\"url\":\"http://examples.com#givenext_child\"," +
|
|
||||||
"\"valueString\":\"CHILD\"" +
|
|
||||||
"}" +
|
|
||||||
"]" +
|
|
||||||
"}" +
|
|
||||||
"]" +
|
|
||||||
"}"));
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Now parse this back
|
* Now parse this back
|
||||||
|
@ -307,8 +316,6 @@ public class JsonParserTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* #65
|
* #65
|
||||||
*/
|
*/
|
||||||
|
@ -394,8 +401,6 @@ public class JsonParserTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParsePatientInBundle() {
|
public void testParsePatientInBundle() {
|
||||||
|
|
||||||
|
@ -455,22 +460,25 @@ public class JsonParserTest {
|
||||||
String content = IOUtils.toString(JsonParserTest.class.getResourceAsStream("/bundle-transaction.json"));
|
String content = IOUtils.toString(JsonParserTest.class.getResourceAsStream("/bundle-transaction.json"));
|
||||||
|
|
||||||
Bundle parsed = ourCtx.newJsonParser().parseBundle(content);
|
Bundle parsed = ourCtx.newJsonParser().parseBundle(content);
|
||||||
// assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue());
|
// assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue());
|
||||||
// assertEquals("1", parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION));
|
// assertEquals("1", parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION));
|
||||||
// assertEquals("1", parsed.getId().getVersionIdPart());
|
// assertEquals("1", parsed.getId().getVersionIdPart());
|
||||||
// assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED));
|
// assertEquals(new InstantDt("2014-08-18T01:43:30Z"),
|
||||||
// assertEquals("searchset", parsed.getType().getValue());
|
// parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED));
|
||||||
// assertEquals(3, parsed.getTotalResults().getValue().intValue());
|
// assertEquals("searchset", parsed.getType().getValue());
|
||||||
// assertEquals("http://example.com/base", parsed.getLinkBase().getValue());
|
// assertEquals(3, parsed.getTotalResults().getValue().intValue());
|
||||||
// assertEquals("https://example.com/base/MedicationPrescription?patient=347&searchId=ff15fd40-ff71-4b48-b366-09c706bed9d0&page=2", parsed.getLinkNext().getValue());
|
// assertEquals("http://example.com/base", parsed.getLinkBase().getValue());
|
||||||
// assertEquals("https://example.com/base/MedicationPrescription?patient=347&_include=MedicationPrescription.medication", parsed.getLinkSelf().getValue());
|
// assertEquals("https://example.com/base/MedicationPrescription?patient=347&searchId=ff15fd40-ff71-4b48-b366-09c706bed9d0&page=2",
|
||||||
//
|
// parsed.getLinkNext().getValue());
|
||||||
// assertEquals(2, parsed.getEntries().size());
|
// assertEquals("https://example.com/base/MedicationPrescription?patient=347&_include=MedicationPrescription.medication",
|
||||||
//
|
// parsed.getLinkSelf().getValue());
|
||||||
// MedicationPrescription p = (MedicationPrescription) parsed.getEntries().get(0).getResource();
|
//
|
||||||
// assertEquals("Patient/347", p.getPatient().getReference().getValue());
|
// assertEquals(2, parsed.getEntries().size());
|
||||||
// assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString());
|
//
|
||||||
// assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue());
|
// MedicationPrescription p = (MedicationPrescription) parsed.getEntries().get(0).getResource();
|
||||||
|
// assertEquals("Patient/347", p.getPatient().getReference().getValue());
|
||||||
|
// assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString());
|
||||||
|
// assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue());
|
||||||
|
|
||||||
String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed);
|
String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed);
|
||||||
ourLog.info(reencoded);
|
ourLog.info(reencoded);
|
||||||
|
@ -483,7 +491,7 @@ public class JsonParserTest {
|
||||||
String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§");
|
String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§");
|
||||||
String act = actual.toString().replace("\\r\\n", "\\n");
|
String act = actual.toString().replace("\\r\\n", "\\n");
|
||||||
|
|
||||||
exp=exp.replace(",\"ifNoneExist\":\"Patient?identifier=234234\"", "");
|
exp = exp.replace(",\"ifNoneExist\":\"Patient?identifier=234234\"", "");
|
||||||
|
|
||||||
ourLog.info("Expected: {}", exp);
|
ourLog.info("Expected: {}", exp);
|
||||||
ourLog.info("Actual : {}", act);
|
ourLog.info("Actual : {}", act);
|
||||||
|
@ -532,8 +540,6 @@ public class JsonParserTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseAndEncodeBundleWithDeletedEntry() {
|
public void testParseAndEncodeBundleWithDeletedEntry() {
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
package ca.uhn.fhir.parser;
|
package ca.uhn.fhir.parser;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -37,6 +33,7 @@ import ca.uhn.fhir.model.dstu2.resource.Composition;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Encounter;
|
import ca.uhn.fhir.model.dstu2.resource.Encounter;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Medication;
|
import ca.uhn.fhir.model.dstu2.resource.Medication;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.MedicationPrescription;
|
import ca.uhn.fhir.model.dstu2.resource.MedicationPrescription;
|
||||||
|
import ca.uhn.fhir.model.dstu2.resource.Observation;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Organization;
|
import ca.uhn.fhir.model.dstu2.resource.Organization;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Patient;
|
import ca.uhn.fhir.model.dstu2.resource.Patient;
|
||||||
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
|
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
|
||||||
|
@ -57,6 +54,44 @@ public class XmlParserTest {
|
||||||
XMLUnit.setIgnoreWhitespace(true);
|
XMLUnit.setIgnoreWhitespace(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* see #144 and #146
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testParseContained() {
|
||||||
|
|
||||||
|
FhirContext c = FhirContext.forDstu2();
|
||||||
|
IParser parser = c.newXmlParser().setPrettyPrint(true);
|
||||||
|
|
||||||
|
Observation o = new Observation();
|
||||||
|
o.getCode().setText("obs text");
|
||||||
|
|
||||||
|
Patient p = new Patient();
|
||||||
|
p.addName().addFamily("patient family");
|
||||||
|
o.getSubject().setResource(p);
|
||||||
|
|
||||||
|
String enc = parser.encodeResourceToString(o);
|
||||||
|
ourLog.info(enc);
|
||||||
|
|
||||||
|
//@formatter:off
|
||||||
|
assertThat(enc, stringContainsInOrder(
|
||||||
|
"<Observation xmlns=\"http://hl7.org/fhir\">",
|
||||||
|
"<contained>",
|
||||||
|
"<Patient xmlns=\"http://hl7.org/fhir\">",
|
||||||
|
"<id value=\"1\"/>",
|
||||||
|
"</contained>",
|
||||||
|
"<reference value=\"#1\"/>"
|
||||||
|
));
|
||||||
|
//@formatter:on
|
||||||
|
|
||||||
|
o = parser.parseResource(Observation.class, enc);
|
||||||
|
assertEquals("obs text", o.getCode().getText());
|
||||||
|
|
||||||
|
assertNotNull(o.getSubject().getResource());
|
||||||
|
p = (Patient) o.getSubject().getResource();
|
||||||
|
assertEquals("patient family", p.getNameFirstRep().getFamilyAsSingleString());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See #113
|
* See #113
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue