2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2018 - 2021 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.Ignore;
30 import org.junit.Test;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Arrays;
38 import java.util.Objects;
40 import static java.util.Collections.singletonList;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
43 import static org.junit.Assert.assertFalse;
44 import static org.junit.Assert.assertNull;
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 shouldTellIfSchemaValidationIsEnabled() throws IOException {
238 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1")
239 .eventSchemaValidationEnabled();
242 assertTrue(jsonSchemaValidationEnabled);
246 public void shouldByDefaultSchemaValidationBeDisabled() throws IOException {
248 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().eventSchemaValidationEnabled();
251 assertFalse(jsonSchemaValidationEnabled);
255 public void shouldReportValidateJSONSchemaErrorWhenJsonContainsIntegerValueNotString() throws IOException {
257 Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
260 JsonSchema schema = fromTemporaryConfiguration(
261 String.format("collector.schema.file={\"v1\": \"%s\"}", temporarySchemaFile))
265 JsonNode incorrectTestObject = new ObjectMapper().readTree("{ \"state\": 1 }");
267 assertFalse(schema.validate(incorrectTestObject).isEmpty());
272 public void shouldDoNotReportAnyValidateJSONSchemaError() throws IOException {
274 Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
277 JsonSchema schema = fromTemporaryConfiguration(
278 String.format("collector.schema.file={\"v1\": \"%s\"}", temporarySchemaFile))
282 JsonNode correctTestObject = new ObjectMapper().readTree("{ \"state\": \"hi\" }");
283 assertTrue(schema.validate(correctTestObject).isEmpty());
287 public void shouldReturnExceptionConfigFileLocation() throws IOException {
289 String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile")
290 .exceptionConfigFileLocation();
293 assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation);
297 public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException {
299 String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation();
302 assertNull(exceptionConfigFileLocation);
307 public void shouldReturnDMAAPStreamId() throws IOException {
309 Map<String, String> expected = HashMap.of(
315 Map<String, String> dmaapStreamID = fromTemporaryConfiguration(
316 "collector.dmaap.streamid=fault=ves-fault,stream1|log=ves-syslog,stream2,stream3")
317 .getDmaapStreamIds();
320 assertEquals(expected.get("log").get(), Objects.requireNonNull(dmaapStreamID).get("log").get());
321 assertEquals(expected.get("fault").get(), Objects.requireNonNull(dmaapStreamID).get("fault").get());
322 assertEquals(expected.keySet(), dmaapStreamID.keySet());
326 public void shouldReturnDefaultDMAAPStreamId() throws IOException {
328 Map<String, String> dmaapStreamID = fromTemporaryConfiguration().getDmaapStreamIds();
331 assertEquals(dmaapStreamID, HashMap.empty());
335 public void shouldAuthorizationBeDisabledByDefault() throws IOException {
337 boolean authorizationEnabled = fromTemporaryConfiguration().authMethod().contains("noAuth");
340 assertTrue(authorizationEnabled);
344 public void shouldReturnValidCredentials() throws IOException {
346 Map<String, String> allowedUsers = fromTemporaryConfiguration(
347 "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg=="
348 ).validAuthorizationCredentials();
351 assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("pasza").get());
352 assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("someoneelse").get());
356 public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
358 Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
359 validAuthorizationCredentials();
362 assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty());
366 public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
368 boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0")
369 .eventTransformingEnabled();
372 assertFalse(isEventTransformingEnabled);
376 public void shouldEventTransformingBeEnabledByDefault() throws IOException {
378 boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled();
381 assertTrue(isEventTransformingEnabled);
385 public void shouldReturnConfigurationFileLocation() throws IOException {
387 String configurationFileLocation = fromTemporaryConfiguration(
388 "collector.dmaapfile=/somewhere/etc/ves-dmaap-config.json")
389 .dMaaPConfigurationFileLocation();
392 assertEquals(sanitizePath("/somewhere/etc/ves-dmaap-config.json"), configurationFileLocation);
396 public void shouldReturnDefaultConfigurationFileLocation() throws IOException {
398 String configurationFileLocation = fromTemporaryConfiguration()
399 .dMaaPConfigurationFileLocation();
402 assertEquals(sanitizePath("etc/ves-dmaap-config.json"), configurationFileLocation);
406 public void shouldReturnDefaultExternalSchemaSchemasLocation() throws IOException {
408 String externalSchemaSchemasLocation = fromTemporaryConfiguration()
409 .getExternalSchemaSchemasLocation();
412 assertEquals(sanitizePath("./etc/externalRepo"), externalSchemaSchemasLocation);
416 public void shouldReturnDefaultExternalSchemaMappingFileLocation() throws IOException {
418 String externalSchemaMappingFileLocation = fromTemporaryConfiguration()
419 .getExternalSchemaMappingFileLocation();
422 assertEquals(sanitizePath("./etc/externalRepo/schema-map.json"), externalSchemaMappingFileLocation);
426 public void shouldReturnDefaultExternalSchemaSchemaRefPath() throws IOException {
428 String externalSchemaSchemaRefPath = fromTemporaryConfiguration()
429 .getExternalSchemaSchemaRefPath();
432 assertEquals(sanitizePath("/event/stndDefinedFields/schemaReference"), externalSchemaSchemaRefPath);
436 public void shouldReturnDefaultExternalSchemaStndDefinedDataPath() throws IOException {
438 String externalSchemaStndDefinedDataPath = fromTemporaryConfiguration()
439 .getExternalSchemaStndDefinedDataPath();
442 assertEquals(sanitizePath("/event/stndDefinedFields/data"), externalSchemaStndDefinedDataPath);
446 public void shouldReturnEnabledExternalSchema2ndStageValidation() throws IOException {
448 boolean externalSchema2ndStageValidation = fromTemporaryConfiguration("collector.externalSchema.2ndStageValidation=-1")
449 .getExternalSchemaValidationCheckflag();
452 assertFalse(externalSchema2ndStageValidation);
455 private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
457 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
458 Files.write(tempConfFile.toPath(), Arrays.asList(fileLines));
459 tempConfFile.deleteOnExit();
460 return new ApplicationSettings(new String[]{"-c", tempConfFile.toString()}, args -> processCmdLine(args), "");
463 private String sanitizePath(String path) {
464 return Paths.get(path).toString();