6c294834cb80b3bae1469dc8c55451b15421d591
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / 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.truststoremerger.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.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.security.cert.Certificate;
31 import java.security.cert.CertificateEncodingException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.stream.Stream;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.params.ParameterizedTest;
38 import org.junit.jupiter.params.provider.Arguments;
39 import org.junit.jupiter.params.provider.MethodSource;
40 import org.onap.oom.truststoremerger.api.ExitableException;
41 import org.onap.oom.truststoremerger.merger.exception.CreateBackupException;
42 import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException;
43 import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException;
44 import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException;
45 import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException;
46 import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException;
47 import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias;
48 import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAliasFactory;
49
50 class TruststoreTest {
51
52     private static final String BACKUP_EXTENSION = ".bak";
53
54     private static final int EXPECTED_ONE = 1;
55     public static final int EXPECTED_THREE = 3;
56     public static final int FIRST_ELEMENT = 0;
57
58     private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory();
59
60     @Test
61     void createBackupShouldCreateFileWithExtension() throws CreateBackupException {
62         //given
63         File pemFile = new File(TestCertificateProvider.PEM_FILE_PATH);
64         Truststore truststore = new PemTruststore(pemFile);
65         //when
66         truststore.createBackup();
67
68         //then
69         File backupFile = new File(TestCertificateProvider.PEM_BACKUP_FILE_PATH);
70         assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue();
71         assertThat(backupFile.isFile()).isTrue();
72     }
73
74     @ParameterizedTest
75     @MethodSource("truststoreProvider")
76     void truststoreShouldReadCertificatesFromFile(Truststore truststore) throws ExitableException {
77         //when
78
79         List<CertificateWithAlias> certificates = truststore.getCertificates();
80         Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate();
81
82         //then
83         assertThat(certificates).hasSize(EXPECTED_ONE);
84         assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
85     }
86
87     @Test
88     void jksTruststoreShouldAddDifferentCertificates() throws Exception {
89         //given
90         Truststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias();
91
92         List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
93             .getCertificates();
94
95         List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
96             .getSamplePemTruststoreFile().getCertificates();
97
98         //when
99
100         jksTruststore.addCertificates(certificateFromP12);
101
102         jksTruststore.addCertificates(certificateFromPem);
103
104         jksTruststore.saveFile();
105
106         //then
107
108         assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE);
109     }
110
111     @Test
112     void p12TruststoreShouldAddDifferentCertificates() throws Exception {
113         //given
114         Truststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile();
115
116         List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
117             .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
118
119         List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
120             .getSamplePemTruststoreFile().getCertificates();
121
122         //when
123
124         p12Truststore.addCertificates(certificateFromJks);
125         p12Truststore.addCertificates(certificateFromPem);
126         p12Truststore.saveFile();
127
128         //then
129
130         assertThat(p12Truststore.getCertificates()).hasSize(EXPECTED_THREE);
131     }
132
133     @Test
134     void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException {
135         //given
136         Truststore pemTruststore = TestCertificateProvider
137             .createTmpPemTruststoreFile();
138
139         List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
140             .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
141
142         List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
143             .getCertificates();
144
145         //when
146
147         pemTruststore.addCertificates(certificateFromJks);
148
149         pemTruststore.addCertificates(certificateFromP12);
150
151         pemTruststore.saveFile();
152
153         //then
154
155         List<CertificateWithAlias> addedCertificates = pemTruststore.getCertificates();
156         Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate();
157
158         assertThat(pemTruststore.getCertificates()).hasSize(EXPECTED_THREE);
159         assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
160     }
161
162     @Test
163     void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException {
164         //given
165         Truststore tmpPemTruststoreFile = TestCertificateProvider
166             .createEmptyTmpPemTruststoreFile();
167         //when//then
168         assertThatExceptionOfType(MissingTruststoreException.class)
169             .isThrownBy(() -> tmpPemTruststoreFile.getCertificates());
170     }
171
172     @Test
173     void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception {
174         //given
175         Truststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile();
176         Certificate certificate = mock(Certificate.class);
177
178         when(certificate.getEncoded()).thenThrow(new CertificateEncodingException());
179
180         List<CertificateWithAlias> certificateFromPem = new ArrayList<>();
181         certificateFromPem.add(factory.createPemCertificate(certificate));
182
183         pemTruststore.addCertificates(certificateFromPem);
184
185         //when //then
186         assertThatExceptionOfType(TruststoreDataOperationException.class)
187             .isThrownBy(() -> pemTruststore.saveFile());
188     }
189
190     @AfterEach
191     void removeTemporaryFiles() throws IOException {
192         TestCertificateProvider.removeTemporaryFiles();
193     }
194
195     private static Stream<Arguments> truststoreProvider()
196         throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
197         return Stream.of(
198             Arguments.of(TestCertificateProvider.getSampleJksTruststoreFile()),
199             Arguments.of(TestCertificateProvider.getSampleP12Truststore()),
200             Arguments.of(TestCertificateProvider.getSamplePemTruststoreFile())
201         );
202     }
203
204 }