2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aaf.certservice.certification;
23 import com.google.gson.Gson;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException;
27 import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException;
28 import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
29 import org.onap.aaf.certservice.certification.exception.ErrorResponseModel;
30 import org.onap.aaf.certservice.certification.exception.KeyDecryptionException;
31 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
35 import static org.junit.jupiter.api.Assertions.assertEquals;
36 import static org.junit.jupiter.api.Assertions.assertThrows;
38 class CertificationExceptionControllerTest {
40 private CertificationExceptionController certificationExceptionController;
44 certificationExceptionController =
45 new CertificationExceptionController();
49 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
51 String expectedMessage = "Wrong certificate signing request (CSR) format";
52 CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
55 ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
57 ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
60 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
61 assertEquals(expectedMessage, response.getErrorMessage());
65 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
67 String expectedMessage = "Wrong key (PK) format";
68 KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
71 ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
73 ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
76 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
77 assertEquals(expectedMessage, response.getErrorMessage());
81 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
83 String expectedMessage = "Certification authority not found for given CAName";
84 Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
87 ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
89 ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
92 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
93 assertEquals(expectedMessage, response.getErrorMessage());
97 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
99 String expectedMessage = "Exception occurred during call to cmp client";
100 CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
103 ResponseEntity<String> responseEntity = certificationExceptionController.handle(cmpClientException);
105 ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
108 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
109 assertEquals(expectedMessage, response.getErrorMessage());
113 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
115 String expectedMessage = "Exception occurred parsing cmp client response";
116 Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
119 ResponseEntity<String> responseEntity = certificationExceptionController.handle(cmpv2ClientAdapterException);
121 ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
124 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
125 assertEquals(expectedMessage, response.getErrorMessage());
129 void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
131 String expectedMessage = "Runtime exception occurred calling cmp client business logic";
132 RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
135 Exception exception = assertThrows(
136 CmpClientException.class, () ->
137 certificationExceptionController.handle(runtimeException)
141 assertEquals(expectedMessage, exception.getMessage());