summaryrefslogtreecommitdiffstats
path: root/certServiceClient
diff options
context:
space:
mode:
Diffstat (limited to 'certServiceClient')
-rw-r--r--certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java22
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java32
2 files changed, 47 insertions, 7 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..abf3cb1d 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,11 +57,13 @@ public class HttpClient {
throws CertServiceApiResponseException, HttpClientException {
try (CloseableHttpClient httpClient = httpClientProvider.getClient()) {
- HttpResponse httpResponse = httpClient.execute(createHttpPayload(caName, csr, encodedPk));
+ LOGGER.info(String.format("Sending request to API. Url: %s ", 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'",
+ LOGGER.error(String.format("Failed execute request to API for URL: '%s' . Exception message: '%s'",
certServiceAddress + caName, e.getMessage()));
throw new HttpClientException(e);
}
@@ -72,7 +74,7 @@ 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));
@@ -82,11 +84,16 @@ public class HttpClient {
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);