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