fix the issue (#5214)

This commit is contained in:
VK-SMILECDR 2023-08-17 14:01:50 -04:00 committed by GitHub
parent fdfda37f73
commit 3512f82a6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 13 deletions

View File

@ -0,0 +1,4 @@
---
type: fix
issue: 5212
title: "Previously, when a CDS hook was registered and called with a empty context, the server returned a 500. This behaviour has been fixed."

View File

@ -22,6 +22,7 @@ package ca.uhn.hapi.fhir.cdshooks.api.json;
import ca.uhn.fhir.model.api.IModelJson;
import org.hl7.fhir.instance.model.api.IBaseResource;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -39,7 +40,7 @@ public class CdsServiceRequestContextJson extends BaseCdsServiceJson implements
public List<String> getArray(String theKey) {
if (myMap == null) {
return null;
return Collections.emptyList();
}
return (List<String>) myMap.get(theKey);
}
@ -59,14 +60,23 @@ public class CdsServiceRequestContextJson extends BaseCdsServiceJson implements
}
public Set<String> getKeys() {
if (myMap == null) {
return Collections.emptySet();
}
return myMap.keySet();
}
public Object get(String theKey) {
if (myMap == null) {
return null;
}
return myMap.get(theKey);
}
public boolean containsKey(String theKey) {
if (myMap == null) {
return false;
}
return myMap.containsKey(theKey);
}
}

View File

@ -1,13 +1,13 @@
package ca.uhn.hapi.fhir.cdshooks.module;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceRequestContextJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceRequestJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceResponseCardJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceResponseJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceResponseSuggestionActionJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceResponseSuggestionJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceResponseSystemActionJson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
@ -23,17 +23,17 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.io.IOException;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
import static org.hamcrest.Matchers.equalToCompressingWhiteSpace;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
@ExtendWith(SpringExtension.class)
public class SerializationTest {
class SerializationTest {
public static final String HOOK_NAME = "Hook Name";
public static final String FHIR_SERVER = "https://localhost:2401";
public static final String FAMILY = "Jehoshaphat";
@ -60,7 +60,7 @@ public class SerializationTest {
private String myRequestJson;
@BeforeEach
public void loadJson() throws IOException {
public void loadJson() throws Exception {
myRequestJson = IOUtils.toString(myRequestJsonResource.getInputStream(), Charsets.UTF_8);
myResponseJson = IOUtils.toString(myResponseJsonResource.getInputStream(), Charsets.UTF_8);
}
@ -78,7 +78,7 @@ public class SerializationTest {
// These tests verify that nulls prefetch values are preserved in serialization and deserialization so their
// missing status is properly determined
@Test
public void testSerializeRequest() throws JsonProcessingException {
void testSerializeRequest() throws Exception {
CdsServiceRequestJson cdsServiceRequestJson = new CdsServiceRequestJson();
cdsServiceRequestJson.setHook(HOOK_NAME);
cdsServiceRequestJson.setFhirServer(FHIR_SERVER);
@ -106,11 +106,11 @@ public class SerializationTest {
String json = ourObjectMapper.writeValueAsString(cdsServiceRequestJson);
ourLog.debug(json);
assertThat(json, equalToIgnoringWhiteSpace(myRequestJson));
assertThat(json, equalToCompressingWhiteSpace(myRequestJson));
}
@Test
public void testDeserializeRequest() throws JsonProcessingException {
void testDeserializeRequest() throws Exception {
CdsServiceRequestJson cdsServiceRequestJson = ourObjectMapper.readValue(myRequestJson, CdsServiceRequestJson.class);
assertEquals(HOOK_NAME, cdsServiceRequestJson.getHook());
@ -127,11 +127,11 @@ public class SerializationTest {
assertEquals(CONTEXT_PATIENT_VALUE, cdsServiceRequestJson.getContext().getString(CONTEXT_PATIENT_KEY));
List<String> selections = cdsServiceRequestJson.getContext().getArray(CONTEXT_SELECTIONS_KEY);
assertThat(selections, contains(CONTEXT_SELECTIONS_VALUE1, CONTEXT_SELECTIONS_VALUE2));
Bundle bundle = (Bundle) cdsServiceRequestJson.getContext().getResource(CONTEXT_DRAFT_ORDERS_KEY);
cdsServiceRequestJson.getContext().getResource(CONTEXT_DRAFT_ORDERS_KEY);
}
@Test
public void testSerializeResponse() throws JsonProcessingException {
void testSerializeResponse() throws Exception {
Patient patient = new Patient();
patient.addName().setFamily(FAMILY);
CdsServiceResponseSystemActionJson systemAction = new CdsServiceResponseSystemActionJson();
@ -151,13 +151,41 @@ public class SerializationTest {
String json = ourObjectMapper.writeValueAsString(cdsServiceRequestJson);
ourLog.debug(json);
assertThat(json, equalToIgnoringWhiteSpace(myResponseJson));
assertThat(json, equalToCompressingWhiteSpace(myResponseJson));
}
@Test
public void testDeserializeResponse() throws JsonProcessingException {
void testDeserializeResponse() throws Exception {
CdsServiceResponseJson cdsServiceResponseJson = ourObjectMapper.readValue(myResponseJson, CdsServiceResponseJson.class);
Patient patient = (Patient) cdsServiceResponseJson.getServiceActions().get(0).getResource();
assertEquals(FAMILY, patient.getNameFirstRep().getFamily());
}
@Test
void testSerializeDeserializeEmptyRequest() throws Exception {
// setup
final CdsServiceRequestJson expected = new CdsServiceRequestJson();
final String expectedAsJson = ourObjectMapper.writeValueAsString(expected);
// execute
final CdsServiceRequestJson actual = ourObjectMapper.readValue(expectedAsJson, CdsServiceRequestJson.class);
// validate
final String actualAsJson = ourObjectMapper.writeValueAsString(actual);
assertNotSame(expected, actual);
assertEquals(expectedAsJson, actualAsJson);
}
@Test
void testSerializeDeserializeRequestWithEmptyContext() throws Exception {
// setup
final CdsServiceRequestJson expected = new CdsServiceRequestJson();
expected.setContext(new CdsServiceRequestContextJson());
final String expectedAsJson = ourObjectMapper.writeValueAsString(expected);
// execute
final CdsServiceRequestJson actual = ourObjectMapper.readValue(expectedAsJson, CdsServiceRequestJson.class);
// validate
final String actualAsJson = ourObjectMapper.writeValueAsString(actual);
assertNotSame(expected, actual);
assertEquals(expectedAsJson, actualAsJson);
}
}