VESCollector Test optimization
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / ApplicationSettingsTest.java
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.dcae;
23
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;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.net.URI;
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;
41
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;
49
50 public class ApplicationSettingsTest {
51     
52     /**
53     * The Unix separator character.
54     */
55     private static final char UNIX_SEPARATOR = '/';
56     
57     /**
58     * The Windows separator character.
59     */
60     private static final char WINDOWS_SEPARATOR = '\\';    
61
62     private static final String SAMPLE_JSON_SCHEMA = "{"
63             + "  \"type\": \"object\","
64             + "  \"properties\": {"
65             + "     \"state\": { \"type\": \"string\" }"
66             + "  }"
67             + "}";
68     
69     /**
70     * Converts all separators to the Unix separator of forward slash.
71     *
72     * @param path  the path to be changed, null ignored
73     * @return the updated path
74     */    
75     private static String separatorsToUnix(final String path) {
76         if (path == null || path.indexOf(WINDOWS_SEPARATOR) == -1) {
77             return path;
78         }
79         return path.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR);
80     }    
81
82     @Test
83     public void shouldMakeApplicationSettingsOutOfCLIArguments() {
84         // given
85         String[] cliArguments = {"-param1", "param1value", "-param2", "param2value"};
86
87         // when
88         ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
89         String param1value = configurationAccessor.getStringDirectly("param1");
90         String param2value = configurationAccessor.getStringDirectly("param2");
91
92         // then
93         assertEquals("param1value", param1value);
94         assertEquals("param2value", param2value);
95     }
96
97     @Test
98     public void shouldMakeApplicationSettingsOutOfCLIArgumentsAndAConfigurationFile()
99             throws IOException {
100         // given
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()};
105
106         // when
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");
112
113         // then
114         assertEquals("param1value", param1value);
115         assertEquals("param2value", param2value);
116         assertEquals("abc", fromFileParam1Value);
117         assertEquals("zxc", fromFileParam2Value);
118     }
119
120     @Test
121     public void shouldCLIArgumentsOverrideConfigFileParameters() throws IOException {
122         // given
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();
127
128         // when
129         ApplicationSettings configurationAccessor = new ApplicationSettings(cliArguments, CLIUtils::processCmdLine);
130         String actuallyOverridenByCLIParam = configurationAccessor.getStringDirectly("section.subSection1");
131
132         // then
133         assertEquals("abc", actuallyOverridenByCLIParam);
134     }
135
136     @Test
137     public void shouldReturnHTTPPort() throws IOException {
138         // when
139         int applicationPort = fromTemporaryConfiguration("collector.service.port=8090")
140                 .httpPort();
141         // then
142         assertEquals(8090, applicationPort);
143     }
144
145     @Test
146     public void shouldReturnDefaultHTTPPort() throws IOException {
147         // when
148         int applicationPort = fromTemporaryConfiguration().httpPort();
149
150         // then
151         assertEquals(8080, applicationPort);
152     }
153
154     @Test
155     public void shouldReturnIfHTTPSIsEnabled() throws IOException {
156         // when
157         boolean httpsEnabled = fromTemporaryConfiguration("collector.service.secure.port=8443")
158                 .httpsEnabled();
159
160         // then
161         assertTrue(httpsEnabled);
162     }
163
164     @Test
165     public void shouldReturnIfHTTPIsEnabled() throws IOException {
166         // when
167         boolean httpsEnabled = fromTemporaryConfiguration("collector.service.port=8080").httpsEnabled();
168         // then
169         assertTrue(httpsEnabled);
170     }
171
172     @Test
173     public void shouldByDefaultHTTPSBeDisabled() throws IOException {
174         // when
175         boolean httpsEnabled = fromTemporaryConfiguration().httpsEnabled();
176
177         // then
178         assertTrue(httpsEnabled);
179     }
180
181     @Test
182     public void shouldReturnHTTPSPort() throws IOException {
183         // when
184         int httpsPort = fromTemporaryConfiguration("collector.service.secure.port=8443")
185                 .httpsPort();
186
187         // then
188         assertEquals(8443, httpsPort);
189     }
190
191     @Test
192     public void shouldReturnConfigurationUpdateInterval() throws IOException {
193         // when
194         int updateFrequency = fromTemporaryConfiguration("collector.dynamic.config.update.frequency=10")
195                 .configurationUpdateFrequency();
196
197         // then
198         assertEquals(10, updateFrequency);
199     }
200
201     @Test
202     public void shouldReturnDefaultConfigurationUpdateInterval() throws IOException {
203         // when
204         int updateFrequency = fromTemporaryConfiguration()
205                 .configurationUpdateFrequency();
206
207         // then
208         assertEquals(5, updateFrequency);
209     }
210
211     @Test
212     public void shouldReturnLocationOfThePasswordFile() throws IOException {
213         // when
214         String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password")
215                 .keystorePasswordFileLocation();
216
217         // then
218         assertEquals(sanitizePath("/somewhere/password"), passwordFileLocation);
219     }
220
221     @Test
222     public void shouldReturnDefaultLocationOfThePasswordFile() throws IOException {
223         // when
224         String passwordFileLocation = fromTemporaryConfiguration().keystorePasswordFileLocation();
225
226         // then
227         assertEquals(sanitizePath("etc/passwordfile"), passwordFileLocation);
228     }
229
230     @Test
231     public void shouldReturnLocationOfTheKeystoreFile() throws IOException {
232         // when
233         String keystoreFileLocation = fromTemporaryConfiguration("collector.keystore.file.location=/somewhere/keystore")
234                 .keystoreFileLocation();
235
236         // then
237         assertEquals(sanitizePath("/somewhere/keystore"), keystoreFileLocation);
238     }
239
240     @Test
241     public void shouldReturnLocationOfTheDefaultKeystoreFile() throws IOException {
242         // when
243         String keystoreFileLocation = fromTemporaryConfiguration().keystoreFileLocation();
244
245         // then
246         assertEquals(sanitizePath("etc/keystore"), keystoreFileLocation);
247     }
248
249     @Test
250     public void shouldReturnDMAAPConfigFileLocation() throws IOException {
251         // when
252         String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile")
253                 .dMaaPConfigurationFileLocation();
254
255         // then
256         assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation);
257     }
258
259     @Test
260     public void shouldTellIfSchemaValidationIsEnabled() throws IOException {
261         // when
262         boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration("collector.schema.checkflag=1")
263                 .eventSchemaValidationEnabled();
264
265         // then
266         assertTrue(jsonSchemaValidationEnabled);
267     }
268
269     @Test
270     public void shouldByDefaultSchemaValidationBeDisabled() throws IOException {
271         // when
272         boolean jsonSchemaValidationEnabled = fromTemporaryConfiguration().eventSchemaValidationEnabled();
273
274         // then
275         assertFalse(jsonSchemaValidationEnabled);
276     }
277
278     @Test
279     public void shouldReportValidateJSONSchemaErrorWhenJsonContainsIntegerValueNotString() throws IOException, URISyntaxException {
280         // when
281         Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
282         String normalizedSchemaFile = separatorsToUnix(temporarySchemaFile.toString());
283         // when
284         JsonSchema schema = fromTemporaryConfiguration(
285                 String.format("collector.schema.file={\"v1\": \"%s\"}", normalizedSchemaFile))
286                 .jsonSchema("v1");
287
288         // then
289         JsonNode incorrectTestObject = new ObjectMapper().readTree("{ \"state\": 1 }");
290
291         assertFalse(schema.validate(incorrectTestObject).isEmpty());
292
293     }
294
295     @Test
296     public void shouldDoNotReportAnyValidateJSONSchemaError() throws IOException {
297         // when
298         Path temporarySchemaFile = createTemporaryFile(SAMPLE_JSON_SCHEMA);
299         String normalizedSchemaFile = separatorsToUnix(temporarySchemaFile.toString());
300         // when
301         JsonSchema schema = fromTemporaryConfiguration(
302                 String.format("collector.schema.file={\"v1\": \"%s\"}", normalizedSchemaFile))
303                 .jsonSchema("v1");
304
305         // then
306         JsonNode correctTestObject = new ObjectMapper().readTree("{ \"state\": \"hi\" }");
307         assertTrue(schema.validate(correctTestObject).isEmpty());
308     }
309
310     @Test
311     public void shouldReturnExceptionConfigFileLocation() throws IOException {
312         // when
313         String exceptionConfigFileLocation = fromTemporaryConfiguration("exceptionConfig=/somewhere/exceptionFile")
314                 .exceptionConfigFileLocation();
315
316         // then
317         assertEquals("/somewhere/exceptionFile", exceptionConfigFileLocation);
318     }
319
320     @Test
321     public void shouldReturnDefaultExceptionConfigFileLocation() throws IOException {
322         // when
323         String exceptionConfigFileLocation = fromTemporaryConfiguration().exceptionConfigFileLocation();
324
325         // then
326         assertNull(exceptionConfigFileLocation);
327     }
328
329
330     @Test
331     public void shouldReturnDMAAPStreamId() throws IOException {
332         // given
333         Map<String, String> expected = HashMap.of(
334                 "log", "ves-syslog",
335                 "fault", "ves-fault"
336         );
337
338         // when
339         Map<String, String> dmaapStreamID = fromTemporaryConfiguration(
340                 "collector.dmaap.streamid=fault=ves-fault,stream1|log=ves-syslog,stream2,stream3")
341                 .getDmaapStreamIds();
342
343         // then
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());
347     }
348
349     @Test
350     public void shouldReturnDefaultDMAAPStreamId() throws IOException {
351         // when
352         Map<String, String> dmaapStreamID = fromTemporaryConfiguration().getDmaapStreamIds();
353
354         // then
355         assertEquals(dmaapStreamID, HashMap.empty());
356     }
357
358     @Test
359     public void shouldAuthorizationBeDisabledByDefault() throws IOException {
360         // when
361         boolean authorizationEnabled = fromTemporaryConfiguration().authMethod().contains("noAuth");
362
363         // then
364         assertTrue(authorizationEnabled);
365     }
366
367     @Test
368     public void shouldReturnValidCredentials() throws IOException {
369         // when
370         Map<String, String> allowedUsers = fromTemporaryConfiguration(
371                 "header.authlist=pasza,c2ltcGxlcGFzc3dvcmQNCg==|someoneelse,c2ltcGxlcGFzc3dvcmQNCg=="
372         ).validAuthorizationCredentials();
373
374         // then
375         assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("pasza").get());
376         assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", allowedUsers.get("someoneelse").get());
377     }
378
379     @Test
380     public void shouldbyDefaultThereShouldBeNoValidCredentials() throws IOException {
381         // when
382         Map<String, String> userToBase64PasswordDelimitedByCommaSeparatedByPipes = fromTemporaryConfiguration().
383                 validAuthorizationCredentials();
384
385         // then
386         assertTrue(userToBase64PasswordDelimitedByCommaSeparatedByPipes.isEmpty());
387     }
388
389     @Test
390     public void shouldReturnIfEventTransformingIsEnabled() throws IOException {
391         // when
392         boolean isEventTransformingEnabled = fromTemporaryConfiguration("event.transform.flag=0")
393                 .eventTransformingEnabled();
394
395         // then
396         assertFalse(isEventTransformingEnabled);
397     }
398
399     @Test
400     public void shouldEventTransformingBeEnabledByDefault() throws IOException {
401         // when
402         boolean isEventTransformingEnabled = fromTemporaryConfiguration().eventTransformingEnabled();
403
404         // then
405         assertTrue(isEventTransformingEnabled);
406     }
407
408     @Test
409     public void shouldReturnConfigurationFileLocation() throws IOException {
410         // when
411         String configurationFileLocation = fromTemporaryConfiguration(
412                 "collector.dmaapfile=/somewhere/etc/ves-dmaap-config.json")
413                 .dMaaPConfigurationFileLocation();
414
415         // then
416         assertEquals(sanitizePath("/somewhere/etc/ves-dmaap-config.json"), configurationFileLocation);
417     }
418
419     @Test
420     public void shouldReturnDefaultConfigurationFileLocation() throws IOException {
421         // when
422         String configurationFileLocation = fromTemporaryConfiguration()
423                 .dMaaPConfigurationFileLocation();
424
425         // then
426         assertEquals(sanitizePath("etc/ves-dmaap-config.json"), configurationFileLocation);
427     }
428
429     @Test
430     public void shouldReturnDefaultExternalSchemaSchemasLocation() throws IOException {
431         //when
432         String externalSchemaSchemasLocation = fromTemporaryConfiguration()
433                 .getExternalSchemaSchemasLocation();
434
435         //then
436         assertEquals("./etc/externalRepo", externalSchemaSchemasLocation);
437     }
438
439     @Test
440     public void shouldReturnDefaultExternalSchemaMappingFileLocation() throws IOException {
441         //when
442         String externalSchemaMappingFileLocation = fromTemporaryConfiguration()
443                 .getExternalSchemaMappingFileLocation();
444
445         //then
446         assertEquals("./etc/externalRepo/schema-map.json", externalSchemaMappingFileLocation);
447     }
448
449     @Test
450     public void shouldReturnDefaultExternalSchemaSchemaRefPath() throws IOException {
451         //when
452         String externalSchemaSchemaRefPath = fromTemporaryConfiguration()
453                 .getExternalSchemaSchemaRefPath();
454
455         //then
456         assertEquals("/event/stndDefinedFields/schemaReference", externalSchemaSchemaRefPath);
457     }
458
459     @Test
460     public void shouldReturnDefaultExternalSchemaStndDefinedDataPath() throws IOException {
461         //when
462         String externalSchemaStndDefinedDataPath = fromTemporaryConfiguration()
463                 .getExternalSchemaStndDefinedDataPath();
464
465         //then
466         assertEquals("/event/stndDefinedFields/data", externalSchemaStndDefinedDataPath);
467     }
468
469     @Test
470     public void shouldReturnEnabledExternalSchema2ndStageValidation() throws IOException {
471         //when
472         boolean externalSchema2ndStageValidation = fromTemporaryConfiguration("collector.externalSchema.2ndStageValidation=-1")
473                 .getExternalSchemaValidationCheckflag();
474
475         //then
476         assertFalse(externalSchema2ndStageValidation);
477     }
478
479     private static ApplicationSettings fromTemporaryConfiguration(String... fileLines)
480             throws IOException {
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), "");
485     }
486
487     private String sanitizePath(String path) {
488         return Paths.get(path).toString();
489     }
490 }