2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.oom.certservice.api;
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;
28 import java.util.Arrays;
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;
49 @ExtendWith(MockitoExtension.class)
50 class CertificationControllerTest {
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)
68 private CertificationController certificationController;
71 private CertificationModelFactory certificationModelFactory;
75 certificationController = new CertificationController(certificationModelFactory);
79 void shouldReturnDataAboutCsrBaseOnEncodedParameters()
80 throws DecryptionException, CmpClientException {
82 CertificationModel testCertificationModel = new CertificationModel(
83 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
84 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
86 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
87 .thenReturn(testCertificationModel);
90 ResponseEntity<CertificationModel> responseCertificationModel =
91 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
94 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
95 assertThat(responseCertificationModel.getBody()
96 ).isEqualToComparingFieldByField(testCertificationModel);
101 void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
102 throws DecryptionException, CmpClientException {
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));
109 Exception exception = assertThrows(
110 CsrDecryptionException.class, () ->
111 certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
114 String actualMessage = exception.getMessage();
117 assertEquals(expectedMessage, actualMessage);
121 void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
122 throws DecryptionException, CmpClientException {
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));
129 Exception exception = assertThrows(
130 KeyDecryptionException.class, () ->
131 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
134 String actualMessage = exception.getMessage();
137 assertEquals(expectedMessage, actualMessage);
141 void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
142 throws DecryptionException, CmpClientException {
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));
149 Exception exception = assertThrows(
150 Cmpv2ServerNotFoundException.class, () ->
151 certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
154 String actualMessage = exception.getMessage();
157 assertEquals(expectedMessage, actualMessage);
161 void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
162 throws DecryptionException, CertificateDecryptionException {
164 CertificationModel testCertificationModel = new CertificationModel(
165 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
166 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
168 when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL)).thenReturn(testCertificationModel);
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);
176 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
177 assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationModel);
181 void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
182 throws DecryptionException, CertificateDecryptionException {
184 String expectedMessage = "Incorrect certificate, decryption failed";
185 when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL))
186 .thenThrow(new CertificateDecryptionException(expectedMessage));
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)
195 String actualMessage = exception.getMessage();
198 assertEquals(expectedMessage, actualMessage);