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.oom.certservice.api.advice;
23 import com.google.gson.Gson;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
27 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
28 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
29 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
30 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
31 import org.onap.oom.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 CertificationExceptionAdviceTest {
40 private CertificationExceptionAdvice certificationExceptionAdvice;
44 certificationExceptionAdvice =
45 new CertificationExceptionAdvice();
49 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
51 String expectedMessage = "Wrong certificate signing request (CSR) format";
52 CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
55 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
58 assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
59 assertEquals(expectedMessage, response.getBody().getErrorMessage());
63 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
65 String expectedMessage = "Wrong key (PK) format";
66 KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
69 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
72 assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
73 assertEquals(expectedMessage, response.getBody().getErrorMessage());
77 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
79 String expectedMessage = "Certification authority not found for given CAName";
80 Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
83 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
86 assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
87 assertEquals(expectedMessage, response.getBody().getErrorMessage());
91 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
93 String expectedMessage = "Exception occurred during call to cmp client";
94 CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
97 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpClientException);
100 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
101 assertEquals(expectedMessage, response.getBody().getErrorMessage());
105 void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
107 String expectedMessage = "Exception occurred parsing cmp client response";
108 Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
111 ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpv2ClientAdapterException);
114 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
115 assertEquals(expectedMessage, response.getBody().getErrorMessage());
119 void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
121 String expectedMessage = "Runtime exception occurred calling cmp client business logic";
122 RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
125 Exception exception = assertThrows(
126 CmpClientException.class, () ->
127 certificationExceptionAdvice.handle(runtimeException)
131 assertEquals(expectedMessage, exception.getMessage());