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.Cmpv2ClientAdapterException;
31 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
32 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
33 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
34 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
35 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
36 import org.onap.oom.certservice.cmpv2client.exceptions.CmpServerException;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
40 class CertificationExceptionAdviceTest {
42 private CertificationExceptionAdvice certificationExceptionAdvice;
46 certificationExceptionAdvice =
47 new CertificationExceptionAdvice();
51 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
53 String expectedMessage = "Wrong certificate signing request (CSR) format";
54 CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
57 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
60 assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
61 assertEquals(expectedMessage, response.getBody().getErrorMessage());
65 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
67 String expectedMessage = "Wrong key (PK) format";
68 KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
71 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
74 assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
75 assertEquals(expectedMessage, response.getBody().getErrorMessage());
79 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
81 String expectedMessage = "Certification authority not found for given CAName";
82 Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
85 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
88 assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
89 assertEquals(expectedMessage, response.getBody().getErrorMessage());
93 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
95 String expectedMessage = "Exception occurred during call to cmp client";
96 CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
99 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpClientException);
102 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
103 assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
107 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
109 String expectedMessage = "Exception occurred parsing cmp client response";
110 Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
113 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpv2ClientAdapterException);
116 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
117 assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
121 void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
123 String expectedMessage = "Runtime exception occurred calling cmp client business logic";
124 RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
127 Exception exception = assertThrows(
128 CmpClientException.class, () ->
129 certificationExceptionAdvice.handle(runtimeException)
133 assertEquals(expectedMessage, exception.getMessage());
137 void shouldReturnResponseEntityWithCmpErrorMessage() {
139 String expectedMessage = "CMPv2 server returned following error: EJBCA fault";
140 CmpServerException exception = new CmpServerException("EJBCA fault");
143 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(exception);
146 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
147 assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));