diff --git a/feasibility-dsf-process/src/main/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImpl.java b/feasibility-dsf-process/src/main/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImpl.java index de7cf51..fb0a5ac 100644 --- a/feasibility-dsf-process/src/main/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImpl.java +++ b/feasibility-dsf-process/src/main/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImpl.java @@ -3,6 +3,7 @@ import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.message.BasicHeader; @@ -11,6 +12,7 @@ import java.net.URI; import static ca.uhn.fhir.rest.api.Constants.HEADER_CONTENT_TYPE; +import static java.lang.String.format; /** * Client for communicating with a Flare instance. @@ -31,19 +33,28 @@ public int requestFeasibility(byte[] structuredQuery) throws IOException, Interr req.setEntity(new ByteArrayEntity(structuredQuery)); req.setHeader(new BasicHeader(HEADER_CONTENT_TYPE, "application/sq+json")); - var response = httpClient.execute(req, new BasicResponseHandler()); + var response = sendRequest(req); return Integer.parseInt(response); } - private URI resolve(String path) { - return flareBaseUrl.resolve((flareBaseUrl.getPath() + path).replaceAll("//", "/")); - } - @Override public void testConnection() throws IOException { var req = new HttpGet(resolve("/cache/stats")); - httpClient.execute(req, new BasicResponseHandler()); + sendRequest(req); + } + + private URI resolve(String path) { + return flareBaseUrl.resolve((flareBaseUrl.getPath() + path).replaceAll("//", "/")); + } + + private String sendRequest(HttpUriRequest req) throws IOException { + try { + return httpClient.execute(req, new BasicResponseHandler()); + } catch (IOException e) { + throw new IOException( + format("Error sending %s request to flare webservice url '%s'.", req.getMethod(), req.getURI()), e); + } } } diff --git a/feasibility-dsf-process/src/test/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImplTest.java b/feasibility-dsf-process/src/test/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImplTest.java index 8043878..b633e7f 100644 --- a/feasibility-dsf-process/src/test/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImplTest.java +++ b/feasibility-dsf-process/src/test/java/de/medizininformatik_initiative/feasibility_dsf_process/client/flare/FlareWebserviceClientImplTest.java @@ -18,6 +18,8 @@ import java.net.URISyntaxException; import java.time.Duration; +import static jakarta.ws.rs.HttpMethod.POST; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -28,14 +30,16 @@ @ExtendWith(MockitoExtension.class) public class FlareWebserviceClientImplTest { - private org.apache.http.client.HttpClient httpClient; + private HttpClient httpClient; + private URI flareBaseUrl; private FlareWebserviceClient flareWebserviceClient; @Captor ArgumentCaptor postCaptor; @BeforeEach public void setUp() throws URISyntaxException { httpClient = mock(HttpClient.class); - flareWebserviceClient = new FlareWebserviceClientImpl(httpClient, new URI("http://localhost:5000/")); + flareBaseUrl = new URI("http://localhost:5000/"); + flareWebserviceClient = new FlareWebserviceClientImpl(httpClient, flareBaseUrl); } @Test @@ -134,4 +138,18 @@ void testOtherEvaluationStrategyFails() throws Exception { assertEquals("EVALUATION_STRATEGY is not set to 'structured-query'.", e.getMessage()); } + + @Test + void sendErrorGetsRethrownWithAdditionalInformation() throws Exception { + byte[] structuredQuery = "foo".getBytes(); + IOException error = new IOException("error-151930"); + when(httpClient.execute(any(HttpPost.class), any(BasicResponseHandler.class))).thenThrow(error); + + assertThatThrownBy(() -> { + flareWebserviceClient.requestFeasibility(structuredQuery); + }) + .hasMessage("Error sending %s request to flare webservice url '%s'.", POST, + flareBaseUrl.resolve("/query/execute")) + .hasCause(error); + } }