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 org.bouncycastle.asn1.x500.X500Name;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
31 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
32 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
33 import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
35 import java.util.List;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.ArgumentMatchers.startsWith;
41 @ExtendWith(MockitoExtension.class)
42 class CmpServersConfigTest {
44 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() {
60 this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call
61 Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH));
65 void shouldReturnLoadedServersWhenGetCalled() {
67 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
68 this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call
71 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
74 assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
78 void shouldReturnLoadedServersAfterRefreshWhenGetCalled() {
80 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
82 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
83 assertThat(receivedCmpServers).isNull();
85 this.cmpServersConfig.onRefreshScope(new RefreshScopeRefreshedEvent());
88 receivedCmpServers = this.cmpServersConfig.getCmpServers();
91 assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
95 void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() {
97 Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
98 this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call
101 List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
104 receivedCmpServers.forEach((server)-> assertThat(server.toString())
106 server.getAuthentication().getIak(),
107 server.getAuthentication().getRv()
111 private static List<Cmpv2Server> generateTestConfiguration() {
112 Cmpv2Server testServer1 = new Cmpv2Server();
113 testServer1.setCaName("TEST_CA1");
114 testServer1.setIssuerDN(new X500Name("CN=testIssuer"));
115 testServer1.setUrl("http://test.ca.server");
116 Authentication testAuthentication1 = new Authentication();
117 testAuthentication1.setIak("testIak");
118 testAuthentication1.setRv("testRv");
119 testServer1.setAuthentication(testAuthentication1);
120 testServer1.setCaMode(CaMode.RA);
122 Cmpv2Server testServer2 = new Cmpv2Server();
123 testServer2.setCaName("TEST_CA2");
124 testServer2.setIssuerDN(new X500Name("CN=testIssuer2"));
125 testServer2.setUrl("http://test.ca.server");
126 Authentication testAuthentication2 = new Authentication();
127 testAuthentication2.setIak("test2Iak");
128 testAuthentication2.setRv("test2Rv");
129 testServer2.setAuthentication(testAuthentication2);
130 testServer2.setCaMode(CaMode.CLIENT);
132 return List.of(testServer1, testServer2);