10a818e4e007608613aa664170dbaa596bd28c91
[oom/platform/cert-service.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aaf.certservice.certification;
22
23 import com.google.gson.Gson;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException;
27 import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException;
28 import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
29 import org.onap.aaf.certservice.certification.exception.ErrorResponseModel;
30 import org.onap.aaf.certservice.certification.exception.KeyDecryptionException;
31 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34
35 import static org.junit.jupiter.api.Assertions.assertEquals;
36 import static org.junit.jupiter.api.Assertions.assertThrows;
37
38 class CertificationExceptionControllerTest {
39
40     private CertificationExceptionController certificationExceptionController;
41
42     @BeforeEach
43     void setUp() {
44         certificationExceptionController =
45                 new CertificationExceptionController();
46     }
47
48     @Test
49     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
50         // Given
51         String expectedMessage = "Wrong certificate signing request (CSR) format";
52         CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
53
54         // When
55         ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
56
57         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
58
59         // Then
60         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
61         assertEquals(expectedMessage, response.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<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
72
73         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
74
75         // Then
76         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
77         assertEquals(expectedMessage, response.getErrorMessage());
78     }
79
80     @Test
81     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
82         // Given
83         String expectedMessage = "Certification authority not found for given CAName";
84         Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
85
86         // When
87         ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
88
89         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
90
91         // Then
92         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
93         assertEquals(expectedMessage, response.getErrorMessage());
94     }
95
96     @Test
97     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
98         // Given
99         String expectedMessage = "Exception occurred during call to cmp client";
100         CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
101
102         // When
103         ResponseEntity<String> responseEntity = certificationExceptionController.handle(cmpClientException);
104
105         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
106
107         // Then
108         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
109         assertEquals(expectedMessage, response.getErrorMessage());
110     }
111
112     @Test
113     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
114         // Given
115         String expectedMessage = "Exception occurred parsing cmp client response";
116         Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
117
118         // When
119         ResponseEntity<String> responseEntity = certificationExceptionController.handle(cmpv2ClientAdapterException);
120
121         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
122
123         // Then
124         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
125         assertEquals(expectedMessage, response.getErrorMessage());
126     }
127
128     @Test
129     void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
130         // Given
131         String expectedMessage = "Runtime exception occurred calling cmp client business logic";
132         RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
133
134         // When
135         Exception exception = assertThrows(
136                 CmpClientException.class, () ->
137                         certificationExceptionController.handle(runtimeException)
138         );
139
140         // Then
141         assertEquals(expectedMessage, exception.getMessage());
142     }
143
144 }