aboutsummaryrefslogtreecommitdiffstats
path: root/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigTest.java
blob: 511c396664f4b7943e5cace72d8c4710dfc5e40d (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * ============LICENSE_START=======================================================
 * PROJECT
 * ================================================================================
 * 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.aaf.certservice.certification.configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.startsWith;

import java.util.List;

import org.bouncycastle.asn1.x500.X500Name;
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.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.aaf.certservice.certification.configuration.model.Authentication;
import org.onap.aaf.certservice.certification.configuration.model.CaMode;
import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;

@ExtendWith(MockitoExtension.class)
class CmpServersConfigTest {

    private static final String ERROR_MESSAGE = "Exception occurred during CMP Servers configuration loading";
    private static final String APP_CONFIG_PATH = "/fake/path/to/config";
    private static final List<Cmpv2Server> SAMPLE_CMP_SERVERS = generateTestConfiguration();

    @Mock
    private CmpServersConfigLoader cmpServersConfigLoader;

    private CmpServersConfig cmpServersConfig;

    @BeforeEach
    void setUp() {
        cmpServersConfig = new CmpServersConfig(APP_CONFIG_PATH, cmpServersConfigLoader);
    }

    @Test
    void shouldCallLoaderWithPathFromPropertiesWhenCreated() throws CmpServersConfigLoadingException {
        // When
        this.cmpServersConfig.init();      // Manual PostConstruct call

        // Then
        Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH));
    }

    @Test
    void shouldReturnLoadedServersWhenGetCalled() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
        this.cmpServersConfig.init();      // Manual PostConstruct call

        // When
        List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();

        // Then
        assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
    }

    @Test
    void shouldReturnLoadedServersAfterReloadWhenGetCalled() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
        List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
        assertThat(receivedCmpServers).isNull();

        // When
        this.cmpServersConfig.reloadConfiguration();
        receivedCmpServers = this.cmpServersConfig.getCmpServers();

        // Then
        assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
    }

    @Test
    void shouldRethrowExceptionWhenReloaded() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(
                ERROR_MESSAGE));

        // Then
        assertThrows(
                CmpServersConfigLoadingException.class,
                () -> cmpServersConfig.reloadConfiguration());
    }

    @Test
    void shouldPassMessageToRethrownErrorWhenReloadingFails() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));

        // When
        Exception exception = assertThrows(
                CmpServersConfigLoadingException.class,
                () -> cmpServersConfig.reloadConfiguration());

        // Then
        assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
    }

    @Test
    void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
        this.cmpServersConfig.init();      // Manual PostConstruct call

        // When
        List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();

        // Then
        receivedCmpServers.forEach((server) -> assertThat(server.toString())
                .doesNotContain(
                        server.getAuthentication().getIak(),
                        server.getAuthentication().getRv()
                ));
    }

    @Test
    void shouldRethrowErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));

        // Then
        assertThrows(
                CmpServersConfigLoadingException.class,
                () -> cmpServersConfig.loadConfiguration());
    }

    @Test
    void shouldPassMessageToRethrownErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));

        // When
        Exception exception = assertThrows(
                CmpServersConfigLoadingException.class,
                () -> cmpServersConfig.loadConfiguration());

        // Then
        assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
    }

    @Test
    void shouldBeNotReadyWhenCreated() {
        assertThat(cmpServersConfig.isReady()).isFalse();
    }

    @Test
    void shouldBeReadyWhenSuccessfullyInitialized() {
        // When
        this.cmpServersConfig.init();      // Manual PostConstruct call

        // Then
        assertThat(cmpServersConfig.isReady()).isTrue();
    }

    @Test
    void shouldNotBeReadyWhenFailedToInitialize() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));

        // When
        this.cmpServersConfig.init();      // Manual PostConstruct call

        // Then
        assertThat(cmpServersConfig.isReady()).isFalse();
    }

    @Test
    void shouldBeReadyWhenSuccessfullyReloaded() throws CmpServersConfigLoadingException {
        // When
        this.cmpServersConfig.reloadConfiguration();

        // Then
        assertThat(cmpServersConfig.isReady()).isTrue();
    }

    @Test
    void shouldNotBeReadyWhenFailedToReload() throws CmpServersConfigLoadingException {
        // Given
        Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));

        // When
        assertThrows(
                CmpServersConfigLoadingException.class,
                () -> cmpServersConfig.loadConfiguration());

        // Then
        assertThat(cmpServersConfig.isReady()).isFalse();
    }

    private static List<Cmpv2Server> generateTestConfiguration() {
        Cmpv2Server testServer1 = new Cmpv2Server();
        testServer1.setCaName("TEST_CA1");
        testServer1.setIssuerDN(new X500Name("CN=testIssuer"));
        testServer1.setUrl("http://test.ca.server");
        Authentication testAuthentication1 = new Authentication();
        testAuthentication1.setIak("testIak");
        testAuthentication1.setRv("testRv");
        testServer1.setAuthentication(testAuthentication1);
        testServer1.setCaMode(CaMode.RA);

        Cmpv2Server testServer2 = new Cmpv2Server();
        testServer2.setCaName("TEST_CA2");
        testServer2.setIssuerDN(new X500Name("CN=testIssuer2"));
        testServer2.setUrl("http://test.ca.server");
        Authentication testAuthentication2 = new Authentication();
        testAuthentication2.setIak("test2Iak");
        testAuthentication2.setRv("test2Rv");
        testServer2.setAuthentication(testAuthentication2);
        testServer2.setCaMode(CaMode.CLIENT);

        return List.of(testServer1, testServer2);
    }

}