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.model.CertificateUpdateModel;
36 import org.onap.oom.certservice.certification.CertificationModelFactory;
37 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
38 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
39 import org.onap.oom.certservice.certification.exception.DecryptionException;
40 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
41 import org.onap.oom.certservice.certification.model.CertificationModel;
42 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
46 @ExtendWith(MockitoExtension.class)
47 class CertificationControllerTest {
49 private static final String TEST_CA_NAME = "TestCa";
50 private static final String TEST_ENCODED_CSR = "encodedCSR";
51 private static final String TEST_ENCODED_PK = "encodedPK";
52 private static final String TEST_WRONG_ENCODED_CSR = "wrongEncodedCSR";
53 private static final String TEST_WRONG_ENCODED_PK = "wrongEncodedPK";
54 private static final String TEST_WRONG_CA_NAME = "wrongTestCa";
55 private static final String TEST_ENCODED_OLD_PK = "encodedOldPK";
56 private static final String TEST_ENCODED_OLD_CERT = "encodedOldCert";
58 private CertificationController certificationController;
61 private CertificationModelFactory certificationModelFactory;
65 certificationController = new CertificationController(certificationModelFactory);
69 void shouldReturnDataAboutCsrBaseOnEncodedParameters()
70 throws DecryptionException, CmpClientException {
72 CertificationModel testCertificationModel = new CertificationModel(
73 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
74 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
76 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
77 .thenReturn(testCertificationModel);
80 ResponseEntity<CertificationModel> responseCertificationModel =
81 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
84 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
85 assertThat(responseCertificationModel.getBody()
86 ).isEqualToComparingFieldByField(testCertificationModel);
91 void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
92 throws DecryptionException, CmpClientException {
94 String expectedMessage = "Incorrect CSR, decryption failed";
95 when(certificationModelFactory.createCertificationModel(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
96 .thenThrow(new CsrDecryptionException(expectedMessage));
99 Exception exception = assertThrows(
100 CsrDecryptionException.class, () ->
101 certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
104 String actualMessage = exception.getMessage();
107 assertEquals(expectedMessage, actualMessage);
111 void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
112 throws DecryptionException, CmpClientException {
114 String expectedMessage = "Incorrect PEM, decryption failed";
115 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
116 .thenThrow(new KeyDecryptionException(expectedMessage));
119 Exception exception = assertThrows(
120 KeyDecryptionException.class, () ->
121 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
124 String actualMessage = exception.getMessage();
127 assertEquals(expectedMessage, actualMessage);
131 void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
132 throws DecryptionException, CmpClientException {
134 String expectedMessage = "No server found for given CA name";
135 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
136 .thenThrow(new Cmpv2ServerNotFoundException(expectedMessage));
139 Exception exception = assertThrows(
140 Cmpv2ServerNotFoundException.class, () ->
141 certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
144 String actualMessage = exception.getMessage();
147 assertEquals(expectedMessage, actualMessage);
151 void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters() {
153 CertificationModel testCertificationModel = new CertificationModel(
154 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
155 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
157 CertificateUpdateModel certificateUpdateModel = new CertificateUpdateModel.CertificateUpdateModelBuilder()
158 .setEncodedCsr(TEST_ENCODED_CSR)
159 .setEncodedPrivateKey(TEST_ENCODED_PK)
160 .setEncodedOldCert(TEST_ENCODED_OLD_CERT)
161 .setEncodedOldPrivateKey(TEST_ENCODED_OLD_PK)
162 .setCaName(TEST_CA_NAME)
164 when(certificationModelFactory.createCertificationModel(certificateUpdateModel)).thenReturn(testCertificationModel);
167 ResponseEntity<CertificationModel> responseCertificationModel =
168 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
169 TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK);
172 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
173 assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationModel);