[OOM-CPMv2] Allow optional input parameters
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / configuration / AppConfigurationProviderTest.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.model.EnvVariable.KEYSTORE_DESTINATION_PATHS_ENV;
26 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.KEYSTORE_SOURCE_PATHS_ENV;
27 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS_ENV;
28 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PATHS_ENV;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Optional;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.onap.oom.truststoremerger.configuration.exception.CertificatesPathsValidationException;
39 import org.onap.oom.truststoremerger.configuration.exception.ConfigurationException;
40 import org.onap.oom.truststoremerger.configuration.model.AppConfiguration;
41 import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsSplitter;
42 import org.onap.oom.truststoremerger.configuration.path.env.EnvReader;
43
44 @ExtendWith(MockitoExtension.class)
45 class AppConfigurationProviderTest {
46
47     private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_";
48     private static final String JKS_EXTENSION = ".jks";
49     private static final String PASS_EXTENSION = ".pass";
50     private static final String TRUSTSTORES_PATHS = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
51     private static final String TRUSTSTORES_PASSWORDS_PATHS = "/opt/app/certificates/truststore.pass:/trust.pass";
52
53     @Mock
54     private DelimitedPathsSplitter pathsSplitter;
55     @Mock
56     private EnvReader envReader;
57     private AppConfigurationProvider provider;
58
59     @BeforeEach
60     void setUp() {
61         provider = new AppConfigurationProvider(pathsSplitter, envReader);
62     }
63
64     @Test
65     void shouldThrowExceptionWhenMandatoryEnvNotPresent() {
66         // given
67         when(envReader.getEnv(TRUSTSTORES_PATHS_ENV.name())).thenReturn(Optional.empty());
68         // when, then
69         assertThatExceptionOfType(ConfigurationException.class).isThrownBy(() -> provider.createConfiguration())
70             .withMessageContaining(TRUSTSTORES_PATHS_ENV + " mandatory environment variable is not defined");
71     }
72
73     @Test
74     void shouldThrowExceptionWhenTrustorePathsSizesDoNotMatch() {
75         // given
76         List<String> truststores = createListOfPathsWithExtension(2, JKS_EXTENSION);
77         List<String> truststoresPasswords = createListOfPathsWithExtension(1, PASS_EXTENSION);
78
79         mockTruststorePaths(truststores, truststoresPasswords);
80         // when
81         assertThatExceptionOfType(ConfigurationException.class)
82             .isThrownBy(() -> provider.createConfiguration())
83             .withMessageContaining("Size of " + TRUSTSTORES_PATHS_ENV
84                 + " does not match size of " + TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variables");
85     }
86
87     @Test
88     void shouldReturnEmptyListWhenOptionalEnvNotPresent() {
89         // given
90         List<String> truststores = createListOfPathsWithExtension(2, JKS_EXTENSION);
91         List<String> truststoresPasswords = createListOfPathsWithExtension(2, PASS_EXTENSION);
92         mockTruststorePaths(truststores, truststoresPasswords);
93         mockKeystorePaths(Optional.empty(), Optional.empty());
94         // when
95         AppConfiguration paths = provider.createConfiguration();
96         // then
97         assertThat(paths.getDestinationKeystorePaths()).isEmpty();
98         assertThat(paths.getSourceKeystorePaths()).isEmpty();
99     }
100
101     private void mockTruststorePaths(List<String> truststores, List<String> truststoresPasswords) {
102         mockTruststores(truststores);
103         mockTruststoresPasswords(truststoresPasswords);
104     }
105
106     private void mockKeystorePaths(Optional<String> sourceKeystoresPairPaths, Optional<String> destKeystoresPairPaths) {
107         mockKeystoreCopierSourcePaths(sourceKeystoresPairPaths);
108         mockKeystoreCopierDestinationPaths(destKeystoresPairPaths);
109     }
110
111     private void mockTruststores(List<String> truststores) throws CertificatesPathsValidationException {
112         when(envReader.getEnv(TRUSTSTORES_PATHS_ENV.name())).thenReturn(Optional.of(TRUSTSTORES_PATHS));
113         when(pathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of(TRUSTSTORES_PATHS)))
114             .thenReturn(truststores);
115     }
116
117     private void mockTruststoresPasswords(List<String> truststoresPasswords)
118         throws CertificatesPathsValidationException {
119         Optional<String> passwordsPaths = Optional.of(TRUSTSTORES_PASSWORDS_PATHS);
120         when(envReader.getEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV.name())).thenReturn(passwordsPaths);
121         when(pathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, passwordsPaths))
122             .thenReturn(truststoresPasswords);
123     }
124
125     private void mockKeystoreCopierSourcePaths(Optional<String> paths) {
126         when(envReader.getEnv(KEYSTORE_SOURCE_PATHS_ENV.name())).thenReturn(paths);
127     }
128
129     private void mockKeystoreCopierDestinationPaths(Optional<String> paths) {
130         when(envReader.getEnv(KEYSTORE_DESTINATION_PATHS_ENV.name())).thenReturn(paths);
131     }
132
133     private List<String> createListOfPathsWithExtension(int numberOfPaths, String passwordExtension) {
134         List<String> paths = new ArrayList<>();
135         while (numberOfPaths-- > 0) {
136             paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + passwordExtension);
137         }
138         return paths;
139     }
140
141 }