Merge "[OOM-CPMv2] Fix sonar issue"
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / certservice / postprocessor / merger / model / TruststoreTest.java
1 /*============LICENSE_START=======================================================
2  * oom-truststore-merger
3  * ================================================================================
4  * Copyright (C) 2020 Nokia. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.oom.certservice.postprocessor.merger.model;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.X_509_CERTIFICATE;
27 import static org.onap.oom.certservice.postprocessor.merger.model.TestCertificateProvider.PEM_BACKUP_FILE_PATH;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.security.cert.Certificate;
32 import java.security.cert.CertificateEncodingException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.stream.Stream;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.params.ParameterizedTest;
39 import org.junit.jupiter.params.provider.Arguments;
40 import org.junit.jupiter.params.provider.MethodSource;
41 import org.onap.oom.certservice.postprocessor.api.CertificateConstants;
42 import org.onap.oom.certservice.postprocessor.api.ExitableException;
43 import org.onap.oom.certservice.postprocessor.merger.exception.CreateBackupException;
44 import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
45 import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
46 import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException;
47 import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
48 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreDataOperationException;
49 import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias;
50 import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAliasFactory;
51
52 class TruststoreTest {
53
54     private static final String BACKUP_EXTENSION = ".bak";
55
56     private static final int EXPECTED_ONE = 1;
57     public static final int EXPECTED_THREE = 3;
58     public static final int FIRST_ELEMENT = 0;
59
60     private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory();
61
62     @Test
63     void createBackupShouldCreateFileWithExtension() throws CreateBackupException {
64         //given
65         File pemFile = new File(TestCertificateProvider.PEM_FILE_PATH);
66         Truststore truststore = new PemTruststore(pemFile);
67         //when
68         truststore.createBackup();
69
70         //then
71         File backupFile = new File(PEM_BACKUP_FILE_PATH);
72         assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue();
73         assertThat(backupFile.isFile()).isTrue();
74     }
75
76     @ParameterizedTest
77     @MethodSource("truststoreProvider")
78     void truststoreShouldReadCertificatesFromFile(Truststore truststore) throws ExitableException {
79         //when
80
81         List<CertificateWithAlias> certificates = truststore.getCertificates();
82         Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate();
83
84         //then
85         assertThat(certificates).hasSize(EXPECTED_ONE);
86         assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
87     }
88
89     @Test
90     void jksTruststoreShouldAddDifferentCertificates() throws Exception {
91         //given
92         Truststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias();
93
94         List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
95             .getCertificates();
96
97         List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
98             .getSamplePemTruststoreFile().getCertificates();
99
100         //when
101
102         jksTruststore.addCertificates(certificateFromP12);
103
104         jksTruststore.addCertificates(certificateFromPem);
105
106         jksTruststore.saveFile();
107
108         //then
109
110         assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE);
111     }
112
113     @Test
114     void p12TruststoreShouldAddDifferentCertificates() throws Exception {
115         //given
116         Truststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile();
117
118         List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
119             .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
120
121         List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
122             .getSamplePemTruststoreFile().getCertificates();
123
124         //when
125
126         p12Truststore.addCertificates(certificateFromJks);
127         p12Truststore.addCertificates(certificateFromPem);
128         p12Truststore.saveFile();
129
130         //then
131
132         assertThat(p12Truststore.getCertificates()).hasSize(EXPECTED_THREE);
133     }
134
135     @Test
136     void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException {
137         //given
138         Truststore pemTruststore = TestCertificateProvider
139             .createTmpPemTruststoreFile();
140
141         List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
142             .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
143
144         List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
145             .getCertificates();
146
147         //when
148
149         pemTruststore.addCertificates(certificateFromJks);
150
151         pemTruststore.addCertificates(certificateFromP12);
152
153         pemTruststore.saveFile();
154
155         //then
156
157         List<CertificateWithAlias> addedCertificates = pemTruststore.getCertificates();
158         Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate();
159
160         assertThat(pemTruststore.getCertificates()).hasSize(EXPECTED_THREE);
161         assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
162     }
163
164     @Test
165     void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException {
166         //given
167         Truststore tmpPemTruststoreFile = TestCertificateProvider
168             .createEmptyTmpPemTruststoreFile();
169         //when//then
170         assertThatExceptionOfType(MissingTruststoreException.class)
171             .isThrownBy(() -> tmpPemTruststoreFile.getCertificates());
172     }
173
174     @Test
175     void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception {
176         //given
177         Truststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile();
178         Certificate certificate = mock(Certificate.class);
179
180         when(certificate.getEncoded()).thenThrow(new CertificateEncodingException());
181
182         List<CertificateWithAlias> certificateFromPem = new ArrayList<>();
183         certificateFromPem.add(factory.createPemCertificate(certificate));
184
185         pemTruststore.addCertificates(certificateFromPem);
186
187         //when //then
188         assertThatExceptionOfType(TruststoreDataOperationException.class)
189             .isThrownBy(() -> pemTruststore.saveFile());
190     }
191
192     @AfterEach
193     void removeTemporaryFiles() throws IOException {
194         TestCertificateProvider.removeTemporaryFiles();
195     }
196
197     private static Stream<Arguments> truststoreProvider()
198         throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
199         return Stream.of(
200             Arguments.of(TestCertificateProvider.getSampleJksTruststoreFile()),
201             Arguments.of(TestCertificateProvider.getSampleP12Truststore()),
202             Arguments.of(TestCertificateProvider.getSamplePemTruststoreFile())
203         );
204     }
205
206 }