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