[OOM-CMPv2] Create KeystoreCopier
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / configuration / MergerConfigurationProviderTest.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.configuration;
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.when;
25 import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_DESTINATION_PATHS_ENV;
26 import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_SOURCE_PATHS_ENV;
27 import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV;
28 import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.onap.oom.truststoremerger.configuration.exception.MergerConfigurationException;
38 import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException;
39 import org.onap.oom.truststoremerger.configuration.model.AppConfiguration;
40 import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader;
41
42 @ExtendWith(MockitoExtension.class)
43 class MergerConfigurationProviderTest {
44
45     private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_";
46     private static final String KEYSTORE_PATH = "/opt/app/keystore_";
47     private static final String ANOTHER_KEYSTORE_PATH = "/opt/app/external/keystore_";
48     private static final String JKS_EXTENSION = ".jks";
49     private static final String PEM_EXTENSION = ".pem";
50     private static final String PASS_EXTENSION = ".pass";
51
52     @Mock
53     private DelimitedPathsReader certificatesPathsProvider;
54     @Mock
55     private DelimitedPathsReader passwordsPathsProvider;
56     @Mock
57     private DelimitedPathsReader copierPathsReader;
58     private AppConfigurationProvider factory;
59
60     @BeforeEach
61     void setUp() {
62         factory = new AppConfigurationProvider(certificatesPathsProvider, passwordsPathsProvider, copierPathsReader);
63     }
64
65     @Test
66     void shouldReturnConfigurationWithCorrectPaths()
67         throws TruststoresPathsProviderException, MergerConfigurationException {
68         int numberOfPaths = 5;
69         List<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, JKS_EXTENSION);
70         List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASS_EXTENSION);
71         mockTruststorePaths(truststoresPaths, truststorePasswordPaths);
72
73         List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH,
74             numberOfPaths, PEM_EXTENSION);
75         List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH,
76             numberOfPaths, PEM_EXTENSION);
77         mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths);
78
79         AppConfiguration configuration = factory.createConfiguration();
80
81         assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
82         assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
83         assertThat(configuration.getSourceKeystorePaths()).containsAll(sourceKeystoresPairPaths);
84         assertThat(configuration.getDestinationKeystorePaths()).containsAll(destKeystoresPairPaths);
85     }
86
87     @Test
88     void shouldThrowExceptionWhenTruststoresLengthDifferentThanTruststoresPasswordsLength()
89         throws TruststoresPathsProviderException {
90         int numberOfCertificates = 5;
91         int numberOfTruststoresPasswords = 4;
92         List<String> truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION);
93         List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASS_EXTENSION);
94         mockTruststorePaths(truststoresPaths, truststorePasswordPaths);
95
96         List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH,
97             numberOfCertificates, PEM_EXTENSION);
98         List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH,
99             numberOfCertificates, PEM_EXTENSION);
100         mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths);
101
102         assertThatExceptionOfType(MergerConfigurationException.class)
103             .isThrownBy(factory::createConfiguration);
104     }
105
106     @Test
107     void shouldThrowExceptionWhenSourceLengthDifferentThanDestinationLength()
108         throws TruststoresPathsProviderException {
109         int numberOfCertificates = 5;
110         int anotherNumberOfCertificates = 1;
111         List<String> truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION);
112         List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfCertificates, PASS_EXTENSION);
113         mockTruststorePaths(truststoresPaths, truststorePasswordPaths);
114
115         List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH,
116             numberOfCertificates, PEM_EXTENSION);
117         List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH,
118             anotherNumberOfCertificates, PEM_EXTENSION);
119         mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths);
120
121         assertThatExceptionOfType(MergerConfigurationException.class)
122             .isThrownBy(factory::createConfiguration);
123     }
124
125     private void mockTruststorePaths(List<String> truststores, List<String> truststoresPasswords)
126         throws TruststoresPathsProviderException {
127         mockTruststores(truststores);
128         mockTruststoresPasswords(truststoresPasswords);
129     }
130
131     private void mockKeystorePaths(List<String> sourceKeystoresPairPaths, List<String> destKeystoresPairPaths)
132         throws TruststoresPathsProviderException {
133         mockKeystoreCopierSourcePaths(sourceKeystoresPairPaths);
134         mockKeystoreCopierDestinationPaths(destKeystoresPairPaths);
135     }
136
137     private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
138         when(certificatesPathsProvider.get(TRUSTSTORES_PATHS_ENV)).thenReturn(truststores);
139     }
140
141     private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
142         when(passwordsPathsProvider.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).thenReturn(truststoresPasswords);
143     }
144
145     private void mockKeystoreCopierSourcePaths(List<String> paths) throws TruststoresPathsProviderException {
146         when(copierPathsReader.get(KEYSTORE_SOURCE_PATHS_ENV)).thenReturn(paths);
147     }
148
149     private void mockKeystoreCopierDestinationPaths(List<String> paths) throws TruststoresPathsProviderException {
150         when(copierPathsReader.get(KEYSTORE_DESTINATION_PATHS_ENV)).thenReturn(paths);
151     }
152
153     private List<String> createListOfPathsWithExtension(int numberOfPaths, String passwordExtension) {
154         List<String> paths = new ArrayList<>();
155         while (numberOfPaths-- > 0) {
156             paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + passwordExtension);
157         }
158         return paths;
159     }
160
161     private List<String> createListOfKeystorePairsPathsWithExtension(String path, int numberOfPaths,
162         String certExtension) {
163         List<String> paths = new ArrayList<>();
164         String passExtension = certExtension.equalsIgnoreCase(".pem") ? certExtension : ".pass";
165         while (numberOfPaths-- > 0) {
166             paths.add(path + numberOfPaths + certExtension);
167             paths.add(ANOTHER_KEYSTORE_PATH + numberOfPaths + passExtension);
168         }
169         return paths;
170     }
171
172 }