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;
27 import org.onap.oom.truststoremerger.certification.file.TruststoresPathsProvider;
28 import org.onap.oom.truststoremerger.certification.file.TruststoresPathsProviderException;
30 import java.util.ArrayList;
31 import java.util.List;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
35 import static org.mockito.Mockito.when;
37 @ExtendWith(MockitoExtension.class)
38 class MergerConfigurationFactoryTest {
40 private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_";
41 private static final String TRUSTSTORE_EXTENSION = ".jks";
42 private static final String PASSWORD_EXTENSION = ".pass";
45 private TruststoresPathsProvider pathsProvider;
46 private MergerConfigurationFactory factory;
50 factory = new MergerConfigurationFactory(pathsProvider);
54 void shouldReturnConfigurationWithCorrectPaths() throws TruststoresPathsProviderException, MergerConfigurationException {
55 int numberOfPaths = 5;
56 List<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION);
57 List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION);
58 mockPaths(truststoresPaths, truststorePasswordPaths);
60 MergerConfiguration configuration = factory.createConfiguration();
62 assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
63 assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
67 void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException {
68 int numberOfTruststores = 5;
69 int numberOfTruststoresPasswords = 4;
70 List<String> truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION);
71 List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION);
72 mockPaths(truststoresPaths, truststorePasswordPaths);
74 assertThatExceptionOfType(MergerConfigurationException.class)
75 .isThrownBy(factory::createConfiguration);
78 private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException {
79 mockTruststores(truststores);
80 mockTruststoresPasswords(truststoresPasswords);
83 private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
84 when(pathsProvider.getTruststores()).thenReturn(truststores);
87 private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
88 when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords);
91 private List<String> createListOfPathsWithExtension(int numberOfPaths, String password_extension) {
92 List<String> paths = new ArrayList<>();
93 while (numberOfPaths-- > 0) {
94 paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension);