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;
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;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.onap.oom.truststoremerger.configuration.exception.MergerConfigurationException;
31 import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException;
32 import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration;
33 import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProvider;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
37 import static org.mockito.Mockito.when;
39 @ExtendWith(MockitoExtension.class)
40 class MergerConfigurationProviderTest {
42 private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_";
43 private static final String TRUSTSTORE_EXTENSION = ".jks";
44 private static final String PASSWORD_EXTENSION = ".pass";
47 private TruststoresPathsProvider pathsProvider;
48 private MergerConfigurationProvider factory;
52 factory = new MergerConfigurationProvider(pathsProvider);
56 void shouldReturnConfigurationWithCorrectPaths() throws TruststoresPathsProviderException, MergerConfigurationException {
57 int numberOfPaths = 5;
58 List<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION);
59 List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION);
60 mockPaths(truststoresPaths, truststorePasswordPaths);
62 MergerConfiguration configuration = factory.createConfiguration();
64 assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
65 assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
69 void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException {
70 int numberOfTruststores = 5;
71 int numberOfTruststoresPasswords = 4;
72 List<String> truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION);
73 List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION);
74 mockPaths(truststoresPaths, truststorePasswordPaths);
76 assertThatExceptionOfType(MergerConfigurationException.class)
77 .isThrownBy(factory::createConfiguration);
80 private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException {
81 mockTruststores(truststores);
82 mockTruststoresPasswords(truststoresPasswords);
85 private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
86 when(pathsProvider.getTruststores()).thenReturn(truststores);
89 private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
90 when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords);
93 private List<String> createListOfPathsWithExtension(int numberOfPaths, String password_extension) {
94 List<String> paths = new ArrayList<>();
95 while (numberOfPaths-- > 0) {
96 paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension);