[OOM-CMPv2] Create KeystoreCopier
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / configuration / path / DelimitedPathsReaderTest.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.path;
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.TRUSTSTORES_PASSWORDS_PATHS_ENV;
26 import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV;
27 import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths;
28 import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths;
29
30 import java.util.Optional;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException;
36 import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider;
37
38 @ExtendWith(MockitoExtension.class)
39 class DelimitedPathsReaderTest {
40
41     private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
42     private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:";
43     private static final String VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE = "/opt/app/certificates/truststore.pass::/etc/truststore.pass";
44     private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
45     private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
46
47     @Mock
48     private EnvProvider envProvider;
49     private DelimitedPathsReader delimitedPathsReader;
50
51     @Test
52     void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
53         // given
54         delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths());
55         mockTruststoresEnv(VALID_TRUSTSTORES);
56
57         // when, then
58         assertThat(delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV))
59             .containsSequence("/opt/app/certificates/truststore.jks",
60                 "/opt/app/certificates/truststore.pem");
61     }
62
63     @Test
64     void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() {
65         // given
66         delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths());
67         mockTruststoresEnv("");
68
69         // when, then
70         assertThatExceptionOfType(TruststoresPathsProviderException.class)
71             .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV));
72     }
73
74     @Test
75     void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
76         // given
77         delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths());
78         mockTruststoresEnv(INVALID_TRUSTSTORES);
79
80         // when, then
81         assertThatExceptionOfType(TruststoresPathsProviderException.class)
82             .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV));
83     }
84
85     @Test
86     void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
87         // given
88         delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths());
89         mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
90
91         // when, then
92         assertThat(delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV))
93             .containsSequence("/opt/app/certificates/truststore.pass", "");
94     }
95
96     @Test
97     void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle()
98         throws TruststoresPathsProviderException {
99         // given
100         delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths());
101         mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE);
102
103         // when, then
104         assertThat(delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).containsSequence(
105             "/opt/app/certificates/truststore.pass",
106             "",
107             "/etc/truststore.pass"
108         );
109     }
110
111     @Test
112     void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() {
113         // given
114         delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths());
115         mockTruststoresPasswordsEnv("");
116
117         // when, then
118         assertThatExceptionOfType(TruststoresPathsProviderException.class)
119             .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV));
120     }
121
122     @Test
123     void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
124         // given
125         delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths());
126         mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
127
128         // when, then
129         assertThatExceptionOfType(TruststoresPathsProviderException.class)
130             .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV));
131     }
132
133     private void mockTruststoresEnv(String truststores) {
134         mockEnv(TRUSTSTORES_PATHS_ENV, truststores);
135     }
136
137     private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
138         mockEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV, truststoresPasswords);
139     }
140
141     private void mockEnv(String envName, String truststores) {
142         when(envProvider.getEnv(envName)).thenReturn(Optional.of(truststores));
143     }
144 }