[OOM-CPMv2] Allow optional input parameters
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / configuration / path / DelimitedPathsSplitterTest.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.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS_ENV;
25 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PATHS_ENV;
26
27 import java.util.Optional;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.onap.oom.truststoremerger.configuration.exception.CertificatesPathsValidationException;
33
34 @ExtendWith(MockitoExtension.class)
35 class DelimitedPathsSplitterTest {
36
37     private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
38     private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:";
39     private static final String VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE = "/opt/app/certificates/truststore.pass::/etc/truststore.pass";
40     private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
41     private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
42
43     private DelimitedPathsSplitter delimitedPathsSplitter;
44
45     @BeforeEach
46     void setUp() {
47         delimitedPathsSplitter = new DelimitedPathsSplitter();
48     }
49
50     @Test
51     void shouldReturnCorrectListWhenTruststoresValid() {
52         // when, then
53         assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of(VALID_TRUSTSTORES)))
54             .containsSequence("/opt/app/certificates/truststore.jks",
55                 "/opt/app/certificates/truststore.pem");
56     }
57
58     @Test
59     void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() {
60         // when, then
61         assertThatExceptionOfType(CertificatesPathsValidationException.class)
62             .isThrownBy(() -> delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of("")));
63     }
64
65     @Test
66     void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
67         // when, then
68         assertThatExceptionOfType(CertificatesPathsValidationException.class)
69             .isThrownBy(() -> delimitedPathsSplitter
70                 .getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of(INVALID_TRUSTSTORES)));
71     }
72
73     @Test
74     void shouldReturnCorrectListWhenTruststoresPasswordsValid() {
75         // when, then
76         assertThat(delimitedPathsSplitter
77             .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, Optional.of(VALID_TRUSTSTORES_PASSWORDS)))
78             .containsSequence("/opt/app/certificates/truststore.pass", "");
79     }
80
81     @Test
82     void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() {
83         // when, then
84         assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV,
85             Optional.of(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE))).containsSequence(
86             "/opt/app/certificates/truststore.pass",
87             "",
88             "/etc/truststore.pass"
89         );
90     }
91
92     @Test
93     void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() {
94         // when, then
95         assertThatExceptionOfType(CertificatesPathsValidationException.class)
96             .isThrownBy(
97                 () -> delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, Optional.of("")));
98     }
99
100     @Test
101     void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
102         // when, then
103         assertThatExceptionOfType(CertificatesPathsValidationException.class)
104             .isThrownBy(() -> delimitedPathsSplitter
105                 .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, Optional.of(INVALID_TRUSTSTORES_PASSWORDS)));
106     }
107 }