Add custom type test

This commit is contained in:
James Agnew 2016-06-27 17:51:08 -04:00
parent d4baf38656
commit d7fdaf7618
3 changed files with 209 additions and 95 deletions

View File

@ -73,6 +73,7 @@ import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.PatientWithExtendedContactDstu3.CustomContactComponent;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.util.TestUtil;
import net.sf.json.JSON;
@ -87,30 +88,29 @@ public class JsonParserDstu3Test {
public void after() {
ourCtx.setNarrativeGenerator(null);
}
@Test
public void testEncodeNarrativeShouldIncludeNamespace() {
Patient p = new Patient();
p.getText().setDivAsString("<div>VALUE</div>");
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
assertThat(output, containsString("\"div\":\"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
@Test
public void testEncodeNarrativeShouldIncludeNamespaceWithProcessingInstruction() {
Patient p = new Patient();
p.getText().setDivAsString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><div>VALUE</div>");
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
assertThat(output, containsString("\"div\":\"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
@Test
public void testEncodeNarrativeShouldIncludeNamespaceWithProcessingInstruction() {
Patient p = new Patient();
p.getText().setDivAsString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><div>VALUE</div>");
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
assertThat(output, containsString("\"div\":\"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
/**
* See #344
*/
@ -122,13 +122,13 @@ public class JsonParserDstu3Test {
data.setOrigin((SimpleQuantity) new SimpleQuantity().setValue(0L));
data.setPeriod(1000L);
obs.setValue(data);
IParser p = ourCtx.newJsonParser().setPrettyPrint(true).setParserErrorHandler(new StrictErrorHandler());
String encoded = p.encodeResourceToString(obs);
ourLog.info(encoded);
p.parseResource(encoded);
try {
p.parseResource(encoded.replace("Observation", "observation"));
fail();
@ -143,9 +143,7 @@ public class JsonParserDstu3Test {
assertEquals("Unknown element 'valueSampleddata' found during parse", e.getMessage());
}
}
@Test
public void testEncodeAndParseExtensions() throws Exception {
@ -189,8 +187,10 @@ public class JsonParserDstu3Test {
assertThat(enc, Matchers.stringContainsInOrder("{\"resourceType\":\"Patient\",", "\"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\"}]}"));
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\":[" + "{"
+ "\"url\":\"http://examples.com#givenext_child\"," + "\"valueString\":\"CHILD\"" + "}" + "]" + "}" + "]" + "}"));
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
@ -229,7 +229,7 @@ public class JsonParserDstu3Test {
assertEquals("CHILD", ((StringType) given2ext2.getValue()).getValue());
}
@Test
public void testEncodeAndParseMetaProfileAndTags() {
Patient p = new Patient();
@ -313,7 +313,7 @@ public class JsonParserDstu3Test {
*/
@Test
public void testEncodeAndParseNullPrimitiveWithExtensions() {
Patient p = new Patient();
p.setId("patid");
HumanName name = p.addName();
@ -325,43 +325,42 @@ public class JsonParserDstu3Test {
String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(output);
output = ourCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(p);
String expected = "{\"resourceType\":\"Patient\",\"id\":\"patid\",\"name\":[{\"id\":\"nameid\",\"family\":[null,\"V1\",null],\"_family\":[{\"id\":\"f0\",\"extension\":[{\"url\":\"http://foo\",\"valueString\":\"FOOEXT0\"}]},{\"id\":\"f1\",\"extension\":[{\"id\":\"ext1id\",\"url\":\"http://foo\",\"valueString\":\"FOOEXT1\"}]},{\"extension\":[{\"url\":\"http://foo\",\"valueString\":\"FOOEXT3\"}]}]}]}";
assertEquals(expected, output);
p = ourCtx.newJsonParser().parseResource(Patient.class, output);
assertEquals("patid", p.getIdElement().getIdPart());
name = p.getName().get(0);
assertEquals("nameid", name.getId());
assertEquals(3, name.getFamily().size());
assertEquals(null, name.getFamily().get(0).getValue());
assertEquals("V1", name.getFamily().get(1).getValue());
assertEquals(null, name.getFamily().get(2).getValue());
assertEquals("f0", name.getFamily().get(0).getId());
assertEquals("f1", name.getFamily().get(1).getId());
assertEquals(null, name.getFamily().get(2).getId());
assertEquals(1, name.getFamily().get(0).getExtension().size());
assertEquals("http://foo", name.getFamily().get(0).getExtension().get(0).getUrl());
assertEquals("FOOEXT0", ((StringType)name.getFamily().get(0).getExtension().get(0).getValue()).getValue());
assertEquals("FOOEXT0", ((StringType) name.getFamily().get(0).getExtension().get(0).getValue()).getValue());
assertEquals(null, name.getFamily().get(0).getExtension().get(0).getId());
assertEquals(1, name.getFamily().get(1).getExtension().size());
assertEquals("http://foo", name.getFamily().get(1).getExtension().get(0).getUrl());
assertEquals("FOOEXT1", ((StringType)name.getFamily().get(1).getExtension().get(0).getValue()).getValue());
assertEquals("FOOEXT1", ((StringType) name.getFamily().get(1).getExtension().get(0).getValue()).getValue());
assertEquals("ext1id", name.getFamily().get(1).getExtension().get(0).getId());
assertEquals(1, name.getFamily().get(2).getExtension().size());
assertEquals("http://foo", name.getFamily().get(2).getExtension().get(0).getUrl());
assertEquals("FOOEXT3", ((StringType)name.getFamily().get(2).getExtension().get(0).getValue()).getValue());
assertEquals("FOOEXT3", ((StringType) name.getFamily().get(2).getExtension().get(0).getValue()).getValue());
assertEquals(null, name.getFamily().get(2).getExtension().get(0).getId());
}
@Test
public void testEncodeAndParseSecurityLabels() {
@ -449,10 +448,10 @@ public class JsonParserDstu3Test {
Patient patient = new Patient();
patient.getBirthDateElement().setValueAsString("2016-04-05");
patient.addExtension().setUrl("test").setValue(new Reference(new Condition()));
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient);
ourLog.info(encoded);
//@formatter:off
assertThat(encoded, stringContainsInOrder(
"{",
@ -476,7 +475,6 @@ public class JsonParserDstu3Test {
));
//@formatter:on
}
@Test
public void testEncodeDoesntIncludeUuidId() {
@ -487,14 +485,13 @@ public class JsonParserDstu3Test {
String actual = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
assertThat(actual, not(containsString("78ef6f64c2f2")));
}
@Test
public void testEncodeEmptyBinary() {
String output = ourCtx.newJsonParser().encodeResourceToString(new Binary());
assertEquals("{\"resourceType\":\"Binary\"}", output);
}
/**
* #158
*/
@ -511,7 +508,6 @@ public class JsonParserDstu3Test {
assertThat(encoded, not(containsString("tag")));
}
/**
* #158
*/
@ -529,7 +525,7 @@ public class JsonParserDstu3Test {
assertThat(encoded, containsString("scheme"));
assertThat(encoded, not(containsString("Label")));
}
@Test
public void testEncodeExtensionInPrimitiveElement() {
@ -559,7 +555,6 @@ public class JsonParserDstu3Test {
}
@Test
public void testEncodeExtensionUndeclaredNonModifier() {
Observation obs = new Observation();
@ -567,11 +562,11 @@ public class JsonParserDstu3Test {
obs.getMeta().addProfile("http://profile");
Extension ext = obs.addExtension();
ext.setUrl("http://exturl").setValue(new StringType("ext_url_value"));
obs.getCode().setText("CODE");
IParser parser = ourCtx.newJsonParser();
String output = parser.setPrettyPrint(true).encodeResourceToString(obs);
ourLog.info(output);
@ -590,13 +585,13 @@ public class JsonParserDstu3Test {
"\"url\":\"http://exturl\""
)));
//@formatter:on
obs = parser.parseResource(Observation.class, output);
assertEquals(1, obs.getExtension().size());
assertEquals("http://exturl", obs.getExtension().get(0).getUrl());
assertEquals("ext_url_value", ((StringType)obs.getExtension().get(0).getValue()).getValue());
assertEquals("ext_url_value", ((StringType) obs.getExtension().get(0).getValue()).getValue());
}
@Test
public void testEncodeExtensionUndeclaredNonModifierWithChildExtension() {
Observation obs = new Observation();
@ -604,14 +599,14 @@ public class JsonParserDstu3Test {
obs.getMeta().addProfile("http://profile");
Extension ext = obs.addExtension();
ext.setUrl("http://exturl");
Extension subExt = ext.addExtension();
subExt.setUrl("http://subext").setValue(new StringType("sub_ext_value"));
obs.getCode().setText("CODE");
IParser parser = ourCtx.newJsonParser();
String output = parser.setPrettyPrint(true).encodeResourceToString(obs);
ourLog.info(output);
@ -632,13 +627,13 @@ public class JsonParserDstu3Test {
"\"url\":\"http://exturl\""
)));
//@formatter:on
obs = parser.parseResource(Observation.class, output);
assertEquals(1, obs.getExtension().size());
assertEquals("http://exturl", obs.getExtension().get(0).getUrl());
assertEquals(1, obs.getExtension().get(0).getExtension().size());
assertEquals("http://subext", obs.getExtension().get(0).getExtension().get(0).getUrl());
assertEquals("sub_ext_value", ((StringType)obs.getExtension().get(0).getExtension().get(0).getValue()).getValue());
assertEquals("sub_ext_value", ((StringType) obs.getExtension().get(0).getExtension().get(0).getValue()).getValue());
}
@Test
@ -707,12 +702,13 @@ public class JsonParserDstu3Test {
ourLog.info(encoded);
assertThat(encoded, containsString("Patient"));
assertThat(encoded, stringContainsInOrder("\"tag\"", "\"system\":\"foo\",", "\"code\":\"bar\"", "\"system\":\"" + Constants.TAG_SUBSETTED_SYSTEM + "\"", "\"code\":\"" + Constants.TAG_SUBSETTED_CODE + "\""));
assertThat(encoded,
stringContainsInOrder("\"tag\"", "\"system\":\"foo\",", "\"code\":\"bar\"", "\"system\":\"" + Constants.TAG_SUBSETTED_SYSTEM + "\"", "\"code\":\"" + Constants.TAG_SUBSETTED_CODE + "\""));
assertThat(encoded, not(containsString("THE DIV")));
assertThat(encoded, containsString("family"));
assertThat(encoded, not(containsString("maritalStatus")));
}
/**
* See #205
*/
@ -726,11 +722,11 @@ public class JsonParserDstu3Test {
String enc = ourCtx.newJsonParser().encodeResourceToString(pt);
ourLog.info(enc);
assertEquals("{\"resourceType\":\"Patient\",\"meta\":{\"tag\":[{\"system\":\"scheme\",\"code\":\"term\",\"display\":\"display\"}]},\"identifier\":[{\"system\":\"sys\",\"value\":\"val\"}]}", enc);
assertEquals("{\"resourceType\":\"Patient\",\"meta\":{\"tag\":[{\"system\":\"scheme\",\"code\":\"term\",\"display\":\"display\"}]},\"identifier\":[{\"system\":\"sys\",\"value\":\"val\"}]}",
enc);
}
/**
* See #241
*/
@ -746,43 +742,66 @@ public class JsonParserDstu3Test {
String json = parser.encodeResourceToString(bundle);
ourLog.info(json);
bundle = (Bundle) parser.parseResource(json);
assertEquals("123", bundle.getEntry().get(0).getId());
condition = (Condition) bundle.getEntry().get(0).getResource();
assertEquals(null, condition.getId());
}
@Test
public void testEncodeUndeclaredExtensionWithEnumerationContent() {
IParser parser = ourCtx.newJsonParser();
public void testEncodeUndeclaredExtensionWithEnumerationContent() {
IParser parser = ourCtx.newJsonParser();
Patient patient = new Patient();
patient.addAddress().setUse(AddressUse.HOME);
EnumFactory<AddressUse> fact = new AddressUseEnumFactory();
PrimitiveType<AddressUse> enumeration = new Enumeration<AddressUse>(fact).setValue(AddressUse.HOME);
patient.addExtension().setUrl("urn:foo").setValue(enumeration);
Patient patient = new Patient();
patient.addAddress().setUse(AddressUse.HOME);
EnumFactory<AddressUse> fact = new AddressUseEnumFactory();
PrimitiveType<AddressUse> enumeration = new Enumeration<AddressUse>(fact).setValue(AddressUse.HOME);
patient.addExtension().setUrl("urn:foo").setValue(enumeration);
String val = parser.encodeResourceToString(patient);
ourLog.info(val);
assertThat(val, StringContains.containsString("\"extension\":[{\"url\":\"urn:foo\",\"valueCode\":\"home\"}]"));
String val = parser.encodeResourceToString(patient);
ourLog.info(val);
assertThat(val, StringContains.containsString("\"extension\":[{\"url\":\"urn:foo\",\"valueCode\":\"home\"}]"));
MyPatientWithOneDeclaredEnumerationExtensionDstu3 actual = parser.parseResource(MyPatientWithOneDeclaredEnumerationExtensionDstu3.class, val);
assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse());
Enumeration<AddressUse> ref = actual.getFoo();
assertEquals("home", ref.getValue().toCode());
MyPatientWithOneDeclaredEnumerationExtensionDstu3 actual = parser.parseResource(MyPatientWithOneDeclaredEnumerationExtensionDstu3.class, val);
assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse());
Enumeration<AddressUse> ref = actual.getFoo();
assertEquals("home", ref.getValue().toCode());
}
}
@Test
public void testEncodeExtendedInfrastructureComponent() {
IParser parser = ourCtx.newJsonParser();
PatientWithExtendedContactDstu3 patient = new PatientWithExtendedContactDstu3();
patient.setId("123");
CustomContactComponent customContactComponent = new CustomContactComponent();
customContactComponent.getEyeColour().setValue("EYE");
customContactComponent.getName().addFamily("FAMILY");
patient.getCustomContact().add(customContactComponent);
String val = parser.encodeResourceToString(patient);
ourLog.info(val);
assertEquals("{\"resourceType\":\"Patient\",\"id\":\"123\",\"contact\":[{\"extension\":[{\"url\":\"http://foo.com/contact-eyecolour\",\"valueIdentifier\":{\"value\":\"EYE\"}}],\"name\":{\"family\":[\"FAMILY\"]}}]}", val);
FhirContext newCtx = FhirContext.forDstu3();
PatientWithExtendedContactDstu3 actual = newCtx.newJsonParser().parseResource(PatientWithExtendedContactDstu3.class, val);
assertEquals("EYE", actual.getCustomContact().get(0).getEyeColour().getValue());
}
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
patient.getMeta().addProfile(("http://profile"));
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
@ -882,24 +901,12 @@ public class JsonParserDstu3Test {
*/
@Test
public void testExplanationOfBenefit() {
String input =
"{" +
" \"resourceType\":\"ExplanationOfBenefit\"," +
" \"coverage\": {\n" +
" \"coverageReference\": {\n" +
" \"reference\": \"Coverage/123\"\n" +
" }\n" +
" },\n" +
" \"relationship\": {\n" +
" \"system\": \"http://hl7.org/fhir/relationship\",\n" +
" \"code\": \"1\",\n" +
" \"display\": \"self\"\n" +
" }\n" +
"}";
String input = "{" + " \"resourceType\":\"ExplanationOfBenefit\"," + " \"coverage\": {\n" + " \"coverageReference\": {\n" + " \"reference\": \"Coverage/123\"\n" + " }\n" + " },\n"
+ " \"relationship\": {\n" + " \"system\": \"http://hl7.org/fhir/relationship\",\n" + " \"code\": \"1\",\n" + " \"display\": \"self\"\n" + " }\n" + "}";
ExplanationOfBenefit eob = ourCtx.newJsonParser().parseResource(ExplanationOfBenefit.class, input);
assertEquals(Reference.class, eob.getCoverage().getCoverage().getClass());
Reference coverage = (Reference) eob.getCoverage().getCoverage();
assertEquals("Coverage/123", coverage.getReference());
}
@ -1332,7 +1339,7 @@ public class JsonParserDstu3Test {
@Test
public void testParseExtensionWithId() throws Exception {
String input = IOUtils.toString(getClass().getResourceAsStream("/json-edge-case-modified-335.json"));
Patient p = ourCtx.newJsonParser().parseResource(Patient.class, input);
StringType family1 = p.getContact().get(0).getName().getFamily().get(1);
assertEquals("du", family1.getValue());
@ -1342,7 +1349,7 @@ public class JsonParserDstu3Test {
/**
* See #342
*/
@Test(expected=DataFormatException.class)
@Test(expected = DataFormatException.class)
public void testParseInvalid() {
ourCtx.newJsonParser().parseResource("FOO");
}

View File

@ -1,10 +1,20 @@
package ca.uhn.fhir.parser;
import org.hl7.fhir.dstu3.model.Address.AddressUse;
import org.hl7.fhir.dstu3.model.Enumeration;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Patient.ContactComponent;
import java.util.List;
import org.hl7.fhir.dstu3.model.BackboneElement;
import org.hl7.fhir.dstu3.model.Enumeration;
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.ResourceFactory;
import ca.uhn.fhir.model.api.annotation.Block;
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;
@ResourceDef(name = "Patient")
@ -16,6 +26,13 @@ public class MyPatientWithOneDeclaredEnumerationExtensionDstu3 extends Patient {
@ca.uhn.fhir.model.api.annotation.Extension(url = "urn:foo", definedLocally = true, isModifier = false)
private Enumeration<AddressUse> myFoo;
/**
* A contact party (e.g. guardian, partner, friend) for the patient.
*/
@Child(name = "contact", type = {}, order=Child.REPLACE_PARENT, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=false)
@Description(shortDefinition="A contact party (e.g. guardian, partner, friend) for the patient", formalDefinition="A contact party (e.g. guardian, partner, friend) for the patient." )
protected List<ContactComponent> contact;
public Enumeration<AddressUse> getFoo() {
return myFoo;
}
@ -24,4 +41,32 @@ public class MyPatientWithOneDeclaredEnumerationExtensionDstu3 extends Patient {
myFoo = theFoo;
}
@Block()
public static class MessageSourceComponent extends Patient.ContactComponent {
private static final long serialVersionUID = 1L;
@Child(name = "contact-eyecolour", type = { Identifier.class }, modifier = true)
@Description(shortDefinition = "Application ID")
@Extension(url = "http://foo.com/contact-eyecolour", definedLocally = false, isModifier = false)
private Identifier myEyeColour;
/*
* Get messageHeaderApplicationId
*/
public Identifier getEyeColour() {
if (myEyeColour == null) {
myEyeColour = new Identifier();
}
return myEyeColour;
}
/*
* Set messageHeaderApplicationId
*/
public void setEyeColour(Identifier messageHeaderApplicationId) {
this.myEyeColour = messageHeaderApplicationId;
}
}
}

View File

@ -0,0 +1,62 @@
package ca.uhn.fhir.parser;
import java.util.ArrayList;
import java.util.List;
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.Patient;
import ca.uhn.fhir.model.api.annotation.Block;
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;
@ResourceDef(name = "Patient")
public class PatientWithExtendedContactDstu3 extends Patient {
private static final long serialVersionUID = 1L;
/**
* A contact party (e.g. guardian, partner, friend) for the patient.
*/
@Child(name = "contact", type = {}, order = Child.REPLACE_PARENT, min = 0, max = Child.MAX_UNLIMITED, modifier = false, summary = false)
@Description(shortDefinition = "A contact party (e.g. guardian, partner, friend) for the patient", formalDefinition = "A contact party (e.g. guardian, partner, friend) for the patient.")
protected List<CustomContactComponent> customContact;
public List<CustomContactComponent> getCustomContact() {
if (customContact == null) {
customContact = new ArrayList<CustomContactComponent>();
}
return customContact;
}
@Block()
public static class CustomContactComponent extends Patient.ContactComponent {
private static final long serialVersionUID = 1L;
@Child(name = "contact-eyecolour", type = { Identifier.class }, modifier = true)
@Description(shortDefinition = "Application ID")
@Extension(url = "http://foo.com/contact-eyecolour", definedLocally = false, isModifier = false)
private Identifier myEyeColour;
/*
* Get messageHeaderApplicationId
*/
public Identifier getEyeColour() {
if (myEyeColour == null) {
myEyeColour = new Identifier();
}
return myEyeColour;
}
/*
* Set messageHeaderApplicationId
*/
public void setEyeColour(Identifier messageHeaderApplicationId) {
this.myEyeColour = messageHeaderApplicationId;
}
}
}