2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.oom.certservice.certification.configuration;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.startsWith;
28 import java.util.List;
30 import org.bouncycastle.asn1.x500.X500Name;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.onap.oom.certservice.certification.configuration.model.Authentication;
38 import org.onap.oom.certservice.certification.configuration.model.Cmpv2Server;
40 @ExtendWith(MockitoExtension.class)
41 class CmpServersConfigTest {
43 private static final String ERROR_MESSAGE = "Exception occurred during CMP Servers configuration loading";
44 private static final String APP_CONFIG_PATH = "/fake/path/to/config";
45 private static final List<Cmpv2Server> SAMPLE_CMP_SERVERS = generateTestConfiguration();
48 private CmpServersConfigLoader cmpServersConfigLoader;
50 private CmpServersConfig cmpServersConfig;
54 cmpServersConfig = new CmpServersConfig(APP_CONFIG_PATH, cmpServersConfigLoader);
58 void shouldCallLoaderWithPathFromPropertiesWhenCreated() throws CmpServersConfigLoadingException {
60 this.cmpServersConfig.init(); // Manual PostConstruct call
63 Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH));
67 void shouldReturnLoadedServersWhenGetCalled() throws CmpServersConfigLoadingException {
69 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
70 this.cmpServersConfig.init(); // Manual PostConstruct call
73 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
76 assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
80 void shouldReturnLoadedServersAfterReloadWhenGetCalled() throws CmpServersConfigLoadingException {
82 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
83 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
84 assertThat(receivedCmpServers).isNull();
87 this.cmpServersConfig.reloadConfiguration();
88 receivedCmpServers = this.cmpServersConfig.getCmpServers();
91 assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
95 void shouldRethrowExceptionWhenReloaded() throws CmpServersConfigLoadingException {
97 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(
102 CmpServersConfigLoadingException.class,
103 () -> cmpServersConfig.reloadConfiguration());
107 void shouldPassMessageToRethrownErrorWhenReloadingFails() throws CmpServersConfigLoadingException {
109 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
112 Exception exception = assertThrows(
113 CmpServersConfigLoadingException.class,
114 () -> cmpServersConfig.reloadConfiguration());
117 assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
121 void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() throws CmpServersConfigLoadingException {
123 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
124 this.cmpServersConfig.init(); // Manual PostConstruct call
127 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
130 receivedCmpServers.forEach((server) -> assertThat(server.toString())
132 server.getAuthentication().getIak(),
133 server.getAuthentication().getRv()
138 void shouldRethrowErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
140 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
144 CmpServersConfigLoadingException.class,
145 () -> cmpServersConfig.loadConfiguration());
149 void shouldPassMessageToRethrownErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
151 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
154 Exception exception = assertThrows(
155 CmpServersConfigLoadingException.class,
156 () -> cmpServersConfig.loadConfiguration());
159 assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
163 void shouldBeNotReadyWhenCreated() {
164 assertThat(cmpServersConfig.isReady()).isFalse();
168 void shouldBeReadyWhenSuccessfullyInitialized() {
170 this.cmpServersConfig.init(); // Manual PostConstruct call
173 assertThat(cmpServersConfig.isReady()).isTrue();
177 void shouldNotBeReadyWhenFailedToInitialize() throws CmpServersConfigLoadingException {
179 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
182 this.cmpServersConfig.init(); // Manual PostConstruct call
185 assertThat(cmpServersConfig.isReady()).isFalse();
189 void shouldBeReadyWhenSuccessfullyReloaded() throws CmpServersConfigLoadingException {
191 this.cmpServersConfig.reloadConfiguration();
194 assertThat(cmpServersConfig.isReady()).isTrue();
198 void shouldNotBeReadyWhenFailedToReload() throws CmpServersConfigLoadingException {
200 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
204 CmpServersConfigLoadingException.class,
205 () -> cmpServersConfig.loadConfiguration());
208 assertThat(cmpServersConfig.isReady()).isFalse();
211 private static List<Cmpv2Server> generateTestConfiguration() {
212 Cmpv2Server testServer1 = new Cmpv2Server();
213 testServer1.setCaName("TEST_CA1");
214 testServer1.setIssuerDN(new X500Name("CN=testIssuer"));
215 testServer1.setUrl("http://test.ca.server");
216 Authentication testAuthentication1 = new Authentication();
217 testAuthentication1.setIak("testIak");
218 testAuthentication1.setRv("testRv");
219 testServer1.setAuthentication(testAuthentication1);
221 Cmpv2Server testServer2 = new Cmpv2Server();
222 testServer2.setCaName("TEST_CA2");
223 testServer2.setIssuerDN(new X500Name("CN=testIssuer2"));
224 testServer2.setUrl("http://test.ca.server");
225 Authentication testAuthentication2 = new Authentication();
226 testAuthentication2.setIak("test2Iak");
227 testAuthentication2.setRv("test2Rv");
228 testServer2.setAuthentication(testAuthentication2);
230 return List.of(testServer1, testServer2);