Add truststore merger description in readme
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / certification / 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.certification.path;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.jupiter.MockitoExtension;
27
28 import java.util.Optional;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
32 import static org.mockito.Mockito.when;
33 import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV;
34 import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV;
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 INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
43     private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
44
45     @Mock
46     private EnvProvider envProvider;
47     private TruststoresPathsProvider truststoresPathsProvider;
48
49     @BeforeEach
50     void setUp() {
51         truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator());
52     }
53
54     @Test
55     void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
56         mockTruststoresEnv(VALID_TRUSTSTORES);
57
58         assertThat(truststoresPathsProvider.getTruststores())
59                 .contains("/opt/app/certificates/truststore.jks",
60                         "/opt/app/certificates/truststore.pem");
61     }
62
63     @Test
64     void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
65         mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
66
67         assertThat(truststoresPathsProvider.getTruststoresPasswords())
68                 .contains("/opt/app/certificates/truststore.pass",
69                         "");
70     }
71
72     @Test
73     void shouldThrowExceptionWhenTruststoresEmpty() {
74         mockTruststoresEnv("");
75
76         assertThatExceptionOfType(TruststoresPathsProviderException.class)
77                 .isThrownBy(truststoresPathsProvider::getTruststores);
78     }
79
80     @Test
81     void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
82         mockTruststoresEnv(INVALID_TRUSTSTORES);
83
84         assertThatExceptionOfType(TruststoresPathsProviderException.class)
85                 .isThrownBy(truststoresPathsProvider::getTruststores);
86     }
87
88     @Test
89     void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
90         mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
91
92         assertThatExceptionOfType(TruststoresPathsProviderException.class)
93                 .isThrownBy(truststoresPathsProvider::getTruststoresPasswords);
94     }
95
96     private void mockTruststoresEnv(String truststores) {
97         mockEnv(truststores, TRUSTSTORES_PATHS_ENV);
98     }
99
100     private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
101         mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_PATHS_ENV);
102     }
103
104     private void mockEnv(String envValue, String envName) {
105         when(envProvider.getEnv(envName))
106                 .thenReturn(Optional.of(envValue));
107     }
108 }