2ac42080030a523288c7fd2b17689004c869c133
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / ApplicationSettingsTest.java
1 package org.onap.dcae;
2
3 /*-
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 import io.vavr.collection.HashMap;
24 import io.vavr.collection.Map;
25 import org.json.JSONObject;
26 import org.junit.Test;
27
28 import java.io.File;
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;
34
35 import static java.util.Collections.singletonList;
36 import static org.junit.Assert.*;
37 import static org.onap.dcae.CLIUtils.processCmdLine;
38
39 public class ApplicationSettingsTest {
40
41     @Test
42     public void shouldMakeApplicationSettingsOutOfCLIArguments() {
43         // given
44         String[] cliArguments = {"-param1", "param1value", "-param2", "param2value"};
45
46         // when
47         ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
48         String param1value = configurationAccessor.getStringDirectly("param1");
49         String param2value = configurationAccessor.getStringDirectly("param2");
50
51         // then
52         assertEquals("param1value", param1value);
53         assertEquals("param2value", param2value);
54     }
55
56     @Test
57     public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile()
58             throws IOException {
59         // given
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()};
64
65         // when
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");
71
72         // then
73         assertEquals("param1value", param1value);
74         assertEquals("param2value", param2value);
75         assertEquals("abc", fromFileParam1Value);
76         assertEquals("zxc", fromFileParam2Value);
77     }
78
79     @Test
80     public void shouldCLIArgumentsOverrideConfigFileParameters() throws IOException {
81         // given
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();
86
87         // when
88         ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
89         String actuallyOverridenByCLIParam = configurationAccessor.getStringDirectly("section.subSection1");
90
91         // then
92         assertEquals("abc", actuallyOverridenByCLIParam);
93     }
94
95     @Test
96     public void shouldReturnHTTPPort() throws IOException {
97         // when
98         int applicationPort = fromTemporaryConfiguration("collector.service.port=8090")
99                 .httpPort();
100
101         // then
102         assertEquals(8090, applicationPort);
103     }
104
105     @Test
106     public void shouldReturnDefaultHTTPPort() throws IOException {
107         // when
108         int applicationPort = fromTemporaryConfiguration().httpPort();
109
110         // then
111         assertEquals(8080, applicationPort);
112     }
113
114     @Test
115     public void shouldReturnIfHTTPSIsEnabled() throws IOException {
116         // when
117         boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443")
118                 .httpsEnabled();
119
120         // then
121         assertTrue(httpsEnabled);
122     }
123
124     @Test
125     public void shouldReturnIfHTTPIsEnabled() throws IOException {
126         // when
127         boolean httpsEnabled = fromTemporaryConfiguration("collector.service.port=8080").httpsEnabled();
128         // then
129         assertTrue(httpsEnabled);
130     }
131
132     @Test
133     public void shouldByDefaultHTTPSBeDisabled() throws IOException {
134         // when
135         boolean httpsEnabled = fromTemporaryConfiguration().httpsEnabled();
136
137         // then
138         assertTrue(httpsEnabled);
139     }
140
141     @Test
142     public void shouldReturnHTTPSPort() throws IOException {
143         // when
144         int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443")
145                 .httpsPort();
146
147         // then
148         assertEquals(8443, httpsPort);
149     }
150
151     @Test
152     public void shouldReturnConfigurationUpdateInterval() throws IOException {
153         // when
154         int updateFrequency = fromTemporaryConfiguration("collector.dynamic.config.update.frequency=10")
155                 .configurationUpdateFrequency();
156
157         // then
158         assertEquals(10, updateFrequency);
159     }
160
161     @Test
162     public void shouldReturnDefaultConfigurationUpdateInterval() throws IOException {
163         // when
164         int updateFrequency = fromTemporaryConfiguration()
165                 .configurationUpdateFrequency();
166
167         // then
168         assertEquals(5, updateFrequency);
169     }
170
171     @Test
172     public void shouldReturnLocationOfThePasswordFile() throws IOException {
173         // when
174         String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password").keystorePasswordFileLocation();
175
176         // then
177         assertEquals(sanitizePath("/somewhere/password"), passwordFileLocation);
178     }
179
180     @Test
181     public void shouldReturnDefaultLocationOfThePasswordFile() throws IOException {
182         // when
183         String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation();
184
185         // then
186         assertEquals(sanitizePath("etc/passwordfile"), passwordFileLocation);
187     }
188
189     @Test
190     public void shouldReturnLocationOfTheKeystoreFile() throws IOException {
191         // when
192         String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore")
193                 .keystoreFileLocation();
194
195         // then
196         assertEquals(sanitizePath("/somewhere/keystore"), keystoreFileLocation);
197     }
198
199     @Test
200     public void shouldReturnLocationOfTheDefaultKeystoreFile() throws IOException {
201         // when
202         String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation();
203
204         // then
205         assertEquals(sanitizePath("etc/keystore"), keystoreFileLocation);
206     }
207
208
209     @Test
210     public void shouldReturnKeystoreAlias() throws IOException {
211         // when
212         String keystoreAlias = fromTemporaryConfiguration("collector.keystore.alias=alias").keystoreAlias();
213
214         // then
215         assertEquals("alias", keystoreAlias);
216     }
217
218     @Test
219     public void shouldReturnDefaultKeystoreAlias() throws IOException {
220         // when
221         String keystoreAlias = fromTemporaryConfiguration().keystoreAlias();
222
223         // then
224         assertEquals("tomcat", keystoreAlias);
225     }
226
227     @Test
228     public void shouldReturnDMAAPConfigFileLocation() throws IOException {
229         // when
230         String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile").dMaaPConfigurationFileLocation();
231
232         // then
233         assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation);
234     }
235
236     @Test
237     public void shouldReturnDefaultDMAAPConfigFileLocation() throws IOException {
238         // when
239         String dmaapConfigFileLocation = fromTemporaryConfiguration().dMaaPConfigurationFileLocation();
240
241         // then
242         assertEquals(sanitizePath("etc/DmaapConfig.json"), dmaapConfigFileLocation);
243     }
244
245     @Test
246     public void shouldReturnMaximumAllowedQueuedEvents() throws IOException {
247         // when
248         int maximumAllowedQueuedEvents = fromTemporaryConfiguration("collector.inputQueue.maxPending=10000")
249                 .maximumAllowedQueuedEvents();
250
251         // then
252         assertEquals(10000, maximumAllowedQueuedEvents);
253     }
254
255     @Test
256     public void shouldReturnDefaultMaximumAllowedQueuedEvents() throws IOException {
257         // when
258         int maximumAllowedQueuedEvents = fromTemporaryConfiguration().maximumAllowedQueuedEvents();
259
260         // then
261         assertEquals(1024 * 4, maximumAllowedQueuedEvents);
262     }
263
264     @Test
265     public void shouldTellIfSchemaValidationIsEnabled() throws IOException {
266         // when
267         boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1")
268                 .jsonSchemaValidationEnabled();
269
270         // then
271         assertTrue(jsonSchemaValidationEnabled);
272     }
273
274     @Test
275     public void shouldByDefaultSchemaValidationBeDisabled() throws IOException {
276         // when
277         boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().jsonSchemaValidationEnabled();
278
279         // then
280         assertFalse(jsonSchemaValidationEnabled);
281     }
282
283     @Test
284     public void shouldReturnJSONSchema() throws IOException {
285         // when
286         JSONObject jsonSchema = fromTemporaryConfiguration("collector.schema.file={\"v1\": {}}")
287                 .jsonSchema();
288
289         // then
290         assertEquals(new JSONObject("{\"v1\": {}}").toMap(), jsonSchema.toMap());
291     }
292
293     @Test
294     public void shouldReturnDefaultJSONSchema() throws IOException {
295         // when
296         JSONObject jsonSchema = fromTemporaryConfiguration().jsonSchema();
297
298         // then
299         assertEquals(new JSONObject("{\"v5\":\"./etc/CommonEventFormat_28.3.json\"}").toMap(), jsonSchema.toMap());
300     }
301
302     @Test
303     public void shouldReturnExceptionConfigFileLocation() throws IOException {
304         // when
305         String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile")
306                 .exceptionConfigFileLocation();
307
308         // then
309         assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation);
310     }
311
312     @Test
313     public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException {
314         // when
315         String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation();
316
317         // then
318         assertNull(exceptionConfigFileLocation);
319     }
320
321
322     @Test
323     public void shouldReturnDMAAPStreamId() throws IOException {
324         // given
325         Map<String, String[]> expected = HashMap.of(
326                 "s", new String[]{"something", "something2"},
327                 "s2", new String[]{"something3"}
328         );
329
330         // when
331         Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration("collector.dmaap.streamid=s=something,something2|s2=something3")
332                 .dMaaPStreamsMapping();
333
334         // then
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());
338     }
339
340     @Test
341     public void shouldReturnDefaultDMAAPStreamId() throws IOException {
342         // when
343         Map<String, String[]> dmaapStreamID = fromTemporaryConfiguration().dMaaPStreamsMapping();
344
345         // then
346         assertEquals(dmaapStreamID, HashMap.empty());
347     }
348
349     @Test
350     public void shouldReturnIfAuthorizationIsEnabled() throws IOException {
351         // when
352         boolean authorizationEnabled = fromTemporaryConfiguration("header.authflag=1")
353                 .authorizationEnabled();
354
355         // then
356         assertTrue(authorizationEnabled);
357     }
358
359     @Test
360     public void shouldAuthorizationBeDisabledByDefault() throws IOException {
361         // when
362         boolean authorizationEnabled = fromTemporaryConfiguration().authorizationEnabled();
363
364         // then
365         assertFalse(authorizationEnabled);
366     }
367
368     @Test
369     public void shouldReturnValidCredentials() throws IOException {
370         // when
371         Map<String, String> allowedUsers = fromTemporaryConfiguration(
372                 "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg=="
373         ).validAuthorizationCredentials();
374
375         // then
376         assertEquals(allowedUsers.get("pasza").get(), "simplepassword");
377         assertEquals(allowedUsers.get("someoneelse").get(), "simplepassword");
378     }
379
380     @Test
381     public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
382         // when
383         Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
384                 validAuthorizationCredentials();
385
386         // then
387         assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty());
388     }
389
390     @Test
391     public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
392         // when
393         boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0")
394                 .eventTransformingEnabled();
395
396         // then
397         assertFalse(isEventTransformingEnabled);
398     }
399
400     @Test
401     public void shouldEventTransformingBeEnabledByDefault() throws IOException {
402         // when
403         boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled();
404
405         // then
406         assertTrue(isEventTransformingEnabled);
407     }
408
409     @Test
410     public void shouldReturnCambriaConfigurationFileLocation() throws IOException {
411         // when
412         String cambriaConfigurationFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapConfig")
413                 .dMaaPConfigurationFileLocation();
414
415         // then
416         assertEquals(sanitizePath("/somewhere/dmaapConfig"), cambriaConfigurationFileLocation);
417     }
418
419     @Test
420     public void shouldReturnDefaultCambriaConfigurationFileLocation() throws IOException {
421         // when
422         String cambriaConfigurationFileLocation = fromTemporaryConfiguration()
423                 .dMaaPConfigurationFileLocation();
424
425         // then
426         assertEquals(sanitizePath("etc/DmaapConfig.json"), cambriaConfigurationFileLocation);
427     }
428
429     private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
430             throws IOException {
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), "");
435     }
436
437     private String sanitizePath(String path) {
438         return Paths.get(path).toString();
439     }
440 }