2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2018 - 2020 Nokia. All rights reserved.
6 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.dcae;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.networknt.schema.JsonSchema;
27 import io.vavr.collection.HashMap;
28 import io.vavr.collection.Map;
29 import org.junit.Test;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.Arrays;
37 import java.util.Objects;
39 import static java.util.Collections.singletonList;
40 import static org.junit.Assert.assertArrayEquals;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertFalse;
43 import static org.junit.Assert.assertNull;
44 import static org.junit.Assert.assertTrue;
45 import static org.onap.dcae.CLIUtils.processCmdLine;
46 import static org.onap.dcae.TestingUtilities.createTemporaryFile;
48 public class ApplicationSettingsTest {
50 private static final String SAMPLE_JSON_SCHEMA = "{"
51 + " \"type\": \"object\","
52 + " \"properties\": {"
53 + " \"state\": { \"type\": \"string\" }"
58 public void shouldMakeApplicationSettingsOutOfCLIArguments() {
60 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value"};
63 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
64 String param1value = configurationAccessor.getStringDirectly("param1");
65 String param2value = configurationAccessor.getStringDirectly("param2");
68 assertEquals("param1value", param1value);
69 assertEquals("param2value", param2value);
73 public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile()
76 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
77 Files.write(tempConfFile.toPath(), Arrays.asList("section.subSection1=abc", "section.subSection2=zxc"));
78 tempConfFile.deleteOnExit();
79 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value", "-c", tempConfFile.toString()};
82 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
83 String param1value = configurationAccessor.getStringDirectly("param1");
84 String param2value = configurationAccessor.getStringDirectly("param2");
85 String fromFileParam1Value = configurationAccessor.getStringDirectly("section.subSection1");
86 String fromFileParam2Value = configurationAccessor.getStringDirectly("section.subSection2");
89 assertEquals("param1value", param1value);
90 assertEquals("param2value", param2value);
91 assertEquals("abc", fromFileParam1Value);
92 assertEquals("zxc", fromFileParam2Value);
96 public void shouldCLIArgumentsOverrideConfigFileParameters() throws IOException {
98 String[] cliArguments = {"-section.subSection1", "abc"};
99 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
100 Files.write(tempConfFile.toPath(), singletonList("section.subSection1=zxc"));
101 tempConfFile.deleteOnExit();
104 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
105 String actuallyOverridenByCLIParam = configurationAccessor.getStringDirectly("section.subSection1");
108 assertEquals("abc", actuallyOverridenByCLIParam);
112 public void shouldReturnHTTPPort() throws IOException {
114 int applicationPort = fromTemporaryConfiguration("collector.service.port=8090")
118 assertEquals(8090, applicationPort);
122 public void shouldReturnDefaultHTTPPort() throws IOException {
124 int applicationPort = fromTemporaryConfiguration().httpPort();
127 assertEquals(8080, applicationPort);
131 public void shouldReturnIfHTTPSIsEnabled() throws IOException {
133 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443")
137 assertTrue(httpsEnabled);
141 public void shouldReturnIfHTTPIsEnabled() throws IOException {
143 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.port=8080").httpsEnabled();
145 assertTrue(httpsEnabled);
149 public void shouldByDefaultHTTPSBeDisabled() throws IOException {
151 boolean httpsEnabled = fromTemporaryConfiguration().httpsEnabled();
154 assertTrue(httpsEnabled);
158 public void shouldReturnHTTPSPort() throws IOException {
160 int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443")
164 assertEquals(8443, httpsPort);
168 public void shouldReturnConfigurationUpdateInterval() throws IOException {
170 int updateFrequency = fromTemporaryConfiguration("collector.dynamic.config.update.frequency=10")
171 .configurationUpdateFrequency();
174 assertEquals(10, updateFrequency);
178 public void shouldReturnDefaultConfigurationUpdateInterval() throws IOException {
180 int updateFrequency = fromTemporaryConfiguration()
181 .configurationUpdateFrequency();
184 assertEquals(5, updateFrequency);
188 public void shouldReturnLocationOfThePasswordFile() throws IOException {
190 String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password")
191 .keystorePasswordFileLocation();
194 assertEquals(sanitizePath("/somewhere/password"), passwordFileLocation);
198 public void shouldReturnDefaultLocationOfThePasswordFile() throws IOException {
200 String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation();
203 assertEquals(sanitizePath("etc/passwordfile"), passwordFileLocation);
207 public void shouldReturnLocationOfTheKeystoreFile() throws IOException {
209 String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore")
210 .keystoreFileLocation();
213 assertEquals(sanitizePath("/somewhere/keystore"), keystoreFileLocation);
217 public void shouldReturnLocationOfTheDefaultKeystoreFile() throws IOException {
219 String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation();
222 assertEquals(sanitizePath("etc/keystore"), keystoreFileLocation);
226 public void shouldReturnDMAAPConfigFileLocation() throws IOException {
228 String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile")
229 .dMaaPConfigurationFileLocation();
232 assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation);
236 public void shouldReturnDefaultDMAAPConfigFileLocation() throws IOException {
238 String dmaapConfigFileLocation = fromTemporaryConfiguration().dMaaPConfigurationFileLocation();
241 assertEquals(sanitizePath("etc/DmaapConfig.json"), dmaapConfigFileLocation);
245 public void shouldTellIfSchemaValidationIsEnabled() throws IOException {
247 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1")
248 .eventSchemaValidationEnabled();
251 assertTrue(jsonSchemaValidationEnabled);
255 public void shouldByDefaultSchemaValidationBeDisabled() throws IOException {
257 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().eventSchemaValidationEnabled();
260 assertFalse(jsonSchemaValidationEnabled);
264 public void shouldReportValidateJSONSchemaErrorWhenJsonContainsIntegerValueNotString() throws IOException {
266 Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
269 JsonSchema schema = fromTemporaryConfiguration(
270 String.format("collector.schema.file={\"v1\": \"%s\"}", temporarySchemaFile))
274 JsonNode incorrectTestObject = new ObjectMapper().readTree("{ \"state\": 1 }");
276 assertFalse(schema.validate(incorrectTestObject).isEmpty());
281 public void shouldDoNotReportAnyValidateJSONSchemaError() throws IOException {
283 Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
286 JsonSchema schema = fromTemporaryConfiguration(
287 String.format("collector.schema.file={\"v1\": \"%s\"}", temporarySchemaFile))
291 JsonNode correctTestObject = new ObjectMapper().readTree("{ \"state\": \"hi\" }");
292 assertTrue(schema.validate(correctTestObject).isEmpty());
296 public void shouldReturnExceptionConfigFileLocation() throws IOException {
298 String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile")
299 .exceptionConfigFileLocation();
302 assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation);
306 public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException {
308 String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation();
311 assertNull(exceptionConfigFileLocation);
316 public void shouldReturnDMAAPStreamId() throws IOException {
318 Map<String, String[]> expected = HashMap.of(
319 "log", new String[]{"ves-syslog", "ves-auditlog"},
320 "fault", new String[]{"ves-fault"}
324 Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration(
325 "collector.dmaap.streamid=fault=ves-fault|log=ves-syslog,ves-auditlog")
326 .getDmaapStreamIds();
329 assertArrayEquals(expected.get("log").get(), Objects.requireNonNull(dmaapStreamID).get("log").get());
330 assertArrayEquals(expected.get("fault").get(), Objects.requireNonNull(dmaapStreamID).get("fault").get());
331 assertEquals(expected.keySet(), dmaapStreamID.keySet());
335 public void shouldReturnDefaultDMAAPStreamId() throws IOException {
337 Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration().getDmaapStreamIds();
340 assertEquals(dmaapStreamID, HashMap.empty());
344 public void shouldAuthorizationBeDisabledByDefault() throws IOException {
346 boolean authorizationEnabled = fromTemporaryConfiguration().authMethod().contains("noAuth");
349 assertTrue(authorizationEnabled);
353 public void shouldReturnValidCredentials() throws IOException {
355 Map<String, String> allowedUsers = fromTemporaryConfiguration(
356 "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg=="
357 ).validAuthorizationCredentials();
360 assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("pasza").get());
361 assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("someoneelse").get());
365 public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
367 Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
368 validAuthorizationCredentials();
371 assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty());
375 public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
377 boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0")
378 .eventTransformingEnabled();
381 assertFalse(isEventTransformingEnabled);
385 public void shouldEventTransformingBeEnabledByDefault() throws IOException {
387 boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled();
390 assertTrue(isEventTransformingEnabled);
394 public void shouldReturnCambriaConfigurationFileLocation() throws IOException {
396 String cambriaConfigurationFileLocation = fromTemporaryConfiguration(
397 "collector.dmaapfile=/somewhere/dmaapConfig")
398 .dMaaPConfigurationFileLocation();
401 assertEquals(sanitizePath("/somewhere/dmaapConfig"), cambriaConfigurationFileLocation);
405 public void shouldReturnDefaultCambriaConfigurationFileLocation() throws IOException {
407 String cambriaConfigurationFileLocation = fromTemporaryConfiguration()
408 .dMaaPConfigurationFileLocation();
411 assertEquals(sanitizePath("etc/DmaapConfig.json"), cambriaConfigurationFileLocation);
415 public void shouldReturnDefaultExternalSchemaSchemasLocation() throws IOException {
417 String externalSchemaSchemasLocation = fromTemporaryConfiguration()
418 .getExternalSchemaSchemasLocation();
421 assertEquals(sanitizePath("./etc/externalRepo"), externalSchemaSchemasLocation);
425 public void shouldReturnDefaultExternalSchemaMappingFileLocation() throws IOException {
427 String externalSchemaMappingFileLocation = fromTemporaryConfiguration()
428 .getExternalSchemaMappingFileLocation();
431 assertEquals(sanitizePath("./etc/externalRepo/schema-map.json"), externalSchemaMappingFileLocation);
435 public void shouldReturnDefaultExternalSchemaSchemaRefPath() throws IOException {
437 String externalSchemaSchemaRefPath = fromTemporaryConfiguration()
438 .getExternalSchemaSchemaRefPath();
441 assertEquals(sanitizePath("/event/stndDefinedFields/schemaReference"), externalSchemaSchemaRefPath);
445 public void shouldReturnDefaultExternalSchemaStndDefinedDataPath() throws IOException {
447 String externalSchemaStndDefinedDataPath = fromTemporaryConfiguration()
448 .getExternalSchemaStndDefinedDataPath();
451 assertEquals(sanitizePath("/event/stndDefinedFields/data"), externalSchemaStndDefinedDataPath);
455 public void shouldReturnEnabledExternalSchema2ndStageValidation() throws IOException {
457 boolean externalSchema2ndStageValidation = fromTemporaryConfiguration("collector.externalSchema.2ndStageValidation=-1")
458 .getExternalSchemaValidationCheckflag();
461 assertFalse(externalSchema2ndStageValidation);
464 private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
466 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
467 Files.write(tempConfFile.toPath(), Arrays.asList(fileLines));
468 tempConfFile.deleteOnExit();
469 return new ApplicationSettings(new String[]{"-c", tempConfFile.toString()}, args -> processCmdLine(args), "");
472 private String sanitizePath(String path) {
473 return Paths.get(path).toString();