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.util.Arrays;
32 import java.util.Objects;
34 import static java.util.Collections.singletonList;
35 import static org.junit.Assert.*;
36 import static org.onap.dcae.CLIUtils.processCmdLine;
38 public class ApplicationSettingsTest {
41 public void shouldMakeApplicationSettingsOutOfCLIArguments() {
43 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value"};
46 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
47 String param1value = configurationAccessor.getStringDirectly("param1");
48 String param2value = configurationAccessor.getStringDirectly("param2");
51 assertEquals("param1value", param1value);
52 assertEquals("param2value", param2value);
56 public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile()
59 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
60 Files.write(tempConfFile.toPath(), Arrays.asList("section.subSection1=abc", "section.subSection2=zxc"));
61 tempConfFile.deleteOnExit();
62 String[] cliArguments = {"-param1", "param1value", "-param2", "param2value", "-c", tempConfFile.toString()};
65 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
66 String param1value = configurationAccessor.getStringDirectly("param1");
67 String param2value = configurationAccessor.getStringDirectly("param2");
68 String fromFileParam1Value = configurationAccessor.getStringDirectly("section.subSection1");
69 String fromFileParam2Value = configurationAccessor.getStringDirectly("section.subSection2");
72 assertEquals("param1value", param1value);
73 assertEquals("param2value", param2value);
74 assertEquals("abc", fromFileParam1Value);
75 assertEquals("zxc", fromFileParam2Value);
79 public void shouldCLIArgumentsOverrideConfigFileParameters() throws IOException {
81 String[] cliArguments = {"-section.subSection1", "abc"};
82 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
83 Files.write(tempConfFile.toPath(), singletonList("section.subSection1=zxc"));
84 tempConfFile.deleteOnExit();
87 ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
88 String actuallyOverridenByCLIParam = configurationAccessor.getStringDirectly("section.subSection1");
91 assertEquals("abc", actuallyOverridenByCLIParam);
95 public void shouldReturnHTTPPort() throws IOException {
97 int applicationPort = fromTemporaryConfiguration("collector.service.port=8090")
101 assertEquals(8090, applicationPort);
105 public void shouldReturnDefaultHTTPPort() throws IOException {
107 int applicationPort = fromTemporaryConfiguration().httpPort();
110 assertEquals(8080, applicationPort);
114 public void shouldReturnIfHTTPSIsEnabled() throws IOException {
116 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443")
120 assertTrue(httpsEnabled);
124 public void shouldReturnIfHTTPIsEnabled() throws IOException {
126 boolean httpsEnabled = fromTemporaryConfiguration("collector.service.port=8080").httpsEnabled();
128 assertTrue(httpsEnabled);
132 public void shouldByDefaultHTTPSBeDisabled() throws IOException {
134 boolean httpsEnabled = fromTemporaryConfiguration().httpsEnabled();
137 assertTrue(httpsEnabled);
141 public void shouldReturnHTTPSPort() throws IOException {
143 int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443")
147 assertEquals(8443, httpsPort);
151 public void shouldReturnLocationOfThePasswordFile() throws IOException {
153 String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password").keystorePasswordFileLocation();
156 assertEquals("/somewhere/password", passwordFileLocation);
160 public void shouldReturnDefaultLocationOfThePasswordFile() throws IOException {
162 String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation();
165 assertEquals("./etc/passwordfile", passwordFileLocation);
169 public void shouldReturnLocationOfTheKeystoreFile() throws IOException {
171 String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore")
172 .keystoreFileLocation();
175 assertEquals("/somewhere/keystore", keystoreFileLocation);
179 public void shouldReturnLocationOfTheDefaultKeystoreFile() throws IOException {
181 String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation();
184 assertEquals("../etc/keystore", keystoreFileLocation);
189 public void shouldReturnKeystoreAlias() throws IOException {
191 String keystoreAlias = fromTemporaryConfiguration("collector.keystore.alias=alias").keystoreAlias();
194 assertEquals("alias", keystoreAlias);
198 public void shouldReturnDefaultKeystoreAlias() throws IOException {
200 String keystoreAlias = fromTemporaryConfiguration().keystoreAlias();
203 assertEquals("tomcat", keystoreAlias);
207 public void shouldReturnDMAAPConfigFileLocation() throws IOException {
209 String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile").cambriaConfigurationFileLocation();
212 assertEquals("/somewhere/dmaapFile", dmaapConfigFileLocation);
216 public void shouldReturnDefaultDMAAPConfigFileLocation() throws IOException {
218 String dmaapConfigFileLocation = fromTemporaryConfiguration().cambriaConfigurationFileLocation();
221 assertEquals("./etc/DmaapConfig.json", dmaapConfigFileLocation);
225 public void shouldReturnMaximumAllowedQueuedEvents() throws IOException {
227 int maximumAllowedQueuedEvents = fromTemporaryConfiguration("collector.inputQueue.maxPending=10000")
228 .maximumAllowedQueuedEvents();
231 assertEquals(10000, maximumAllowedQueuedEvents);
235 public void shouldReturnDefaultMaximumAllowedQueuedEvents() throws IOException {
237 int maximumAllowedQueuedEvents = fromTemporaryConfiguration().maximumAllowedQueuedEvents();
240 assertEquals(1024 * 4, maximumAllowedQueuedEvents);
244 public void shouldTellIfSchemaValidationIsEnabled() throws IOException {
246 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1")
247 .jsonSchemaValidationEnabled();
250 assertTrue(jsonSchemaValidationEnabled);
254 public void shouldByDefaultSchemaValidationBeDisabled() throws IOException {
256 boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().jsonSchemaValidationEnabled();
259 assertFalse(jsonSchemaValidationEnabled);
263 public void shouldReturnJSONSchema() throws IOException {
265 JSONObject jsonSchema = fromTemporaryConfiguration("collector.schema.file={\"v1\": {}}")
269 assertEquals(new JSONObject("{\"v1\": {}}").toMap(), jsonSchema.toMap());
273 public void shouldReturnDefaultJSONSchema() throws IOException {
275 JSONObject jsonSchema = fromTemporaryConfiguration().jsonSchema();
278 assertEquals(new JSONObject("{\"v5\":\"./etc/CommonEventFormat_28.3.json\"}").toMap(), jsonSchema.toMap());
282 public void shouldReturnExceptionConfigFileLocation() throws IOException {
284 String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile")
285 .exceptionConfigFileLocation();
288 assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation);
292 public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException {
294 String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation();
297 assertNull(exceptionConfigFileLocation);
302 public void shouldReturnDMAAPStreamId() throws IOException {
304 Map<String, String[]> expected = HashMap.of(
305 "s", new String[]{"something", "something2"},
306 "s2", new String[]{"something3"}
310 Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration("collector.dmaap.streamid=s=something,something2|s2=something3")
311 .dMaaPStreamsMapping();
314 assertArrayEquals(expected.get("s").get(), Objects.requireNonNull(dmaapStreamID).get("s").get());
315 assertArrayEquals(expected.get("s2").get(), Objects.requireNonNull(dmaapStreamID).get("s2").get());
316 assertEquals(expected.keySet(), dmaapStreamID.keySet());
320 public void shouldReturnDefaultDMAAPStreamId() throws IOException {
322 Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration().dMaaPStreamsMapping();
325 assertEquals(dmaapStreamID, HashMap.empty());
329 public void shouldReturnIfAuthorizationIsEnabled() throws IOException {
331 boolean authorizationEnabled = fromTemporaryConfiguration("header.authflag=1")
332 .authorizationEnabled();
335 assertTrue(authorizationEnabled);
339 public void shouldAuthorizationBeDisabledByDefault() throws IOException {
341 boolean authorizationEnabled = fromTemporaryConfiguration().authorizationEnabled();
344 assertFalse(authorizationEnabled);
348 public void shouldReturnValidCredentials() throws IOException {
350 String userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration(
351 "header.authlist=pasza,123jsad1|someoneelse,12asd31"
352 ).validAuthorizationCredentials();
355 assertEquals("pasza,123jsad1|someoneelse,12asd31", userToBase64PasswordDelimitedByCommaSeparatedByPipes);
359 public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
361 String userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
362 validAuthorizationCredentials();
365 assertNull(userToBase64PasswordDelimitedByCommaSeparatedByPipes);
370 public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
372 boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0")
373 .eventTransformingEnabled();
376 assertFalse(isEventTransformingEnabled);
380 public void shouldEventTransformingBeEnabledByDefault() throws IOException {
382 boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled();
385 assertTrue(isEventTransformingEnabled);
389 public void shouldReturnCambriaConfigurationFileLocation() throws IOException {
391 String cambriaConfigurationFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapConfig")
392 .cambriaConfigurationFileLocation();
395 assertEquals("/somewhere/dmaapConfig", cambriaConfigurationFileLocation);
399 public void shouldReturnDefaultCambriaConfigurationFileLocation() throws IOException {
401 String cambriaConfigurationFileLocation = fromTemporaryConfiguration()
402 .cambriaConfigurationFileLocation();
405 assertEquals("./etc/DmaapConfig.json", cambriaConfigurationFileLocation);
408 private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
410 File tempConfFile = File.createTempFile("doesNotMatter", "doesNotMatter");
411 Files.write(tempConfFile.toPath(), Arrays.asList(fileLines));
412 tempConfFile.deleteOnExit();
413 return new ApplicationSettings(new String[]{"-c", tempConfFile.toString()}, args -> processCmdLine(args));