2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2018 - 2021 Nokia. All rights reserved.
6 * Copyright (C) 2018,2023 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.Ignore;
30 import org.junit.Test;
33 import java.io.IOException;
35 import java.net.URISyntaxException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Arrays;
40 import java.util.Objects;
42 import static java.util.Collections.singletonList;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertTrue;
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertNull;
47 import static org.onap.dcae.CLIUtils.processCmdLine;
48 import static org.onap.dcae.TestingUtilities.createTemporaryFile;
50 public class ApplicationSettingsTest {
53 * The Unix separator character.
55 private static final char UNIX_SEPARATOR = '/';
58 * The Windows separator character.
60 private static final char WINDOWS_SEPARATOR = '\\';
62 private static final String SAMPLE_JSON_SCHEMA = "{"
63 + " \"type\": \"object\","
64 + " \"properties\": {"
65 + " \"state\": { \"type\": \"string\" }"
70 * Converts all separators to the Unix separator of forward slash.
72 * @param path the path to be changed, null ignored
73 * @return the updated path
75 private static String separatorsToUnix(final String path) {
76 if (path == null || path.indexOf(WINDOWS_SEPARATOR) == -1) {
79 return path.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR);
83 public void shouldMakeApplicationSettingsOutOfCLIArguments() {
85 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value"};
88 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
89 String param1value = configurationAccessor.getStringDirectly("param1");
90 String param2value = configurationAccessor.getStringDirectly("param2");
93 assertEquals("param1value", param1value);
94 assertEquals("param2value", param2value);
98 public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile()
101 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
102 Files.write(tempConfFile.toPath(), Arrays.asList("section.subSection1=abc", "section.subSection2=zxc"));
103 tempConfFile.deleteOnExit();
104 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value", "-c", tempConfFile.toString()};
107 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
108 String param1value = configurationAccessor.getStringDirectly("param1");
109 String param2value = configurationAccessor.getStringDirectly("param2");
110 String fromFileParam1Value = configurationAccessor.getStringDirectly("section.subSection1");
111 String fromFileParam2Value = configurationAccessor.getStringDirectly("section.subSection2");
114 assertEquals("param1value", param1value);
115 assertEquals("param2value", param2value);
116 assertEquals("abc", fromFileParam1Value);
117 assertEquals("zxc", fromFileParam2Value);
121 public void shouldCLIArgumentsOverrideConfigFileParameters() throws IOException {
123 String[] cliArguments = {"-section.subSection1", "abc"};
124 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
125 Files.write(tempConfFile.toPath(), singletonList("section.subSection1=zxc"));
126 tempConfFile.deleteOnExit();
129 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
130 String actuallyOverridenByCLIParam = configurationAccessor.getStringDirectly("section.subSection1");
133 assertEquals("abc", actuallyOverridenByCLIParam);
137 public void shouldReturnHTTPPort() throws IOException {
139 int applicationPort = fromTemporaryConfiguration("collector.service.port=8090")
142 assertEquals(8090, applicationPort);
146 public void shouldReturnDefaultHTTPPort() throws IOException {
148 int applicationPort = fromTemporaryConfiguration().httpPort();
151 assertEquals(8080, applicationPort);
155 public void shouldReturnIfHTTPSIsEnabled() throws IOException {
157 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443")
161 assertTrue(httpsEnabled);
165 public void shouldReturnIfHTTPIsEnabled() throws IOException {
167 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.port=8080").httpsEnabled();
169 assertTrue(httpsEnabled);
173 public void shouldByDefaultHTTPSBeDisabled() throws IOException {
175 boolean httpsEnabled = fromTemporaryConfiguration().httpsEnabled();
178 assertTrue(httpsEnabled);
182 public void shouldReturnHTTPSPort() throws IOException {
184 int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443")
188 assertEquals(8443, httpsPort);
192 public void shouldReturnConfigurationUpdateInterval() throws IOException {
194 int updateFrequency = fromTemporaryConfiguration("collector.dynamic.config.update.frequency=10")
195 .configurationUpdateFrequency();
198 assertEquals(10, updateFrequency);
202 public void shouldReturnDefaultConfigurationUpdateInterval() throws IOException {
204 int updateFrequency = fromTemporaryConfiguration()
205 .configurationUpdateFrequency();
208 assertEquals(5, updateFrequency);
212 public void shouldReturnLocationOfThePasswordFile() throws IOException {
214 String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password")
215 .keystorePasswordFileLocation();
218 assertEquals(sanitizePath("/somewhere/password"), passwordFileLocation);
222 public void shouldReturnDefaultLocationOfThePasswordFile() throws IOException {
224 String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation();
227 assertEquals(sanitizePath("etc/passwordfile"), passwordFileLocation);
231 public void shouldReturnLocationOfTheKeystoreFile() throws IOException {
233 String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore")
234 .keystoreFileLocation();
237 assertEquals(sanitizePath("/somewhere/keystore"), keystoreFileLocation);
241 public void shouldReturnLocationOfTheDefaultKeystoreFile() throws IOException {
243 String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation();
246 assertEquals(sanitizePath("etc/keystore"), keystoreFileLocation);
250 public void shouldReturnDMAAPConfigFileLocation() throws IOException {
252 String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile")
253 .dMaaPConfigurationFileLocation();
256 assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation);
260 public void shouldTellIfSchemaValidationIsEnabled() throws IOException {
262 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1")
263 .eventSchemaValidationEnabled();
266 assertTrue(jsonSchemaValidationEnabled);
270 public void shouldByDefaultSchemaValidationBeDisabled() throws IOException {
272 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().eventSchemaValidationEnabled();
275 assertFalse(jsonSchemaValidationEnabled);
279 public void shouldReportValidateJSONSchemaErrorWhenJsonContainsIntegerValueNotString() throws IOException, URISyntaxException {
281 Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
282 String normalizedSchemaFile = separatorsToUnix(temporarySchemaFile.toString());
284 JsonSchema schema = fromTemporaryConfiguration(
285 String.format("collector.schema.file={\"v1\": \"%s\"}", normalizedSchemaFile))
289 JsonNode incorrectTestObject = new ObjectMapper().readTree("{ \"state\": 1 }");
291 assertFalse(schema.validate(incorrectTestObject).isEmpty());
296 public void shouldDoNotReportAnyValidateJSONSchemaError() throws IOException {
298 Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
299 String normalizedSchemaFile = separatorsToUnix(temporarySchemaFile.toString());
301 JsonSchema schema = fromTemporaryConfiguration(
302 String.format("collector.schema.file={\"v1\": \"%s\"}", normalizedSchemaFile))
306 JsonNode correctTestObject = new ObjectMapper().readTree("{ \"state\": \"hi\" }");
307 assertTrue(schema.validate(correctTestObject).isEmpty());
311 public void shouldReturnExceptionConfigFileLocation() throws IOException {
313 String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile")
314 .exceptionConfigFileLocation();
317 assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation);
321 public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException {
323 String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation();
326 assertNull(exceptionConfigFileLocation);
331 public void shouldReturnDMAAPStreamId() throws IOException {
333 Map<String, String> expected = HashMap.of(
339 Map<String, String> dmaapStreamID = fromTemporaryConfiguration(
340 "collector.dmaap.streamid=fault=ves-fault,stream1|log=ves-syslog,stream2,stream3")
341 .getDmaapStreamIds();
344 assertEquals(expected.get("log").get(), Objects.requireNonNull(dmaapStreamID).get("log").get());
345 assertEquals(expected.get("fault").get(), Objects.requireNonNull(dmaapStreamID).get("fault").get());
346 assertEquals(expected.keySet(), dmaapStreamID.keySet());
350 public void shouldReturnDefaultDMAAPStreamId() throws IOException {
352 Map<String, String> dmaapStreamID = fromTemporaryConfiguration().getDmaapStreamIds();
355 assertEquals(dmaapStreamID, HashMap.empty());
359 public void shouldAuthorizationBeDisabledByDefault() throws IOException {
361 boolean authorizationEnabled = fromTemporaryConfiguration().authMethod().contains("noAuth");
364 assertTrue(authorizationEnabled);
368 public void shouldReturnValidCredentials() throws IOException {
370 Map<String, String> allowedUsers = fromTemporaryConfiguration(
371 "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg=="
372 ).validAuthorizationCredentials();
375 assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("pasza").get());
376 assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("someoneelse").get());
380 public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
382 Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
383 validAuthorizationCredentials();
386 assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty());
390 public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
392 boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0")
393 .eventTransformingEnabled();
396 assertFalse(isEventTransformingEnabled);
400 public void shouldEventTransformingBeEnabledByDefault() throws IOException {
402 boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled();
405 assertTrue(isEventTransformingEnabled);
409 public void shouldReturnConfigurationFileLocation() throws IOException {
411 String configurationFileLocation = fromTemporaryConfiguration(
412 "collector.dmaapfile=/somewhere/etc/ves-dmaap-config.json")
413 .dMaaPConfigurationFileLocation();
416 assertEquals(sanitizePath("/somewhere/etc/ves-dmaap-config.json"), configurationFileLocation);
420 public void shouldReturnDefaultConfigurationFileLocation() throws IOException {
422 String configurationFileLocation = fromTemporaryConfiguration()
423 .dMaaPConfigurationFileLocation();
426 assertEquals(sanitizePath("etc/ves-dmaap-config.json"), configurationFileLocation);
430 public void shouldReturnDefaultExternalSchemaSchemasLocation() throws IOException {
432 String externalSchemaSchemasLocation = fromTemporaryConfiguration()
433 .getExternalSchemaSchemasLocation();
436 assertEquals("./etc/externalRepo", externalSchemaSchemasLocation);
440 public void shouldReturnDefaultExternalSchemaMappingFileLocation() throws IOException {
442 String externalSchemaMappingFileLocation = fromTemporaryConfiguration()
443 .getExternalSchemaMappingFileLocation();
446 assertEquals("./etc/externalRepo/schema-map.json", externalSchemaMappingFileLocation);
450 public void shouldReturnDefaultExternalSchemaSchemaRefPath() throws IOException {
452 String externalSchemaSchemaRefPath = fromTemporaryConfiguration()
453 .getExternalSchemaSchemaRefPath();
456 assertEquals("/event/stndDefinedFields/schemaReference", externalSchemaSchemaRefPath);
460 public void shouldReturnDefaultExternalSchemaStndDefinedDataPath() throws IOException {
462 String externalSchemaStndDefinedDataPath = fromTemporaryConfiguration()
463 .getExternalSchemaStndDefinedDataPath();
466 assertEquals("/event/stndDefinedFields/data", externalSchemaStndDefinedDataPath);
470 public void shouldReturnEnabledExternalSchema2ndStageValidation() throws IOException {
472 boolean externalSchema2ndStageValidation = fromTemporaryConfiguration("collector.externalSchema.2ndStageValidation=-1")
473 .getExternalSchemaValidationCheckflag();
476 assertFalse(externalSchema2ndStageValidation);
479 private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
481 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
482 Files.write(tempConfFile.toPath(), Arrays.asList(fileLines));
483 tempConfFile.deleteOnExit();
484 return new ApplicationSettings(new String[]{"-c", tempConfFile.toString()}, args -> processCmdLine(args), "");
487 private String sanitizePath(String path) {
488 return Paths.get(path).toString();