[OOM-CERT-SERVICE] Add logic for KUR/CR detection
[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.exception.StringToCertificateConversionException;
37 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
38 import org.onap.oom.certservice.certification.CertificationModelFactory;
39 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
40 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
41 import org.onap.oom.certservice.certification.exception.DecryptionException;
42 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
43 import org.onap.oom.certservice.certification.model.CertificateUpdateModel.CertificateUpdateModelBuilder;
44 import org.onap.oom.certservice.certification.model.CertificationModel;
45 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48
49 @ExtendWith(MockitoExtension.class)
50 class CertificationControllerTest {
51
52     private static final String TEST_CA_NAME = "TestCa";
53     private static final String TEST_ENCODED_CSR = "encodedCSR";
54     private static final String TEST_ENCODED_PK = "encodedPK";
55     private static final String TEST_WRONG_ENCODED_CSR = "wrongEncodedCSR";
56     private static final String TEST_WRONG_ENCODED_PK = "wrongEncodedPK";
57     private static final String TEST_WRONG_CA_NAME = "wrongTestCa";
58     private static final String TEST_ENCODED_OLD_PK = "encodedOldPK";
59     private static final String TEST_ENCODED_OLD_CERT = "encodedOldCert";
60     private static final CertificateUpdateModel TEST_CERTIFICATE_UPDATE_MODEL = new CertificateUpdateModelBuilder()
61         .setEncodedCsr(TEST_ENCODED_CSR)
62         .setEncodedPrivateKey(TEST_ENCODED_PK)
63         .setEncodedOldCert(TEST_ENCODED_OLD_CERT)
64         .setEncodedOldPrivateKey(TEST_ENCODED_OLD_PK)
65         .setCaName(TEST_CA_NAME)
66         .build();
67
68     private CertificationController certificationController;
69
70     @Mock
71     private CertificationModelFactory certificationModelFactory;
72
73     @BeforeEach
74     void serUp() {
75         certificationController = new CertificationController(certificationModelFactory);
76     }
77
78     @Test
79     void shouldReturnDataAboutCsrBaseOnEncodedParameters()
80             throws DecryptionException, CmpClientException {
81         // Given
82         CertificationModel testCertificationModel = new CertificationModel(
83                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
84                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
85         );
86         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
87                 .thenReturn(testCertificationModel);
88
89         // When
90         ResponseEntity<CertificationModel> 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(testCertificationModel);
97
98     }
99
100     @Test
101     void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
102             throws DecryptionException, CmpClientException {
103         // Given
104         String expectedMessage = "Incorrect CSR, decryption failed";
105         when(certificationModelFactory.createCertificationModel(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
106                 .thenThrow(new CsrDecryptionException(expectedMessage));
107
108         // When
109         Exception exception = assertThrows(
110                 CsrDecryptionException.class, () ->
111                         certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
112         );
113
114         String actualMessage = exception.getMessage();
115
116         // Then
117         assertEquals(expectedMessage, actualMessage);
118     }
119
120     @Test
121     void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
122             throws DecryptionException, CmpClientException {
123         // Given
124         String expectedMessage = "Incorrect PEM, decryption failed";
125         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
126                 .thenThrow(new KeyDecryptionException(expectedMessage));
127
128         // When
129         Exception exception = assertThrows(
130                 KeyDecryptionException.class, () ->
131                         certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
132         );
133
134         String actualMessage = exception.getMessage();
135
136         // Then
137         assertEquals(expectedMessage, actualMessage);
138     }
139
140     @Test
141     void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
142             throws DecryptionException, CmpClientException {
143         // Given
144         String expectedMessage = "No server found for given CA name";
145         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
146                 .thenThrow(new Cmpv2ServerNotFoundException(expectedMessage));
147
148         // When
149         Exception exception = assertThrows(
150                 Cmpv2ServerNotFoundException.class, () ->
151                         certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
152         );
153
154         String actualMessage = exception.getMessage();
155
156         // Then
157         assertEquals(expectedMessage, actualMessage);
158     }
159
160     @Test
161     void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
162         throws DecryptionException, CertificateDecryptionException {
163         // Given
164         CertificationModel testCertificationModel = new CertificationModel(
165                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
166                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
167         );
168         when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL)).thenReturn(testCertificationModel);
169
170         // When
171         ResponseEntity<CertificationModel> responseCertificationModel =
172                 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
173                         TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK);
174
175         // Then
176         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
177         assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationModel);
178     }
179
180     @Test
181     void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
182         throws DecryptionException, CertificateDecryptionException {
183         // Given
184         String expectedMessage = "Incorrect certificate, decryption failed";
185         when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL))
186             .thenThrow(new CertificateDecryptionException(expectedMessage));
187
188         // When
189         Exception exception = assertThrows(
190             CertificateDecryptionException.class, () ->
191                 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
192                     TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK)
193         );
194
195         String actualMessage = exception.getMessage();
196
197         // Then
198         assertEquals(expectedMessage, actualMessage);
199     }
200
201 }