|
1 | 1 | package ca.uhn.hapi.fhir.cdshooks.svc;
|
2 | 2 |
|
3 | 3 | import ca.uhn.fhir.context.ConfigurationException;
|
| 4 | +import ca.uhn.fhir.rest.api.server.cdshooks.CdsServiceRequestJson; |
| 5 | +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; |
| 6 | +import ca.uhn.hapi.fhir.cdshooks.api.CDSHooksVersion; |
| 7 | +import ca.uhn.hapi.fhir.cdshooks.api.ICdsServiceMethod; |
4 | 8 | import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceFeedbackJson;
|
5 | 9 | import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceJson;
|
6 | 10 | import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceResponseJson;
|
|
10 | 14 | import ca.uhn.hapi.fhir.cdshooks.svc.prefetch.CdsPrefetchSvc;
|
11 | 15 | import com.fasterxml.jackson.core.JsonProcessingException;
|
12 | 16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 17 | +import jakarta.annotation.Nullable; |
13 | 18 | import org.junit.jupiter.api.BeforeEach;
|
14 | 19 | import org.junit.jupiter.api.Test;
|
15 | 20 | import org.junit.jupiter.api.extension.ExtendWith;
|
|
18 | 23 |
|
19 | 24 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 25 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 26 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.ArgumentMatchers.anyString; |
| 29 | +import static org.mockito.ArgumentMatchers.eq; |
21 | 30 | import static org.mockito.Mockito.doReturn;
|
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | +import static org.mockito.Mockito.when; |
22 | 33 |
|
23 | 34 | @ExtendWith(MockitoExtension.class)
|
24 | 35 | class CdsServiceRegistryImplTest {
|
@@ -177,4 +188,68 @@ void getCdsServiceJsonWhenServiceIsNotPresent() {
|
177 | 188 | myFixture.getCdsServiceJson(serviceId);
|
178 | 189 | }).withMessage("HAPI-2536: No service with " + serviceId + " is registered.");
|
179 | 190 | }
|
| 191 | + |
| 192 | + @Test |
| 193 | + void testCallService_doesNotEnforceFhirServerIsHttpsForCDSHooksV1() { |
| 194 | + String fhirServer = "http://localhost:8000/remote_fhir"; |
| 195 | + String response = "{\"cards\": []}"; |
| 196 | + CdsServiceRegistryImpl serviceRegistryImpl = createAndSetupCdsServiceRegistryImplForCallService(CDSHooksVersion.V_1_1, fhirServer, response); |
| 197 | + CdsServiceResponseJson responseJson = serviceRegistryImpl.callService(SERVICE_ID, ""); |
| 198 | + assertThat(responseJson).isNotNull(); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void testCallService_noValidationIfFhirServerIsNullForCDSHooksV2() { |
| 203 | + String response = "{\"cards\": []}"; |
| 204 | + CdsServiceRegistryImpl serviceRegistryImpl = createAndSetupCdsServiceRegistryImplForCallService(CDSHooksVersion.V_2_0, null, response); |
| 205 | + CdsServiceResponseJson responseJson = serviceRegistryImpl.callService(SERVICE_ID, ""); |
| 206 | + assertThat(responseJson).isNotNull(); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + void testCallService_enforcesFhirServerIsHttpsForCDSHooksV2_InvalidInput() { |
| 211 | + String fhirServer = "http://localhost:8000/remote_fhir"; |
| 212 | + CdsServiceRegistryImpl serviceRegistryImpl = createAndSetupCdsServiceRegistryImplForCallService(CDSHooksVersion.V_2_0, fhirServer, null); |
| 213 | + InvalidRequestException ex = assertThrows(InvalidRequestException.class, () -> serviceRegistryImpl.callService(SERVICE_ID, "")); |
| 214 | + assertThat(ex.getMessage()).isEqualTo("HAPI-2632: The scheme for the fhirServer must be https"); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + void testCallService_enforcesFhirServerIsHttpsForCDSHooksV2_ValidInput() { |
| 219 | + String fhirServer = "https://localhost:8000/remote_fhir"; |
| 220 | + String response = "{\"cards\": []}"; |
| 221 | + CdsServiceRegistryImpl serviceRegistryImpl = createAndSetupCdsServiceRegistryImplForCallService(CDSHooksVersion.V_2_0, fhirServer, response); |
| 222 | + CdsServiceResponseJson responseJson = serviceRegistryImpl.callService(SERVICE_ID, ""); |
| 223 | + assertThat(responseJson).isNotNull(); |
| 224 | + } |
| 225 | + |
| 226 | + private CdsServiceRegistryImpl createAndSetupCdsServiceRegistryImplForCallService(CDSHooksVersion theCdsHooksVersion, @Nullable String theFhirServer, @Nullable String theSuccessResponse) { |
| 227 | + CdsServiceRegistryImpl serviceRegistryImpl = new CdsServiceRegistryImpl( |
| 228 | + myCdsHooksContextBooter, |
| 229 | + myCdsPrefetchSvc, |
| 230 | + myObjectMapper, |
| 231 | + myCdsCrServiceFactory, |
| 232 | + myCrDiscoveryServiceFactory, |
| 233 | + myCdsServiceRequestJsonDeserializer, |
| 234 | + theCdsHooksVersion); |
| 235 | + |
| 236 | + final CdsServiceJson cdsService = new CdsServiceJson(); |
| 237 | + cdsService.setId(SERVICE_ID); |
| 238 | + serviceRegistryImpl.setServiceCache(myCdsServiceCache); |
| 239 | + doReturn(cdsService).when(myCdsServiceCache).getCdsServiceJson(SERVICE_ID); |
| 240 | + |
| 241 | + CdsServiceRequestJson requestJson = new CdsServiceRequestJson(); |
| 242 | + requestJson.setFhirServer(theFhirServer); |
| 243 | + doReturn(requestJson).when(myCdsServiceRequestJsonDeserializer).deserialize(eq(cdsService), anyString()); |
| 244 | + |
| 245 | + if (theSuccessResponse != null) { |
| 246 | + ICdsServiceMethod theMethodMock= mock(ICdsServiceMethod.class); |
| 247 | + when(theMethodMock.invoke(myObjectMapper, requestJson, SERVICE_ID)).thenReturn(theSuccessResponse); |
| 248 | + when(myCdsServiceCache.getServiceMethod(SERVICE_ID)).thenReturn(theMethodMock); |
| 249 | + } |
| 250 | + |
| 251 | + |
| 252 | + return serviceRegistryImpl; |
| 253 | + } |
| 254 | + |
180 | 255 | }
|
0 commit comments