Config fetch for VESCollector through DCAE-SDK (CBS Client)
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / configuration / ConfigLoaderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
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 package org.onap.dcae.configuration;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.argThat;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.onap.dcae.TestingUtilities.readJSONFromFile;
29 import static org.onap.dcae.common.publishing.VavrUtils.f;
30
31 import io.vavr.collection.HashMap;
32 import io.vavr.collection.Map;
33 import io.vavr.control.Try;
34 import java.nio.file.Paths;
35 import io.vavr.control.Option;
36 import org.json.JSONObject;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.dcae.configuration.cbs.CbsConfigResolver;
44
45 @RunWith(MockitoJUnitRunner.Silent.class)
46 public class ConfigLoaderTest {
47
48     private static final String COLLECTOR_PORT = "collector.port";
49     private static final String PORT_8080 = "8080";
50     private static final String PORT_8081 = "8081";
51     private static final String COLLECTOR_KEYSTORE_FILE_LOCATION = "collector.keystore.file.location";
52     private static final String SOME_PATH = "some/path";
53     private static final String COLLECTOR_SCHEMA_FILE = "collector.schema.file";
54     private static final String SOME_SCHEMA = "some schema";
55
56     @Mock
57     private CbsConfigResolver cbsConfigResolverMock;
58
59     @Mock
60     private ConfigFilesFacade configFilesFacadeMock;
61
62     @InjectMocks
63     private ConfigLoader configLoader;
64
65     @Mock
66     private Runnable applicationRestarter;
67
68
69     @Before
70     public void setup() {
71         when(configFilesFacadeMock.readCollectorProperties()).thenReturn(Try.of(HashMap::empty));
72         when(configFilesFacadeMock.readDMaaPConfiguration()).thenReturn(Try.of(JSONObject::new));
73     }
74
75     @Test
76     public void shouldCallConfigSourceForData() {
77         // given
78         HashMap<String, String> properties = HashMap.of(COLLECTOR_PORT, PORT_8080);
79         mockVesInitialProperties(properties);
80         mockVesConfigInCbs(properties);
81
82         // when
83         configLoader.updateConfig();
84
85         // then
86         verify(cbsConfigResolverMock).getAppConfig();
87     }
88
89     @Test
90     public void shouldNotUpdatePropertiesWhenSameKeySetAndSameValues() {
91         // given
92         HashMap<String, String> properties = HashMap.of(COLLECTOR_PORT, PORT_8080);
93         mockVesInitialProperties(properties);
94         mockVesConfigInCbs(properties);
95
96         // when
97         configLoader.updateConfig();
98
99         // then
100         verify(configFilesFacadeMock, never()).writeProperties(any());
101         verify(applicationRestarter, never()).run();
102     }
103
104     @Test
105     public void shouldUpdatePropertiesWhenSameKeySetButDifferentValues() {
106         // given
107         HashMap<String, String> initialProperties = HashMap.of(COLLECTOR_PORT, PORT_8080);
108         HashMap<String, String> cbsProperties = HashMap.of(COLLECTOR_PORT, PORT_8081);
109         mockVesInitialProperties(initialProperties);
110         mockVesConfigInCbs(cbsProperties);
111
112         // when
113         configLoader.updateConfig();
114
115         // then
116         verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
117         verify(applicationRestarter, times(1)).run();
118     }
119
120     @Test
121     public void shouldUpdatePropertiesWhenVesKeysAreSubsetOfCbsKeysAndSubsetHasSameValues() {
122         // given
123         HashMap<String, String> initialProperties = HashMap.of(
124             COLLECTOR_PORT, PORT_8080);
125         HashMap<String, String> cbsProperties = HashMap.of(
126             COLLECTOR_PORT, PORT_8080,
127             COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH);
128         mockVesInitialProperties(initialProperties);
129         mockVesConfigInCbs(cbsProperties);
130
131         // when
132         configLoader.updateConfig();
133
134         // then
135         verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
136         verify(applicationRestarter, times(1)).run();
137     }
138
139     @Test
140     public void shouldUpdatePropertiesWhenVesKeysAreSubsetOfCbsKeysAndSubsetHasDifferentValues() {
141         HashMap<String, String> initialProperties = HashMap.of(
142             COLLECTOR_PORT, PORT_8080);
143         HashMap<String, String> cbsProperties = HashMap.of(
144             COLLECTOR_PORT, PORT_8081,
145             COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH);
146         mockVesInitialProperties(initialProperties);
147         mockVesConfigInCbs(cbsProperties);
148
149         // when
150         configLoader.updateConfig();
151
152         // then
153         verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
154         verify(applicationRestarter, times(1)).run();
155     }
156
157     @Test
158     public void shouldNotUpdatePropertiesWhenCbsKeysAreSubsetOfVesKeysAndSubsetHasSameValues() {
159         HashMap<String, String> initialProperties = HashMap.of(
160             COLLECTOR_PORT, PORT_8080,
161             COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH);
162         HashMap<String, String> cbsProperties = HashMap.of(
163             COLLECTOR_PORT, PORT_8080);
164         mockVesInitialProperties(initialProperties);
165         mockVesConfigInCbs(cbsProperties);
166
167         // when
168         configLoader.updateConfig();
169
170         // then
171         verify(configFilesFacadeMock, never()).writeProperties(any());
172         verify(applicationRestarter, never()).run();
173     }
174
175     @Test
176     public void shouldUpdatePropertiesWhenCbsKeysAreSubsetOfVesKeysAndSubsetHasDifferentValues() {
177         HashMap<String, String> initialProperties = HashMap.of(
178             COLLECTOR_PORT, PORT_8080,
179             COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH);
180         HashMap<String, String> cbsProperties = HashMap.of(
181             COLLECTOR_PORT, PORT_8081);
182         mockVesInitialProperties(initialProperties);
183         mockVesConfigInCbs(cbsProperties);
184
185         // when
186         configLoader.updateConfig();
187
188         // then
189         verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
190         verify(applicationRestarter, times(1)).run();
191     }
192
193     @Test
194     public void shouldUpdatePropertiesWhenVesAndCbsKeySetsIntersectAndIntersectingKeysHaveSameValues() {
195         HashMap<String, String> initialProperties = HashMap.of(
196             COLLECTOR_PORT, PORT_8080,
197             COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH);
198         HashMap<String, String> cbsProperties = HashMap.of(
199             COLLECTOR_PORT, PORT_8080,
200             COLLECTOR_SCHEMA_FILE, SOME_SCHEMA
201         );
202         mockVesInitialProperties(initialProperties);
203         mockVesConfigInCbs(cbsProperties);
204
205         // when
206         configLoader.updateConfig();
207
208         // then
209         verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
210         verify(applicationRestarter, times(1)).run();
211     }
212
213     @Test
214     public void shouldUpdatePropertiesWhenVesAndCbsKeySetsIntersectAndIntersectingKeysHaveDifferentValues() {
215         HashMap<String, String> initialProperties = HashMap.of(
216             COLLECTOR_PORT, PORT_8080,
217             COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH);
218         HashMap<String, String> cbsProperties = HashMap.of(
219             COLLECTOR_PORT, PORT_8081,
220             COLLECTOR_SCHEMA_FILE, SOME_SCHEMA
221         );
222         mockVesInitialProperties(initialProperties);
223         mockVesConfigInCbs(cbsProperties);
224
225         // when
226         configLoader.updateConfig();
227
228         // then
229         verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
230         verify(applicationRestarter, times(1)).run();
231     }
232
233     @Test
234     public void shouldUpdateDmaapConfigWhenConfigurationChanged() {
235         // given
236         JSONObject emptyDmaapConfig = new JSONObject();
237         JSONObject dmaapConfig = loadSampleDmaapConfig();
238         mockVesInitialDmaapConfig(emptyDmaapConfig);
239         mockVesDmaapConfigInCbs(dmaapConfig);
240
241         // when
242         configLoader.updateConfig();
243
244         // then
245         verify(configFilesFacadeMock).writeDMaaPConfiguration(argThat(dmaapConfig::similar));
246         verify(applicationRestarter, times(1)).run();
247     }
248
249     @Test
250     public void shouldNotUpdateDmaapConfigWhenConfigurationNotChanged() {
251         // given
252         JSONObject dmaapConf = loadSampleDmaapConfig();
253         mockVesInitialDmaapConfig(dmaapConf);
254         mockVesDmaapConfigInCbs(dmaapConf);
255
256         // when
257         configLoader.updateConfig();
258
259         // then
260         verify(configFilesFacadeMock, never()).writeDMaaPConfiguration(any());
261         verify(applicationRestarter, never()).run();
262     }
263
264     private void mockVesInitialDmaapConfig(JSONObject dmaapConf) {
265         when(configFilesFacadeMock.readDMaaPConfiguration()).thenReturn(Try.of(() -> dmaapConf));
266     }
267
268     private void mockVesDmaapConfigInCbs(JSONObject dmaapConf) {
269         JSONObject jsonObject = new JSONObject(f("{\"streams_publishes\": %s}}", dmaapConf));
270         when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(jsonObject));
271     }
272
273     private void mockVesConfigInCbs(HashMap<String, String> properties) {
274         when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(prepareConfigurationJson(properties)));
275     }
276
277     private void mockVesInitialProperties(HashMap<String, String> properties) {
278         when(configFilesFacadeMock.readCollectorProperties()).thenReturn(Try.of(() -> properties));
279     }
280
281
282     private JSONObject loadSampleDmaapConfig() {
283         return readJSONFromFile(Paths.get("src/test/resources/testParseDMaaPCredentialsGen2.json"));
284     }
285
286     private JSONObject prepareConfigurationJson(Map<String, String> properties) {
287         String template = "{%s, \"streams_publishes\": {}}";
288         String customProperties = properties
289             .map(property -> "\"" + property._1 + "\": \"" + property._2 + "\"")
290             .mkString(", ");
291         String jsonBody = f(template, customProperties);
292         return new JSONObject(jsonBody);
293     }
294 }