Add deleted bundle entry handling

This commit is contained in:
jamesagnew 2014-05-21 18:12:00 -04:00
parent 1b44e7282d
commit fab60d1ac8
6 changed files with 300 additions and 100 deletions

View File

@ -34,21 +34,41 @@ public class BundleEntry extends BaseBundle {
* NB: add any new fields to the isEmpty() method!!!
*****************************************************/
//@formatter:on
private TagList myCategories;
private boolean myDeleted;
private InstantDt myDeletedAt;
private StringDt myLinkSelf;
private InstantDt myPublished;
private IResource myResource;
private XhtmlDt mySummary;
private StringDt myTitle;
private InstantDt myUpdated;
private XhtmlDt mySummary;
private TagList myCategories;
@Override
public boolean isEmpty() {
//@formatter:off
return super.isEmpty() &&
ElementUtil.isEmpty(myLinkSelf, myPublished, myResource, myTitle, myUpdated, mySummary) &&
ElementUtil.isEmpty(myCategories);
//@formatter:on
public Tag addCategory() {
Tag retVal = new Tag();
getCategories().add(retVal);
return retVal;
}
public void addCategory(Tag theTag) {
getCategories().add(theTag);
}
public TagList getCategories() {
if (myCategories == null) {
myCategories = new TagList();
}
return myCategories;
}
/**
* Gets the date/time that thius entry was deleted.
*/
public InstantDt getDeletedAt() {
if (myDeletedAt==null) {
myDeletedAt=new InstantDt();
}
return myDeletedAt;
}
public StringDt getLinkSelf() {
@ -69,6 +89,13 @@ public class BundleEntry extends BaseBundle {
return myResource;
}
public XhtmlDt getSummary() {
if (mySummary == null) {
mySummary = new XhtmlDt();
}
return mySummary;
}
public StringDt getTitle() {
if (myTitle == null) {
myTitle = new StringDt();
@ -83,6 +110,29 @@ public class BundleEntry extends BaseBundle {
return myUpdated;
}
public boolean isDeleted() {
return myDeleted;
}
@Override
public boolean isEmpty() {
//@formatter:off
return super.isEmpty() &&
ElementUtil.isEmpty(myCategories, myDeletedAt, myLinkSelf, myPublished, myResource, mySummary, myTitle, myUpdated);
//@formatter:on
}
public void setDeleted(boolean theDeleted) {
myDeleted = theDeleted;
}
/**
* Sets the date/time that this entry was deleted.
*/
public void setDeleted(InstantDt theDeletedAt) {
myDeletedAt = theDeletedAt;
}
public void setLinkSelf(StringDt theLinkSelf) {
if (myLinkSelf == null) {
myLinkSelf = new StringDt();
@ -90,42 +140,18 @@ public class BundleEntry extends BaseBundle {
myLinkSelf = theLinkSelf;
}
public void setResource(IResource theResource) {
myResource = theResource;
}
public XhtmlDt getSummary() {
if (mySummary == null) {
mySummary = new XhtmlDt();
}
return mySummary;
}
public Tag addCategory() {
Tag retVal = new Tag();
getCategories().add(retVal);
return retVal;
}
public TagList getCategories() {
if (myCategories == null) {
myCategories = new TagList();
}
return myCategories;
}
public void setPublished(InstantDt thePublished) {
Validate.notNull(thePublished, "Published may not be null");
myPublished = thePublished;
}
public void setResource(IResource theResource) {
myResource = theResource;
}
public void setUpdated(InstantDt theUpdated) {
Validate.notNull(theUpdated, "Updated may not be null");
myUpdated = theUpdated;
}
public void addCategory(Tag theTag) {
getCategories().add(theTag);
}
}

View File

@ -160,14 +160,15 @@ public class JsonParser extends BaseParser implements IParser {
writeOptionalTagWithTextNode(eventWriter, "updated", theBundle.getUpdated());
writeOptionalTagWithTextNode(eventWriter, "published", theBundle.getPublished());
eventWriter.writeStartArray("link");
writeAtomLink(eventWriter, "self", theBundle.getLinkSelf());
writeAtomLink(eventWriter, "first", theBundle.getLinkFirst());
writeAtomLink(eventWriter, "previous", theBundle.getLinkPrevious());
writeAtomLink(eventWriter, "next", theBundle.getLinkNext());
writeAtomLink(eventWriter, "last", theBundle.getLinkLast());
writeAtomLink(eventWriter, "fhir-base", theBundle.getLinkBase());
eventWriter.writeEnd();
boolean linkStarted = false;
linkStarted = writeAtomLink(eventWriter, "self", theBundle.getLinkSelf(), linkStarted);
linkStarted = writeAtomLink(eventWriter, "first", theBundle.getLinkFirst(), linkStarted);
linkStarted = writeAtomLink(eventWriter, "previous", theBundle.getLinkPrevious(), linkStarted);
linkStarted = writeAtomLink(eventWriter, "next", theBundle.getLinkNext(), linkStarted);
linkStarted = writeAtomLink(eventWriter, "last", theBundle.getLinkLast(), linkStarted);
linkStarted = writeAtomLink(eventWriter, "fhir-base", theBundle.getLinkBase(), linkStarted);
if (linkStarted) {
eventWriter.writeEnd();}
writeOptionalTagWithTextNode(eventWriter, "totalResults", theBundle.getTotalResults());
@ -177,17 +178,19 @@ public class JsonParser extends BaseParser implements IParser {
for (BundleEntry nextEntry : theBundle.getEntries()) {
eventWriter.writeStartObject();
writeTagWithTextNode(eventWriter, "deleted", nextEntry.getDeletedAt());
writeTagWithTextNode(eventWriter, "title", nextEntry.getTitle());
writeTagWithTextNode(eventWriter, "id", nextEntry.getId());
eventWriter.writeStartArray("link");
writeAtomLink(eventWriter, "self", nextEntry.getLinkSelf());
eventWriter.writeEnd();
linkStarted = false;
linkStarted=writeAtomLink(eventWriter, "self", nextEntry.getLinkSelf(), linkStarted);
if (linkStarted) {
eventWriter.writeEnd();}
writeOptionalTagWithTextNode(eventWriter, "updated", nextEntry.getUpdated());
writeOptionalTagWithTextNode(eventWriter, "published", nextEntry.getPublished());
if (nextEntry.getCategories() != null) {
if (nextEntry.getCategories() != null && nextEntry.getCategories().size() > 0) {
eventWriter.writeStartArray("category");
for (Tag next : nextEntry.getCategories()) {
eventWriter.writeStartObject();
@ -202,8 +205,10 @@ public class JsonParser extends BaseParser implements IParser {
writeAuthor(nextEntry, eventWriter);
IResource resource = nextEntry.getResource();
RuntimeResourceDefinition resDef = myContext.getResourceDefinition(resource);
encodeResourceToJsonStreamWriter(resDef, resource, eventWriter, "content");
if (resource != null) {
RuntimeResourceDefinition resDef = myContext.getResourceDefinition(resource);
encodeResourceToJsonStreamWriter(resDef, resource, eventWriter, "content");
}
eventWriter.writeEnd(); // entry object
}
@ -213,8 +218,7 @@ public class JsonParser extends BaseParser implements IParser {
eventWriter.close();
}
private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, JsonGenerator theWriter, IElement theValue, BaseRuntimeElementDefinition<?> theChildDef,
String theChildName) throws IOException {
private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, JsonGenerator theWriter, IElement theValue, BaseRuntimeElementDefinition<?> theChildDef, String theChildName) throws IOException {
switch (theChildDef.getChildType()) {
case PRIMITIVE_DATATYPE: {
@ -316,8 +320,7 @@ public class JsonParser extends BaseParser implements IParser {
}
private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, JsonGenerator theEventWriter,
List<? extends BaseRuntimeChildDefinition> theChildren) throws IOException {
private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, JsonGenerator theEventWriter, List<? extends BaseRuntimeChildDefinition> theChildren) throws IOException {
for (BaseRuntimeChildDefinition nextChild : theChildren) {
if (nextChild instanceof RuntimeChildNarrativeDefinition) {
INarrativeGenerator gen = myContext.getNarrativeGenerator();
@ -449,8 +452,7 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, JsonGenerator theEventWriter,
BaseRuntimeElementCompositeDefinition<?> resDef) throws IOException, DataFormatException {
private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, JsonGenerator theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef) throws IOException, DataFormatException {
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getExtensions());
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getChildren());
}
@ -520,10 +522,10 @@ public class JsonParser extends BaseParser implements IParser {
}
/**
* This is useful only for the two cases where extensions are encoded as direct children (e.g. not in some object called _name): resource extensions, and extension extensions
* This is useful only for the two cases where extensions are encoded as direct children (e.g. not in some object
* called _name): resource extensions, and extension extensions
*/
private void extractAndWriteExtensionsAsDirectChild(IElement theElement, JsonGenerator theEventWriter, BaseRuntimeElementDefinition<?> theElementDef, RuntimeResourceDefinition theResDef,
IResource theResource) throws IOException {
private void extractAndWriteExtensionsAsDirectChild(IElement theElement, JsonGenerator theEventWriter, BaseRuntimeElementDefinition<?> theElementDef, RuntimeResourceDefinition theResDef, IResource theResource) throws IOException {
List<HeldExtension> extensions = new ArrayList<HeldExtension>(0);
List<HeldExtension> modifierExtensions = new ArrayList<HeldExtension>(0);
@ -833,13 +835,20 @@ public class JsonParser extends BaseParser implements IParser {
return this;
}
private void writeAtomLink(JsonGenerator theEventWriter, String theRel, StringDt theLink) {
private boolean writeAtomLink(JsonGenerator theEventWriter, String theRel, StringDt theLink, boolean theStarted) {
boolean retVal = false;
if (isNotBlank(theLink.getValue())) {
if (theStarted==false) {
theEventWriter.writeStartArray("link");
retVal=true;
}
theEventWriter.writeStartObject();
theEventWriter.write("rel", theRel);
theEventWriter.write("href", theLink.getValue());
theEventWriter.writeEnd();
}
return retVal;
}
private void writeAuthor(BaseBundle theBundle, JsonGenerator eventWriter) {
@ -853,8 +862,7 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void writeExtensionsAsDirectChild(IResource theResource, JsonGenerator theEventWriter, RuntimeResourceDefinition resDef, List<HeldExtension> extensions,
List<HeldExtension> modifierExtensions) throws IOException {
private void writeExtensionsAsDirectChild(IResource theResource, JsonGenerator theEventWriter, RuntimeResourceDefinition resDef, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions) throws IOException {
if (extensions.isEmpty() == false) {
theEventWriter.writeStartArray("extension");
for (HeldExtension next : extensions) {
@ -878,9 +886,9 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void writeTagWithTextNode(JsonGenerator theEventWriter, String theElementName, IdDt theIdDt) {
if (StringUtils.isNotBlank(theIdDt.getValue())) {
theEventWriter.write(theElementName, theIdDt.getValue());
private void writeTagWithTextNode(JsonGenerator theEventWriter, String theElementName, IPrimitiveDatatype<?> theIdDt) {
if (theIdDt != null && !theIdDt.isEmpty()) {
theEventWriter.write(theElementName, theIdDt.getValueAsString());
} else {
theEventWriter.writeNull(theElementName);
}
@ -889,9 +897,10 @@ public class JsonParser extends BaseParser implements IParser {
private void writeTagWithTextNode(JsonGenerator theEventWriter, String theElementName, StringDt theStringDt) {
if (StringUtils.isNotBlank(theStringDt.getValue())) {
theEventWriter.write(theElementName, theStringDt.getValue());
} else {
theEventWriter.writeNull(theElementName);
}
}
// else {
// theEventWriter.writeNull(theElementName);
// }
}
private class HeldExtension {

View File

@ -23,7 +23,6 @@ package ca.uhn.fhir.parser;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -40,7 +39,6 @@ import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeChildDeclaredExtensionDefinition;
import ca.uhn.fhir.context.RuntimeChildResourceDefinition;
import ca.uhn.fhir.context.RuntimeElemContainedResources;
import ca.uhn.fhir.context.RuntimePrimitiveDatatypeDefinition;
import ca.uhn.fhir.context.RuntimePrimitiveDatatypeNarrativeDefinition;
@ -64,6 +62,7 @@ import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.dstu.composite.ContainedDt;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.model.primitive.XhtmlDt;
import ca.uhn.fhir.rest.server.Constants;
@ -274,6 +273,10 @@ class ParserState<T> {
theInstance.getEntries().add(myEntry);
}
protected BundleEntry getEntry() {
return myEntry;
}
@Override
public void endingElement() throws DataFormatException {
populateResourceMetadata();
@ -300,6 +303,10 @@ class ParserState<T> {
push(new XhtmlState(getPreResourceState(), myEntry.getSummary(), false));
} else if ("category".equals(theLocalPart)) {
push(new AtomCategoryState(myEntry.addCategory()));
} else if ("deleted".equals(theLocalPart) && myJsonMode) {
// JSON and XML deleted entries are completely different for some reason
myEntry.setDeleted(true);
push(new AtomDeletedJsonWhenState(myEntry.getDeletedAt()));
} else {
throw new DataFormatException("Unexpected element in entry: " + theLocalPart);
}
@ -373,6 +380,28 @@ class ParserState<T> {
}
}
public class AtomDeletedEntryState extends AtomEntryState {
public AtomDeletedEntryState(Bundle theInstance, Class<? extends IResource> theResourceType) {
super(theInstance, theResourceType);
getEntry().setDeleted(true);
}
@Override
public void attributeValue(String theName, String theValue) throws DataFormatException {
if ("ref".equals(theName)) {
getEntry().setId(new IdDt(theValue));
} else if ("when".equals(theName)) {
getEntry().setDeleted(new InstantDt(theValue));
}
}
}
private class AtomLinkState extends BaseState {
@ -440,6 +469,14 @@ class ParserState<T> {
myPrimitive = thePrimitive;
}
@Override
public void attributeValue(String theName, String theValue) throws DataFormatException {
if (myJsonMode) {
string(theValue);
}
super.attributeValue(theName, theValue);
}
@Override
public void endingElement() throws DataFormatException {
myPrimitive.setValueAsString(myData);
@ -469,6 +506,42 @@ class ParserState<T> {
}
private class AtomDeletedJsonWhenState extends BaseState {
private String myData;
private IPrimitiveDatatype<?> myPrimitive;
public AtomDeletedJsonWhenState(IPrimitiveDatatype<?> thePrimitive) {
super(null);
Validate.notNull(thePrimitive, "thePrimitive");
myPrimitive = thePrimitive;
}
@Override
public void endingElement() throws DataFormatException {
myPrimitive.setValueAsString(myData);
pop();
}
@Override
public void enteringNewElement(String theNamespaceURI, String theLocalPart) throws DataFormatException {
throw new DataFormatException("Unexpected nested element in atom tag: " + theLocalPart);
}
@Override
public void attributeValue(String theName, String theValue) throws DataFormatException {
myData = theValue;
}
@Override
protected IElement getCurrentElement() {
return null;
}
}
private class AtomState extends BaseState {
private Bundle myInstance;
@ -503,6 +576,8 @@ class ParserState<T> {
push(new AtomPrimitiveState(myInstance.getUpdated()));
} else if ("author".equals(theLocalPart)) {
push(new AtomAuthorState(myInstance));
} else if ("deleted-entry".equals(theLocalPart) && verifyNamespace(XmlParser.TOMBSTONES_NS, theNamespaceURI)) {
push(new AtomDeletedEntryState(myInstance,myResourceType));
} else {
if (theNamespaceURI != null) {
throw new DataFormatException("Unexpected element: {" + theNamespaceURI + "}" + theLocalPart);

View File

@ -82,6 +82,7 @@ public class XmlParser extends BaseParser implements IParser {
static final String ATOM_NS = "http://www.w3.org/2005/Atom";
static final String FHIR_NS = "http://hl7.org/fhir";
static final String OPENSEARCH_NS = "http://a9.com/-/spec/opensearch/1.1/";
static final String TOMBSTONES_NS = "http://purl.org/atompub/tombstones/1.0";
static final String XHTML_NS = "http://www.w3.org/1999/xhtml";
// private static final Set<String> RESOURCE_NAMESPACES;

View File

@ -1,9 +1,13 @@
package ca.uhn.fhir.parser;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.OutputStreamWriter;
@ -17,6 +21,7 @@ import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.StringContains;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
@ -50,6 +55,7 @@ import ca.uhn.fhir.narrative.INarrativeGenerator;
public class JsonParserTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserTest.class);
private static FhirContext ourCtx;
@Test
public void testEncodingNullExtension() {
@ -156,11 +162,10 @@ public class JsonParserTest {
public void testEncodeContainedResources() throws IOException {
String msg = IOUtils.toString(XmlParser.class.getResourceAsStream("/contained-diagnosticreport.xml"));
FhirContext ctx = new FhirContext(DiagnosticReport.class);
IParser p = ctx.newXmlParser();
IParser p = ourCtx.newXmlParser();
DiagnosticReport res = p.parseResource(DiagnosticReport.class, msg);
String encoded = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(res);
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(res);
ourLog.info(encoded);
}
@ -444,11 +449,10 @@ public class JsonParserTest {
public void testParseBundle() throws DataFormatException, IOException {
String msg = IOUtils.toString(XmlParser.class.getResourceAsStream("/atom-document-large.json"));
FhirContext ctx = new FhirContext(Patient.class);
IParser p = ctx.newJsonParser();
IParser p = ourCtx.newJsonParser();
Bundle bundle = p.parseBundle(msg);
String encoded = ctx.newXmlParser().setPrettyPrint(true).encodeBundleToString(bundle);
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(bundle);
ourLog.info(encoded);
assertEquals("http://fhir.healthintersections.com.au/open/DiagnosticReport/_search?_format=application/json+fhir&search-id=46d5f0e7-9240-4d4f-9f51-f8ac975c65&search-sort=_id", bundle
@ -472,7 +476,7 @@ public class JsonParserTest {
public void testParseFuroreMetadataWithExtraElements() throws IOException {
String msg = IOUtils.toString(JsonParserTest.class.getResourceAsStream("/furore-conformance.json"));
IParser p = new FhirContext(ValueSet.class).newJsonParser();
IParser p = ourCtx.newJsonParser();
Conformance conf = p.parseResource(Conformance.class, msg);
RestResource res = conf.getRestFirstRep().getResourceFirstRep();
assertEquals("_id", res.getSearchParam().get(1).getName().getValue());
@ -482,12 +486,11 @@ public class JsonParserTest {
public void testParseWithContained() throws DataFormatException, IOException {
String msg = IOUtils.toString(XmlParser.class.getResourceAsStream("/diagnostic-report.json"));
FhirContext ctx = new FhirContext(Patient.class);
IParser p = ctx.newJsonParser();
IParser p = ourCtx.newJsonParser();
// ourLog.info("Reading in message: {}", msg);
DiagnosticReport res = p.parseResource(DiagnosticReport.class, msg);
String encoded = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res);
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res);
ourLog.info(encoded);
ResourceReferenceDt reference = res.getResult().get(1);
@ -496,14 +499,55 @@ public class JsonParserTest {
assertEquals("789-8", obs.getName().getCoding().get(0).getCode().getValue());
}
@BeforeClass
public static void beforeClass() {
ourCtx = new FhirContext();
}
@Test
public void testParseBundleDeletedEntry() {
//@formatter:off
String bundleString =
"{" +
"\"resourceType\":\"Bundle\"," +
"\"totalResults\":\"1\"," +
"\"entry\":[" +
"{" +
"\"deleted\":\"2012-05-29T23:45:32+00:00\"," +
"\"id\":\"http://fhir.furore.com/fhir/Patient/1\"," +
"\"link\":[" +
"{" +
"\"rel\":\"self\"," +
"\"href\":\"http://fhir.furore.com/fhir/Patient/1/_history/2\"" +
"}" +
"]" +
"}" +
"]" +
"}";
//@formatter:on
Bundle bundle = ourCtx.newJsonParser().parseBundle(bundleString);
BundleEntry entry = bundle.getEntries().get(0);
assertTrue(entry.isDeleted());
assertEquals("2012-05-29T23:45:32+00:00", entry.getDeletedAt().getValueAsString());
assertEquals("http://fhir.furore.com/fhir/Patient/1/_history/2", entry.getLinkSelf().getValue());
// Now encode
ourLog.info(ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(bundle));
String encoded = ourCtx.newJsonParser().encodeBundleToString(bundle);
assertEquals(bundleString,encoded);
}
@Test
public void testSimpleBundleEncode() throws IOException {
FhirContext ctx = new FhirContext(Observation.class, Patient.class);
String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/atom-document-large.xml"), Charset.forName("UTF-8"));
Bundle obs = ctx.newXmlParser().parseBundle(xmlString);
Bundle obs = ourCtx.newXmlParser().parseBundle(xmlString);
String encoded = ctx.newJsonParser().encodeBundleToString(obs);
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(obs);
ourLog.info(encoded);
}
@ -512,15 +556,14 @@ public class JsonParserTest {
public void testSimpleParse() throws DataFormatException, IOException {
String msg = IOUtils.toString(XmlParser.class.getResourceAsStream("/example-patient-general.json"));
FhirContext ctx = new FhirContext(Patient.class);
IParser p = ctx.newJsonParser();
IParser p = ourCtx.newJsonParser();
// ourLog.info("Reading in message: {}", msg);
Patient res = p.parseResource(Patient.class, msg);
assertEquals(2, res.getUndeclaredExtensions().size());
assertEquals(1, res.getUndeclaredModifierExtensions().size());
String encoded = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res);
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res);
ourLog.info(encoded);
}
@ -528,17 +571,16 @@ public class JsonParserTest {
@Test
public void testSimpleResourceEncode() throws IOException {
FhirContext ctx = new FhirContext(Observation.class);
String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"), Charset.forName("UTF-8"));
Patient obs = ctx.newXmlParser().parseResource(Patient.class, xmlString);
Patient obs = ourCtx.newXmlParser().parseResource(Patient.class, xmlString);
List<ExtensionDt> undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getUndeclaredExtensions();
ExtensionDt undeclaredExtension = undeclaredExtensions.get(0);
assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl().getValue());
ctx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out));
ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out));
IParser jsonParser = ctx.newJsonParser();
IParser jsonParser = ourCtx.newJsonParser();
String encoded = jsonParser.encodeResourceToString(obs);
ourLog.info(encoded);
@ -556,9 +598,9 @@ public class JsonParserTest {
@Test
public void testSimpleResourceEncodeWithCustomType() throws IOException {
FhirContext ctx = new FhirContext(MyObservationWithExtensions.class);
FhirContext fhirCtx = new FhirContext(MyObservationWithExtensions.class);
String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"), Charset.forName("UTF-8"));
MyObservationWithExtensions obs = ctx.newXmlParser().parseResource(MyObservationWithExtensions.class, xmlString);
MyObservationWithExtensions obs = fhirCtx.newXmlParser().parseResource(MyObservationWithExtensions.class, xmlString);
assertEquals(0, obs.getAllUndeclaredExtensions().size());
assertEquals("aaaa", obs.getExtAtt().getContentType().getValue());
@ -569,9 +611,9 @@ public class JsonParserTest {
ExtensionDt undeclaredExtension = undeclaredExtensions.get(0);
assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl().getValue());
ctx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out));
fhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out));
IParser jsonParser = ctx.newJsonParser();
IParser jsonParser = fhirCtx.newJsonParser();
String encoded = jsonParser.encodeResourceToString(obs);
ourLog.info(encoded);

View File

@ -469,6 +469,26 @@ public class XmlParserTest {
assertTrue(d.toString(), d.identical());
}
@Test
public void testParseWithXmlHeader() throws ConfigurationException, DataFormatException {
FhirContext ctx = new FhirContext(Patient.class);
IParser p = new XmlParser(ctx);
//@formatter:off
String msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <identifier>\n" +
" <label value=\"IdentifierLabel\"/>\n" +
" </identifier>\n" +
"</Patient>";
//@formatter:on
Patient resource = (Patient) p.parseResource(msg);
assertEquals("IdentifierLabel", resource.getIdentifier().get(0).getLabel().getValue());
}
@Test
public void testLoadObservation() throws ConfigurationException, DataFormatException, IOException {
@ -659,6 +679,33 @@ public class XmlParserTest {
}
@Test
public void testParseBundleDeletedEntry() {
//@formatter:off
String msg = "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n" +
" <title>FHIR Core Valuesets</title>\n" +
" <id>http://hl7.org/fhir/profile/valuesets</id>\n" +
" <link href=\"http://hl7.org/implement/standards/fhir/valuesets.xml\" rel=\"self\"/>\n" +
" <updated>2014-02-10T04:11:24.435-00:00</updated>\n" +
" <at:deleted-entry xmlns:at=\"http://purl.org/atompub/tombstones/1.0\" " +
" ref=\"http://foo/Patient/1\" when=\"2013-02-10T04:11:24.435-00:00\">\n" +
" <link rel=\"self\" href=\"http://foo/Patient/1/_history/2\"></link>\n" +
" </at:deleted-entry>" +
"</feed>";
//@formatter:on
IParser p = new FhirContext(ValueSet.class).newXmlParser();
Bundle bundle = p.parseBundle(msg);
BundleEntry entry = bundle.getEntries().get(0);
assertTrue(entry.isDeleted());
assertEquals("http://foo/Patient/1", entry.getId().getValue());
assertEquals("2013-02-10T04:11:24.435+00:00", entry.getDeletedAt().getValueAsString());
assertEquals("http://foo/Patient/1/_history/2", entry.getLinkSelf().getValue());
}
@Test
public void testParseBundleLarge() throws IOException {