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;
34 import org.onap.aaf.certservice.cmpv2client.model.Cmpv2CertificationModel;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.charset.StandardCharsets;
39 import java.security.NoSuchProviderException;
40 import java.security.cert.CertificateException;
41 import java.security.cert.X509Certificate;
42 import java.util.Collections;
43 import java.util.Objects;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.junit.jupiter.api.Assertions.assertThrows;
47 import static org.mockito.ArgumentMatchers.any;
48 import static org.mockito.Mockito.when;
50 @ExtendWith(MockitoExtension.class)
51 class CertificationProviderTest {
54 private CsrModel csrModel;
56 private Cmpv2Server server;
58 private CsrModel testCsrModel;
60 private Cmpv2Server testServer;
62 private CmpClient cmpClient;
64 private CertificationProvider certificationProvider;
68 certificationProvider = new CertificationProvider(cmpClient);
72 void shouldConvertToCertificationModel()
73 throws CertificateException, NoSuchProviderException, IOException, CmpClientException {
76 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
77 ).thenReturn(createCorrectClientResponse());
79 CertificationModel certificationModel = certificationProvider.signCsr(csrModel, server);
82 InputStream certificate = getClass().getClassLoader().getResourceAsStream("certificateModelChain.first");
83 InputStream trustedCertificate =
84 getClass().getClassLoader().getResourceAsStream("trustedCertificatesModel.first");
85 String certificateModel = removeLineEndings(certificationModel.getCertificateChain().get(0));
86 String expectedCertificate =
87 removeLineEndings(IOUtils.toString(Objects.requireNonNull(certificate), StandardCharsets.UTF_8));
88 String trustedCertificateModel = removeLineEndings(certificationModel.getTrustedCertificates().get(0));
89 String expectedTrustedCertificate =
90 removeLineEndings(IOUtils.toString(Objects.requireNonNull(trustedCertificate), StandardCharsets.UTF_8));
92 assertThat(certificateModel).isEqualTo(expectedCertificate);
93 assertThat(trustedCertificateModel).isEqualTo(expectedTrustedCertificate);
98 void certificationProviderThrowCmpClientWhenCallingClientFails()
99 throws CmpClientException {
101 String expectedErrorMessage = "connecting to CMP client failed";
104 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
105 ).thenThrow(new CmpClientException(expectedErrorMessage));
108 Exception exception = assertThrows(
109 CmpClientException.class, () ->
110 certificationProvider.signCsr(testCsrModel, testServer)
114 assertThat(exception.getMessage()).isEqualTo(expectedErrorMessage);
117 private Cmpv2CertificationModel createCorrectClientResponse()
118 throws CertificateException, NoSuchProviderException {
119 InputStream certificateChain = getClass().getClassLoader().getResourceAsStream("certificateChain.first");
120 InputStream trustedCertificate = getClass().getClassLoader().getResourceAsStream("trustedCertificates.first");
121 X509Certificate x509Certificate = new CertificateFactoryProvider().generateCertificate(certificateChain);
122 X509Certificate x509TrustedCertificate =
123 new CertificateFactoryProvider().generateCertificate(trustedCertificate);
124 return new Cmpv2CertificationModel(
125 Collections.singletonList(x509Certificate),
126 Collections.singletonList(x509TrustedCertificate));
129 private String removeLineEndings(String string) {
130 return string.replace("\n", "").replace("\r", "");