From b6b482e71132f0fdf478326e26756aa119749858 Mon Sep 17 00:00:00 2001 From: Tomasz Wrobel Date: Mon, 2 Mar 2020 13:20:50 +0100 Subject: Add tests to HttpClient Issue-ID: AAF-996 Signed-off-by: Tomasz Wrobel Change-Id: I2d8883552421d055622d5d9ca55b437647c36da4 --- .../client/httpclient/HttpClientTest.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'certServiceClient/src/test') 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); -- cgit 1.2.3-korg