Merge branch 'master' of github.com:jamesagnew/hapi-fhir

This commit is contained in:
James Agnew 2015-04-24 12:40:24 -04:00
commit e147c74df7
5 changed files with 94 additions and 19 deletions

View File

@ -357,6 +357,13 @@ public class FhirContext {
return new FhirTerser(this); return new FhirTerser(this);
} }
/**
* Create a new validator instance.
* <p>
* Note on thread safety: Validators are thread safe, you may use a single validator
* in multiple threads. (This is in contrast to parsers)
* </p>
*/
public FhirValidator newValidator() { public FhirValidator newValidator() {
return new FhirValidator(this); return new FhirValidator(this);
} }

View File

@ -1569,12 +1569,12 @@ class ParserState<T> {
return; return;
} }
case RESOURCE: { case RESOURCE: {
if (myInstance instanceof IResource) { if (myInstance instanceof IResource || myInstance instanceof IElement) {
ParserState<T>.PreResourceStateHapi state = new PreResourceStateHapi(myInstance, child.getMutator(), null); ParserState<T>.PreResourceStateHapi state = new PreResourceStateHapi(myInstance, child.getMutator(), null);
push(state); push(state);
} else { } else {
ParserState<T>.PreResourceStateHl7Org state = new PreResourceStateHl7Org(myInstance, child.getMutator(), null); ParserState<T>.PreResourceStateHl7Org state = new PreResourceStateHl7Org(myInstance, child.getMutator(), null);
push(state); push(state);
} }
return; return;
} }
@ -1842,7 +1842,7 @@ class ParserState<T> {
@Override @Override
public void wereBack() { public void wereBack() {
super.wereBack(); super.wereBack();
if (myEntry == null) { if (myEntry == null && myMutator == null) {
myObject = (T) getCurrentElement(); myObject = (T) getCurrentElement();
} }

View File

@ -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.UUID;
import net.sf.json.JSON; import net.sf.json.JSON;
import net.sf.json.JSONSerializer; import net.sf.json.JSONSerializer;
@ -37,6 +38,8 @@ import ca.uhn.fhir.model.dstu2.resource.MedicationPrescription;
import ca.uhn.fhir.model.dstu2.resource.Observation; 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.AdministrativeGenderEnum;
import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum;
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum; import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
import ca.uhn.fhir.model.dstu2.valueset.ObservationReliabilityEnum; import ca.uhn.fhir.model.dstu2.valueset.ObservationReliabilityEnum;
import ca.uhn.fhir.model.dstu2.valueset.ObservationStatusEnum; import ca.uhn.fhir.model.dstu2.valueset.ObservationStatusEnum;
@ -61,6 +64,36 @@ public class JsonParserDstu2Test {
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 #163
*/
@Test
public void testParseResourceType() {
IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true);
// Patient
Patient patient = new Patient();
String patientId = UUID.randomUUID().toString();
patient.setId(new IdDt("Patient", patientId));
patient.addName().addGiven("John").addFamily("Smith");
patient.setGender(AdministrativeGenderEnum.MALE);
patient.setBirthDate(new DateDt("1987-04-16"));
// Bundle
ca.uhn.fhir.model.dstu2.resource.Bundle bundle = new ca.uhn.fhir.model.dstu2.resource.Bundle();
bundle.setType(BundleTypeEnum.COLLECTION);
bundle.addEntry().setResource(patient);
String bundleText = jsonParser.encodeResourceToString(bundle);
ourLog.info(bundleText);
ca.uhn.fhir.model.dstu2.resource.Bundle reincarnatedBundle = jsonParser.parseResource (ca.uhn.fhir.model.dstu2.resource.Bundle.class, bundleText);
Patient reincarnatedPatient = reincarnatedBundle.getAllPopulatedChildElementsOfType(Patient.class).get(0);
assertEquals("Patient", patient.getId().getResourceType());
assertEquals("Patient", reincarnatedPatient.getId().getResourceType());
}
/** /**
* See #144 and #146 * See #144 and #146
*/ */
@ -73,7 +106,7 @@ public class JsonParserDstu2Test {
obsv.setStatus(ObservationStatusEnum.FINAL); obsv.setStatus(ObservationStatusEnum.FINAL);
obsv.setReliability(ObservationReliabilityEnum.OK); obsv.setReliability(ObservationReliabilityEnum.OK);
obsv.addIdentifier().setSystem("System").setValue("id value"); obsv.addIdentifier().setSystem("System").setValue("id value");
DiagnosticReport report = new DiagnosticReport(); DiagnosticReport report = new DiagnosticReport();
report.getContained().getContainedResources().add(obsv); report.getContained().getContainedResources().add(obsv);
report.addResult().setResource(obsv); report.addResult().setResource(obsv);
@ -83,13 +116,12 @@ public class JsonParserDstu2Test {
ourLog.info(message); ourLog.info(message);
Assert.assertThat(message, containsString("contained")); Assert.assertThat(message, containsString("contained"));
} }
@Test @Test
public void testEncodeBundleOldBundleNoText() { public void testEncodeBundleOldBundleNoText() {
Bundle b = new Bundle(); Bundle b = new Bundle();
BundleEntry e = b.addEntry(); BundleEntry e = b.addEntry();
e.setResource(new Patient()); e.setResource(new Patient());
b.addCategory("scheme", "term", "label"); b.addCategory("scheme", "term", "label");
@ -109,8 +141,9 @@ public class JsonParserDstu2Test {
ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle(); ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle();
b.getText().setDiv(""); b.getText().setDiv("");
b.getText().getStatus().setValueAsString("");; b.getText().getStatus().setValueAsString("");
;
Entry e = b.addEntry(); Entry e = b.addEntry();
e.setResource(new Patient()); e.setResource(new Patient());
@ -136,10 +169,10 @@ public class JsonParserDstu2Test {
obsv.setStatus(ObservationStatusEnum.FINAL); obsv.setStatus(ObservationStatusEnum.FINAL);
obsv.setReliability(ObservationReliabilityEnum.OK); obsv.setReliability(ObservationReliabilityEnum.OK);
obsv.addIdentifier().setSystem("System").setValue("id value"); obsv.addIdentifier().setSystem("System").setValue("id value");
DiagnosticReport report = new DiagnosticReport(); DiagnosticReport report = new DiagnosticReport();
report.getContained().getContainedResources().add(obsv); report.getContained().getContainedResources().add(obsv);
obsv.setId("#123"); obsv.setId("#123");
report.addResult().setReference("#123"); report.addResult().setReference("#123");
@ -518,10 +551,10 @@ public class JsonParserDstu2Test {
String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example2.xml")); String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example2.xml"));
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content); ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content);
MedicationPrescription p = (MedicationPrescription) parsed.getEntry().get(0).getResource(); MedicationPrescription p = (MedicationPrescription) parsed.getEntry().get(0).getResource();
assertEquals("#med", p.getMedication().getReference().getValue()); assertEquals("#med", p.getMedication().getReference().getValue());
Medication m = (Medication) p.getMedication().getResource(); Medication m = (Medication) p.getMedication().getResource();
assertNotNull(m); assertNotNull(m);
assertEquals("#med", m.getId().getValue()); assertEquals("#med", m.getId().getValue());
@ -537,7 +570,6 @@ public class JsonParserDstu2Test {
assertThat(reencoded, containsString("contained")); assertThat(reencoded, containsString("contained"));
} }
@Test @Test
public void testParseAndEncodeBundle() throws Exception { public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json"));
@ -563,7 +595,7 @@ public class JsonParserDstu2Test {
Medication m = (Medication) parsed.getEntries().get(1).getResource(); Medication m = (Medication) parsed.getEntries().get(1).getResource();
assertEquals("http://example.com/base/Medication/example", m.getId().getValue()); assertEquals("http://example.com/base/Medication/example", m.getId().getValue());
assertSame(p.getMedication().getResource(), m); assertSame(p.getMedication().getResource(), m);
String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed); String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed);
ourLog.info(reencoded); ourLog.info(reencoded);
@ -587,7 +619,7 @@ public class JsonParserDstu2Test {
String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json"));
Bundle parsed = ourCtx.newJsonParser().parseBundle(content); Bundle parsed = ourCtx.newJsonParser().parseBundle(content);
assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED)); assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED));
assertEquals("searchset", parsed.getType().getValue()); assertEquals("searchset", parsed.getType().getValue());
assertEquals(3, parsed.getTotalResults().getValue().intValue()); assertEquals(3, parsed.getTotalResults().getValue().intValue());
@ -604,7 +636,6 @@ public class JsonParserDstu2Test {
assertEquals("Medication/example", p.getMedication().getReference().getValue()); assertEquals("Medication/example", p.getMedication().getReference().getValue());
assertSame(p.getMedication().getResource(), m); assertSame(p.getMedication().getResource(), m);
String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed); String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed);
ourLog.info(reencoded); ourLog.info(reencoded);

View File

@ -6,6 +6,7 @@ import static org.junit.Assert.*;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.Diff;
@ -36,6 +37,8 @@ import ca.uhn.fhir.model.dstu2.resource.MedicationPrescription;
import ca.uhn.fhir.model.dstu2.resource.Observation; 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.AdministrativeGenderEnum;
import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum;
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum; import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
import ca.uhn.fhir.model.primitive.DateDt; import ca.uhn.fhir.model.primitive.DateDt;
import ca.uhn.fhir.model.primitive.DateTimeDt; import ca.uhn.fhir.model.primitive.DateTimeDt;
@ -54,7 +57,36 @@ public class XmlParserDstu2Test {
XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreWhitespace(true);
} }
/**
* See #163
*/
@Test
public void testParseResourceType() {
IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
// Patient
Patient patient = new Patient();
String patientId = UUID.randomUUID().toString();
patient.setId(new IdDt("Patient", patientId));
patient.addName().addGiven("John").addFamily("Smith");
patient.setGender(AdministrativeGenderEnum.MALE);
patient.setBirthDate(new DateDt("1987-04-16"));
// Bundle
ca.uhn.fhir.model.dstu2.resource.Bundle bundle = new ca.uhn.fhir.model.dstu2.resource.Bundle();
bundle.setType(BundleTypeEnum.COLLECTION);
bundle.addEntry().setResource(patient);
String bundleText = xmlParser.encodeResourceToString(bundle);
ourLog.info(bundleText);
ca.uhn.fhir.model.dstu2.resource.Bundle reincarnatedBundle = xmlParser.parseResource (ca.uhn.fhir.model.dstu2.resource.Bundle.class, bundleText);
Patient reincarnatedPatient = reincarnatedBundle.getAllPopulatedChildElementsOfType(Patient.class).get(0);
assertEquals("Patient", patient.getId().getResourceType());
assertEquals("Patient", reincarnatedPatient.getId().getResourceType());
}
/** /**
* see #144 and #146 * see #144 and #146
*/ */

View File

@ -150,6 +150,11 @@
credentials to client requests it makes). Thanks to Harsha Kumara for credentials to client requests it makes). Thanks to Harsha Kumara for
the suggestion! the suggestion!
</action> </action>
<action type="fix" issue="163">
Fix regression in early 1.0 builds where resource type sometimes does not get
populated in a resource ID when the resource is parsed. Thanks to
Nick Peterson for reporting, and for providing a test case!
</action>
</release> </release>
<release version="0.9" date="2015-Mar-14"> <release version="0.9" date="2015-Mar-14">
<action type="add"> <action type="add">