Add Certification merge logic
[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.TruststoreFileFactory;
26 import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider;
27 import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore;
28 import org.onap.oom.truststoremerger.certification.file.model.PemTruststore;
29 import org.onap.oom.truststoremerger.certification.file.model.Truststore;
30 import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException;
31 import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
32
33 import java.io.File;
34 import java.util.Arrays;
35 import java.util.List;
36 import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException;
37 import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40
41 class TruststoreFilesListProviderTest {
42
43     private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks";
44     private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass";
45     private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12";
46     private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass";
47     private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem";
48     private static final String EMPTY_PASS_PATH = "";
49
50     private TruststoreFilesListProvider truststoreFilesListProvider;
51
52     @BeforeEach
53     void setUp() {
54         TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader());
55         truststoreFilesListProvider = new TruststoreFilesListProvider(truststoreFileFactory);
56     }
57
58     @Test
59     void shouldReturnTruststoreFilesList()
60         throws TruststoreFileFactoryException, PasswordReaderException, LoadTruststoreException, KeystoreInstanceException {
61         List<String> truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH);
62         List<String> truststorePasswordPaths = Arrays.asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH);
63         List<Truststore> truststoreFilesList = truststoreFilesListProvider.getTruststoreFilesList(truststorePaths, truststorePasswordPaths);
64         assertThat(truststoreFilesList.size()).isEqualTo(3);
65         assertCorrectJksTruststore(truststoreFilesList.get(0), TRUSTSTORE_JKS_PATH);
66         assertCorrectP12Truststore(truststoreFilesList.get(1), TRUSTSTORE_P12_PATH);
67         assertCorrectPemTruststore(truststoreFilesList.get(2), TRUSTSTORE_PEM_PATH);
68     }
69
70     private void assertCorrectJksTruststore(Truststore truststore, String truststorePath) {
71         assertCorrectTypeAndTruststorePath(truststore, truststorePath, JavaTruststore.class);
72     }
73
74     private void assertCorrectP12Truststore(Truststore truststore, String truststorePath) {
75         assertCorrectTypeAndTruststorePath(truststore, truststorePath, JavaTruststore.class);
76     }
77
78     private void assertCorrectPemTruststore(Truststore truststore, String truststorePath) {
79         assertCorrectTypeAndTruststorePath(truststore, truststorePath, PemTruststore.class);
80     }
81
82     private void assertCorrectTypeAndTruststorePath(Truststore truststore, String truststorePath, Class<?> truststoreType) {
83         assertThat(truststore).isInstanceOf(truststoreType);
84         assertThat(truststore.getFile()).isEqualTo(new File(truststorePath));
85     }
86
87 }