Removed dependency to cmpv2client adapter
[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
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;
45
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;
50
51 @ExtendWith(MockitoExtension.class)
52 class CertificationProviderTest {
53
54     @Mock
55     private CsrModel csrModel;
56     @Mock
57     private Cmpv2Server server;
58     @Mock
59     private CsrModel testCsrModel;
60     @Mock
61     private Cmpv2Server testServer;
62     @Mock
63     private CmpClient cmpClient;
64
65     private CertificationProvider certificationProvider;
66
67     @BeforeEach
68     public void init() {
69         certificationProvider = new CertificationProvider(cmpClient);
70     }
71
72     @Test
73     void shouldConvertToCertificationModel()
74             throws CertificateException, NoSuchProviderException, IOException, CmpClientException {
75         // When
76         when(
77                 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
78         ).thenReturn(createCorrectClientResponse());
79
80         CertificationModel certificationModel = certificationProvider.signCsr(csrModel, server);
81
82         // Then
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));
92
93         assertThat(certificateModel).isEqualTo(expectedCertificate);
94         assertThat(trustedCertificateModel).isEqualTo(expectedTrustedCertificate);
95     }
96
97
98     @Test
99     void certificationProviderThrowCmpClientWhenCallingClientFails()
100             throws CmpClientException {
101         // Given
102         String expectedErrorMessage = "connecting to CMP client failed";
103
104         when(
105                 cmpClient.createCertificate(any(CsrModel.class), any(Cmpv2Server.class))
106         ).thenThrow(new CmpClientException(expectedErrorMessage));
107
108         // When
109         Exception exception = assertThrows(
110                 CmpClientException.class, () ->
111                         certificationProvider.signCsr(testCsrModel, testServer)
112         );
113
114         // Then
115         assertThat(exception.getMessage()).isEqualTo(expectedErrorMessage);
116     }
117
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));
127     }
128
129     private String removeLineEndings(String string) {
130         return string.replace("\n", "").replace("\r", "");
131     }
132 }