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.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.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
49 import static org.onap.oom.certservice.certification.CertificationData.CA_CERT;
50 import static org.onap.oom.certservice.certification.CertificationData.ENTITY_CERT;
51 import static org.onap.oom.certservice.certification.CertificationData.INTERMEDIATE_CERT;
52 import static org.onap.oom.certservice.certification.CertificationData.EXTRA_CA_CERT;
53 import static org.onap.oom.certservice.certification.TestData.TEST_CSR;
54 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
55 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_CSR;
56 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_PEM;
58 @ExtendWith(MockitoExtension.class)
59 class CertificationModelFactoryTest {
61 private static final String TEST_CA = "testCA";
62 private static final String ENCODED_CSR = getEncodedString(TEST_CSR);
63 private static final String ENCODED_PK = getEncodedString(TEST_PK);
64 private static final String ENCODED_WRONG_CSR = getEncodedString(TEST_WRONG_CSR);
65 private static final String ENCODED_WRONG_PK = getEncodedString(TEST_WRONG_PEM);
67 private CertificationModelFactory certificationModelFactory;
70 private Cmpv2ServerProvider cmpv2ServerProvider;
72 private CsrModelFactory csrModelFactory;
74 private CertificationProvider certificationProvider;
77 private static String getEncodedString(String testCsr) {
78 return Base64.getEncoder().encodeToString(testCsr.getBytes());
83 certificationModelFactory =
84 new CertificationModelFactory(csrModelFactory, cmpv2ServerProvider, certificationProvider);
88 void shouldCreateProperCertificationModelWhenGivenProperCsrModelAndCaName()
89 throws CmpClientException, DecryptionException, Cmpv2ClientAdapterException {
92 CsrModel csrModel = mockCsrFactoryModelCreation();
93 Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
94 mockCertificateProviderCertificateSigning(csrModel, testServer);
97 CertificationModel certificationModel =
98 certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA);
101 assertEquals(2, certificationModel.getCertificateChain().size());
102 assertThat(certificationModel.getCertificateChain()).contains(INTERMEDIATE_CERT, ENTITY_CERT);
103 assertEquals(2, certificationModel.getTrustedCertificates().size());
104 assertThat(certificationModel.getTrustedCertificates()).contains(CA_CERT, EXTRA_CA_CERT);
108 void shouldThrowDecryptionExceptionWhenGivenWrongEncodedCsr()
109 throws DecryptionException {
111 String expectedMessage = "Incorrect CSR, decryption failed";
113 csrModelFactory.createCsrModel(
114 eq(new CsrModelFactory.StringBase64(ENCODED_WRONG_CSR)),
115 eq(new CsrModelFactory.StringBase64(ENCODED_WRONG_PK))
118 new CsrDecryptionException(expectedMessage)
122 Exception exception = assertThrows(
123 DecryptionException.class, () ->
124 certificationModelFactory.createCertificationModel(ENCODED_WRONG_CSR, ENCODED_WRONG_PK, TEST_CA)
128 assertTrue(exception.getMessage().contains(expectedMessage));
132 void shouldThrowCmpv2ServerNotFoundExceptionWhenGivenWrongCaName()
133 throws DecryptionException {
135 String expectedMessage = "CA not found";
136 mockCsrFactoryModelCreation();
138 cmpv2ServerProvider.getCmpv2Server(TEST_CA)
140 new Cmpv2ServerNotFoundException(expectedMessage)
144 Exception exception = assertThrows(
145 Cmpv2ServerNotFoundException.class, () ->
146 certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA)
150 assertTrue(exception.getMessage().contains(expectedMessage));
154 void shouldThrowCmpClientExceptionWhenSigningCsrFailed()
155 throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
157 String expectedMessage = "failed to sign certificate";
158 CsrModel csrModel = mockCsrFactoryModelCreation();
159 Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
161 certificationProvider.signCsr(eq(csrModel), eq(testServer))
163 new CmpClientException(expectedMessage)
167 Exception exception = assertThrows(
168 CmpClientException.class, () ->
169 certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA)
173 assertTrue(exception.getMessage().contains(expectedMessage));
177 private void mockCertificateProviderCertificateSigning(CsrModel csrModel, Cmpv2Server testServer)
178 throws CmpClientException, Cmpv2ClientAdapterException {
179 CertificationModel expectedCertificationModel = getCertificationModel();
181 certificationProvider.signCsr(eq(csrModel), eq(testServer))
182 ).thenReturn(expectedCertificationModel);
185 private Cmpv2Server mockCmpv2ProviderServerSelection() {
186 Cmpv2Server testServer = getCmpv2Server();
188 cmpv2ServerProvider.getCmpv2Server(eq(TEST_CA))
189 ).thenReturn(testServer);
193 private CsrModel mockCsrFactoryModelCreation()
194 throws DecryptionException {
195 CsrModel csrModel = getCsrModel();
197 csrModelFactory.createCsrModel(
198 eq(new CsrModelFactory.StringBase64(ENCODED_CSR)),
199 eq(new CsrModelFactory.StringBase64(ENCODED_PK))
201 ).thenReturn(csrModel);
205 private Cmpv2Server getCmpv2Server() {
206 return new Cmpv2Server();
209 private CsrModel getCsrModel() {
210 return mock(CsrModel.class);
213 private CertificationModel getCertificationModel() {
214 List<String> testTrustedCertificates = Arrays.asList(CA_CERT, EXTRA_CA_CERT);
215 List<String> testCertificationChain = Arrays.asList(INTERMEDIATE_CERT, ENTITY_CERT);
216 return new CertificationModel(testCertificationChain, testTrustedCertificates);