From 8b74643e0738bd0266e510a422bf71cc67604f37 Mon Sep 17 00:00:00 2001 From: Tomasz Wrobel Date: Wed, 18 Mar 2020 14:35:36 +0100 Subject: Improve exceptions logging in certservice client Issue-ID: AAF-1107 Signed-off-by: Tomasz Wrobel Change-Id: I94a6477a7b1f086b698be4f0388e7a3ded0cd0f3 --- .../certservice/client/CertServiceClientTest.java | 14 +++--- .../certservice/client/DummyExitableException.java | 35 -------------- .../model/CsrConfigurationFactoryTest.java | 5 +- .../client/httpclient/HttpClientTest.java | 53 ++++++++-------------- 4 files changed, 29 insertions(+), 78 deletions(-) delete mode 100644 certServiceClient/src/test/java/org/onap/aaf/certservice/client/DummyExitableException.java (limited to 'certServiceClient/src/test/java/org/onap/aaf/certservice/client') diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java index 9e733017..7e7bf5db 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java @@ -25,8 +25,8 @@ import org.mockito.junit.jupiter.MockitoExtension; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.verify; -import static org.onap.aaf.certservice.client.api.ExitCode.CLIENT_CONFIGURATION_EXCEPTION; -import static org.onap.aaf.certservice.client.api.ExitCode.SUCCESS_EXIT_CODE; +import static org.onap.aaf.certservice.client.api.ExitStatus.CLIENT_CONFIGURATION_EXCEPTION; +import static org.onap.aaf.certservice.client.api.ExitStatus.SUCCESS; @ExtendWith(MockitoExtension.class) class CertServiceClientTest { @@ -35,13 +35,13 @@ class CertServiceClientTest { @Test public void shouldExitWithDefinedExitCode_onRunCallWhenNoEnvsPresent() { // given - doNothing().when(appExitHandler).exit(CLIENT_CONFIGURATION_EXCEPTION.getValue()); - doNothing().when(appExitHandler).exit(SUCCESS_EXIT_CODE.getValue()); + doNothing().when(appExitHandler).exit(CLIENT_CONFIGURATION_EXCEPTION); + doNothing().when(appExitHandler).exit(SUCCESS); CertServiceClient certServiceClient = new CertServiceClient(appExitHandler); // when certServiceClient.run(); // then - verify(appExitHandler).exit(CLIENT_CONFIGURATION_EXCEPTION.getValue()); - verify(appExitHandler).exit(SUCCESS_EXIT_CODE.getValue()); + verify(appExitHandler).exit(CLIENT_CONFIGURATION_EXCEPTION); + verify(appExitHandler).exit(SUCCESS); } -} \ No newline at end of file +} diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/DummyExitableException.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/DummyExitableException.java deleted file mode 100644 index 80a2f723..00000000 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/DummyExitableException.java +++ /dev/null @@ -1,35 +0,0 @@ -/*============LICENSE_START======================================================= - * aaf-certservice-client - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.aaf.certservice.client; - -import org.onap.aaf.certservice.client.api.ExitableException; - -class DummyExitableException extends ExitableException { - private static final int EXIT_CODE = 888; - - DummyExitableException() { - super("This is Test Exitable Exception"); - } - - @Override - public int applicationExitCode() { - return EXIT_CODE; - } - -} diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java index bb566e81..6ab1bffc 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java @@ -23,19 +23,18 @@ package org.onap.aaf.certservice.client.configuration.model; import org.assertj.core.api.Condition; 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.configuration.CsrConfigurationEnvs; import org.onap.aaf.certservice.client.configuration.EnvsForCsr; import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException; import org.onap.aaf.certservice.client.configuration.factory.CsrConfigurationFactory; import java.util.Optional; -import java.util.function.Predicate; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.onap.aaf.certservice.client.api.ExitStatus.CSR_CONFIGURATION_EXCEPTION; public class CsrConfigurationFactoryTest { @@ -55,7 +54,7 @@ public class CsrConfigurationFactoryTest { private Condition expectedExitCodeCondition = new Condition<>("Correct exit code"){ @Override public boolean matches(CsrConfigurationException e) { - return e.applicationExitCode() == ExitCode.CSR_CONFIGURATION_EXCEPTION.getValue(); + return e.applicationExitStatus() == CSR_CONFIGURATION_EXCEPTION; } }; 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 461b7a34..2a539414 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 @@ -27,7 +27,6 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; 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; @@ -38,9 +37,8 @@ import java.util.List; import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; import static java.net.HttpURLConnection.HTTP_OK; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -89,60 +87,49 @@ class HttpClientTest { List trustedCertificate = certServiceResponse.getTrustedCertificates(); // then - assertNotNull(certServiceResponse); + assertThat(certServiceResponse).isNotNull(); final int expectedTwoElements = 2; - assertEquals(expectedTwoElements, certificateChain.size()); - assertEquals(expectedTwoElements, trustedCertificate.size()); - assertEquals(EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN, certificateChain.get(0)); - assertEquals(EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES, trustedCertificate.get(0)); + assertThat(certificateChain).hasSize(expectedTwoElements); + assertThat(trustedCertificate).hasSize(expectedTwoElements); + + assertThat(certificateChain.get(0)).isEqualTo(EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN); + assertThat(trustedCertificate.get(0)).isEqualTo(EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES); } @Test void shouldThrowCertServiceApiResponseException_WhenPkHeaderIsMissing() throws Exception { - // given + //given mockServerResponse(HTTP_BAD_REQUEST, MISSING_PK_RESPONSE); - // when - CertServiceApiResponseException exception = - assertThrows(CertServiceApiResponseException.class, - () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, "")); - - // then - assertEquals(ExitCode.CERT_SERVICE_API_CONNECTION_EXCEPTION.getValue(), exception.applicationExitCode()); + //when //then + assertThatExceptionOfType(CertServiceApiResponseException.class) + .isThrownBy(()->httpClient.retrieveCertServiceData(CA_NAME, CSR, "")); } @Test - void shouldThrowHttpClientException_WhenCannotExecuteRequestToAPI() throws Exception{ + 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()); + //when //then + assertThatExceptionOfType(HttpClientException.class) + .isThrownBy(()->httpClient.retrieveCertServiceData(CA_NAME, CSR, "")); } @Test - void shouldThrowHttpClientException_WhenCannotParseResponseToString() throws Exception{ + 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()); + //when //then + assertThatExceptionOfType(HttpClientException.class) + .isThrownBy(()->httpClient.retrieveCertServiceData(CA_NAME, CSR, "")); } private void mockServerResponse(int serverCodeResponse, String stringResponse) -- cgit 1.2.3-korg