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