2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.CertificationModelFactory;
36 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
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";
56 private CertificationController certificationController;
59 private CertificationModelFactory certificationModelFactory;
63 certificationController = new CertificationController(certificationModelFactory);
67 void shouldReturnDataAboutCsrBaseOnEncodedParameters()
68 throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
70 CertificationModel testCertificationModel = new CertificationModel(
71 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
72 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
74 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
75 .thenReturn(testCertificationModel);
78 ResponseEntity<CertificationModel> responseCertificationModel =
79 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
82 assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
83 assertThat(responseCertificationModel.getBody()
84 ).isEqualToComparingFieldByField(testCertificationModel);
89 void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
90 throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
92 String expectedMessage = "Incorrect CSR, decryption failed";
93 when(certificationModelFactory.createCertificationModel(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
94 .thenThrow(new CsrDecryptionException(expectedMessage));
97 Exception exception = assertThrows(
98 CsrDecryptionException.class, () ->
99 certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
102 String actualMessage = exception.getMessage();
105 assertEquals(expectedMessage, actualMessage);
109 void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
110 throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
112 String expectedMessage = "Incorrect PEM, decryption failed";
113 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
114 .thenThrow(new KeyDecryptionException(expectedMessage));
117 Exception exception = assertThrows(
118 KeyDecryptionException.class, () ->
119 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
122 String actualMessage = exception.getMessage();
125 assertEquals(expectedMessage, actualMessage);
129 void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
130 throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
132 String expectedMessage = "No server found for given CA name";
133 when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
134 .thenThrow(new Cmpv2ServerNotFoundException(expectedMessage));
137 Exception exception = assertThrows(
138 Cmpv2ServerNotFoundException.class, () ->
139 certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
142 String actualMessage = exception.getMessage();
145 assertEquals(expectedMessage, actualMessage);