4ac0b50d13b1bb7b78f17c0e5d1a638ecb54f3b4
[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.CertificationModelFactory;
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.CertificationModel;
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 CertificationModelFactory certificationModelFactory;
71
72     @BeforeEach
73     void serUp() {
74         certificationController = new CertificationController(certificationModelFactory);
75     }
76
77     @Test
78     void shouldReturnDataAboutCsrBaseOnEncodedParameters()
79             throws DecryptionException, CmpClientException {
80         // Given
81         CertificationModel testCertificationModel = new CertificationModel(
82                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
83                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
84         );
85         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
86                 .thenReturn(testCertificationModel);
87
88         // When
89         ResponseEntity<CertificationModel> responseCertificationModel =
90                 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
91
92         // Then
93         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
94         assertThat(responseCertificationModel.getBody()
95         ).isEqualToComparingFieldByField(testCertificationModel);
96
97     }
98
99     @Test
100     void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
101             throws DecryptionException, CmpClientException {
102         // Given
103         String expectedMessage = "Incorrect CSR, decryption failed";
104         when(certificationModelFactory.createCertificationModel(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
105                 .thenThrow(new CsrDecryptionException(expectedMessage));
106
107         // When
108         Exception exception = assertThrows(
109                 CsrDecryptionException.class, () ->
110                         certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
111         );
112
113         String actualMessage = exception.getMessage();
114
115         // Then
116         assertEquals(expectedMessage, actualMessage);
117     }
118
119     @Test
120     void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
121             throws DecryptionException, CmpClientException {
122         // Given
123         String expectedMessage = "Incorrect PEM, decryption failed";
124         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
125                 .thenThrow(new KeyDecryptionException(expectedMessage));
126
127         // When
128         Exception exception = assertThrows(
129                 KeyDecryptionException.class, () ->
130                         certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
131         );
132
133         String actualMessage = exception.getMessage();
134
135         // Then
136         assertEquals(expectedMessage, actualMessage);
137     }
138
139     @Test
140     void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
141             throws DecryptionException, CmpClientException {
142         // Given
143         String expectedMessage = "No server found for given CA name";
144         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
145                 .thenThrow(new Cmpv2ServerNotFoundException(expectedMessage));
146
147         // When
148         Exception exception = assertThrows(
149                 Cmpv2ServerNotFoundException.class, () ->
150                         certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
151         );
152
153         String actualMessage = exception.getMessage();
154
155         // Then
156         assertEquals(expectedMessage, actualMessage);
157     }
158
159     @Test
160     void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
161         throws DecryptionException, CmpClientException, CertificateDecryptionException {
162         // Given
163         CertificationModel testCertificationModel = new CertificationModel(
164                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
165                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
166         );
167         when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL)).thenReturn(testCertificationModel);
168
169         // When
170         ResponseEntity<CertificationModel> responseCertificationModel =
171                 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
172                         TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK);
173
174         // Then
175         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
176         assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationModel);
177     }
178
179     @Test
180     void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
181         throws DecryptionException, CertificateDecryptionException, CmpClientException {
182         // Given
183         String expectedMessage = "Incorrect certificate, decryption failed";
184         when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL))
185             .thenThrow(new CertificateDecryptionException(expectedMessage));
186
187         // When
188         Exception exception = assertThrows(
189             CertificateDecryptionException.class, () ->
190                 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
191                     TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK)
192         );
193
194         String actualMessage = exception.getMessage();
195
196         // Then
197         assertEquals(expectedMessage, actualMessage);
198     }
199
200 }