Add tests to HttpClient
authorTomasz Wrobel <tomasz.wrobel@nokia.com>
Mon, 2 Mar 2020 12:20:50 +0000 (13:20 +0100)
committerTomasz Wrobel <tomasz.wrobel@nokia.com>
Tue, 3 Mar 2020 12:36:34 +0000 (13:36 +0100)
Issue-ID: AAF-996
Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com>
Change-Id: I2d8883552421d055622d5d9ca55b437647c36da4

certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java
certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java

index 603d584..abf3cb1 100644 (file)
@@ -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);
index f65aefd..461b7a3 100644 (file)
@@ -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);