(Arrays.asList("*.id", "*.meta")));
@@ -777,20 +780,20 @@ public class JsonParserDstu3Test {
//@formatter:off
assertThat(output, stringContainsInOrder(
- "\"id\": \"1\"",
- "\"meta\"",
- "\"extension\"",
- "\"url\": \"http://exturl\"",
- "\"extension\"",
- "\"url\": \"http://subext\"",
- "\"valueString\": \"sub_ext_value\"",
- "\"code\":"
- ));
- assertThat(output, not(stringContainsInOrder(
- "\"url\": \"http://exturl\"",
- ",",
- "\"url\": \"http://exturl\""
- )));
+ "\"id\": \"1\"",
+ "\"meta\"",
+ "\"extension\"",
+ "\"url\": \"http://exturl\"",
+ "\"extension\"",
+ "\"url\": \"http://subext\"",
+ "\"valueString\": \"sub_ext_value\"",
+ "\"code\":"
+ ));
+ assertThat(output, not(stringContainsInOrder(
+ "\"url\": \"http://exturl\"",
+ ",",
+ "\"url\": \"http://exturl\""
+ )));
//@formatter:on
obs = parser.parseResource(Observation.class, output);
@@ -821,17 +824,17 @@ public class JsonParserDstu3Test {
//@formatter:off
assertThat(encoded, stringContainsInOrder(
- "\"resourceType\": \"Patient\"",
+ "\"resourceType\": \"Patient\"",
"\"contained\": [",
- "\"resourceType\": \"Condition\"",
- "\"id\": \"1\"",
- "\"bodySite\": [",
- "\"text\": \"BODY SITE\"",
- "\"extension\": [",
- "\"url\": \"testCondition\",",
- "\"valueReference\": {",
- "\"reference\": \"#1\"",
- "\"birthDate\": \"2016-04-14\"",
+ "\"resourceType\": \"Condition\"",
+ "\"id\": \"1\"",
+ "\"bodySite\": [",
+ "\"text\": \"BODY SITE\"",
+ "\"extension\": [",
+ "\"url\": \"testCondition\",",
+ "\"valueReference\": {",
+ "\"reference\": \"#1\"",
+ "\"birthDate\": \"2016-04-14\"",
"}"
));
//@formatter:on
@@ -1071,7 +1074,7 @@ public class JsonParserDstu3Test {
assertThat(encoded, containsString("Patient"));
assertThat(encoded, stringContainsInOrder("\"tag\"", "\"system\": \"foo\",", "\"code\": \"bar\"", "\"system\": \"" + ca.uhn.fhir.rest.api.Constants.TAG_SUBSETTED_SYSTEM + "\"",
- "\"code\": \"" + ca.uhn.fhir.rest.api.Constants.TAG_SUBSETTED_CODE + "\""));
+ "\"code\": \"" + ca.uhn.fhir.rest.api.Constants.TAG_SUBSETTED_CODE + "\""));
assertThat(encoded, not(containsString("THE DIV")));
assertThat(encoded, containsString("family"));
assertThat(encoded, not(containsString("maritalStatus")));
@@ -1091,7 +1094,7 @@ public class JsonParserDstu3Test {
ourLog.info(enc);
assertEquals("{\"resourceType\":\"Patient\",\"meta\":{\"tag\":[{\"system\":\"scheme\",\"code\":\"term\",\"display\":\"display\"}]},\"identifier\":[{\"system\":\"sys\",\"value\":\"val\"}]}",
- enc);
+ enc);
}
@@ -1268,19 +1271,19 @@ public class JsonParserDstu3Test {
@Test
public void testExplanationOfBenefit() {
//@formatter:off
- String input = "{" +
- " \"resourceType\": \"ExplanationOfBenefit\"," +
- " \"insurance\": {\n" +
- " \"coverage\": {\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\"," +
+ " \"insurance\": {\n" +
+ " \"coverage\": {\n" +
+ " \"reference\": \"Coverage/123\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"relationship\": {\n" +
+ " \"system\": \"http://hl7.org/fhir/relationship\",\n" +
+ " \"code\": \"1\",\n" +
+ " \"display\": \"self\"\n" +
+ " }\n" +
+ "}";
//@formatter:on
ExplanationOfBenefit eob = ourCtx.newJsonParser().parseResource(ExplanationOfBenefit.class, input);
@@ -1334,14 +1337,14 @@ public class JsonParserDstu3Test {
// ID should be a String and communication should be an Array
String input = "{\"resourceType\": \"Patient\",\n" +
- " \"id\": 123,\n" +
- " \"communication\": {\n" +
- " \"language\": {\n" +
- " \"text\": \"Hindi\"\n" +
- " },\n" +
- " \"preferred\": true\n" +
- " }\n" +
- "}";
+ " \"id\": 123,\n" +
+ " \"communication\": {\n" +
+ " \"language\": {\n" +
+ " \"text\": \"Hindi\"\n" +
+ " },\n" +
+ " \"preferred\": true\n" +
+ " }\n" +
+ "}";
IParser p = ourCtx.newJsonParser();
@@ -1375,14 +1378,14 @@ public class JsonParserDstu3Test {
// ID should be a String and communication should be an Array
String input = "{\"resourceType\": \"Patient\",\n" +
- " \"id\": \"123\",\n" +
- " \"communication\": [{\n" +
- " \"language\": {\n" +
- " \"text\": \"Hindi\"\n" +
- " },\n" +
- " \"preferred\": true\n" +
- " }]\n" +
- "}";
+ " \"id\": \"123\",\n" +
+ " \"communication\": [{\n" +
+ " \"language\": {\n" +
+ " \"text\": \"Hindi\"\n" +
+ " },\n" +
+ " \"preferred\": true\n" +
+ " }]\n" +
+ "}";
IParser p = ourCtx.newJsonParser();
@@ -1503,14 +1506,14 @@ public class JsonParserDstu3Test {
@Ignore
public void testNamespacePreservationEncode() throws Exception {
//@formatter:off
- String input = "" +
- "" +
- "" +
- "" +
- "@fhirabend" +
- "" +
- "" +
- "";
+ String input = "" +
+ "" +
+ "" +
+ "" +
+ "@fhirabend" +
+ "" +
+ "" +
+ "";
//@formatter:on
Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, input);
@@ -1712,69 +1715,69 @@ public class JsonParserDstu3Test {
@Test
public void testParseAndEncodeBundleWithUuidBase() {
//@formatter:off
- String input =
- "{\n" +
- " \"resourceType\":\"Bundle\",\n" +
- " \"type\":\"document\",\n" +
- " \"entry\":[\n" +
- " {\n" +
- " \"fullUrl\":\"urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57\",\n" +
- " \"resource\":{\n" +
- " \"resourceType\":\"Composition\",\n" +
- " \"id\":\"180f219f-97a8-486d-99d9-ed631fe4fc57\",\n" +
- " \"meta\":{\n" +
- " \"lastUpdated\":\"2013-05-28T22:12:21Z\"\n" +
- " },\n" +
- " \"text\":{\n" +
- " \"status\":\"generated\",\n" +
- " \"div\":\"\"\n" +
- " },\n" +
- " \"date\":\"2013-02-01T12:30:02Z\",\n" +
- " \"type\":{\n" +
- " \"coding\":[\n" +
- " {\n" +
- " \"system\":\"http://loinc.org\",\n" +
- " \"code\":\"28655-9\"\n" +
- " }\n" +
- " ],\n" +
- " \"text\":\"Discharge Summary from Responsible Clinician\"\n" +
- " },\n" +
- " \"status\":\"final\",\n" +
- " \"confidentiality\":\"N\",\n" +
- " \"subject\":{\n" +
- " \"reference\":\"http://fhir.healthintersections.com.au/open/Patient/d1\",\n" +
- " \"display\":\"Eve Everywoman\"\n" +
- " },\n" +
- " \"author\":[\n" +
- " {\n" +
- " \"reference\":\"Practitioner/example\",\n" +
- " \"display\":\"Doctor Dave\"\n" +
- " }\n" +
- " ],\n" +
- " \"encounter\":{\n" +
- " \"reference\":\"http://fhir.healthintersections.com.au/open/Encounter/doc-example\"\n" +
- " },\n" +
- " \"section\":[\n" +
- " {\n" +
- " \"title\":\"Reason for admission\",\n" +
- " \"content\":{\n" +
- " \"reference\":\"urn:uuid:d0dd51d3-3ab2-4c84-b697-a630c3e40e7a\"\n" +
- " }\n" +
- " },\n" +
- " {\n" +
- " \"title\":\"Medications on Discharge\",\n" +
- " \"content\":{\n" +
- " \"reference\":\"urn:uuid:673f8db5-0ffd-4395-9657-6da00420bbc1\"\n" +
- " }\n" +
- " },\n" +
- " {\n" +
- " \"title\":\"Known allergies\",\n" +
- " \"content\":{\n" +
- " \"reference\":\"urn:uuid:68f86194-e6e1-4f65-b64a-5314256f8d7b\"\n" +
- " }\n" +
- " }\n" +
- " ]\n" +
- " }\n" +
+ String input =
+ "{\n" +
+ " \"resourceType\":\"Bundle\",\n" +
+ " \"type\":\"document\",\n" +
+ " \"entry\":[\n" +
+ " {\n" +
+ " \"fullUrl\":\"urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57\",\n" +
+ " \"resource\":{\n" +
+ " \"resourceType\":\"Composition\",\n" +
+ " \"id\":\"180f219f-97a8-486d-99d9-ed631fe4fc57\",\n" +
+ " \"meta\":{\n" +
+ " \"lastUpdated\":\"2013-05-28T22:12:21Z\"\n" +
+ " },\n" +
+ " \"text\":{\n" +
+ " \"status\":\"generated\",\n" +
+ " \"div\":\"\"\n" +
+ " },\n" +
+ " \"date\":\"2013-02-01T12:30:02Z\",\n" +
+ " \"type\":{\n" +
+ " \"coding\":[\n" +
+ " {\n" +
+ " \"system\":\"http://loinc.org\",\n" +
+ " \"code\":\"28655-9\"\n" +
+ " }\n" +
+ " ],\n" +
+ " \"text\":\"Discharge Summary from Responsible Clinician\"\n" +
+ " },\n" +
+ " \"status\":\"final\",\n" +
+ " \"confidentiality\":\"N\",\n" +
+ " \"subject\":{\n" +
+ " \"reference\":\"http://fhir.healthintersections.com.au/open/Patient/d1\",\n" +
+ " \"display\":\"Eve Everywoman\"\n" +
+ " },\n" +
+ " \"author\":[\n" +
+ " {\n" +
+ " \"reference\":\"Practitioner/example\",\n" +
+ " \"display\":\"Doctor Dave\"\n" +
+ " }\n" +
+ " ],\n" +
+ " \"encounter\":{\n" +
+ " \"reference\":\"http://fhir.healthintersections.com.au/open/Encounter/doc-example\"\n" +
+ " },\n" +
+ " \"section\":[\n" +
+ " {\n" +
+ " \"title\":\"Reason for admission\",\n" +
+ " \"content\":{\n" +
+ " \"reference\":\"urn:uuid:d0dd51d3-3ab2-4c84-b697-a630c3e40e7a\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"title\":\"Medications on Discharge\",\n" +
+ " \"content\":{\n" +
+ " \"reference\":\"urn:uuid:673f8db5-0ffd-4395-9657-6da00420bbc1\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"title\":\"Known allergies\",\n" +
+ " \"content\":{\n" +
+ " \"reference\":\"urn:uuid:68f86194-e6e1-4f65-b64a-5314256f8d7b\"\n" +
+ " }\n" +
+ " }\n" +
+ " ]\n" +
+ " }\n" +
" }" +
" ]" +
"}";
@@ -1794,34 +1797,34 @@ public class JsonParserDstu3Test {
@Test
public void testParseAndEncodeComments() {
//@formatter:off
- String input = "{\n" +
- " \"resourceType\": \"Patient\",\n" +
- " \"id\": \"pat1\",\n" +
- " \"text\": {\n" +
- " \"status\": \"generated\",\n" +
- " \"div\": \"\\n \\n
Patient Donald DUCK @ Acme Healthcare, Inc. MR = 654321
\\n \\n
\"\n" +
- " },\n" +
- " \"identifier\": [\n" +
- " {\n" +
- " \"fhir_comments\":[\"identifier comment 1\",\"identifier comment 2\"],\n" +
- " \"use\": \"usual\",\n" +
- " \"_use\": {\n" +
- " \"fhir_comments\":[\"use comment 1\",\"use comment 2\"]\n" +
- " },\n" +
- " \"type\": {\n" +
- " \"coding\": [\n" +
- " {\n" +
- " \"system\": \"http://hl7.org/fhir/v2/0203\",\n" +
- " \"code\": \"MR\"\n" +
- " }\n" +
- " ]\n" +
- " },\n" +
- " \"system\": \"urn:oid:0.1.2.3.4.5.6.7\",\n" +
- " \"value\": \"654321\"\n" +
- " }\n" +
- " ],\n" +
- " \"active\": true" +
- "}";
+ String input = "{\n" +
+ " \"resourceType\": \"Patient\",\n" +
+ " \"id\": \"pat1\",\n" +
+ " \"text\": {\n" +
+ " \"status\": \"generated\",\n" +
+ " \"div\": \"\\n \\n
Patient Donald DUCK @ Acme Healthcare, Inc. MR = 654321
\\n \\n
\"\n" +
+ " },\n" +
+ " \"identifier\": [\n" +
+ " {\n" +
+ " \"fhir_comments\":[\"identifier comment 1\",\"identifier comment 2\"],\n" +
+ " \"use\": \"usual\",\n" +
+ " \"_use\": {\n" +
+ " \"fhir_comments\":[\"use comment 1\",\"use comment 2\"]\n" +
+ " },\n" +
+ " \"type\": {\n" +
+ " \"coding\": [\n" +
+ " {\n" +
+ " \"system\": \"http://hl7.org/fhir/v2/0203\",\n" +
+ " \"code\": \"MR\"\n" +
+ " }\n" +
+ " ]\n" +
+ " },\n" +
+ " \"system\": \"urn:oid:0.1.2.3.4.5.6.7\",\n" +
+ " \"value\": \"654321\"\n" +
+ " }\n" +
+ " ],\n" +
+ " \"active\": true" +
+ "}";
//@formatter:off
Patient res = ourCtx.newJsonParser().parseResource(Patient.class, input);
@@ -1829,33 +1832,33 @@ public class JsonParserDstu3Test {
assertEquals("Patient/pat1", res.getId());
assertEquals("654321", res.getIdentifier().get(0).getValue());
assertEquals(true, res.getActive());
-
+
assertThat(res.getIdentifier().get(0).getFormatCommentsPre(), contains("identifier comment 1", "identifier comment 2"));
assertThat(res.getIdentifier().get(0).getUseElement().getFormatCommentsPre(), contains("use comment 1", "use comment 2"));
-
+
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(res);
ourLog.info(encoded);
-
+
//@formatter:off
assertThat(encoded, stringContainsInOrder(
- "\"identifier\": [",
- "{",
- "\"fhir_comments\":",
- "[",
- "\"identifier comment 1\"",
- ",",
- "\"identifier comment 2\"",
- "]",
- "\"use\": \"usual\",",
- "\"_use\": {",
- "\"fhir_comments\":",
- "[",
- "\"use comment 1\"",
- ",",
- "\"use comment 2\"",
- "]",
- "},",
- "\"type\""
+ "\"identifier\": [",
+ "{",
+ "\"fhir_comments\":",
+ "[",
+ "\"identifier comment 1\"",
+ ",",
+ "\"identifier comment 2\"",
+ "]",
+ "\"use\": \"usual\",",
+ "\"_use\": {",
+ "\"fhir_comments\":",
+ "[",
+ "\"use comment 1\"",
+ ",",
+ "\"use comment 2\"",
+ "]",
+ "},",
+ "\"type\""
));
//@formatter:off
}
@@ -1865,7 +1868,7 @@ public class JsonParserDstu3Test {
Binary patient = new Binary();
patient.setId(new IdType("http://base/Binary/11/_history/22"));
patient.setContentType("foo");
- patient.setContent(new byte[] { 1, 2, 3, 4 });
+ patient.setContent(new byte[]{1, 2, 3, 4});
String val = ourCtx.newJsonParser().encodeResourceToString(patient);
@@ -1916,7 +1919,7 @@ public class JsonParserDstu3Test {
"\"resourceType\":\"Patient\",",
"\"id\":\"1\"",
"\"reference\":\"#1\""
- ));
+ ));
//@formatter:on
o = parser.parseResource(Observation.class, enc);
@@ -1927,6 +1930,21 @@ public class JsonParserDstu3Test {
assertEquals("patient family", p.getName().get(0).getFamilyElement().getValue());
}
+ /**
+ * See #720
+ */
+ @Test
+ public void testParseCustomResourceType() {
+ String input = "{\"resourceType\":\"Bug720ResourceType\",\"meta\":{\"profile\":[\"http://example.com/StructureDefinition/dontuse#Bug720ResourceType\"]},\"supportedVersion\":\"2.5.x\",\"templatesConsentTemplate\":[{\"domainName\":\"name\",\"Name\":\"template_01\",\"version\":\"1.0\",\"title\":\"title\",\"comment\":\"comment\",\"contact\":{\"resourceType\":\"Person\",\"name\":[{\"family\":\"Mustermann\",\"given\":[\"Max\"]}],\"telecom\":[{\"system\":\"email\",\"value\":\"max.mustermann@mail.de\"},{\"system\":\"phone\",\"value\":\"+49 1234 23232\"}],\"address\":[{\"text\":\"street 1-2\",\"city\":\"city\",\"postalCode\":\"12345\",\"country\":\"Germany\"}]}}]}";
+ Bug720ResourceType parsed = ourCtx.newJsonParser().parseResource(Bug720ResourceType.class, input);
+
+ assertEquals(1, parsed.getTemplates().size());
+ assertEquals(Bug720Datatype.class, parsed.getTemplates().get(0).getClass());
+ assertEquals("Mustermann", ((Bug720Datatype) parsed.getTemplates().get(0)).getContact().getNameFirstRep().getFamily());
+
+ ourLog.info(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed));
+ }
+
/**
* #480
*/
@@ -1987,10 +2005,10 @@ public class JsonParserDstu3Test {
@Test
public void testParseJsonExtensionWithoutUrl() {
//@formatter:off
- String input =
+ String input =
"{\"resourceType\":\"Patient\"," +
- "\"extension\":[ {\"valueDateTime\":\"2011-01-02T11:13:15\"} ]" +
- "}";
+ "\"extension\":[ {\"valueDateTime\":\"2011-01-02T11:13:15\"} ]" +
+ "}";
//@formatter:on
IParser parser = ourCtx.newJsonParser();
@@ -2017,10 +2035,10 @@ public class JsonParserDstu3Test {
@Test
public void testParseJsonModifierExtensionWithoutUrl() {
//@formatter:off
- String input =
+ String input =
"{\"resourceType\":\"Patient\"," +
- "\"modifierExtension\":[ {\"valueDateTime\":\"2011-01-02T11:13:15\"} ]" +
- "}";
+ "\"modifierExtension\":[ {\"valueDateTime\":\"2011-01-02T11:13:15\"} ]" +
+ "}";
//@formatter:on
IParser parser = ourCtx.newJsonParser();
@@ -2044,24 +2062,24 @@ public class JsonParserDstu3Test {
@Test
public void testParseMetadata() throws Exception {
//@formatter:off
- String bundle = "{\n" +
- " \"resourceType\" : \"Bundle\",\n" +
- " \"total\" : 1,\n" +
- " \"link\": [{\n" +
- " \"relation\" : \"self\",\n" +
- " \"url\" : \"http://localhost:52788/Binary?_pretty=true\"\n" +
- " }],\n" +
- " \"entry\" : [{\n" +
+ String bundle = "{\n" +
+ " \"resourceType\" : \"Bundle\",\n" +
+ " \"total\" : 1,\n" +
+ " \"link\": [{\n" +
+ " \"relation\" : \"self\",\n" +
+ " \"url\" : \"http://localhost:52788/Binary?_pretty=true\"\n" +
+ " }],\n" +
+ " \"entry\" : [{\n" +
" \"fullUrl\" : \"http://foo/fhirBase2/Patient/1/_history/2\",\n" +
- " \"resource\" : {\n" +
- " \"resourceType\" : \"Patient\",\n" +
- " \"id\" : \"1\",\n" +
+ " \"resource\" : {\n" +
+ " \"resourceType\" : \"Patient\",\n" +
+ " \"id\" : \"1\",\n" +
" \"meta\" : {\n" +
" \"versionId\" : \"2\",\n" +
" \"lastUpdated\" : \"2001-02-22T11:22:33-05:00\"\n" +
- " },\n" +
- " \"birthDate\" : \"2012-01-02\"\n" +
- " },\n" +
+ " },\n" +
+ " \"birthDate\" : \"2012-01-02\"\n" +
+ " },\n" +
" \"search\" : {\n" +
" \"mode\" : \"match\",\n" +
" \"score\" : 0.123\n" +
@@ -2070,7 +2088,7 @@ public class JsonParserDstu3Test {
" \"method\" : \"POST\",\n" +
" \"url\" : \"http://foo/Patient?identifier=value\"\n" +
" }\n" +
- " }]\n" +
+ " }]\n" +
"}";
//@formatter:on
@@ -2182,6 +2200,43 @@ public class JsonParserDstu3Test {
}
}
+ @Test
+ public void testParseWithPrecision() {
+ String input = "{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}";
+ Observation obs = ourCtx.newJsonParser().parseResource(Observation.class, input);
+
+ DecimalType valueElement = ((Quantity) obs.getValue()).getValueElement();
+ assertEquals("0.000000000000000100", valueElement.getValueAsString());
+
+ String str = ourCtx.newJsonParser().encodeResourceToString(obs);
+ ourLog.info(str);
+ assertEquals("{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}", str);
+ }
+
+ @Test(expected = DataFormatException.class)
+ public void testParseWithTrailingContent() throws Exception {
+ //@formatter:off
+ String bundle = "{\n" +
+ " \"resourceType\" : \"Bundle\",\n" +
+ " \"total\" : 1\n" +
+ "}}";
+ //@formatter:on
+
+ Bundle b = ourCtx.newJsonParser().parseResource(Bundle.class, bundle);
+ }
+
+ @Test
+ @Ignore
+ public void testParseWithWrongTypeObjectShouldBeArray() throws Exception {
+ String input = IOUtils.toString(getClass().getResourceAsStream("/invalid_metadata.json"));
+ try {
+ ourCtx.newJsonParser().parseResource(CapabilityStatement.class, input);
+ fail();
+ } catch (DataFormatException e) {
+ assertEquals("Syntax error parsing JSON FHIR structure: Expected ARRAY at element 'modifierExtension', found 'OBJECT'", e.getMessage());
+ }
+ }
+
/**
* See #344
*/
@@ -2215,43 +2270,6 @@ public class JsonParserDstu3Test {
}
}
- @Test
- public void testParseWithPrecision() {
- String input = "{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}";
- Observation obs = ourCtx.newJsonParser().parseResource(Observation.class, input);
-
- DecimalType valueElement = ((Quantity) obs.getValue()).getValueElement();
- assertEquals("0.000000000000000100", valueElement.getValueAsString());
-
- String str = ourCtx.newJsonParser().encodeResourceToString(obs);
- ourLog.info(str);
- assertEquals("{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}", str);
- }
-
- @Test(expected = DataFormatException.class)
- public void testParseWithTrailingContent() throws Exception {
- //@formatter:off
- String bundle = "{\n" +
- " \"resourceType\" : \"Bundle\",\n" +
- " \"total\" : 1\n" +
- "}}";
- //@formatter:on
-
- Bundle b = ourCtx.newJsonParser().parseResource(Bundle.class, bundle);
- }
-
- @Test
- @Ignore
- public void testParseWithWrongTypeObjectShouldBeArray() throws Exception {
- String input = IOUtils.toString(getClass().getResourceAsStream("/invalid_metadata.json"));
- try {
- ourCtx.newJsonParser().parseResource(CapabilityStatement.class, input);
- fail();
- } catch (DataFormatException e) {
- assertEquals("Syntax error parsing JSON FHIR structure: Expected ARRAY at element 'modifierExtension', found 'OBJECT'", e.getMessage());
- }
- }
-
/**
* See #144 and #146
*/
@@ -2323,13 +2341,13 @@ public class JsonParserDstu3Test {
ArgumentCaptor expectedScalar = ArgumentCaptor.forClass(ScalarType.class);
ArgumentCaptor actualScalar = ArgumentCaptor.forClass(ScalarType.class);
verify(errorHandler, atLeastOnce()).incorrectJsonType(Mockito.any(IParseLocation.class), elementName.capture(), expected.capture(), expectedScalar.capture(), actual.capture(),
- actualScalar.capture());
+ actualScalar.capture());
verify(errorHandler, atLeastOnce()).incorrectJsonType(Mockito.any(IParseLocation.class), Mockito.eq("_id"), Mockito.eq(ValueType.OBJECT), expectedScalar.capture(), Mockito.eq(ValueType.SCALAR),
- actualScalar.capture());
+ actualScalar.capture());
verify(errorHandler, atLeastOnce()).incorrectJsonType(Mockito.any(IParseLocation.class), Mockito.eq("__v"), Mockito.eq(ValueType.OBJECT), expectedScalar.capture(), Mockito.eq(ValueType.SCALAR),
- actualScalar.capture());
+ actualScalar.capture());
verify(errorHandler, atLeastOnce()).incorrectJsonType(Mockito.any(IParseLocation.class), Mockito.eq("_status"), Mockito.eq(ValueType.OBJECT), expectedScalar.capture(),
- Mockito.eq(ValueType.SCALAR), actualScalar.capture());
+ Mockito.eq(ValueType.SCALAR), actualScalar.capture());
assertEquals("_id", elementName.getAllValues().get(0));
assertEquals(ValueType.OBJECT, expected.getAllValues().get(0));
@@ -2361,26 +2379,6 @@ public class JsonParserDstu3Test {
assertTrue(result.isSuccessful());
}
- @Test
- public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
- String refVal = "http://my.org/FooBar";
-
- Patient fhirPat = new Patient();
- fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
-
- IParser parser = ourCtx.newJsonParser();
-
- String output = parser.encodeResourceToString(fhirPat);
- System.out.println("output: " + output);
-
- // Deserialize then check that valueReference value is still correct
- fhirPat = parser.parseResource(Patient.class, output);
-
- List extlst = fhirPat.getExtensionsByUrl("x1");
- Assert.assertEquals(1, extlst.size());
- Assert.assertEquals(refVal, ((Reference) extlst.get(0).getValue()).getReference());
- }
-
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java
index 4c63567c71f..fe8a29f4f42 100644
--- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java
+++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java
@@ -82,6 +82,23 @@ public class XmlParserDstu3Test {
ourCtx.setNarrativeGenerator(null);
}
+ @Test
+ public void testEncodeBinaryWithSecurityContext() {
+ Binary bin = new Binary();
+ bin.setContentType("text/plain");
+ bin.setContent("Now is the time".getBytes());
+ Reference securityContext = new Reference();
+ securityContext.setReference("DiagnosticReport/1");
+ bin.setSecurityContext(securityContext);
+ String encoded = ourCtx.newXmlParser().encodeResourceToString(bin);
+ ourLog.info(encoded);
+ assertThat(encoded, containsString("Binary"));
+ assertThat(encoded, containsString(""));
+ assertThat(encoded, containsString(""));
+ assertThat(encoded, containsString(""));
+ }
+
+
/**
* See #544
*/
diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/BinaryServerR4Test.java
similarity index 50%
rename from hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryR4Test.java
rename to hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/BinaryServerR4Test.java
index 51d2abef86f..4bee8510f9e 100644
--- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryR4Test.java
+++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/BinaryServerR4Test.java
@@ -1,14 +1,16 @@
package ca.uhn.fhir.rest.server;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import java.util.concurrent.TimeUnit;
-
+import ca.uhn.fhir.context.FhirContext;
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.Constants;
+import ca.uhn.fhir.rest.api.MethodOutcome;
+import ca.uhn.fhir.util.PortUtil;
+import ca.uhn.fhir.util.TestUtil;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
@@ -18,18 +20,20 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.instance.model.api.IBaseResource;
-import org.hl7.fhir.r4.model.*;
-import org.junit.*;
+import org.hl7.fhir.r4.model.Binary;
+import org.hl7.fhir.r4.model.IdType;
+import org.hl7.fhir.r4.model.Patient;
+import org.hl7.fhir.r4.model.Reference;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
-import ca.uhn.fhir.context.FhirContext;
-import ca.uhn.fhir.rest.annotation.Create;
-import ca.uhn.fhir.rest.annotation.ResourceParam;
-import ca.uhn.fhir.rest.api.Constants;
-import ca.uhn.fhir.rest.api.MethodOutcome;
-import ca.uhn.fhir.util.PortUtil;
-import ca.uhn.fhir.util.TestUtil;
+import java.util.concurrent.TimeUnit;
-public class CreateBinaryR4Test {
+import static org.junit.Assert.*;
+
+public class BinaryServerR4Test {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forR4();
private static Binary ourLastBinary;
@@ -37,24 +41,73 @@ public class CreateBinaryR4Test {
private static String ourLastBinaryString;
private static int ourPort;
private static Server ourServer;
+ private static IdType ourLastId;
+ private static Binary ourNextBinary;
@Before
public void before() {
ourLastBinary = null;
ourLastBinaryBytes = null;
ourLastBinaryString = null;
+ ourLastId = null;
+ ourNextBinary = null;
}
@Test
- public void testRawBytesBinaryContentType() throws Exception {
+ public void testGetWithNoAccept() throws Exception {
+
+ ourNextBinary = new Binary();
+ ourNextBinary.setId("Binary/A/_history/222");
+ ourNextBinary.setContent(new byte[]{0, 1, 2, 3, 4});
+ ourNextBinary.setSecurityContext(new Reference("Patient/1"));
+ ourNextBinary.setContentType("application/foo");
+
+ HttpGet get = new HttpGet("http://localhost:" + ourPort + "/Binary/A");
+ get.addHeader("Content-Type", "application/foo");
+ CloseableHttpResponse status = ourClient.execute(get);
+ try {
+ assertEquals(200, status.getStatusLine().getStatusCode());
+ assertEquals("application/foo", status.getEntity().getContentType().getValue());
+ assertEquals("Patient/1", status.getFirstHeader(Constants.HEADER_X_SECURITY_CONTEXT).getValue());
+ assertEquals("W/\"222\"", status.getFirstHeader(Constants.HEADER_ETAG).getValue());
+ assertEquals("http://localhost:" + ourPort + "/Binary/A/_history/222", status.getFirstHeader(Constants.HEADER_LOCATION).getValue());
+
+ byte[] content = IOUtils.toByteArray(status.getEntity().getContent());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, content);
+ } finally {
+ IOUtils.closeQuietly(status);
+ }
+ }
+
+ @Test
+ public void testPostBinaryWithSecurityContext() throws Exception {
HttpPost post = new HttpPost("http://localhost:" + ourPort + "/Binary");
- post.setEntity(new ByteArrayEntity(new byte[] { 0, 1, 2, 3, 4 }));
+ post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
+ post.addHeader("Content-Type", "application/foo");
+ post.addHeader(Constants.HEADER_X_SECURITY_CONTEXT, "Encounter/2");
+ CloseableHttpResponse status = ourClient.execute(post);
+ try {
+ assertNull(ourLastId);
+ assertEquals("application/foo", ourLastBinary.getContentType());
+ assertEquals("Encounter/2", ourLastBinary.getSecurityContext().getReference());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinary.getContent());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinaryBytes);
+ } finally {
+ IOUtils.closeQuietly(status);
+ }
+ }
+
+ @Test
+ public void testPostRawBytesBinaryContentType() throws Exception {
+ HttpPost post = new HttpPost("http://localhost:" + ourPort + "/Binary");
+ post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
post.addHeader("Content-Type", "application/foo");
CloseableHttpResponse status = ourClient.execute(post);
try {
+ assertNull(ourLastId);
assertEquals("application/foo", ourLastBinary.getContentType());
- assertArrayEquals(new byte[] { 0, 1, 2, 3, 4 }, ourLastBinary.getContent());
- assertArrayEquals(new byte[] { 0, 1, 2, 3, 4 }, ourLastBinaryBytes);
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinary.getContent());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinaryBytes);
} finally {
IOUtils.closeQuietly(status);
}
@@ -64,11 +117,11 @@ public class CreateBinaryR4Test {
* Technically the client shouldn't be doing it this way, but we'll be accepting
*/
@Test
- public void testRawBytesFhirContentType() throws Exception {
+ public void testPostRawBytesFhirContentType() throws Exception {
Binary b = new Binary();
b.setContentType("application/foo");
- b.setContent(new byte[] { 0, 1, 2, 3, 4 });
+ b.setContent(new byte[]{0, 1, 2, 3, 4});
String encoded = ourCtx.newJsonParser().encodeResourceToString(b);
HttpPost post = new HttpPost("http://localhost:" + ourPort + "/Binary");
@@ -77,14 +130,14 @@ public class CreateBinaryR4Test {
CloseableHttpResponse status = ourClient.execute(post);
try {
assertEquals("application/foo", ourLastBinary.getContentType());
- assertArrayEquals(new byte[] { 0, 1, 2, 3, 4 }, ourLastBinary.getContent());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinary.getContent());
} finally {
IOUtils.closeQuietly(status);
}
}
@Test
- public void testRawBytesFhirContentTypeContainingFhir() throws Exception {
+ public void testPostRawBytesFhirContentTypeContainingFhir() throws Exception {
Patient p = new Patient();
p.getText().setDivAsString("A PATIENT");
@@ -109,13 +162,32 @@ public class CreateBinaryR4Test {
}
@Test
- public void testRawBytesNoContentType() throws Exception {
+ public void testPostRawBytesNoContentType() throws Exception {
HttpPost post = new HttpPost("http://localhost:" + ourPort + "/Binary");
- post.setEntity(new ByteArrayEntity(new byte[] { 0, 1, 2, 3, 4 }));
+ post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
CloseableHttpResponse status = ourClient.execute(post);
try {
assertNull(ourLastBinary.getContentType());
- assertArrayEquals(new byte[] { 0, 1, 2, 3, 4 }, ourLastBinary.getContent());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinary.getContent());
+ } finally {
+ IOUtils.closeQuietly(status);
+ }
+ }
+
+ @Test
+ public void testPutBinaryWithSecurityContext() throws Exception {
+ HttpPut post = new HttpPut("http://localhost:" + ourPort + "/Binary/A");
+ post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
+ post.addHeader("Content-Type", "application/foo");
+ post.addHeader(Constants.HEADER_X_SECURITY_CONTEXT, "Encounter/2");
+ CloseableHttpResponse status = ourClient.execute(post);
+ try {
+ assertEquals("Binary/A", ourLastId.getValue());
+ assertEquals("Binary/A", ourLastBinary.getId());
+ assertEquals("application/foo", ourLastBinary.getContentType());
+ assertEquals("Encounter/2", ourLastBinary.getSecurityContext().getReference());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinary.getContent());
+ assertArrayEquals(new byte[]{0, 1, 2, 3, 4}, ourLastBinaryBytes);
} finally {
IOUtils.closeQuietly(status);
}
@@ -149,7 +221,6 @@ public class CreateBinaryR4Test {
}
public static class BinaryProvider implements IResourceProvider {
-
@Create()
public MethodOutcome createBinary(@ResourceParam Binary theBinary, @ResourceParam String theBinaryString, @ResourceParam byte[] theBinaryBytes) {
ourLastBinary = theBinary;
@@ -163,6 +234,20 @@ public class CreateBinaryR4Test {
return Binary.class;
}
+ @Read
+ public Binary read(@IdParam IdType theId) {
+ return ourNextBinary;
+ }
+
+ @Update()
+ public MethodOutcome updateBinary(@IdParam IdType theId, @ResourceParam Binary theBinary, @ResourceParam String theBinaryString, @ResourceParam byte[] theBinaryBytes) {
+ ourLastId = theId;
+ ourLastBinary = theBinary;
+ ourLastBinaryString = theBinaryString;
+ ourLastBinaryBytes = theBinaryBytes;
+ return new MethodOutcome(new IdType("Binary/001/_history/002"));
+ }
+
}
}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 046692dc885..0c3ed55d06b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -175,6 +175,11 @@
was not encoded correctly. Thanks to Malcolm McRoberts for the pull
request with fix and test case!
+
+ The Binary resource endpoint now supports the `X-Security-Context` header when
+ reading or writing Binary contents using their native Content-Type (i.e exchanging
+ the raw binary with the server, as opposed to exchanging a FHIR resource).
+