Automation adds INFO.yaml
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / CertificationModelFactoryTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.onap.aaf.certservice.certification.configuration.Cmpv2ServerProvider;
29 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
30 import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException;
31 import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException;
32 import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
33 import org.onap.aaf.certservice.certification.exception.DecryptionException;
34 import org.onap.aaf.certservice.certification.model.CertificationModel;
35 import org.onap.aaf.certservice.certification.model.CsrModel;
36 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
37
38 import java.util.Arrays;
39 import java.util.Base64;
40 import java.util.List;
41
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.junit.jupiter.api.Assertions.assertEquals;
44 import static org.junit.jupiter.api.Assertions.assertThrows;
45 import static org.junit.jupiter.api.Assertions.assertTrue;
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
49 import static org.onap.aaf.certservice.certification.CertificationData.CA_CERT;
50 import static org.onap.aaf.certservice.certification.CertificationData.ENTITY_CERT;
51 import static org.onap.aaf.certservice.certification.CertificationData.INTERMEDIATE_CERT;
52 import static org.onap.aaf.certservice.certification.CertificationData.EXTRA_CA_CERT;
53 import static org.onap.aaf.certservice.certification.TestData.TEST_CSR;
54 import static org.onap.aaf.certservice.certification.TestData.TEST_PK;
55 import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_CSR;
56 import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_PEM;
57
58 @ExtendWith(MockitoExtension.class)
59 class CertificationModelFactoryTest {
60
61     private static final String TEST_CA = "testCA";
62     private static final String ENCODED_CSR = getEncodedString(TEST_CSR);
63     private static final String ENCODED_PK = getEncodedString(TEST_PK);
64     private static final String ENCODED_WRONG_CSR = getEncodedString(TEST_WRONG_CSR);
65     private static final String ENCODED_WRONG_PK = getEncodedString(TEST_WRONG_PEM);
66
67     private CertificationModelFactory certificationModelFactory;
68
69     @Mock
70     private Cmpv2ServerProvider cmpv2ServerProvider;
71     @Mock
72     private CsrModelFactory csrModelFactory;
73     @Mock
74     private CertificationProvider certificationProvider;
75
76
77     private static String getEncodedString(String testCsr) {
78         return Base64.getEncoder().encodeToString(testCsr.getBytes());
79     }
80
81     @BeforeEach
82     void setUp() {
83         certificationModelFactory =
84                 new CertificationModelFactory(csrModelFactory, cmpv2ServerProvider, certificationProvider);
85     }
86
87     @Test
88     void shouldCreateProperCertificationModelWhenGivenProperCsrModelAndCaName()
89             throws CmpClientException, DecryptionException, Cmpv2ClientAdapterException {
90
91         // Given
92         CsrModel csrModel = mockCsrFactoryModelCreation();
93         Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
94         mockCertificateProviderCertificateSigning(csrModel, testServer);
95
96         // When
97         CertificationModel certificationModel =
98                 certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA);
99
100         // Then
101         assertEquals(2, certificationModel.getCertificateChain().size());
102         assertThat(certificationModel.getCertificateChain()).contains(INTERMEDIATE_CERT, ENTITY_CERT);
103         assertEquals(2, certificationModel.getTrustedCertificates().size());
104         assertThat(certificationModel.getTrustedCertificates()).contains(CA_CERT, EXTRA_CA_CERT);
105     }
106
107     @Test
108     void shouldThrowDecryptionExceptionWhenGivenWrongEncodedCsr()
109             throws DecryptionException {
110         // Given
111         String expectedMessage = "Incorrect CSR, decryption failed";
112         when(
113                 csrModelFactory.createCsrModel(
114                         eq(new CsrModelFactory.StringBase64(ENCODED_WRONG_CSR)),
115                         eq(new CsrModelFactory.StringBase64(ENCODED_WRONG_PK))
116                 )
117         ).thenThrow(
118                 new CsrDecryptionException(expectedMessage)
119         );
120
121         // When
122         Exception exception = assertThrows(
123                 DecryptionException.class, () ->
124                         certificationModelFactory.createCertificationModel(ENCODED_WRONG_CSR, ENCODED_WRONG_PK, TEST_CA)
125         );
126
127         // Then
128         assertTrue(exception.getMessage().contains(expectedMessage));
129     }
130
131     @Test
132     void shouldThrowCmpv2ServerNotFoundExceptionWhenGivenWrongCaName()
133             throws DecryptionException {
134         // Given
135         String expectedMessage = "CA not found";
136         mockCsrFactoryModelCreation();
137         when(
138                 cmpv2ServerProvider.getCmpv2Server(TEST_CA)
139         ).thenThrow(
140                 new Cmpv2ServerNotFoundException(expectedMessage)
141         );
142
143         // When
144         Exception exception = assertThrows(
145                 Cmpv2ServerNotFoundException.class, () ->
146                         certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA)
147         );
148
149         // Then
150         assertTrue(exception.getMessage().contains(expectedMessage));
151     }
152
153     @Test
154     void shouldThrowCmpClientExceptionWhenSigningCsrFailed()
155             throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
156         // Given
157         String expectedMessage = "failed to sign certificate";
158         CsrModel csrModel = mockCsrFactoryModelCreation();
159         Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
160         when(
161                 certificationProvider.signCsr(eq(csrModel), eq(testServer))
162         ).thenThrow(
163                 new CmpClientException(expectedMessage)
164         );
165
166         // When
167         Exception exception = assertThrows(
168                 CmpClientException.class, () ->
169                         certificationModelFactory.createCertificationModel(ENCODED_CSR, ENCODED_PK, TEST_CA)
170         );
171
172         // Then
173         assertTrue(exception.getMessage().contains(expectedMessage));
174     }
175
176
177     private void mockCertificateProviderCertificateSigning(CsrModel csrModel, Cmpv2Server testServer)
178             throws CmpClientException, Cmpv2ClientAdapterException {
179         CertificationModel expectedCertificationModel = getCertificationModel();
180         when(
181                 certificationProvider.signCsr(eq(csrModel), eq(testServer))
182         ).thenReturn(expectedCertificationModel);
183     }
184
185     private Cmpv2Server mockCmpv2ProviderServerSelection() {
186         Cmpv2Server testServer = getCmpv2Server();
187         when(
188                 cmpv2ServerProvider.getCmpv2Server(eq(TEST_CA))
189         ).thenReturn(testServer);
190         return testServer;
191     }
192
193     private CsrModel mockCsrFactoryModelCreation()
194             throws DecryptionException {
195         CsrModel csrModel = getCsrModel();
196         when(
197                 csrModelFactory.createCsrModel(
198                         eq(new CsrModelFactory.StringBase64(ENCODED_CSR)),
199                         eq(new CsrModelFactory.StringBase64(ENCODED_PK))
200                 )
201         ).thenReturn(csrModel);
202         return csrModel;
203     }
204
205     private Cmpv2Server getCmpv2Server() {
206         return new Cmpv2Server();
207     }
208
209     private CsrModel getCsrModel() {
210         return mock(CsrModel.class);
211     }
212
213     private CertificationModel getCertificationModel() {
214         List<String> testTrustedCertificates = Arrays.asList(CA_CERT, EXTRA_CA_CERT);
215         List<String> testCertificationChain = Arrays.asList(INTERMEDIATE_CERT, ENTITY_CERT);
216         return new CertificationModel(testCertificationChain, testTrustedCertificates);
217     }
218
219
220 }