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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.oom.truststoremerger.configuration;
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;
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;
42 @ExtendWith(MockitoExtension.class)
43 class MergerConfigurationProviderTest {
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";
53 private DelimitedPathsReader certificatesPathsProvider;
55 private DelimitedPathsReader passwordsPathsProvider;
57 private DelimitedPathsReader copierPathsReader;
58 private AppConfigurationProvider factory;
62 factory = new AppConfigurationProvider(certificatesPathsProvider, passwordsPathsProvider, copierPathsReader);
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);
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);
79 AppConfiguration configuration = factory.createConfiguration();
81 assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
82 assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
83 assertThat(configuration.getSourceKeystorePaths()).containsAll(sourceKeystoresPairPaths);
84 assertThat(configuration.getDestinationKeystorePaths()).containsAll(destKeystoresPairPaths);
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);
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);
102 assertThatExceptionOfType(MergerConfigurationException.class)
103 .isThrownBy(factory::createConfiguration);
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);
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);
121 assertThatExceptionOfType(MergerConfigurationException.class)
122 .isThrownBy(factory::createConfiguration);
125 private void mockTruststorePaths(List<String> truststores, List<String> truststoresPasswords)
126 throws TruststoresPathsProviderException {
127 mockTruststores(truststores);
128 mockTruststoresPasswords(truststoresPasswords);
131 private void mockKeystorePaths(List<String> sourceKeystoresPairPaths, List<String> destKeystoresPairPaths)
132 throws TruststoresPathsProviderException {
133 mockKeystoreCopierSourcePaths(sourceKeystoresPairPaths);
134 mockKeystoreCopierDestinationPaths(destKeystoresPairPaths);
137 private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
138 when(certificatesPathsProvider.get(TRUSTSTORES_PATHS_ENV)).thenReturn(truststores);
141 private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
142 when(passwordsPathsProvider.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).thenReturn(truststoresPasswords);
145 private void mockKeystoreCopierSourcePaths(List<String> paths) throws TruststoresPathsProviderException {
146 when(copierPathsReader.get(KEYSTORE_SOURCE_PATHS_ENV)).thenReturn(paths);
149 private void mockKeystoreCopierDestinationPaths(List<String> paths) throws TruststoresPathsProviderException {
150 when(copierPathsReader.get(KEYSTORE_DESTINATION_PATHS_ENV)).thenReturn(paths);
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);
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);