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
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.truststoremerger.configuration.path;
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;
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;
37 @ExtendWith(MockitoExtension.class)
38 class TruststoresPathsProviderTest {
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";
47 private EnvProvider envProvider;
48 private TruststoresPathsProvider truststoresPathsProvider;
52 truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator());
56 void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
57 mockTruststoresEnv(VALID_TRUSTSTORES);
59 assertThat(truststoresPathsProvider.getTruststores())
60 .containsSequence("/opt/app/certificates/truststore.jks",
61 "/opt/app/certificates/truststore.pem");
65 void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
66 mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
68 assertThat(truststoresPathsProvider.getTruststoresPasswords())
69 .containsSequence("/opt/app/certificates/truststore.pass", "");
73 void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle()
74 throws TruststoresPathsProviderException {
75 mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE);
77 assertThat(truststoresPathsProvider.getTruststoresPasswords()).containsSequence(
78 "/opt/app/certificates/truststore.pass",
80 "/etc/truststore.pass"
85 void shouldThrowExceptionWhenTruststoresEmpty() {
86 mockTruststoresEnv("");
88 assertThatExceptionOfType(TruststoresPathsProviderException.class)
89 .isThrownBy(truststoresPathsProvider::getTruststores);
93 void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
94 mockTruststoresEnv(INVALID_TRUSTSTORES);
96 assertThatExceptionOfType(TruststoresPathsProviderException.class)
97 .isThrownBy(truststoresPathsProvider::getTruststores);
101 void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
102 mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
104 assertThatExceptionOfType(TruststoresPathsProviderException.class)
105 .isThrownBy(truststoresPathsProvider::getTruststoresPasswords);
108 private void mockTruststoresEnv(String truststores) {
109 mockEnv(truststores, TRUSTSTORES_PATHS_ENV);
112 private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
113 mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_PATHS_ENV);
116 private void mockEnv(String envValue, String envName) {
117 when(envProvider.getEnv(envName)).thenReturn(Optional.of(envValue));