From a57003808d9f8d44a88f95a653a155736dce7500 Mon Sep 17 00:00:00 2001 From: Malcolm McRoberts Date: Sun, 24 Sep 2017 23:55:27 -0400 Subject: [PATCH] Added unit test for encoding Binary with securityContext, and fixed encoding in JasonParser --- .../java/ca/uhn/fhir/parser/JsonParser.java | 16 ++++++++++++++++ .../ca/uhn/fhir/parser/JsonParserDstu3Test.java | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java index 55aa0bc7a7a..2e3a3563413 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java @@ -49,6 +49,7 @@ import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.util.ElementUtil; import static ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum.ID_DATATYPE; import static ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum.PRIMITIVE_DATATYPE; +import java.lang.reflect.Method; /** * This class is the FHIR JSON parser/encoder. Users should not interact with this class directly, but should use @@ -705,6 +706,21 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { if (isNotBlank(contentAsBase64)) { write(theEventWriter, "content", contentAsBase64); } + try { + Method getSC = bin.getClass().getMethod("getSecurityContext"); + Object securityContext = getSC.invoke(bin); + if (securityContext != null) { + Method getRef = securityContext.getClass().getMethod("getReference"); + String securityContextRef = (String) getRef.invoke(securityContext); + if (securityContextRef != null) { + beginObject(theEventWriter, "securityContext"); + writeOptionalTagWithTextNode(theEventWriter, "reference", securityContextRef); + theEventWriter.endObject(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } } else { encodeCompositeElementToStreamWriter(theResDef, theResource, theResource, theEventWriter, theContainedResource, theSubResource, new CompositeChildElement(resDef, theSubResource)); } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java index b62f272fe89..7f1736881ef 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java @@ -559,6 +559,23 @@ public class JsonParserDstu3Test { String output = ourCtx.newJsonParser().encodeResourceToString(new Binary()); assertEquals("{\"resourceType\":\"Binary\"}", output); } + + @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.newJsonParser().encodeResourceToString(bin); + assertThat(encoded, containsString("Binary")); + assertThat(encoded, containsString("contentType")); + assertThat(encoded, containsString("text/plain")); + assertThat(encoded, containsString("Tm93IGlzIHRoZSB0aW1l")); + assertThat(encoded, containsString("securityContext")); + assertThat(encoded, containsString("{\"reference\":\"DiagnosticReport/1\"}")); + } /** * #158