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;
29 import org.bouncycastle.asn1.x500.X500Name;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
37 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
38 import org.onap.aaf.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);
162 private static List<Cmpv2Server> generateTestConfiguration() {
163 Cmpv2Server testServer1 = new Cmpv2Server();
164 testServer1.setCaName("TEST_CA1");
165 testServer1.setIssuerDN(new X500Name("CN=testIssuer"));
166 testServer1.setUrl("http://test.ca.server");
167 Authentication testAuthentication1 = new Authentication();
168 testAuthentication1.setIak("testIak");
169 testAuthentication1.setRv("testRv");
170 testServer1.setAuthentication(testAuthentication1);
171 testServer1.setCaMode(CaMode.RA);
173 Cmpv2Server testServer2 = new Cmpv2Server();
174 testServer2.setCaName("TEST_CA2");
175 testServer2.setIssuerDN(new X500Name("CN=testIssuer2"));
176 testServer2.setUrl("http://test.ca.server");
177 Authentication testAuthentication2 = new Authentication();
178 testAuthentication2.setIak("test2Iak");
179 testAuthentication2.setRv("test2Rv");
180 testServer2.setAuthentication(testAuthentication2);
181 testServer2.setCaMode(CaMode.CLIENT);
183 return List.of(testServer1, testServer2);