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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.oom.certservice.postprocessor.configuration.path;
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;
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;
35 @ExtendWith(MockitoExtension.class)
36 class DelimitedPathsSplitterTest {
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";
44 private DelimitedPathsSplitter delimitedPathsSplitter;
48 delimitedPathsSplitter = new DelimitedPathsSplitter();
52 void shouldReturnCorrectListWhenTruststoresValid() {
54 assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS, Optional.of(VALID_TRUSTSTORES)))
55 .containsSequence("/opt/app/certificates/truststore.jks",
56 "/opt/app/certificates/truststore.pem");
60 void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() {
62 assertCorrectExceptionIsThrownFor(TRUSTSTORES_PATHS, "");
66 void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
68 assertCorrectExceptionIsThrownFor(TRUSTSTORES_PATHS, INVALID_TRUSTSTORES);
72 void shouldReturnCorrectListWhenTruststoresPasswordsValid() {
74 assertThat(delimitedPathsSplitter
75 .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS, Optional.of(VALID_TRUSTSTORES_PASSWORDS)))
76 .containsSequence("/opt/app/certificates/truststore.pass", "");
80 void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() {
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",
86 "/etc/truststore.pass"
91 void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() {
93 assertCorrectExceptionIsThrownFor(TRUSTSTORES_PASSWORDS_PATHS, "");
97 void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
99 assertCorrectExceptionIsThrownFor(TRUSTSTORES_PASSWORDS_PATHS, INVALID_TRUSTSTORES_PASSWORDS);
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));