Fix unit test

This commit is contained in:
jamesagnew 2015-04-02 15:01:25 -04:00
parent 67a88a003f
commit 4054aa781a
6 changed files with 225 additions and 33 deletions

View File

@ -55,6 +55,7 @@
<include>org.glassfish:javax.json</include>
<include>org.codehaus.woodstox:woodstox-core-asl</include>
<include>javax.xml.stream:stax-api</include>
<include>javax.servlet:javax.servlet-api</include>
<include>org.codehaus.woodstox:stax2-api</include>
<include>org.slf4j:slf4j*</include>
<include>org.apache.commons:*</include>
@ -128,12 +129,6 @@
<version>4.3.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -1,15 +1,7 @@
package ca.uhn.fhir.parser;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.InputStreamReader;
@ -84,6 +76,43 @@ public class XmlParserTest {
private static FhirContext ourCtx;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserTest.class);
/**
* see #144 and #146
*/
@Test
public void testParseContained() {
FhirContext c = FhirContext.forDstu1();
IParser parser = c.newXmlParser().setPrettyPrint(true);
Observation o = new Observation();
o.getName().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=\"1\">",
"</contained>",
"<reference value=\"#1\"/>"
));
//@formatter:on
o = parser.parseResource(Observation.class, enc);
assertEquals("obs text", o.getName().getText().getValue());
assertNotNull(o.getSubject().getResource());
p = (Patient) o.getSubject().getResource();
assertEquals("patient family", p.getNameFirstRep().getFamilyAsSingleString());
}
@Test
public void testComposition() {

View File

@ -252,11 +252,11 @@ public class Dstu2BundleFactory implements IVersionSpecificBundleFactory {
if (searchId != null) {
if (theOffset + numToReturn < theResult.size()) {
myBundle.addLink().setRelation(Constants.LINK_NEXT).setUrl(RestfulServerUtils.createPagingLink(theServerBase, searchId, theOffset + numToReturn, numToReturn, theResponseEncoding, thePrettyPrint));
myBundle.addLink().setRelation(Constants.LINK_NEXT).setUrl(RestfulServerUtils.createPagingLink(theIncludes, theServerBase, searchId, theOffset + numToReturn, numToReturn, theResponseEncoding, thePrettyPrint));
}
if (theOffset > 0) {
int start = Math.max(0, theOffset - limit);
myBundle.addLink().setRelation(Constants.LINK_PREVIOUS).setUrl(RestfulServerUtils.createPagingLink(theServerBase, searchId, start, limit, theResponseEncoding, thePrettyPrint));
myBundle.addLink().setRelation(Constants.LINK_PREVIOUS).setUrl(RestfulServerUtils.createPagingLink(theIncludes, theServerBase, searchId, start, limit, theResponseEncoding, thePrettyPrint));
}
}
}

View File

@ -6,14 +6,15 @@ import static org.junit.Assert.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import ca.uhn.fhir.context.ConfigurationException;
@ -28,19 +29,23 @@ import ca.uhn.fhir.model.base.composite.BaseCodingDt;
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
import ca.uhn.fhir.model.dstu2.resource.Binary;
import ca.uhn.fhir.model.dstu2.resource.DiagnosticReport;
import ca.uhn.fhir.model.dstu2.resource.Medication;
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.QuestionnaireAnswers;
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
import ca.uhn.fhir.model.dstu2.valueset.ObservationReliabilityEnum;
import ca.uhn.fhir.model.dstu2.valueset.ObservationStatusEnum;
import ca.uhn.fhir.model.primitive.DateDt;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.model.primitive.StringDt;
public class JsonParserTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserTest.class);
public class JsonParserDstu2Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class);
private static final FhirContext ourCtx = FhirContext.forDstu2();
@Test
@ -54,6 +59,54 @@ public class JsonParserTest {
assertEquals("{\"resourceType\":\"Binary\",\"id\":\"11\",\"meta\":{\"versionId\":\"22\"},\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", val);
}
/**
* See #144 and #146
*/
@Test
public void testReportSerialize() {
ReportObservation obsv = new ReportObservation();
obsv.getCode().addCoding().setCode("name");
obsv.setValue(new StringDt("value test"));
obsv.setStatus(ObservationStatusEnum.FINAL);
obsv.setReliability(ObservationReliabilityEnum.OK);
obsv.addIdentifier().setSystem("System").setValue("id value");
DiagnosticReport report = new DiagnosticReport();
report.getContained().getContainedResources().add(obsv);
report.addResult().setResource(obsv);
IParser parser = new FhirContext().newXmlParser().setPrettyPrint(true);
String message = parser.encodeResourceToString(report);
ourLog.info(message);
Assert.assertThat(message, containsString("contained"));
}
/**
* See #144 and #146
*/
@Test
public void testReportSerializeWithMatchingId() {
ReportObservation obsv = new ReportObservation();
obsv.getCode().addCoding().setCode("name");
obsv.setValue(new StringDt("value test"));
obsv.setStatus(ObservationStatusEnum.FINAL);
obsv.setReliability(ObservationReliabilityEnum.OK);
obsv.addIdentifier().setSystem("System").setValue("id value");
DiagnosticReport report = new DiagnosticReport();
report.getContained().getContainedResources().add(obsv);
obsv.setId("#123");
report.addResult().setReference("#123");
IParser parser = new FhirContext().newXmlParser().setPrettyPrint(true);
String message = parser.encodeResourceToString(report);
ourLog.info(message);
Assert.assertThat(message, containsString("contained"));
}
/**
* see #144 and #146
*/
@ -69,10 +122,10 @@ public class JsonParserTest {
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\"",
@ -82,10 +135,10 @@ public class JsonParserTest {
"\"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());
@ -217,7 +270,7 @@ public class JsonParserTest {
*/
@Test
public void testEncodeBundleWithDeletedEntry() throws ConfigurationException, DataFormatException, IOException {
Bundle b = ourCtx.newXmlParser().parseBundle(IOUtils.toString(JsonParserTest.class.getResourceAsStream("/xml-bundle.xml")));
Bundle b = ourCtx.newXmlParser().parseBundle(IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/xml-bundle.xml")));
String val = ourCtx.newJsonParser().encodeBundleToString(b);
ourLog.info(val);
@ -417,7 +470,7 @@ public class JsonParserTest {
@Test
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(JsonParserTest.class.getResourceAsStream("/bundle-example.json"));
String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json"));
Bundle parsed = ourCtx.newJsonParser().parseBundle(content);
assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue());
@ -437,6 +490,10 @@ public class JsonParserTest {
assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString());
assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue());
Medication m = (Medication) parsed.getEntries().get(1).getResource();
assertEquals("http://example.com/base/Medication/example", m.getId().getValue());
assertSame(p.getMedication().getResource(), m);
String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed);
ourLog.info(reencoded);
@ -457,7 +514,7 @@ public class JsonParserTest {
@Test
public void testParseAndEncodeBundleOldStyle() throws Exception {
String content = IOUtils.toString(JsonParserTest.class.getResourceAsStream("/bundle-transaction.json"));
String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-transaction.json"));
Bundle parsed = ourCtx.newJsonParser().parseBundle(content);
// assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue());
@ -502,7 +559,7 @@ public class JsonParserTest {
@Test
public void testParseAndEncodeBundleNewStyle() throws Exception {
String content = IOUtils.toString(JsonParserTest.class.getResourceAsStream("/bundle-example.json"));
String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json"));
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newJsonParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content);
assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue());

View File

@ -0,0 +1,111 @@
package ca.uhn.fhir.parser;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.primitive.BooleanDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.util.ElementUtil;
@ResourceDef(name = "Observation", id = "reportobservation")
public class ReportObservation extends Observation {
/**
* Each extension is defined in a field. Any valid HAPI Data Type can be used for the field type. Note that the
* [name=""] attribute in the @Child annotation needs to match the name for the bean accessor and mutator methods.
*/
@Child(name = "mandatory", order = 0)
@Extension(url = "#mandatory", definedLocally = true, isModifier = false)
@Description(shortDefinition = "The report observation is mandatory or not")
private BooleanDt mandatory;
@Child(name = "readOnly", order = 1)
@Extension(url = "#readOnly", definedLocally = true, isModifier = false)
@Description(shortDefinition = "The report observation is read only or not")
private BooleanDt readOnly;
@Child(name = "defaultCursor", order = 2)
@Extension(url = "#defaultCursor", definedLocally = true, isModifier = false)
@Description(shortDefinition = "The report observation is default cursor or not")
private BooleanDt defaultCursor;
@Child(name = "sectionContentId", order = 3)
@Extension(url = "#sectionContentId", definedLocally = true, isModifier = false)
@Description(shortDefinition = "The primary key of the report section content")
private StringDt sectionContentId;
/**
* It is important to override the isEmpty() method, adding a check for any newly added fields.
*/
@Override
public boolean isEmpty() {
return super.isEmpty() && ElementUtil.isEmpty(mandatory, readOnly, defaultCursor, sectionContentId);
}
/********
* Accessors and mutators follow
*
* IMPORTANT: Each extension is required to have an getter/accessor and a stter/mutator. You are highly recommended
* to create getters which create instances if they do not already exist, since this is how the rest of the HAPI
* FHIR API works.
********/
/** Getter for mandatory */
public BooleanDt getMandatory() {
if (mandatory == null) {
mandatory = new BooleanDt(false);
}
return mandatory;
}
/** Setter for mandatory */
public ReportObservation setMandatory(Boolean isMandatory) {
mandatory = new BooleanDt(isMandatory);
return this;
}
/** Getter for readOnly */
public BooleanDt getReadOnly() {
if (readOnly == null) {
readOnly = new BooleanDt(false);
}
return readOnly;
}
/** Setter for readOnly */
public ReportObservation setReadOnly(Boolean isReadOnly) {
readOnly = new BooleanDt(isReadOnly);
return this;
}
/** Getter for defaultCursor */
public BooleanDt getDefaultCursor() {
if (defaultCursor == null) {
defaultCursor = new BooleanDt(false);
}
return defaultCursor;
}
/** Setter for defaultCursor */
public ReportObservation setDefaultCursor(Boolean isDefaultCursor) {
defaultCursor = new BooleanDt(isDefaultCursor);
return this;
}
/** Getter for sectionContentId */
public StringDt getSectionContentId() {
if (sectionContentId == null) {
sectionContentId = new StringDt();
}
return sectionContentId;
}
/** Setter for sectionContentId */
public ReportObservation setSectionContentId(String sectionContentId) {
this.sectionContentId = new StringDt(sectionContentId);
return this;
}
}

View File

@ -43,8 +43,8 @@ import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.model.primitive.StringDt;
public class XmlParserTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserTest.class);
public class XmlParserDstu2Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserDstu2Test.class);
private static final FhirContext ourCtx = FhirContext.forDstu2();
@BeforeClass
@ -384,7 +384,7 @@ public class XmlParserTest {
@Test
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/bundle-example.xml"));
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"));
Bundle parsed = ourCtx.newXmlParser().parseBundle(content);
assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue());
@ -415,7 +415,7 @@ public class XmlParserTest {
@Test
public void testParseAndEncodeBundleNewStyle() throws Exception {
String content = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/bundle-example.xml"));
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"));
IParser newXmlParser = ourCtx.newXmlParser();
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = newXmlParser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content);