b73a788e36f293a796e9333a4683178874acad06
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 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.simulators.dcaeapp.config
21
22 import arrow.core.Failure
23 import arrow.core.Success
24 import arrow.core.identity
25 import org.assertj.core.api.Assertions.assertThat
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.given
29 import org.jetbrains.spek.api.dsl.it
30 import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentError
31
32
33 internal class ArgBasedDcaeAppSimConfigurationTest : Spek({
34
35     lateinit var cut: ArgBasedDcaeAppSimConfiguration
36     val kafkaBootstrapServers = "localhosting:123,localhostinger:12345"
37     val kafkaTopics = "top1,top2"
38
39     beforeEachTest {
40         cut = ArgBasedDcaeAppSimConfiguration()
41     }
42
43     fun parseExpectingSuccess(vararg cmdLine: String): DcaeAppSimConfiguration =
44             cut.parse(cmdLine).fold(
45                     { throw AssertionError("Parsing result should be present") },
46                     ::identity
47             )
48
49     fun parseExpectingFailure(vararg cmdLine: String) =
50             cut.parse(cmdLine).fold(
51                     ::identity,
52                     { throw AssertionError("parsing should have failed") }
53             )
54
55     describe("parsing arguments") {
56         lateinit var result: DcaeAppSimConfiguration
57
58         given("all parameters are present in the long form") {
59
60             beforeEachTest {
61                 result = parseExpectingSuccess("--listen-port", "6969",
62                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
63                         "--kafka-topics", kafkaTopics
64                 )
65             }
66
67             it("should set proper port") {
68                 assertThat(result.apiPort).isEqualTo(6969)
69             }
70
71
72             it("should set proper kafka boostrap servers") {
73                 assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
74             }
75
76             it("should set proper kafka topics") {
77                 assertThat(result.kafkaTopics).isEqualTo(
78                         setOf("top1", "top2")
79                 )
80             }
81         }
82
83         given("some parameters are present in the short form") {
84
85             beforeEachTest {
86                 result = parseExpectingSuccess("-p", "666",
87                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
88                         "-f", kafkaTopics)
89             }
90
91             it("should set proper port") {
92                 assertThat(result.apiPort).isEqualTo(666)
93             }
94
95             it("should set proper kafka boostrap servers") {
96                 assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
97             }
98
99             it("should set proper kafka topics") {
100                 assertThat(result.kafkaTopics).isEqualTo(
101                         setOf("top1", "top2")
102                 )
103             }
104         }
105
106         given("all optional parameters are absent") {
107
108             beforeEachTest {
109                 result = parseExpectingSuccess("-s", kafkaBootstrapServers, "-f", kafkaTopics)
110             }
111
112             it("should set default port") {
113                 assertThat(result.apiPort).isEqualTo(DefaultValues.API_PORT)
114             }
115         }
116
117         describe("required parameter is absent") {
118             given("kafka topics are missing") {
119                 it("should throw exception") {
120                     assertThat(parseExpectingFailure("-s", kafkaBootstrapServers))
121                             .isInstanceOf(WrongArgumentError::class.java)
122                 }
123             }
124
125             given("kafka bootstrap servers are missing") {
126                 it("should throw exception") {
127                     assertThat(parseExpectingFailure("-f", kafkaTopics))
128                             .isInstanceOf(WrongArgumentError::class.java)
129                 }
130             }
131         }
132     }
133 })