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.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;
48 @ExtendWith(MockitoExtension.class)
49 class CertificationControllerTest {
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)
67 private CertificationController certificationController;
70 private CertificationModelFactory certificationModelFactory;
74 certificationController = new CertificationController(certificationModelFactory);
78 void shouldReturnDataAboutCsrBaseOnEncodedParameters()
79 throws DecryptionException, CmpClientException {
81 CertificationModel testCertificationModel = new CertificationModel(
82 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
83 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
85 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
86 .thenReturn(testCertificationModel);
89 ResponseEntity<CertificationModel> responseCertificationModel =
90 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
93 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
94 assertThat(responseCertificationModel.getBody()
95 ).isEqualToComparingFieldByField(testCertificationModel);
100 void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
101 throws DecryptionException, CmpClientException {
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));
108 Exception exception = assertThrows(
109 CsrDecryptionException.class, () ->
110 certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
113 String actualMessage = exception.getMessage();
116 assertEquals(expectedMessage, actualMessage);
120 void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
121 throws DecryptionException, CmpClientException {
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));
128 Exception exception = assertThrows(
129 KeyDecryptionException.class, () ->
130 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
133 String actualMessage = exception.getMessage();
136 assertEquals(expectedMessage, actualMessage);
140 void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
141 throws DecryptionException, CmpClientException {
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));
148 Exception exception = assertThrows(
149 Cmpv2ServerNotFoundException.class, () ->
150 certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
153 String actualMessage = exception.getMessage();
156 assertEquals(expectedMessage, actualMessage);
160 void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
161 throws DecryptionException, CmpClientException, CertificateDecryptionException {
163 CertificationModel testCertificationModel = new CertificationModel(
164 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
165 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
167 when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL)).thenReturn(testCertificationModel);
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);
175 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
176 assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationModel);
180 void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
181 throws DecryptionException, CertificateDecryptionException, CmpClientException {
183 String expectedMessage = "Incorrect certificate, decryption failed";
184 when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL))
185 .thenThrow(new CertificateDecryptionException(expectedMessage));
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)
194 String actualMessage = exception.getMessage();
197 assertEquals(expectedMessage, actualMessage);