Merge "Removed unused parameters when creating certificate"
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / adapter / Cmpv2ClientAdapterTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert 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.adapter;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.charset.StandardCharsets;
26 import java.security.NoSuchProviderException;
27 import java.security.PrivateKey;
28 import java.security.cert.CertificateException;
29 import java.security.cert.X509Certificate;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Objects;
34
35 import org.apache.commons.io.IOUtils;
36 import org.bouncycastle.asn1.x509.Certificate;
37 import org.bouncycastle.cert.X509CertificateHolder;
38 import org.bouncycastle.cert.X509v3CertificateBuilder;
39 import org.bouncycastle.operator.ContentSigner;
40 import org.bouncycastle.operator.OperatorCreationException;
41 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
42 import org.junit.jupiter.api.Assertions;
43 import org.junit.jupiter.api.Test;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
48 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
49 import org.onap.aaf.certservice.certification.model.CertificationModel;
50 import org.onap.aaf.certservice.certification.model.CsrModel;
51 import org.onap.aaf.certservice.cmpv2client.api.CmpClient;
52 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
53 import org.springframework.boot.test.context.SpringBootTest;
54
55 @SpringBootTest
56 class Cmpv2ClientAdapterTest {
57
58     @Mock
59     private CmpClient cmpClient;
60     @Mock
61     private CsrModel csrModel;
62     @Mock
63     private Cmpv2Server server;
64     @Mock
65     private RsaContentSignerBuilder rsaContentSignerBuilder;
66     @Mock
67     private X509CertificateBuilder x509CertificateBuilder;
68     @Mock
69     private PKCS10CertificationRequest csr;
70     @Mock
71     private PrivateKey privateKey;
72     @Mock
73     private X509v3CertificateBuilder x509V3CertificateBuilder;
74     @Mock
75     private ContentSigner contentSigner;
76     @Mock
77     private X509CertificateHolder holder;
78     @Mock
79     private Certificate asn1Certificate;
80     @Mock
81     private X509Certificate certificate;
82     @Mock
83     private CertificateFactoryProvider certificateFactoryProvider;
84
85     @InjectMocks
86     private Cmpv2ClientAdapter adapter;
87
88     private static final CaMode CA_MODEL = CaMode.CLIENT;
89     private static final String TEST_MSG = "Test";
90
91     @Test
92     void adapterShouldRethrowClientExceptionOnFailure()
93             throws CmpClientException, IOException, OperatorCreationException, CertificateException,
94             NoSuchProviderException {
95         // Given
96         stubInternalProperties();
97
98         // When
99         Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any()))
100                 .thenThrow(new CmpClientException(TEST_MSG));
101
102         // Then
103         Assertions.assertThrows(CmpClientException.class, () -> adapter.callCmpClient(csrModel, server));
104     }
105
106     @Test
107     void shouldConvertToCertificationModel()
108             throws OperatorCreationException, CertificateException, NoSuchProviderException, IOException,
109             CmpClientException {
110         // Given
111         stubInternalProperties();
112
113         // When
114         Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any()))
115                 .thenReturn(createCorrectClientResponse());
116         CertificationModel certificationModel = adapter.callCmpClient(csrModel, server);
117
118         // Then
119         InputStream certificate = getClass().getClassLoader().getResourceAsStream("certificateModelChain.first");
120         InputStream trustedCertificate =
121                 getClass().getClassLoader().getResourceAsStream("trustedCertificatesModel.first");
122         String certificateModel = removeLineEndings(certificationModel.getCertificateChain().get(0));
123         String expectedCertificate =
124                 removeLineEndings(IOUtils.toString(Objects.requireNonNull(certificate), StandardCharsets.UTF_8));
125         String trustedCertificateModel = removeLineEndings(certificationModel.getTrustedCertificates().get(0));
126         String expectedTrustedCertificate =
127                 removeLineEndings(IOUtils.toString(Objects.requireNonNull(trustedCertificate), StandardCharsets.UTF_8));
128
129         Assertions.assertEquals(certificateModel, expectedCertificate);
130         Assertions.assertEquals(trustedCertificateModel, expectedTrustedCertificate);
131     }
132
133     private List<List<X509Certificate>> createCorrectClientResponse()
134             throws CertificateException, NoSuchProviderException {
135         InputStream certificateChain = getClass().getClassLoader().getResourceAsStream("certificateChain.first");
136         InputStream trustedCertificate = getClass().getClassLoader().getResourceAsStream("trustedCertificates.first");
137         X509Certificate x509Certificate = new CertificateFactoryProvider().generateCertificate(certificateChain);
138         X509Certificate x509TrustedCertificate =
139                 new CertificateFactoryProvider().generateCertificate(trustedCertificate);
140         return Arrays.asList(Collections.singletonList(x509Certificate),
141                 Collections.singletonList(x509TrustedCertificate));
142     }
143
144     private String removeLineEndings(String string) {
145         return string.replace("\n", "").replace("\r", "");
146     }
147
148     private void stubInternalProperties()
149             throws IOException, OperatorCreationException, CertificateException, NoSuchProviderException {
150         Mockito.when(server.getCaMode()).thenReturn(CA_MODEL);
151         Mockito.when(csrModel.getCsr()).thenReturn(csr);
152         Mockito.when(csrModel.getPrivateKey()).thenReturn(privateKey);
153         Mockito.when(x509CertificateBuilder.build(csr)).thenReturn(x509V3CertificateBuilder);
154         Mockito.when(rsaContentSignerBuilder.build(csr, privateKey)).thenReturn(contentSigner);
155         Mockito.when(x509V3CertificateBuilder.build(contentSigner)).thenReturn(holder);
156         Mockito.when(holder.toASN1Structure()).thenReturn(asn1Certificate);
157         Mockito.when(certificateFactoryProvider.generateCertificate(Mockito.any())).thenReturn(certificate);
158         Mockito.when(holder.toASN1Structure().getEncoded()).thenReturn("".getBytes());
159     }
160
161 }