5d850fe9d7c7bfb4d7ed8fc88db5b1684d569a89
[oom/platform/cert-service.git] /
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 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;
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     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);
172
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);
182
183         return List.of(testServer1, testServer2);
184     }
185
186 }