Fix sonar issues
[oom/platform/cert-service.git] / certServicePostProcessor / src / test / java / org / onap / oom / certservice / postprocessor / configuration / path / DelimitedPathsSplitterTest.java
1 /*============LICENSE_START=======================================================
2  * oom-truststore-merger
3  * ================================================================================
4  * Copyright (C) 2020-2021 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.certservice.postprocessor.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.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS;
25 import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PATHS;
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.certservice.postprocessor.configuration.exception.CertificatesPathsValidationException;
33 import org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable;
34
35 @ExtendWith(MockitoExtension.class)
36 class DelimitedPathsSplitterTest {
37
38     private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
39     private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:";
40     private static final String VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE = "/opt/app/certificates/truststore.pass::/etc/truststore.pass";
41     private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
42     private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
43
44     private DelimitedPathsSplitter delimitedPathsSplitter;
45
46     @BeforeEach
47     void setUp() {
48         delimitedPathsSplitter = new DelimitedPathsSplitter();
49     }
50
51     @Test
52     void shouldReturnCorrectListWhenTruststoresValid() {
53         // when, then
54         assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS, Optional.of(VALID_TRUSTSTORES)))
55             .containsSequence("/opt/app/certificates/truststore.jks",
56                 "/opt/app/certificates/truststore.pem");
57     }
58
59     @Test
60     void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() {
61         // when, then
62         assertCorrectExceptionIsThrownFor(TRUSTSTORES_PATHS, "");
63     }
64
65     @Test
66     void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
67         // when, then
68         assertCorrectExceptionIsThrownFor(TRUSTSTORES_PATHS, INVALID_TRUSTSTORES);
69     }
70
71     @Test
72     void shouldReturnCorrectListWhenTruststoresPasswordsValid() {
73         // when, then
74         assertThat(delimitedPathsSplitter
75             .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS, Optional.of(VALID_TRUSTSTORES_PASSWORDS)))
76             .containsSequence("/opt/app/certificates/truststore.pass", "");
77     }
78
79     @Test
80     void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() {
81         // when, then
82         assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS,
83             Optional.of(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE))).containsSequence(
84             "/opt/app/certificates/truststore.pass",
85             "",
86             "/etc/truststore.pass"
87         );
88     }
89
90     @Test
91     void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() {
92         // when, then
93         assertCorrectExceptionIsThrownFor(TRUSTSTORES_PASSWORDS_PATHS, "");
94     }
95
96     @Test
97     void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
98         // when, then
99         assertCorrectExceptionIsThrownFor(TRUSTSTORES_PASSWORDS_PATHS, INVALID_TRUSTSTORES_PASSWORDS);
100     }
101
102     private void assertCorrectExceptionIsThrownFor(EnvVariable envVariable, String envValue) {
103         final Optional<String> envValueOptional = Optional.of(envValue);
104         assertThatExceptionOfType(CertificatesPathsValidationException.class)
105             .isThrownBy(() -> delimitedPathsSplitter
106                 .getValidatedPaths(envVariable, envValueOptional));
107     }
108 }