Update version from 1.2.0 to 2.0.0
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / configuration / MergerConfigurationProviderTest.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
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;
34
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;
38
39 @ExtendWith(MockitoExtension.class)
40 class MergerConfigurationProviderTest {
41
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";
45
46     @Mock
47     private TruststoresPathsProvider pathsProvider;
48     private MergerConfigurationProvider factory;
49
50     @BeforeEach
51     void setUp() {
52         factory = new MergerConfigurationProvider(pathsProvider);
53     }
54
55     @Test
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);
61
62         MergerConfiguration configuration = factory.createConfiguration();
63
64         assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
65         assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
66     }
67
68     @Test
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);
75
76         assertThatExceptionOfType(MergerConfigurationException.class)
77                 .isThrownBy(factory::createConfiguration);
78     }
79
80     private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException {
81         mockTruststores(truststores);
82         mockTruststoresPasswords(truststoresPasswords);
83     }
84
85     private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
86         when(pathsProvider.getTruststores()).thenReturn(truststores);
87     }
88
89     private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
90         when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords);
91     }
92
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);
97         }
98         return paths;
99     }
100 }