[OOM-CERT-SERVICE] Add CertificateDecriptionException handler
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / api / advice / CertificationExceptionAdviceTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.oom.certservice.api.advice;
23
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;
27
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;
40
41 class CertificationExceptionAdviceTest {
42
43     private CertificationExceptionAdvice certificationExceptionAdvice;
44
45     @BeforeEach
46     void setUp() {
47         certificationExceptionAdvice =
48                 new CertificationExceptionAdvice();
49     }
50
51     @Test
52     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
53         // Given
54         String expectedMessage = "Wrong certificate signing request (CSR) format";
55         CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
56
57         // When
58         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
59
60         // Then
61         assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
62         assertEquals(expectedMessage, response.getBody().getErrorMessage());
63     }
64
65     @Test
66     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
67         // Given
68         String expectedMessage = "Wrong key (PK) format";
69         KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
70
71         // When
72         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
73
74         // Then
75         assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
76         assertEquals(expectedMessage, response.getBody().getErrorMessage());
77     }
78
79     @Test
80     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
81         // Given
82         String expectedMessage = "Certification authority not found for given CAName";
83         Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
84
85         // When
86         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
87
88         // Then
89         assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
90         assertEquals(expectedMessage, response.getBody().getErrorMessage());
91     }
92
93     @Test
94     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
95         // Given
96         String expectedMessage = "Exception occurred during call to cmp client";
97         CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
98
99         // When
100         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpClientException);
101
102         // Then
103         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
104         assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
105     }
106
107     @Test
108     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
109         // Given
110         String expectedMessage = "Exception occurred parsing cmp client response";
111         Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
112
113         // When
114         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpv2ClientAdapterException);
115
116         // Then
117         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
118         assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
119     }
120
121     @Test
122     void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
123         // Given
124         String expectedMessage = "Runtime exception occurred calling cmp client business logic";
125         RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
126
127         // When
128         Exception exception = assertThrows(
129                 CmpClientException.class, () ->
130                         certificationExceptionAdvice.handle(runtimeException)
131         );
132
133         // Then
134         assertEquals(expectedMessage, exception.getMessage());
135     }
136
137     @Test
138     void shouldReturnResponseEntityWithCmpErrorMessage() {
139         // Given
140         String expectedMessage = "CMPv2 server returned following error: EJBCA fault";
141         CmpServerException exception = new CmpServerException("EJBCA fault");
142
143         // When
144         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(exception);
145
146         // Then
147         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
148         assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
149     }
150
151     @Test
152     void shouldReturnResponseEntityWithCertificateDecryptionMessage() {
153         // Given
154         String expectedMessage = "Wrong certificate format";
155         CertificateDecryptionException exception = new CertificateDecryptionException("Incorrect certificate, decryption failed");
156
157         // When
158         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(exception);
159
160         // Then
161         assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
162         assertEquals(expectedMessage, response.getBody().getErrorMessage());
163     }
164
165 }