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.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;
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 CertificationResponseModelFactory certificationResponseModelFactory;
74 certificationController = new CertificationController(certificationResponseModelFactory);
78 void shouldReturnDataAboutCsrBaseOnEncodedParameters()
79 throws DecryptionException, CmpClientException {
81 CertificationResponseModel testCertificationResponseModel = new CertificationResponseModel(
82 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
83 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
85 when(certificationResponseModelFactory
86 .provideCertificationModelFromInitialRequest(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
87 .thenReturn(testCertificationResponseModel);
90 ResponseEntity<CertificationResponseModel> 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(testCertificationResponseModel);
101 void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
102 throws DecryptionException, CmpClientException {
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));
110 Exception exception = assertThrows(
111 CsrDecryptionException.class, () ->
112 certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
115 String actualMessage = exception.getMessage();
118 assertEquals(expectedMessage, actualMessage);
122 void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
123 throws DecryptionException, CmpClientException {
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));
131 Exception exception = assertThrows(
132 KeyDecryptionException.class, () ->
133 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
136 String actualMessage = exception.getMessage();
139 assertEquals(expectedMessage, actualMessage);
143 void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
144 throws DecryptionException, CmpClientException {
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));
152 Exception exception = assertThrows(
153 Cmpv2ServerNotFoundException.class, () ->
154 certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
157 String actualMessage = exception.getMessage();
160 assertEquals(expectedMessage, actualMessage);
164 void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
165 throws DecryptionException, CmpClientException, CertificateDecryptionException {
167 CertificationResponseModel testCertificationResponseModel = new CertificationResponseModel(
168 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
169 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
171 when(certificationResponseModelFactory.provideCertificationModelFromUpdateRequest(TEST_CERTIFICATE_UPDATE_MODEL)).thenReturn(
172 testCertificationResponseModel);
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);
180 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
181 assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationResponseModel);
185 void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
186 throws DecryptionException, CertificateDecryptionException, CmpClientException {
188 String expectedMessage = "Incorrect certificate, decryption failed";
189 when(certificationResponseModelFactory.provideCertificationModelFromUpdateRequest(TEST_CERTIFICATE_UPDATE_MODEL))
190 .thenThrow(new CertificateDecryptionException(expectedMessage));
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)
199 String actualMessage = exception.getMessage();
202 assertEquals(expectedMessage, actualMessage);