0fdd41c949b207cb94b592e0a062fb8fd562e98e
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-2019 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.config.impl
21
22 import arrow.core.identity
23 import org.assertj.core.api.Assertions.assertThat
24 import org.jetbrains.spek.api.Spek
25 import org.jetbrains.spek.api.dsl.describe
26 import org.jetbrains.spek.api.dsl.given
27 import org.jetbrains.spek.api.dsl.it
28 import org.jetbrains.spek.api.dsl.on
29 import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError
30 import org.onap.dcae.collectors.veshv.tests.utils.absoluteResourcePath
31 import java.io.File
32
33 /**
34  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
35  * @since May 2018
36  */
37 object HvVesCommandLineParserTest : Spek({
38     lateinit var cut: HvVesCommandLineParser
39     val DEFAULT_HEALTHCHECK_PORT = 6060
40     val emptyConfig = ""
41     val configFilePath = javaClass.absoluteResourcePath("sampleConfig.json")
42
43     beforeEachTest {
44         cut = HvVesCommandLineParser()
45     }
46
47     describe("parsing arguments") {
48         given("all parameters are present in the long form") {
49             lateinit var result: File
50
51             beforeEachTest {
52                 result = cut.parseFileExpectingSuccess(
53                         "--configuration-file", configFilePath
54                 )
55             }
56
57             it("should read proper configuration file") {
58                 assertThat(result.exists()).isTrue()
59             }
60         }
61
62         given("required parameter is absent") {
63             on("missing configuration file path") {
64                 it("should throw exception") {
65                     assertThat(
66                             cut.parseFileExpectingFailure(
67                                     "--non-existing-option", emptyConfig
68                             )
69                     ).isInstanceOf(WrongArgumentError::class.java)
70                 }
71             }
72         }
73
74         given("healthcheck port defined via cmd") {
75             val healthCheckPort = 888
76             val configWithHealthcheckPort = "--health-check-api-port $healthCheckPort"
77             on("parsing command") {
78                 it("should assign proper port") {
79                     assertThat(
80                             cut.getHealthcheckPort(arrayOf(configWithHealthcheckPort))
81                     ).isEqualTo(healthCheckPort)
82                 }
83             }
84         }
85
86         given("no healthcheck port defined via cmd") {
87             on("parsing command") {
88                 it("should return default port") {
89                     assertThat(
90                             cut.getHealthcheckPort(arrayOf(emptyConfig))
91                     ).isEqualTo(DEFAULT_HEALTHCHECK_PORT)
92                 }
93             }
94         }
95     }
96 })
97
98 private fun HvVesCommandLineParser.parseFileExpectingSuccess(vararg cmdLine: String): File =
99         getConfigurationFile(cmdLine).fold(
100                 { throw AssertionError("Parsing result should be present") },
101                 ::identity
102         )
103
104 private fun HvVesCommandLineParser.parseFileExpectingFailure(vararg cmdLine: String): WrongArgumentError =
105         getConfigurationFile(cmdLine).fold(
106                 ::identity
107         ) { throw AssertionError("parsing should have failed") }