Automation adds INFO.yaml
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / CertificationProviderTest.java
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aaf.certservice.certification;
22
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;
35
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;
44
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;
49
50 @ExtendWith(MockitoExtension.class)
51 class CertificationProviderTest {
52
53     @Mock
54     private CsrModel csrModel;
55     @Mock
56     private Cmpv2Server server;
57     @Mock
58     private CsrModel testCsrModel;
59     @Mock
60     private Cmpv2Server testServer;
61     @Mock
62     private CmpClient cmpClient;
63
64     private CertificationProvider certificationProvider;
65
66     @BeforeEach
67     public void init() {
68         certificationProvider = new CertificationProvider(cmpClient);
69     }
70
71     @Test
72     void shouldConvertToCertificationModel()
73             throws CertificateException, NoSuchProviderException, IOException, CmpClientException {
74         // When
75         when(
76                 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
77         ).thenReturn(createCorrectClientResponse());
78
79         CertificationModel certificationModel = certificationProvider.signCsr(csrModel, server);
80
81         // Then
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));
91
92         assertThat(certificateModel).isEqualTo(expectedCertificate);
93         assertThat(trustedCertificateModel).isEqualTo(expectedTrustedCertificate);
94     }
95
96
97     @Test
98     void certificationProviderThrowCmpClientWhenCallingClientFails()
99             throws CmpClientException {
100         // Given
101         String expectedErrorMessage = "connecting to CMP client failed";
102
103         when(
104                 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
105         ).thenThrow(new CmpClientException(expectedErrorMessage));
106
107         // When
108         Exception exception = assertThrows(
109                 CmpClientException.class, () ->
110                         certificationProvider.signCsr(testCsrModel, testServer)
111         );
112
113         // Then
114         assertThat(exception.getMessage()).isEqualTo(expectedErrorMessage);
115     }
116
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));
127     }
128
129     private String removeLineEndings(String string) {
130         return string.replace("\n", "").replace("\r", "");
131     }
132 }