Add TruststoreFile provider
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / certification / file / provider / TruststoreFilesListProviderTest.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
21 package org.onap.oom.truststoremerger.certification.file.provider;
22
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.onap.oom.truststoremerger.certification.file.JksTruststore;
26 import org.onap.oom.truststoremerger.certification.file.P12Truststore;
27 import org.onap.oom.truststoremerger.certification.file.PemTruststore;
28 import org.onap.oom.truststoremerger.certification.file.TruststoreFile;
29 import org.onap.oom.truststoremerger.certification.file.TruststoreFileWithPassword;
30
31 import java.io.File;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36
37 class TruststoreFilesListProviderTest {
38
39     private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks";
40     private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass";
41     private static final String TRUSTSTORE_JKS_PASS = "EOyuFbuYDyq_EhpboM72RHua";
42     private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12";
43     private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass";
44     private static final String TRUSTSTORE_P12_PASS = "88y9v5D8H3SG6bZWRVHDfOAo";
45     private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem";
46     private static final String EMPTY_PASS_PATH = "";
47
48     private TruststoreFilesListProvider truststoreFilesListProvider;
49
50     @BeforeEach
51     void setUp() {
52         TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader());
53         truststoreFilesListProvider = new TruststoreFilesListProvider(truststoreFileFactory);
54     }
55
56     @Test
57     void shouldReturnTruststoreFilesList() throws PasswordReaderException, TruststoreFileFactoryException {
58         List<String> truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH);
59         List<String> truststorePasswordPaths = Arrays.asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH);
60         List<TruststoreFile> truststoreFilesList = truststoreFilesListProvider.getTruststoreFilesList(truststorePaths, truststorePasswordPaths);
61         assertThat(truststoreFilesList.size()).isEqualTo(3);
62         assertCorrectJksTruststore(truststoreFilesList.get(0), TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS);
63         assertCorrectP12Truststore(truststoreFilesList.get(1), TRUSTSTORE_P12_PATH, TRUSTSTORE_P12_PASS);
64         assertCorrectPemTruststore(truststoreFilesList.get(2), TRUSTSTORE_PEM_PATH);
65     }
66
67     private void assertCorrectJksTruststore(TruststoreFile truststoreFile, String truststorePath, String truststorePass) {
68         assertCorrectTypeAndTruststorePath(truststoreFile, truststorePath, JksTruststore.class);
69         assertContainsCorrectPassword(truststoreFile, truststorePass);
70     }
71
72     private void assertCorrectP12Truststore(TruststoreFile truststoreFile, String truststorePath, String truststorePass) {
73         assertCorrectTypeAndTruststorePath(truststoreFile, truststorePath, P12Truststore.class);
74         assertContainsCorrectPassword(truststoreFile, truststorePass);
75     }
76
77     private void assertCorrectPemTruststore(TruststoreFile truststoreFile, String truststorePath) {
78         assertCorrectTypeAndTruststorePath(truststoreFile, truststorePath, PemTruststore.class);
79     }
80
81     private void assertCorrectTypeAndTruststorePath(TruststoreFile truststoreFile, String truststorePath, Class<?> truststoreType) {
82         assertThat(truststoreFile).isInstanceOf(truststoreType);
83         assertThat(truststoreFile.getTruststoreFile()).isEqualTo(new File(truststorePath));
84     }
85
86     private void assertContainsCorrectPassword(TruststoreFile truststoreFile, String truststorePass) {
87         TruststoreFileWithPassword truststoreFileWithPassword = (TruststoreFileWithPassword) truststoreFile;
88         assertThat(truststoreFileWithPassword.getPassword()).isEqualTo(truststorePass);
89     }
90 }