2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * Copyright (C) 2021 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.oom.certservice.api.advice;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
31 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
32 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
33 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
34 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
35 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
36 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
37 import org.onap.oom.certservice.cmpv2client.exceptions.CmpServerException;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
41 class CertificationExceptionAdviceTest {
43 private CertificationExceptionAdvice certificationExceptionAdvice;
47 certificationExceptionAdvice =
48 new CertificationExceptionAdvice();
52 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
54 String expectedMessage = "Wrong certificate signing request (CSR) format";
55 CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
58 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
61 assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
62 assertEquals(expectedMessage, response.getBody().getErrorMessage());
66 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
68 String expectedMessage = "Wrong key (PK) format";
69 KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
72 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
75 assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
76 assertEquals(expectedMessage, response.getBody().getErrorMessage());
80 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
82 String expectedMessage = "Certification authority not found for given CAName";
83 Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
86 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
89 assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
90 assertEquals(expectedMessage, response.getBody().getErrorMessage());
94 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
96 String expectedMessage = "Exception occurred during call to cmp client";
97 CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
100 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpClientException);
103 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
104 assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
108 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
110 String expectedMessage = "Exception occurred parsing cmp client response";
111 Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
114 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpv2ClientAdapterException);
117 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
118 assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
122 void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
124 String expectedMessage = "Runtime exception occurred calling cmp client business logic";
125 RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
128 Exception exception = assertThrows(
129 CmpClientException.class, () ->
130 certificationExceptionAdvice.handle(runtimeException)
134 assertEquals(expectedMessage, exception.getMessage());
138 void shouldReturnResponseEntityWithCmpErrorMessage() {
140 String expectedMessage = "CMPv2 server returned following error: EJBCA fault";
141 CmpServerException exception = new CmpServerException("EJBCA fault");
144 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(exception);
147 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
148 assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
152 void shouldReturnResponseEntityWithCertificateDecryptionMessage() {
154 String expectedMessage = "Wrong certificate format";
155 CertificateDecryptionException exception = new CertificateDecryptionException("Incorrect certificate, decryption failed");
158 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(exception);
161 assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
162 assertEquals(expectedMessage, response.getBody().getErrorMessage());