2 * ============LICENSE_START=======================================================
3 * AAF Certification Service
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.aaf.certservice.certification;
23 import org.apache.commons.io.IOUtils;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
30 import org.onap.aaf.certservice.certification.model.CertificationModel;
31 import org.onap.aaf.certservice.certification.model.CsrModel;
32 import org.onap.aaf.certservice.cmpv2client.api.CmpClient;
33 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.nio.charset.StandardCharsets;
38 import java.security.NoSuchProviderException;
39 import java.security.cert.CertificateException;
40 import java.security.cert.X509Certificate;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.Objects;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.junit.jupiter.api.Assertions.assertThrows;
48 import static org.mockito.ArgumentMatchers.any;
49 import static org.mockito.Mockito.when;
51 @ExtendWith(MockitoExtension.class)
52 class CertificationProviderTest {
55 private CsrModel csrModel;
57 private Cmpv2Server server;
59 private CsrModel testCsrModel;
61 private Cmpv2Server testServer;
63 private CmpClient cmpClient;
65 private CertificationProvider certificationProvider;
69 certificationProvider = new CertificationProvider(cmpClient);
73 void shouldConvertToCertificationModel()
74 throws CertificateException, NoSuchProviderException, IOException, CmpClientException {
77 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
78 ).thenReturn(createCorrectClientResponse());
80 CertificationModel certificationModel = certificationProvider.signCsr(csrModel, server);
83 InputStream certificate = getClass().getClassLoader().getResourceAsStream("certificateModelChain.first");
84 InputStream trustedCertificate =
85 getClass().getClassLoader().getResourceAsStream("trustedCertificatesModel.first");
86 String certificateModel = removeLineEndings(certificationModel.getCertificateChain().get(0));
87 String expectedCertificate =
88 removeLineEndings(IOUtils.toString(Objects.requireNonNull(certificate), StandardCharsets.UTF_8));
89 String trustedCertificateModel = removeLineEndings(certificationModel.getTrustedCertificates().get(0));
90 String expectedTrustedCertificate =
91 removeLineEndings(IOUtils.toString(Objects.requireNonNull(trustedCertificate), StandardCharsets.UTF_8));
93 assertThat(certificateModel).isEqualTo(expectedCertificate);
94 assertThat(trustedCertificateModel).isEqualTo(expectedTrustedCertificate);
99 void certificationProviderThrowCmpClientWhenCallingClientFails()
100 throws CmpClientException {
102 String expectedErrorMessage = "connecting to CMP client failed";
105 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
106 ).thenThrow(new CmpClientException(expectedErrorMessage));
109 Exception exception = assertThrows(
110 CmpClientException.class, () ->
111 certificationProvider.signCsr(testCsrModel, testServer)
115 assertThat(exception.getMessage()).isEqualTo(expectedErrorMessage);
118 private List<List<X509Certificate>> createCorrectClientResponse()
119 throws CertificateException, NoSuchProviderException {
120 InputStream certificateChain = getClass().getClassLoader().getResourceAsStream("certificateChain.first");
121 InputStream trustedCertificate = getClass().getClassLoader().getResourceAsStream("trustedCertificates.first");
122 X509Certificate x509Certificate = new CertificateFactoryProvider().generateCertificate(certificateChain);
123 X509Certificate x509TrustedCertificate =
124 new CertificateFactoryProvider().generateCertificate(trustedCertificate);
125 return Arrays.asList(Collections.singletonList(x509Certificate),
126 Collections.singletonList(x509TrustedCertificate));
129 private String removeLineEndings(String string) {
130 return string.replace("\n", "").replace("\r", "");