Automation adds INFO.yaml
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / configuration / CmpServersConfigTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aaf.certservice.certification.configuration;
22
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;
27
28 import java.util.List;
29
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;
40
41 @ExtendWith(MockitoExtension.class)
42 class CmpServersConfigTest {
43
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();
47
48     @Mock
49     private CmpServersConfigLoader cmpServersConfigLoader;
50
51     private CmpServersConfig cmpServersConfig;
52
53     @BeforeEach
54     void setUp() {
55         cmpServersConfig = new CmpServersConfig(APP_CONFIG_PATH, cmpServersConfigLoader);
56     }
57
58     @Test
59     void shouldCallLoaderWithPathFromPropertiesWhenCreated() throws CmpServersConfigLoadingException {
60         // When
61         this.cmpServersConfig.init();      // Manual PostConstruct call
62
63         // Then
64         Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH));
65     }
66
67     @Test
68     void shouldReturnLoadedServersWhenGetCalled() throws CmpServersConfigLoadingException {
69         // Given
70         Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
71         this.cmpServersConfig.init();      // Manual PostConstruct call
72
73         // When
74         List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
75
76         // Then
77         assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
78     }
79
80     @Test
81     void shouldReturnLoadedServersAfterReloadWhenGetCalled() throws CmpServersConfigLoadingException {
82         // Given
83         Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
84         List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
85         assertThat(receivedCmpServers).isNull();
86
87         // When
88         this.cmpServersConfig.reloadConfiguration();
89         receivedCmpServers = this.cmpServersConfig.getCmpServers();
90
91         // Then
92         assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
93     }
94
95     @Test
96     void shouldRethrowExceptionWhenReloaded() throws CmpServersConfigLoadingException {
97         // Given
98         Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(
99                 ERROR_MESSAGE));
100
101         // Then
102         assertThrows(
103                 CmpServersConfigLoadingException.class,
104                 () -> cmpServersConfig.reloadConfiguration());
105     }
106
107     @Test
108     void shouldPassMessageToRethrownErrorWhenReloadingFails() throws CmpServersConfigLoadingException {
109         // Given
110         Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
111
112         // When
113         Exception exception = assertThrows(
114                 CmpServersConfigLoadingException.class,
115                 () -> cmpServersConfig.reloadConfiguration());
116
117         // Then
118         assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
119     }
120
121     @Test
122     void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() throws CmpServersConfigLoadingException {
123         // Given
124         Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
125         this.cmpServersConfig.init();      // Manual PostConstruct call
126
127         // When
128         List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
129
130         // Then
131         receivedCmpServers.forEach((server) -> assertThat(server.toString())
132                 .doesNotContain(
133                         server.getAuthentication().getIak(),
134                         server.getAuthentication().getRv()
135                 ));
136     }
137
138     @Test
139     void shouldRethrowErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
140         // Given
141         Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
142
143         // Then
144         assertThrows(
145                 CmpServersConfigLoadingException.class,
146                 () -> cmpServersConfig.loadConfiguration());
147     }
148
149     @Test
150     void shouldPassMessageToRethrownErrorWhenLoadingFails() throws CmpServersConfigLoadingException {
151         // Given
152         Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
153
154         // When
155         Exception exception = assertThrows(
156                 CmpServersConfigLoadingException.class,
157                 () -> cmpServersConfig.loadConfiguration());
158
159         // Then
160         assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
161     }
162
163     @Test
164     void shouldBeNotReadyWhenCreated() {
165         assertThat(cmpServersConfig.isReady()).isFalse();
166     }
167
168     @Test
169     void shouldBeReadyWhenSuccessfullyInitialized() {
170         // When
171         this.cmpServersConfig.init();      // Manual PostConstruct call
172
173         // Then
174         assertThat(cmpServersConfig.isReady()).isTrue();
175     }
176
177     @Test
178     void shouldNotBeReadyWhenFailedToInitialize() throws CmpServersConfigLoadingException {
179         // Given
180         Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
181
182         // When
183         this.cmpServersConfig.init();      // Manual PostConstruct call
184
185         // Then
186         assertThat(cmpServersConfig.isReady()).isFalse();
187     }
188
189     @Test
190     void shouldBeReadyWhenSuccessfullyReloaded() throws CmpServersConfigLoadingException {
191         // When
192         this.cmpServersConfig.reloadConfiguration();
193
194         // Then
195         assertThat(cmpServersConfig.isReady()).isTrue();
196     }
197
198     @Test
199     void shouldNotBeReadyWhenFailedToReload() throws CmpServersConfigLoadingException {
200         // Given
201         Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE));
202
203         // When
204         assertThrows(
205                 CmpServersConfigLoadingException.class,
206                 () -> cmpServersConfig.loadConfiguration());
207
208         // Then
209         assertThat(cmpServersConfig.isReady()).isFalse();
210     }
211
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);
222
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);
232
233         return List.of(testServer1, testServer2);
234     }
235
236 }