diff options
Diffstat (limited to 'certServiceClient')
2 files changed, 49 insertions, 9 deletions
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java index 603d5848..30f881bb 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java @@ -57,12 +57,14 @@ public class HttpClient { throws CertServiceApiResponseException, HttpClientException { try (CloseableHttpClient httpClient = httpClientProvider.getClient()) { - HttpResponse httpResponse = httpClient.execute(createHttpPayload(caName, csr, encodedPk)); + LOGGER.info("Sending request to API. Url: {}{} ", certServiceAddress, caName); + HttpResponse httpResponse = httpClient.execute(createHttpRequest(caName, csr, encodedPk)); + LOGGER.info("Received response from API"); return extractCertServiceResponse(httpResponse); } catch (IOException e) { - LOGGER.error(String.format("Failed on communication between client and API for URL: '%s' . Exception message: '%s'", - certServiceAddress + caName, e.getMessage())); + LOGGER.error("Failed execute request to API for URL: {}{} . Exception message: {}", + certServiceAddress, caName, e.getMessage()); throw new HttpClientException(e); } } @@ -72,21 +74,26 @@ public class HttpClient { } private CertServiceResponse extractCertServiceResponse(HttpResponse httpResponse) - throws CertServiceApiResponseException, IOException { + throws CertServiceApiResponseException, HttpClientException { int httpResponseCode = getStatusCode(httpResponse); if (HttpStatus.SC_OK != httpResponseCode) { - LOGGER.error(String.format("Error on API response. Response Code: %d", httpResponseCode)); + LOGGER.error("Error on API response. Response Code: {}", httpResponseCode); throw generateApiResponseException(httpResponse); } String jsonResponse = getStringResponse(httpResponse.getEntity()); return gson.fromJson(jsonResponse, CertServiceResponse.class); } - private String getStringResponse(HttpEntity httpEntity) throws IOException { - return EntityUtils.toString(httpEntity, CHARSET_UTF_8); + private String getStringResponse(HttpEntity httpEntity) throws HttpClientException { + try { + return EntityUtils.toString(httpEntity, CHARSET_UTF_8); + } catch (IOException e) { + LOGGER.error("Cannot parse response to string", e); + throw new HttpClientException(e); + } } - private HttpGet createHttpPayload(String caName, String csr, String pk) { + private HttpGet createHttpRequest(String caName, String csr, String pk) { String url = certServiceAddress + caName; HttpGet httpGet = new HttpGet(url); httpGet.addHeader(CSR_HEADER_NAME, csr); @@ -95,7 +102,8 @@ public class HttpClient { } - private CertServiceApiResponseException generateApiResponseException(HttpResponse httpResponse) throws IOException { + private CertServiceApiResponseException generateApiResponseException(HttpResponse httpResponse) + throws HttpClientException { String stringResponse = getStringResponse(httpResponse.getEntity()); ErrorCertServiceResponse errorCertServiceResponse = gson.fromJson(stringResponse, ErrorCertServiceResponse.class); diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java index f65aefdf..461b7a34 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java @@ -29,6 +29,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.aaf.certservice.client.api.ExitCode; import org.onap.aaf.certservice.client.httpclient.exception.CertServiceApiResponseException; +import org.onap.aaf.certservice.client.httpclient.exception.HttpClientException; import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse; import java.io.ByteArrayInputStream; @@ -113,6 +114,37 @@ class HttpClientTest { assertEquals(ExitCode.CERT_SERVICE_API_CONNECTION_EXCEPTION.getValue(), exception.applicationExitCode()); } + @Test + void shouldThrowHttpClientException_WhenCannotExecuteRequestToAPI() throws Exception{ + + //given + when(closeableHttpClient.execute(any(HttpGet.class))).thenThrow(IOException.class); + + //when + HttpClientException exception = + assertThrows(HttpClientException.class, + () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, "")); + + //then + assertEquals(ExitCode.HTTP_CLIENT_EXCEPTION.getValue(), exception.applicationExitCode()); + } + + @Test + void shouldThrowHttpClientException_WhenCannotParseResponseToString() throws Exception{ + + //given + mockServerResponse(HTTP_OK, CORRECT_RESPONSE); + when(httpEntity.getContent()).thenThrow(IOException.class); + + //when + HttpClientException exception = + assertThrows(HttpClientException.class, + () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, "")); + + //then + assertEquals(ExitCode.HTTP_CLIENT_EXCEPTION.getValue(), exception.applicationExitCode()); + } + private void mockServerResponse(int serverCodeResponse, String stringResponse) throws IOException { when(statusLine.getStatusCode()).thenReturn(serverCodeResponse); |