diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java index 2713477e7..d052f681a 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java @@ -171,7 +171,7 @@ public class FHIRToolingClient { public TerminologyCapabilities getTerminologyCapabilities() { TerminologyCapabilities capabilities = null; - capabilities = (TerminologyCapabilities) getCapabilities(resourceAddress.resolveMetadataTxCaps(), + capabilities = getCapabilities(resourceAddress.resolveMetadataTxCaps(), "TerminologyCapabilities", "Error fetching the server's terminology capabilities"); @@ -181,7 +181,7 @@ public class FHIRToolingClient { public CapabilityStatement getCapabilitiesStatement() { CapabilityStatement capabilityStatement = null; - capabilityStatement = (CapabilityStatement) getCapabilities(resourceAddress.resolveMetadataUri(false), + capabilityStatement = getCapabilities(resourceAddress.resolveMetadataUri(false), "CapabilitiesStatement", "Error fetching the server's conformance statement"); return capabilityStatement; @@ -189,15 +189,12 @@ public class FHIRToolingClient { public CapabilityStatement getCapabilitiesStatementQuick() throws EFhirClientException { if (capabilities != null) return capabilities; - try { - capabilities = (CapabilityStatement) client.issueGetResourceRequest(resourceAddress.resolveMetadataUri(true), - getPreferredResourceFormat(), - generateHeaders(), + + capabilities = getCapabilities(resourceAddress.resolveMetadataUri(true), + "CapabilitiesStatement-Quick", - TIMEOUT_NORMAL).getReference(); - } catch (Exception e) { - throw new FHIRException("Error fetching the server's capability statement: "+e.getMessage(), e); - } + "Error fetching the server's capability statement"); + return capabilities; } diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/utils/client/FHIRToolingClientTest.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/utils/client/FHIRToolingClientTest.java index d642aa9a1..6c180f1e5 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/utils/client/FHIRToolingClientTest.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/utils/client/FHIRToolingClientTest.java @@ -56,9 +56,6 @@ class FHIRToolingClientTest { Mockito.when(mockClient.issueGetResourceRequest(Mockito.any(URI.class), Mockito.anyString(), Mockito.any(Headers.class), Mockito.anyString(), Mockito.anyLong())) .thenReturn(resourceResourceRequest); - Mockito.when(mockClient.issueGetResourceRequest(Mockito.any(URI.class), Mockito.anyString(), - Mockito.any(Headers.class), Mockito.eq("CapabilitiesStatement-Quick"), Mockito.anyLong())) - .thenReturn(new ResourceRequest<>(new CapabilityStatement(), 200, "location")); //PUT Mockito.when(mockClient.issuePutRequest(Mockito.any(URI.class), Mockito.any(byte[].class), Mockito.anyString(), @@ -248,6 +245,11 @@ class FHIRToolingClientTest { @Test void getCapabilitiesStatementQuick() throws IOException { + Mockito.when(mockClient.issueGetResourceRequest(Mockito.any(URI.class), Mockito.anyString(), + Mockito.any(Headers.class), Mockito.eq("CapabilitiesStatement-Quick"), Mockito.anyLong())) + .thenReturn(new ResourceRequest<>(new CapabilityStatement(), 200, "location")); + assertEquals(ResourceFormat.RESOURCE_JSON.getHeader(), toolingClient.getPreferredResourceFormat()); + ArgumentCaptor headersArgumentCaptor = ArgumentCaptor.forClass(Headers.class); toolingClient.setClientHeaders(getHeaders()); toolingClient.getCapabilitiesStatementQuick(); @@ -256,6 +258,46 @@ class FHIRToolingClientTest { Headers argumentCaptorValue = headersArgumentCaptor.getValue(); checkHeaders(argumentCaptorValue); + assertEquals(ResourceFormat.RESOURCE_JSON.getHeader(), toolingClient.getPreferredResourceFormat()); + + } + + @Test + void getCapabilitiesStatementQuickFailsForJSON() throws IOException { + Mockito.when(mockClient.issueGetResourceRequest(Mockito.any(URI.class), Mockito.anyString(), + Mockito.any(Headers.class), Mockito.eq("CapabilitiesStatement-Quick"), Mockito.anyLong())) + .thenThrow(new FHIRFormatError("dummy error")) + .thenReturn(new ResourceRequest<>(new CapabilityStatement(), 200, "location")); + + assertEquals(ResourceFormat.RESOURCE_JSON.getHeader(), toolingClient.getPreferredResourceFormat()); + + ArgumentCaptor headersArgumentCaptor = ArgumentCaptor.forClass(Headers.class); + toolingClient.setClientHeaders(getHeaders()); + toolingClient.getCapabilitiesStatementQuick(); + Mockito.verify(mockClient, times(2)).issueGetResourceRequest(ArgumentMatchers.any(URI.class), ArgumentMatchers.anyString(), + headersArgumentCaptor.capture(), ArgumentMatchers.anyString(), ArgumentMatchers.anyLong()); + + Headers argumentCaptorValue = headersArgumentCaptor.getValue(); + checkHeaders(argumentCaptorValue); + assertEquals(ResourceFormat.RESOURCE_XML.getHeader(), toolingClient.getPreferredResourceFormat()); + + } + + @Test + void getCapabilitiesStatementQuickFailsForJSONandXML() throws IOException { + Mockito.when(mockClient.issueGetResourceRequest(Mockito.any(URI.class), Mockito.anyString(), + Mockito.any(Headers.class), Mockito.eq("CapabilitiesStatement-Quick"), Mockito.anyLong())) + .thenThrow(new FHIRFormatError("dummy error")) + .thenThrow(new FHIRFormatError("dummy error 2")); + + ArgumentCaptor headersArgumentCaptor = ArgumentCaptor.forClass(Headers.class); + toolingClient.setClientHeaders(getHeaders()); + assertEquals(ResourceFormat.RESOURCE_JSON.getHeader(), toolingClient.getPreferredResourceFormat()); + Exception exception = assertThrows(FHIRException.class, () -> { + toolingClient.getCapabilitiesStatementQuick(); + }); + assertEquals(ResourceFormat.RESOURCE_JSON.getHeader(), toolingClient.getPreferredResourceFormat()); + } @Test