Added unit test for encoding Binary with securityContext, and fixed encoding in JasonParser

This commit is contained in:
Malcolm McRoberts 2017-09-24 23:55:27 -04:00
parent be07ebc4ef
commit a57003808d
2 changed files with 33 additions and 0 deletions

View File

@ -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));
}

View File

@ -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