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.aaf.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.aaf.certservice.certification.configuration.model.Authentication;
38 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
39 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
41 @ExtendWith(MockitoExtension.class)
42 class CmpServersConfigTest {
44 private static final String ERROR_MESSAGE = "Exception occurred during CMP Servers configuration loading";
45 private static final String APP_CONFIG_PATH = "/fake/path/to/config";
46 private static final List<Cmpv2Server> SAMPLE_CMP_SERVERS = generateTestConfiguration();
49 private CmpServersConfigLoader cmpServersConfigLoader;
51 private CmpServersConfig cmpServersConfig;
55 cmpServersConfig = new CmpServersConfig(APP_CONFIG_PATH, cmpServersConfigLoader);
59 void shouldCallLoaderWithPathFromPropertiesWhenCreated() throws CmpServersConfigLoadingException {
61 this.cmpServersConfig.init(); // Manual PostConstruct call
64 Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH));
68 void shouldReturnLoadedServersWhenGetCalled() throws CmpServersConfigLoadingException {
70 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
71 this.cmpServersConfig.init(); // Manual PostConstruct call
74 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
77 assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
81 void shouldReturnLoadedServersAfterReloadWhenGetCalled() throws CmpServersConfigLoadingException {
83 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
84 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
85 assertThat(receivedCmpServers).isNull();
88 this.cmpServersConfig.reloadConfiguration();
89 receivedCmpServers = this.cmpServersConfig.getCmpServers();
92 assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
96 void shouldRethrowExceptionWhenReloaded() throws CmpServersConfigLoadingException {
98 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(
103 CmpServersConfigLoadingException.class,
104 () -> cmpServersConfig.reloadConfiguration());
108 void shouldPassMessageToRethrownErrorWhenReloadingFails() throws CmpServersConfigLoadingException {
110 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
113 Exception exception = assertThrows(
114 CmpServersConfigLoadingException.class,
115 () -> cmpServersConfig.reloadConfiguration());
118 assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
122 void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() throws CmpServersConfigLoadingException {
124 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
125 this.cmpServersConfig.init(); // Manual PostConstruct call
128 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
131 receivedCmpServers.forEach((server) -> assertThat(server.toString())
133 server.getAuthentication().getIak(),
134 server.getAuthentication().getRv()
139 void shouldRethrowErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
141 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
145 CmpServersConfigLoadingException.class,
146 () -> cmpServersConfig.loadConfiguration());
150 void shouldPassMessageToRethrownErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
152 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
155 Exception exception = assertThrows(
156 CmpServersConfigLoadingException.class,
157 () -> cmpServersConfig.loadConfiguration());
160 assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
164 void shouldBeNotReadyWhenCreated() {
165 assertThat(cmpServersConfig.isReady()).isFalse();
169 void shouldBeReadyWhenSuccessfullyInitialized() {
171 this.cmpServersConfig.init(); // Manual PostConstruct call
174 assertThat(cmpServersConfig.isReady()).isTrue();
178 void shouldNotBeReadyWhenFailedToInitialize() throws CmpServersConfigLoadingException {
180 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
183 this.cmpServersConfig.init(); // Manual PostConstruct call
186 assertThat(cmpServersConfig.isReady()).isFalse();
190 void shouldBeReadyWhenSuccessfullyReloaded() throws CmpServersConfigLoadingException {
192 this.cmpServersConfig.reloadConfiguration();
195 assertThat(cmpServersConfig.isReady()).isTrue();
199 void shouldNotBeReadyWhenFailedToReload() throws CmpServersConfigLoadingException {
201 Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
205 CmpServersConfigLoadingException.class,
206 () -> cmpServersConfig.loadConfiguration());
209 assertThat(cmpServersConfig.isReady()).isFalse();
212 private static List<Cmpv2Server> generateTestConfiguration() {
213 Cmpv2Server testServer1 = new Cmpv2Server();
214 testServer1.setCaName("TEST_CA1");
215 testServer1.setIssuerDN(new X500Name("CN=testIssuer"));
216 testServer1.setUrl("http://test.ca.server");
217 Authentication testAuthentication1 = new Authentication();
218 testAuthentication1.setIak("testIak");
219 testAuthentication1.setRv("testRv");
220 testServer1.setAuthentication(testAuthentication1);
221 testServer1.setCaMode(CaMode.RA);
223 Cmpv2Server testServer2 = new Cmpv2Server();
224 testServer2.setCaName("TEST_CA2");
225 testServer2.setIssuerDN(new X500Name("CN=testIssuer2"));
226 testServer2.setUrl("http://test.ca.server");
227 Authentication testAuthentication2 = new Authentication();
228 testAuthentication2.setIak("test2Iak");
229 testAuthentication2.setRv("test2Rv");
230 testServer2.setAuthentication(testAuthentication2);
231 testServer2.setCaMode(CaMode.CLIENT);
233 return List.of(testServer1, testServer2);