summaryrefslogtreecommitdiffstats
path: root/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java
blob: 43b7b9e147ec8cf927228f0fcb1585125b5afc15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*============LICENSE_START=======================================================
 * oom-truststore-merger
 * ================================================================================
 * Copyright (C) 2020 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.oom.truststoremerger.configuration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProvider;
import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProviderException;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class MergerConfigurationFactoryTest {

    private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_";
    private static final String TRUSTSTORE_EXTENSION = ".jks";
    private static final String PASSWORD_EXTENSION = ".pass";

    @Mock
    private TruststoresPathsProvider pathsProvider;
    private MergerConfigurationFactory factory;

    @BeforeEach
    void setUp() {
        factory = new MergerConfigurationFactory(pathsProvider);
    }

    @Test
    void shouldReturnConfigurationWithCorrectPaths() throws TruststoresPathsProviderException, MergerConfigurationException {
        int numberOfPaths = 5;
        List<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION);
        List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION);
        mockPaths(truststoresPaths, truststorePasswordPaths);

        MergerConfiguration configuration = factory.createConfiguration();

        assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
        assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
    }

    @Test
    void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException {
        int numberOfTruststores = 5;
        int numberOfTruststoresPasswords = 4;
        List<String> truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION);
        List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION);
        mockPaths(truststoresPaths, truststorePasswordPaths);

        assertThatExceptionOfType(MergerConfigurationException.class)
                .isThrownBy(factory::createConfiguration);
    }

    private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException {
        mockTruststores(truststores);
        mockTruststoresPasswords(truststoresPasswords);
    }

    private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
        when(pathsProvider.getTruststores()).thenReturn(truststores);
    }

    private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
        when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords);
    }

    private List<String> createListOfPathsWithExtension(int numberOfPaths, String password_extension) {
        List<String> paths = new ArrayList<>();
        while (numberOfPaths-- > 0) {
            paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension);
        }
        return paths;
    }
}