Update version from 1.2.0 to 2.0.0
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / configuration / MergerConfigurationFactoryTest.java
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
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.truststoremerger.configuration;
21
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.path.TruststoresPathsProvider;
28 import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProviderException;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
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;
36
37 @ExtendWith(MockitoExtension.class)
38 class MergerConfigurationFactoryTest {
39
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";
43
44     @Mock
45     private TruststoresPathsProvider pathsProvider;
46     private MergerConfigurationFactory factory;
47
48     @BeforeEach
49     void setUp() {
50         factory = new MergerConfigurationFactory(pathsProvider);
51     }
52
53     @Test
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);
59
60         MergerConfiguration configuration = factory.createConfiguration();
61
62         assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
63         assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
64     }
65
66     @Test
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);
73
74         assertThatExceptionOfType(MergerConfigurationException.class)
75                 .isThrownBy(factory::createConfiguration);
76     }
77
78     private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException {
79         mockTruststores(truststores);
80         mockTruststoresPasswords(truststoresPasswords);
81     }
82
83     private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
84         when(pathsProvider.getTruststores()).thenReturn(truststores);
85     }
86
87     private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
88         when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords);
89     }
90
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);
95         }
96         return paths;
97     }
98 }