9e6a6ced486f52ccd1a2f5c9c36db74c37b41939
[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.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;
39
40 class CertificationExceptionAdviceTest {
41
42     private CertificationExceptionAdvice certificationExceptionAdvice;
43
44     @BeforeEach
45     void setUp() {
46         certificationExceptionAdvice =
47                 new CertificationExceptionAdvice();
48     }
49
50     @Test
51     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
52         // Given
53         String expectedMessage = "Wrong certificate signing request (CSR) format";
54         CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
55
56         // When
57         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
58
59         // Then
60         assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
61         assertEquals(expectedMessage, response.getBody().getErrorMessage());
62     }
63
64     @Test
65     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
66         // Given
67         String expectedMessage = "Wrong key (PK) format";
68         KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
69
70         // When
71         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
72
73         // Then
74         assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
75         assertEquals(expectedMessage, response.getBody().getErrorMessage());
76     }
77
78     @Test
79     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
80         // Given
81         String expectedMessage = "Certification authority not found for given CAName";
82         Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
83
84         // When
85         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
86
87         // Then
88         assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
89         assertEquals(expectedMessage, response.getBody().getErrorMessage());
90     }
91
92     @Test
93     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
94         // Given
95         String expectedMessage = "Exception occurred during call to cmp client";
96         CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
97
98         // When
99         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpClientException);
100
101         // Then
102         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
103         assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
104     }
105
106     @Test
107     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
108         // Given
109         String expectedMessage = "Exception occurred parsing cmp client response";
110         Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
111
112         // When
113         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpv2ClientAdapterException);
114
115         // Then
116         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
117         assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
118     }
119
120     @Test
121     void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
122         // Given
123         String expectedMessage = "Runtime exception occurred calling cmp client business logic";
124         RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
125
126         // When
127         Exception exception = assertThrows(
128                 CmpClientException.class, () ->
129                         certificationExceptionAdvice.handle(runtimeException)
130         );
131
132         // Then
133         assertEquals(expectedMessage, exception.getMessage());
134     }
135
136     @Test
137     void shouldReturnResponseEntityWithCmpErrorMessage() {
138         // Given
139         String expectedMessage = "CMPv2 server returned following error: EJBCA fault";
140         CmpServerException exception = new CmpServerException("EJBCA fault");
141
142         // When
143         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(exception);
144
145         // Then
146         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
147         assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
148     }
149 }