diff options
author | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2020-03-02 13:20:50 +0100 |
---|---|---|
committer | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2020-03-03 13:36:34 +0100 |
commit | b6b482e71132f0fdf478326e26756aa119749858 (patch) | |
tree | 6f83012617b14408b8005233f1f1c51ea2016fca /certServiceClient/src/main/java/org | |
parent | be552bb854e00ad79d0854304226829d0f969fb6 (diff) |
Add tests to HttpClient
Issue-ID: AAF-996
Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com>
Change-Id: I2d8883552421d055622d5d9ca55b437647c36da4
Diffstat (limited to 'certServiceClient/src/main/java/org')
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java | 22 |
1 files changed, 15 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); |