[OOM-CERT-SERVICE] Code refactor
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / api / CertificationControllerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.oom.certservice.api;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Arrays;
29
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
36 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
37 import org.onap.oom.certservice.certification.CertificationResponseModelFactory;
38 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
39 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
40 import org.onap.oom.certservice.certification.exception.DecryptionException;
41 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
42 import org.onap.oom.certservice.certification.model.CertificateUpdateModel.CertificateUpdateModelBuilder;
43 import org.onap.oom.certservice.certification.model.CertificationResponseModel;
44 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47
48 @ExtendWith(MockitoExtension.class)
49 class CertificationControllerTest {
50
51     private static final String TEST_CA_NAME = "TestCa";
52     private static final String TEST_ENCODED_CSR = "encodedCSR";
53     private static final String TEST_ENCODED_PK = "encodedPK";
54     private static final String TEST_WRONG_ENCODED_CSR = "wrongEncodedCSR";
55     private static final String TEST_WRONG_ENCODED_PK = "wrongEncodedPK";
56     private static final String TEST_WRONG_CA_NAME = "wrongTestCa";
57     private static final String TEST_ENCODED_OLD_PK = "encodedOldPK";
58     private static final String TEST_ENCODED_OLD_CERT = "encodedOldCert";
59     private static final CertificateUpdateModel TEST_CERTIFICATE_UPDATE_MODEL = new CertificateUpdateModelBuilder()
60         .setEncodedCsr(TEST_ENCODED_CSR)
61         .setEncodedPrivateKey(TEST_ENCODED_PK)
62         .setEncodedOldCert(TEST_ENCODED_OLD_CERT)
63         .setEncodedOldPrivateKey(TEST_ENCODED_OLD_PK)
64         .setCaName(TEST_CA_NAME)
65         .build();
66
67     private CertificationController certificationController;
68
69     @Mock
70     private CertificationResponseModelFactory certificationResponseModelFactory;
71
72     @BeforeEach
73     void serUp() {
74         certificationController = new CertificationController(certificationResponseModelFactory);
75     }
76
77     @Test
78     void shouldReturnDataAboutCsrBaseOnEncodedParameters()
79             throws DecryptionException, CmpClientException {
80         // Given
81         CertificationResponseModel testCertificationResponseModel = new CertificationResponseModel(
82                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
83                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
84         );
85         when(certificationResponseModelFactory
86             .provideCertificationModelFromInitialRequest(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
87                 .thenReturn(testCertificationResponseModel);
88
89         // When
90         ResponseEntity<CertificationResponseModel> responseCertificationModel =
91                 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
92
93         // Then
94         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
95         assertThat(responseCertificationModel.getBody()
96         ).isEqualToComparingFieldByField(testCertificationResponseModel);
97
98     }
99
100     @Test
101     void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
102             throws DecryptionException, CmpClientException {
103         // Given
104         String expectedMessage = "Incorrect CSR, decryption failed";
105         when(certificationResponseModelFactory
106             .provideCertificationModelFromInitialRequest(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
107                 .thenThrow(new CsrDecryptionException(expectedMessage));
108
109         // When
110         Exception exception = assertThrows(
111                 CsrDecryptionException.class, () ->
112                         certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
113         );
114
115         String actualMessage = exception.getMessage();
116
117         // Then
118         assertEquals(expectedMessage, actualMessage);
119     }
120
121     @Test
122     void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
123             throws DecryptionException, CmpClientException {
124         // Given
125         String expectedMessage = "Incorrect PEM, decryption failed";
126         when(certificationResponseModelFactory
127             .provideCertificationModelFromInitialRequest(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
128                 .thenThrow(new KeyDecryptionException(expectedMessage));
129
130         // When
131         Exception exception = assertThrows(
132                 KeyDecryptionException.class, () ->
133                         certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
134         );
135
136         String actualMessage = exception.getMessage();
137
138         // Then
139         assertEquals(expectedMessage, actualMessage);
140     }
141
142     @Test
143     void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
144             throws DecryptionException, CmpClientException {
145         // Given
146         String expectedMessage = "No server found for given CA name";
147         when(certificationResponseModelFactory
148             .provideCertificationModelFromInitialRequest(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
149                 .thenThrow(new Cmpv2ServerNotFoundException(expectedMessage));
150
151         // When
152         Exception exception = assertThrows(
153                 Cmpv2ServerNotFoundException.class, () ->
154                         certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
155         );
156
157         String actualMessage = exception.getMessage();
158
159         // Then
160         assertEquals(expectedMessage, actualMessage);
161     }
162
163     @Test
164     void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
165         throws DecryptionException, CmpClientException, CertificateDecryptionException {
166         // Given
167         CertificationResponseModel testCertificationResponseModel = new CertificationResponseModel(
168                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
169                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
170         );
171         when(certificationResponseModelFactory.provideCertificationModelFromUpdateRequest(TEST_CERTIFICATE_UPDATE_MODEL)).thenReturn(
172             testCertificationResponseModel);
173
174         // When
175         ResponseEntity<CertificationResponseModel> responseCertificationModel =
176                 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
177                         TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK);
178
179         // Then
180         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
181         assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationResponseModel);
182     }
183
184     @Test
185     void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
186         throws DecryptionException, CertificateDecryptionException, CmpClientException {
187         // Given
188         String expectedMessage = "Incorrect certificate, decryption failed";
189         when(certificationResponseModelFactory.provideCertificationModelFromUpdateRequest(TEST_CERTIFICATE_UPDATE_MODEL))
190             .thenThrow(new CertificateDecryptionException(expectedMessage));
191
192         // When
193         Exception exception = assertThrows(
194             CertificateDecryptionException.class, () ->
195                 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
196                     TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK)
197         );
198
199         String actualMessage = exception.getMessage();
200
201         // Then
202         assertEquals(expectedMessage, actualMessage);
203     }
204
205 }