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.certification;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.onap.oom.certservice.certification.configuration.Cmpv2ServerProvider;
29 import org.onap.oom.certservice.certification.configuration.model.Cmpv2Server;
30 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
31 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
32 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
33 import org.onap.oom.certservice.certification.exception.DecryptionException;
34 import org.onap.oom.certservice.certification.model.CertificationModel;
35 import org.onap.oom.certservice.certification.model.CsrModel;
36 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
38 import java.util.Arrays;
39 import java.util.Base64;
40 import java.util.List;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.junit.jupiter.api.Assertions.assertEquals;
44 import static org.junit.jupiter.api.Assertions.assertThrows;
45 import static org.junit.jupiter.api.Assertions.assertTrue;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48 import static org.onap.oom.certservice.certification.CertificationData.CA_CERT;
49 import static org.onap.oom.certservice.certification.CertificationData.ENTITY_CERT;
50 import static org.onap.oom.certservice.certification.CertificationData.INTERMEDIATE_CERT;
51 import static org.onap.oom.certservice.certification.CertificationData.EXTRA_CA_CERT;
52 import static org.onap.oom.certservice.certification.TestData.TEST_CSR;
53 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
54 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_CSR;
55 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_PEM;
57 @ExtendWith(MockitoExtension.class)
58 class CertificationModelFactoryTest {
60 private static final String TEST_CA = "testCA";
61 private static final String ENCODED_CSR = getEncodedString(TEST_CSR);
62 private static final String ENCODED_PK = getEncodedString(TEST_PK);
63 private static final String ENCODED_WRONG_CSR = getEncodedString(TEST_WRONG_CSR);
64 private static final String ENCODED_WRONG_PK = getEncodedString(TEST_WRONG_PEM);
66 private CertificationModelFactory certificationModelFactory;
69 private Cmpv2ServerProvider cmpv2ServerProvider;
71 private CsrModelFactory csrModelFactory;
73 private CertificationProvider certificationProvider;
76 private static String getEncodedString(String testCsr) {
77 return Base64.getEncoder().encodeToString(testCsr.getBytes());
82 certificationModelFactory =
83 new CertificationModelFactory(csrModelFactory, cmpv2ServerProvider, certificationProvider);
87 void shouldCreateProperCertificationModelWhenGivenProperCsrModelAndCaName()
88 throws CmpClientException, DecryptionException, Cmpv2ClientAdapterException {
91 CsrModel csrModel = mockCsrFactoryModelCreation();
92 Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
93 mockCertificateProviderCertificateSigning(csrModel, testServer);
96 CertificationModel certificationModel =
97 certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA);
100 assertEquals(2, certificationModel.getCertificateChain().size());
101 assertThat(certificationModel.getCertificateChain()).contains(INTERMEDIATE_CERT, ENTITY_CERT);
102 assertEquals(2, certificationModel.getTrustedCertificates().size());
103 assertThat(certificationModel.getTrustedCertificates()).contains(CA_CERT, EXTRA_CA_CERT);
107 void shouldThrowDecryptionExceptionWhenGivenWrongEncodedCsr()
108 throws DecryptionException {
110 String expectedMessage = "Incorrect CSR, decryption failed";
112 csrModelFactory.createCsrModel(
113 new CsrModelFactory.StringBase64(ENCODED_WRONG_CSR),
114 new CsrModelFactory.StringBase64(ENCODED_WRONG_PK)
117 new CsrDecryptionException(expectedMessage)
121 Exception exception = assertThrows(
122 DecryptionException.class, () ->
123 certificationModelFactory.createCertificationModel(ENCODED_WRONG_CSR, ENCODED_WRONG_PK, TEST_CA)
127 assertTrue(exception.getMessage().contains(expectedMessage));
131 void shouldThrowCmpv2ServerNotFoundExceptionWhenGivenWrongCaName()
132 throws DecryptionException {
134 String expectedMessage = "CA not found";
135 mockCsrFactoryModelCreation();
137 cmpv2ServerProvider.getCmpv2Server(TEST_CA)
139 new Cmpv2ServerNotFoundException(expectedMessage)
143 Exception exception = assertThrows(
144 Cmpv2ServerNotFoundException.class, () ->
145 certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA)
149 assertTrue(exception.getMessage().contains(expectedMessage));
153 void shouldThrowCmpClientExceptionWhenSigningCsrFailed()
154 throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
156 String expectedMessage = "failed to sign certificate";
157 CsrModel csrModel = mockCsrFactoryModelCreation();
158 Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
160 certificationProvider.signCsr(csrModel, testServer)
162 new CmpClientException(expectedMessage)
166 Exception exception = assertThrows(
167 CmpClientException.class, () ->
168 certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA)
172 assertTrue(exception.getMessage().contains(expectedMessage));
176 private void mockCertificateProviderCertificateSigning(CsrModel csrModel, Cmpv2Server testServer)
177 throws CmpClientException, Cmpv2ClientAdapterException {
178 CertificationModel expectedCertificationModel = getCertificationModel();
180 certificationProvider.signCsr(csrModel, testServer)
181 ).thenReturn(expectedCertificationModel);
184 private Cmpv2Server mockCmpv2ProviderServerSelection() {
185 Cmpv2Server testServer = getCmpv2Server();
187 cmpv2ServerProvider.getCmpv2Server(TEST_CA)
188 ).thenReturn(testServer);
192 private CsrModel mockCsrFactoryModelCreation()
193 throws DecryptionException {
194 CsrModel csrModel = getCsrModel();
196 csrModelFactory.createCsrModel(
197 new CsrModelFactory.StringBase64(ENCODED_CSR),
198 new CsrModelFactory.StringBase64(ENCODED_PK)
200 ).thenReturn(csrModel);
204 private Cmpv2Server getCmpv2Server() {
205 return new Cmpv2Server();
208 private CsrModel getCsrModel() {
209 return mock(CsrModel.class);
212 private CertificationModel getCertificationModel() {
213 List<String> testTrustedCertificates = Arrays.asList(CA_CERT, EXTRA_CA_CERT);
214 List<String> testCertificationChain = Arrays.asList(INTERMEDIATE_CERT, ENTITY_CERT);
215 return new CertificationModel(testCertificationChain, testTrustedCertificates);