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
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=========================================================
20 package org.onap.dcae.configuration;
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;
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;
45 @RunWith(MockitoJUnitRunner.Silent.class)
46 public class ConfigLoaderTest {
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";
57 private CbsConfigResolver cbsConfigResolverMock;
60 private ConfigFilesFacade configFilesFacadeMock;
63 private ConfigLoader configLoader;
66 private Runnable applicationRestarter;
71 when(configFilesFacadeMock.readCollectorProperties()).thenReturn(Try.of(HashMap::empty));
72 when(configFilesFacadeMock.readDMaaPConfiguration()).thenReturn(Try.of(JSONObject::new));
76 public void shouldCallConfigSourceForData() {
78 HashMap<String, String> properties = HashMap.of(COLLECTOR_PORT, PORT_8080);
79 mockVesInitialProperties(properties);
80 mockVesConfigInCbs(properties);
83 configLoader.updateConfig();
86 verify(cbsConfigResolverMock).getAppConfig();
90 public void shouldNotUpdatePropertiesWhenSameKeySetAndSameValues() {
92 HashMap<String, String> properties = HashMap.of(COLLECTOR_PORT, PORT_8080);
93 mockVesInitialProperties(properties);
94 mockVesConfigInCbs(properties);
97 configLoader.updateConfig();
100 verify(configFilesFacadeMock, never()).writeProperties(any());
101 verify(applicationRestarter, never()).run();
105 public void shouldUpdatePropertiesWhenSameKeySetButDifferentValues() {
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);
113 configLoader.updateConfig();
116 verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
117 verify(applicationRestarter, times(1)).run();
121 public void shouldUpdatePropertiesWhenVesKeysAreSubsetOfCbsKeysAndSubsetHasSameValues() {
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);
132 configLoader.updateConfig();
135 verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
136 verify(applicationRestarter, times(1)).run();
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);
150 configLoader.updateConfig();
153 verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
154 verify(applicationRestarter, times(1)).run();
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);
168 configLoader.updateConfig();
171 verify(configFilesFacadeMock, never()).writeProperties(any());
172 verify(applicationRestarter, never()).run();
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);
186 configLoader.updateConfig();
189 verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
190 verify(applicationRestarter, times(1)).run();
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
202 mockVesInitialProperties(initialProperties);
203 mockVesConfigInCbs(cbsProperties);
206 configLoader.updateConfig();
209 verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
210 verify(applicationRestarter, times(1)).run();
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
222 mockVesInitialProperties(initialProperties);
223 mockVesConfigInCbs(cbsProperties);
226 configLoader.updateConfig();
229 verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties);
230 verify(applicationRestarter, times(1)).run();
234 public void shouldUpdateDmaapConfigWhenConfigurationChanged() {
236 JSONObject emptyDmaapConfig = new JSONObject();
237 JSONObject dmaapConfig = loadSampleDmaapConfig();
238 mockVesInitialDmaapConfig(emptyDmaapConfig);
239 mockVesDmaapConfigInCbs(dmaapConfig);
242 configLoader.updateConfig();
245 verify(configFilesFacadeMock).writeDMaaPConfiguration(argThat(dmaapConfig::similar));
246 verify(applicationRestarter, times(1)).run();
250 public void shouldNotUpdateDmaapConfigWhenConfigurationNotChanged() {
252 JSONObject dmaapConf = loadSampleDmaapConfig();
253 mockVesInitialDmaapConfig(dmaapConf);
254 mockVesDmaapConfigInCbs(dmaapConf);
257 configLoader.updateConfig();
260 verify(configFilesFacadeMock, never()).writeDMaaPConfiguration(any());
261 verify(applicationRestarter, never()).run();
264 private void mockVesInitialDmaapConfig(JSONObject dmaapConf) {
265 when(configFilesFacadeMock.readDMaaPConfiguration()).thenReturn(Try.of(() -> dmaapConf));
268 private void mockVesDmaapConfigInCbs(JSONObject dmaapConf) {
269 JSONObject jsonObject = new JSONObject(f("{\"streams_publishes\": %s}}", dmaapConf));
270 when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(jsonObject));
273 private void mockVesConfigInCbs(HashMap<String, String> properties) {
274 when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(prepareConfigurationJson(properties)));
277 private void mockVesInitialProperties(HashMap<String, String> properties) {
278 when(configFilesFacadeMock.readCollectorProperties()).thenReturn(Try.of(() -> properties));
282 private JSONObject loadSampleDmaapConfig() {
283 return readJSONFromFile(Paths.get("src/test/resources/testParseDMaaPCredentialsGen2.json"));
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 + "\"")
291 String jsonBody = f(template, customProperties);
292 return new JSONObject(jsonBody);