diff --git a/service/src/intTest/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClientComponentTest.java b/service/src/intTest/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClientComponentTest.java index 7be39ec8..4e2afe3b 100644 --- a/service/src/intTest/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClientComponentTest.java +++ b/service/src/intTest/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClientComponentTest.java @@ -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(); }); } @@ -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 -> { diff --git a/service/src/main/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClient.java b/service/src/main/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClient.java index dbab9f3e..0557f483 100644 --- a/service/src/main/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClient.java +++ b/service/src/main/java/uk/nhs/adaptors/gpc/consumer/sds/SdsClient.java @@ -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; @@ -34,7 +37,7 @@ public class SdsClient { public Mono 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 callForGetStructuredRecord(String fromOdsCode, String correlationId) { @@ -77,11 +80,11 @@ private Mono retrieveData(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() @@ -93,14 +96,17 @@ private Mono retrieveData(RequestHeadersSpec retrieveAsDeviceNhsSpineAsid(RequestHeadersSpec> request) { + private Mono retrieveAsDeviceNhsSpineAsid(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); }); @@ -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();