Merge "Update version from 1.2.0 to 2.0.0"
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / configuration / path / TruststoresPathsProviderTest.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.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV;
26 import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV;
27
28 import java.util.Optional;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException;
35
36
37 @ExtendWith(MockitoExtension.class)
38 class TruststoresPathsProviderTest {
39
40     private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
41     private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:";
42     private static final String VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE = "/opt/app/certificates/truststore.pass::/etc/truststore.pass";
43     private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
44     private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
45
46     @Mock
47     private EnvProvider envProvider;
48     private TruststoresPathsProvider truststoresPathsProvider;
49
50     @BeforeEach
51     void setUp() {
52         truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator());
53     }
54
55     @Test
56     void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
57         mockTruststoresEnv(VALID_TRUSTSTORES);
58
59         assertThat(truststoresPathsProvider.getTruststores())
60             .containsSequence("/opt/app/certificates/truststore.jks",
61                 "/opt/app/certificates/truststore.pem");
62     }
63
64     @Test
65     void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
66         mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
67
68         assertThat(truststoresPathsProvider.getTruststoresPasswords())
69             .containsSequence("/opt/app/certificates/truststore.pass", "");
70     }
71
72     @Test
73     void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle()
74         throws TruststoresPathsProviderException {
75         mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE);
76
77         assertThat(truststoresPathsProvider.getTruststoresPasswords()).containsSequence(
78             "/opt/app/certificates/truststore.pass",
79             "",
80             "/etc/truststore.pass"
81         );
82     }
83
84     @Test
85     void shouldThrowExceptionWhenTruststoresEmpty() {
86         mockTruststoresEnv("");
87
88         assertThatExceptionOfType(TruststoresPathsProviderException.class)
89             .isThrownBy(truststoresPathsProvider::getTruststores);
90     }
91
92     @Test
93     void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
94         mockTruststoresEnv(INVALID_TRUSTSTORES);
95
96         assertThatExceptionOfType(TruststoresPathsProviderException.class)
97             .isThrownBy(truststoresPathsProvider::getTruststores);
98     }
99
100     @Test
101     void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
102         mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
103
104         assertThatExceptionOfType(TruststoresPathsProviderException.class)
105             .isThrownBy(truststoresPathsProvider::getTruststoresPasswords);
106     }
107
108     private void mockTruststoresEnv(String truststores) {
109         mockEnv(truststores, TRUSTSTORES_PATHS_ENV);
110     }
111
112     private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
113         mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_PATHS_ENV);
114     }
115
116     private void mockEnv(String envValue, String envName) {
117         when(envProvider.getEnv(envName)).thenReturn(Optional.of(envValue));
118     }
119 }