Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ public void When_SdsEndpointReturnsNoResult_Expect_EmptyResultIsReturned() {
stubSdsOperation(pair.getKey(), DEVICE, ResourceReader.asString(sdsDeviceResponse));
assertThatThrownBy(() -> pair.getValue().apply(FROM_ODS_CODE, X_CORRELATION_ID).block())
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("SDS returned no result");
.hasMessageContaining("SDS returned no result")
.hasMessageContaining("lookupContext=provider-endpoint")
.hasMessageContaining("bundleTotal=0")
.hasMessageContaining("entryCount=0");
wireMockServer.resetAll();
});
}
Expand All @@ -269,11 +272,31 @@ public void When_SdsDeviceReturnsNoResults_Expect_EmptyResultIsReturned() {
stubSdsOperation(pair.getKey(), DEVICE, ResourceReader.asString(sdsNoResultResponse));
assertThatThrownBy(() -> pair.getValue().apply(FROM_ODS_CODE, X_CORRELATION_ID).block())
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("SDS returned no result");
.hasMessageContaining("SDS returned no result")
.hasMessageContaining("lookupContext=provider-device-asid")
.hasMessageContaining("bundleTotal=0")
.hasMessageContaining("entryCount=0");
wireMockServer.resetAll();
});
}

@Test
public void When_SdsConsumerAsidLookupReturnsNoResult_Expect_EmptyResultIsReturned() {
wireMockServer.resetAll();

ReflectionTestUtils.setField(sdsClient, "supplierOdsCode", SUPPLIER_ODS_CODE);
stubSdsAsidOperation(GET_STRUCTURED_INTERACTION, DEVICE, ResourceReader.asString(sdsNoResultResponse));

assertThatThrownBy(() -> sdsClient.callForGetAsid(GET_STRUCTURED_INTERACTION, FROM_ODS_CODE, X_CORRELATION_ID).block())
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("SDS returned no result")
.hasMessageContaining("lookupContext=consumer-asid")
.hasMessageContaining("bundleTotal=0")
.hasMessageContaining("entryCount=0");

wireMockServer.resetAll();
}

@Test
public void When_SdsReturnsError_Expect_Exception() {
allInteractions.forEach(pair -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class SdsClient {

private static final String NHS_MHS_ID = "https://fhir.nhs.uk/Id/nhsMHSId";
private static final String NHS_SPINE_ASID = "https://fhir.nhs.uk/Id/nhsSpineASID";
private static final String LOOKUP_CONTEXT_PROVIDER_DEVICE_ASID = "provider-device-asid";
private static final String LOOKUP_CONTEXT_PROVIDER_ENDPOINT = "provider-endpoint";
private static final String LOOKUP_CONTEXT_CONSUMER_ASID = "consumer-asid";
private final IParser fhirParser;
private final SdsRequestBuilder sdsRequestBuilder;

Expand All @@ -34,7 +37,7 @@ public class SdsClient {

public Mono<String> callForGetAsid(String interactionId, String fromOdsCode, String correlationId) {
var sdsDeviceRequest = sdsRequestBuilder.buildAsDeviceAsidRequest(fromOdsCode, supplierOdsCode, interactionId, correlationId);
return retrieveAsDeviceNhsSpineAsid(sdsDeviceRequest);
return retrieveAsDeviceNhsSpineAsid(sdsDeviceRequest, LOOKUP_CONTEXT_CONSUMER_ASID);
}

public Mono<SdsResponseData> callForGetStructuredRecord(String fromOdsCode, String correlationId) {
Expand Down Expand Up @@ -77,11 +80,11 @@ private Mono<SdsResponseData> retrieveData(RequestHeadersSpec<? extends RequestH
RequestHeadersSpec<? extends RequestHeadersSpec<?>> sdsEndpointRequest) {
LOGGER.info("Using SDS Endpoint endpoint to retrieve GPC provider endpoint details");

return retrieveAsDeviceNhsSpineAsid(sdsDeviceRequest)
return retrieveAsDeviceNhsSpineAsid(sdsDeviceRequest, LOOKUP_CONTEXT_PROVIDER_DEVICE_ASID)
.flatMap(nhsSpineAsid -> performRequest(sdsEndpointRequest)
.map(bodyString -> fhirParser.parseResource(Bundle.class, bodyString))
.map(bundle -> {
doBundleEntryCheck(bundle);
doBundleEntryCheck(bundle, LOOKUP_CONTEXT_PROVIDER_ENDPOINT);
var endpoint = (Endpoint) bundle.getEntryFirstRep().getResource();

return SdsResponseData.builder()
Expand All @@ -93,14 +96,17 @@ private Mono<SdsResponseData> retrieveData(RequestHeadersSpec<? extends RequestH
);
}

private Mono<String> retrieveAsDeviceNhsSpineAsid(RequestHeadersSpec<? extends RequestHeadersSpec<?>> request) {
private Mono<String> retrieveAsDeviceNhsSpineAsid(RequestHeadersSpec<? extends RequestHeadersSpec<?>> request,
String lookupContext) {

LOGGER.info("Using SDS Device endpoint to retrieve Spine ASID");
LOGGER.info("Using SDS Device endpoint to retrieve Spine ASID for {} lookup", lookupContext);

return performRequest(request)
.doOnNext(bodyString -> LOGGER.info("Received SDS Device response for {} lookup (payloadLength={})",
lookupContext, bodyString.length()))
.map(bodyString -> fhirParser.parseResource(Bundle.class, bodyString))
.map(bundle -> {
doBundleEntryCheck(bundle);
doBundleEntryCheck(bundle, lookupContext);
var device = (Device) bundle.getEntryFirstRep().getResource();
return getNhsSpineAsid(device);
});
Expand All @@ -124,17 +130,25 @@ private String getNhsMhsId(Endpoint endpoint) {
.orElseThrow(() -> new RuntimeException(String.format("Identifier of system %s not found", NHS_MHS_ID)));
}

private void doBundleEntryCheck(Bundle bundle) {
LOGGER.info("Attempting to parse the bundle response from SDS");
private void doBundleEntryCheck(Bundle bundle, String lookupContext) {
LOGGER.info("Attempting to parse the bundle response from SDS ({})", getBundleSummary(bundle, lookupContext));
if (!bundle.hasEntry()) {
throw new RuntimeException("SDS returned no result");
throw new RuntimeException(String.format("SDS returned no result (%s)", getBundleSummary(bundle, lookupContext)));
}

if (bundle.getEntry().size() > 1) {
LOGGER.warn("SDS returned more than 1 result. Taking the first one");
LOGGER.warn("SDS returned more than 1 result. Taking the first one ({})", getBundleSummary(bundle, lookupContext));
}
}

private String getBundleSummary(Bundle bundle, String lookupContext) {
return String.format("lookupContext=%s, bundleType=%s, bundleTotal=%d, entryCount=%d",
lookupContext,
bundle.getType(),
bundle.getTotal(),
bundle.getEntry().size());
}

@NotNull
private String getAddressFromEndpoint(Endpoint endpoint) {
var address = endpoint.getAddress();
Expand Down
Loading