4 * ============LICENSE_START=======================================================
5 * org.onap.dcaegen2.collectors.ves
6 * ================================================================================
7 * Copyright (C) 2018 Nokia. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 import io.vavr.collection.HashMap;
24 import io.vavr.collection.Map;
25 import org.json.JSONObject;
26 import org.junit.Test;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Arrays;
33 import java.util.Objects;
35 import static java.util.Collections.singletonList;
36 import static org.junit.Assert.*;
37 import static org.onap.dcae.CLIUtils.processCmdLine;
39 public class ApplicationSettingsTest {
42 public void shouldMakeApplicationSettingsOutOfCLIArguments() {
44 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value"};
47 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
48 String param1value = configurationAccessor.getStringDirectly("param1");
49 String param2value = configurationAccessor.getStringDirectly("param2");
52 assertEquals("param1value", param1value);
53 assertEquals("param2value", param2value);
57 public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile()
60 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
61 Files.write(tempConfFile.toPath(), Arrays.asList("section.subSection1=abc", "section.subSection2=zxc"));
62 tempConfFile.deleteOnExit();
63 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value", "-c", tempConfFile.toString()};
66 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
67 String param1value = configurationAccessor.getStringDirectly("param1");
68 String param2value = configurationAccessor.getStringDirectly("param2");
69 String fromFileParam1Value = configurationAccessor.getStringDirectly("section.subSection1");
70 String fromFileParam2Value = configurationAccessor.getStringDirectly("section.subSection2");
73 assertEquals("param1value", param1value);
74 assertEquals("param2value", param2value);
75 assertEquals("abc", fromFileParam1Value);
76 assertEquals("zxc", fromFileParam2Value);
80 public void shouldCLIArgumentsOverrideConfigFileParameters() throws IOException {
82 String[] cliArguments = {"-section.subSection1", "abc"};
83 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
84 Files.write(tempConfFile.toPath(), singletonList("section.subSection1=zxc"));
85 tempConfFile.deleteOnExit();
88 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
89 String actuallyOverridenByCLIParam = configurationAccessor.getStringDirectly("section.subSection1");
92 assertEquals("abc", actuallyOverridenByCLIParam);
96 public void shouldReturnHTTPPort() throws IOException {
98 int applicationPort = fromTemporaryConfiguration("collector.service.port=8090")
102 assertEquals(8090, applicationPort);
106 public void shouldReturnDefaultHTTPPort() throws IOException {
108 int applicationPort = fromTemporaryConfiguration().httpPort();
111 assertEquals(8080, applicationPort);
115 public void shouldReturnIfHTTPSIsEnabled() throws IOException {
117 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443")
121 assertTrue(httpsEnabled);
125 public void shouldReturnIfHTTPIsEnabled() throws IOException {
127 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.port=8080").httpsEnabled();
129 assertTrue(httpsEnabled);
133 public void shouldByDefaultHTTPSBeDisabled() throws IOException {
135 boolean httpsEnabled = fromTemporaryConfiguration().httpsEnabled();
138 assertTrue(httpsEnabled);
142 public void shouldReturnHTTPSPort() throws IOException {
144 int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443")
148 assertEquals(8443, httpsPort);
152 public void shouldReturnConfigurationUpdateInterval() throws IOException {
154 int updateFrequency = fromTemporaryConfiguration("collector.dynamic.config.update.frequency=10")
155 .configurationUpdateFrequency();
158 assertEquals(10, updateFrequency);
162 public void shouldReturnDefaultConfigurationUpdateInterval() throws IOException {
164 int updateFrequency = fromTemporaryConfiguration()
165 .configurationUpdateFrequency();
168 assertEquals(5, updateFrequency);
172 public void shouldReturnLocationOfThePasswordFile() throws IOException {
174 String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password").keystorePasswordFileLocation();
177 assertEquals(sanitizePath("/somewhere/password"), passwordFileLocation);
181 public void shouldReturnDefaultLocationOfThePasswordFile() throws IOException {
183 String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation();
186 assertEquals(sanitizePath("etc/passwordfile"), passwordFileLocation);
190 public void shouldReturnLocationOfTheKeystoreFile() throws IOException {
192 String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore")
193 .keystoreFileLocation();
196 assertEquals(sanitizePath("/somewhere/keystore"), keystoreFileLocation);
200 public void shouldReturnLocationOfTheDefaultKeystoreFile() throws IOException {
202 String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation();
205 assertEquals(sanitizePath("etc/keystore"), keystoreFileLocation);
210 public void shouldReturnKeystoreAlias() throws IOException {
212 String keystoreAlias = fromTemporaryConfiguration("collector.keystore.alias=alias").keystoreAlias();
215 assertEquals("alias", keystoreAlias);
219 public void shouldReturnDefaultKeystoreAlias() throws IOException {
221 String keystoreAlias = fromTemporaryConfiguration().keystoreAlias();
224 assertEquals("tomcat", keystoreAlias);
228 public void shouldReturnDMAAPConfigFileLocation() throws IOException {
230 String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile").dMaaPConfigurationFileLocation();
233 assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation);
237 public void shouldReturnDefaultDMAAPConfigFileLocation() throws IOException {
239 String dmaapConfigFileLocation = fromTemporaryConfiguration().dMaaPConfigurationFileLocation();
242 assertEquals(sanitizePath("etc/DmaapConfig.json"), dmaapConfigFileLocation);
246 public void shouldReturnMaximumAllowedQueuedEvents() throws IOException {
248 int maximumAllowedQueuedEvents = fromTemporaryConfiguration("collector.inputQueue.maxPending=10000")
249 .maximumAllowedQueuedEvents();
252 assertEquals(10000, maximumAllowedQueuedEvents);
256 public void shouldReturnDefaultMaximumAllowedQueuedEvents() throws IOException {
258 int maximumAllowedQueuedEvents = fromTemporaryConfiguration().maximumAllowedQueuedEvents();
261 assertEquals(1024 * 4, maximumAllowedQueuedEvents);
265 public void shouldTellIfSchemaValidationIsEnabled() throws IOException {
267 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1")
268 .jsonSchemaValidationEnabled();
271 assertTrue(jsonSchemaValidationEnabled);
275 public void shouldByDefaultSchemaValidationBeDisabled() throws IOException {
277 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().jsonSchemaValidationEnabled();
280 assertFalse(jsonSchemaValidationEnabled);
284 public void shouldReturnJSONSchema() throws IOException {
286 JSONObject jsonSchema = fromTemporaryConfiguration("collector.schema.file={\"v1\": {}}")
290 assertEquals(new JSONObject("{\"v1\": {}}").toMap(), jsonSchema.toMap());
294 public void shouldReturnDefaultJSONSchema() throws IOException {
296 JSONObject jsonSchema = fromTemporaryConfiguration().jsonSchema();
299 assertEquals(new JSONObject("{\"v5\":\"./etc/CommonEventFormat_28.3.json\"}").toMap(), jsonSchema.toMap());
303 public void shouldReturnExceptionConfigFileLocation() throws IOException {
305 String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile")
306 .exceptionConfigFileLocation();
309 assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation);
313 public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException {
315 String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation();
318 assertNull(exceptionConfigFileLocation);
323 public void shouldReturnDMAAPStreamId() throws IOException {
325 Map<String, String[]> expected = HashMap.of(
326 "s", new String[]{"something", "something2"},
327 "s2", new String[]{"something3"}
331 Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration("collector.dmaap.streamid=s=something,something2|s2=something3")
332 .dMaaPStreamsMapping();
335 assertArrayEquals(expected.get("s").get(), Objects.requireNonNull(dmaapStreamID).get("s").get());
336 assertArrayEquals(expected.get("s2").get(), Objects.requireNonNull(dmaapStreamID).get("s2").get());
337 assertEquals(expected.keySet(), dmaapStreamID.keySet());
341 public void shouldReturnDefaultDMAAPStreamId() throws IOException {
343 Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration().dMaaPStreamsMapping();
346 assertEquals(dmaapStreamID, HashMap.empty());
350 public void shouldReturnIfAuthorizationIsEnabled() throws IOException {
352 boolean authorizationEnabled = fromTemporaryConfiguration("header.authflag=1")
353 .authorizationEnabled();
356 assertTrue(authorizationEnabled);
360 public void shouldAuthorizationBeDisabledByDefault() throws IOException {
362 boolean authorizationEnabled = fromTemporaryConfiguration().authorizationEnabled();
365 assertFalse(authorizationEnabled);
369 public void shouldReturnValidCredentials() throws IOException {
371 Map<String, String> allowedUsers = fromTemporaryConfiguration(
372 "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg=="
373 ).validAuthorizationCredentials();
376 assertEquals(allowedUsers.get("pasza").get(), "simplepassword");
377 assertEquals(allowedUsers.get("someoneelse").get(), "simplepassword");
381 public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
383 Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
384 validAuthorizationCredentials();
387 assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty());
391 public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
393 boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0")
394 .eventTransformingEnabled();
397 assertFalse(isEventTransformingEnabled);
401 public void shouldEventTransformingBeEnabledByDefault() throws IOException {
403 boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled();
406 assertTrue(isEventTransformingEnabled);
410 public void shouldReturnCambriaConfigurationFileLocation() throws IOException {
412 String cambriaConfigurationFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapConfig")
413 .dMaaPConfigurationFileLocation();
416 assertEquals(sanitizePath("/somewhere/dmaapConfig"), cambriaConfigurationFileLocation);
420 public void shouldReturnDefaultCambriaConfigurationFileLocation() throws IOException {
422 String cambriaConfigurationFileLocation = fromTemporaryConfiguration()
423 .dMaaPConfigurationFileLocation();
426 assertEquals(sanitizePath("etc/DmaapConfig.json"), cambriaConfigurationFileLocation);
429 private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
431 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
432 Files.write(tempConfFile.toPath(), Arrays.asList(fileLines));
433 tempConfFile.deleteOnExit();
434 return new ApplicationSettings(new String[]{"-c", tempConfFile.toString()}, args -> processCmdLine(args), "");
437 private String sanitizePath(String path) {
438 return Paths.get(path).toString();