Skip to content

Commit

Permalink
Merge pull request #99 from medizininformatik-initiative/62-catch-ioe…
Browse files Browse the repository at this point in the history
…xceptions-and-log-context-information

Wrap HTTP Exceptions in Flare Client
  • Loading branch information
EmteZogaf authored Jan 11, 2024
2 parents b90ed32 + d9d8317 commit a028eb2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HttpPost> 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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a028eb2

Please sign in to comment.