aboutsummaryrefslogtreecommitdiffstats
path: root/certService/src/test/java/org/onap/aaf/certservice/api
diff options
context:
space:
mode:
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-03-10 16:05:09 +0100
committerBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-03-10 16:05:09 +0100
commit0855846bc0ab2dcdadc5ef7ed9588f70124ce21e (patch)
tree9f1110345ea2f5042a04e9e2129d7ae333359e1c /certService/src/test/java/org/onap/aaf/certservice/api
parent50ec01532a2c3816d964bf6b1b870968536a6fd7 (diff)
Add annotation for OpenAPI 3.0 documentation generation
Issue-ID: AAF-997 Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> Change-Id: I6dcbe2076d7e0095c210b1cae95309fe31ebf243
Diffstat (limited to 'certService/src/test/java/org/onap/aaf/certservice/api')
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java9
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java18
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdviceTest.java134
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdviceTest.java56
4 files changed, 196 insertions, 21 deletions
diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java
index 802b2ea0..8ebac689 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java
@@ -25,7 +25,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
-import com.google.gson.Gson;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -75,14 +74,12 @@ class CertificationControllerTest {
.thenReturn(testCertificationModel);
// When
- ResponseEntity<String> testResponse =
+ ResponseEntity<CertificationModel> responseCertificationModel =
certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
- CertificationModel responseCertificationModel = new Gson().fromJson(testResponse.getBody(), CertificationModel.class);
-
// Then
- assertEquals(HttpStatus.OK, testResponse.getStatusCode());
- assertThat(responseCertificationModel
+ assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
+ assertThat(responseCertificationModel.getBody()
).isEqualToComparingFieldByField(testCertificationModel);
}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java
index 17db24b8..5e0e355c 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java
@@ -21,7 +21,6 @@
package org.onap.aaf.certservice.api;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
-import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doThrow;
@@ -47,12 +46,12 @@ public class ReloadConfigControllerTest {
public CmpServersConfig cmpServersConfig;
@BeforeEach
- public void setUp() {
+ void setUp() {
this.reloadConfigController = new ReloadConfigController(cmpServersConfig);
}
@Test
- public void shouldReturnStatusOkWhenSuccessfullyReloaded() throws CmpServersConfigLoadingException {
+ void shouldReturnStatusOkWhenSuccessfullyReloaded() throws CmpServersConfigLoadingException {
// When
ResponseEntity<String> response = reloadConfigController.reloadConfiguration();
@@ -61,7 +60,7 @@ public class ReloadConfigControllerTest {
}
@Test
- public void shouldRethrowSameErrorWhenFailedToReload() throws CmpServersConfigLoadingException {
+ void shouldRethrowSameErrorWhenFailedToReload() throws CmpServersConfigLoadingException {
// Given
doThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE)).when(cmpServersConfig).reloadConfiguration();
@@ -74,16 +73,5 @@ public class ReloadConfigControllerTest {
Assertions.assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
}
- @Test
- void shouldReturnErrorStatusAndMessageWhenExceptionOccurred() {
- // Given
- CmpServersConfigLoadingException exception = new CmpServersConfigLoadingException(ERROR_MESSAGE);
-
- // When
- ResponseEntity<String> response = reloadConfigController.handle(exception);
- // Then
- assertEquals(ERROR_MESSAGE, response.getBody());
- assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
- }
}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdviceTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdviceTest.java
new file mode 100644
index 00000000..f3c4ec7f
--- /dev/null
+++ b/certService/src/test/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdviceTest.java
@@ -0,0 +1,134 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * 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.api.advice;
+
+import com.google.gson.Gson;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException;
+import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException;
+import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
+import org.onap.aaf.certservice.certification.exception.ErrorResponseModel;
+import org.onap.aaf.certservice.certification.exception.KeyDecryptionException;
+import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class CertificationExceptionAdviceTest {
+
+ private CertificationExceptionAdvice certificationExceptionAdvice;
+
+ @BeforeEach
+ void setUp() {
+ certificationExceptionAdvice =
+ new CertificationExceptionAdvice();
+ }
+
+ @Test
+ void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
+ // Given
+ String expectedMessage = "Wrong certificate signing request (CSR) format";
+ CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
+
+ // When
+ ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
+
+ // Then
+ assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
+ assertEquals(expectedMessage, response.getBody().getErrorMessage());
+ }
+
+ @Test
+ void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
+ // Given
+ String expectedMessage = "Wrong key (PK) format";
+ KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
+
+ // When
+ ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
+
+ // Then
+ assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
+ assertEquals(expectedMessage, response.getBody().getErrorMessage());
+ }
+
+ @Test
+ void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
+ // Given
+ String expectedMessage = "Certification authority not found for given CAName";
+ Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
+
+ // When
+ ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
+
+ // Then
+ assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
+ assertEquals(expectedMessage, response.getBody().getErrorMessage());
+ }
+
+ @Test
+ void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
+ // Given
+ String expectedMessage = "Exception occurred during call to cmp client";
+ CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
+
+ // When
+ ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpClientException);
+
+ // Then
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ assertEquals(expectedMessage, response.getBody().getErrorMessage());
+ }
+
+ @Test
+ void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
+ // Given
+ String expectedMessage = "Exception occurred parsing cmp client response";
+ Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
+
+ // When
+ ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpv2ClientAdapterException);
+
+ // Then
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ assertEquals(expectedMessage, response.getBody().getErrorMessage());
+ }
+
+ @Test
+ void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
+ // Given
+ String expectedMessage = "Runtime exception occurred calling cmp client business logic";
+ RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
+
+ // When
+ Exception exception = assertThrows(
+ CmpClientException.class, () ->
+ certificationExceptionAdvice.handle(runtimeException)
+ );
+
+ // Then
+ assertEquals(expectedMessage, exception.getMessage());
+ }
+
+}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdviceTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdviceTest.java
new file mode 100644
index 00000000..9abecec9
--- /dev/null
+++ b/certService/src/test/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdviceTest.java
@@ -0,0 +1,56 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * 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.api.advice;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.onap.aaf.certservice.certification.configuration.CmpServersConfigLoadingException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class ReloadConfigExceptionAdviceTest {
+
+ private static final String ERROR_MESSAGE = "Exception occurred during CMP Servers configuration loading";
+
+ private ReloadConfigExceptionAdvice reloadConfigExceptionAdvice;
+
+ @BeforeEach
+ void setUp() {
+ reloadConfigExceptionAdvice =
+ new ReloadConfigExceptionAdvice();
+ }
+
+ @Test
+ void shouldReturnErrorStatusAndMessageWhenExceptionOccurred() {
+ // Given
+ CmpServersConfigLoadingException exception = new CmpServersConfigLoadingException(ERROR_MESSAGE);
+
+ // When
+ ResponseEntity<String> response = reloadConfigExceptionAdvice.handle(exception);
+
+ // Then
+ assertEquals(ERROR_MESSAGE, response.getBody());
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ }
+
+}