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;
@ -110,7 +111,6 @@ public class JsonParserDstu3Test {
assertThat(output, containsString("\"div\":\"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">VALUE</div>\""));
}
/**
* See #344
*/
@ -144,8 +144,6 @@ public class JsonParserDstu3Test {
}
}
@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
@ -347,22 +347,21 @@ public class JsonParserDstu3Test {
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() {
Patient p = new Patient();
@ -477,7 +476,6 @@ public class JsonParserDstu3Test {
//@formatter:on
}
@Test
public void testEncodeDoesntIncludeUuidId() {
Patient p = new Patient();
@ -494,7 +492,6 @@ public class JsonParserDstu3Test {
assertEquals("{\"resourceType\":\"Binary\"}", output);
}
/**
* #158
*/
@ -511,7 +508,6 @@ public class JsonParserDstu3Test {
assertThat(encoded, not(containsString("tag")));
}
/**
* #158
*/
@ -559,7 +555,6 @@ public class JsonParserDstu3Test {
}
@Test
public void testEncodeExtensionUndeclaredNonModifier() {
Observation obs = new Observation();
@ -594,7 +589,7 @@ public class JsonParserDstu3Test {
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
@ -638,7 +633,7 @@ public class JsonParserDstu3Test {
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,7 +702,8 @@ 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")));
@ -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
*/
@ -774,6 +770,29 @@ public class JsonParserDstu3Test {
}
@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();
@ -882,20 +901,8 @@ 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());
@ -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;
}
}
}