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