5ca64e3eba8e34b89022df70711b192c90300b06
[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.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.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentError
29
30
31 internal class ArgDcaeAppSimulatorConfigurationTest : Spek({
32
33     lateinit var cut: ArgDcaeAppSimConfiguration
34     val listenPort = "1234"
35     val kafkaBootstrapServers = "localhosting:123,localhostinger:12345"
36     val kafkaTopics = "top1,top2"
37
38     beforeEachTest {
39         cut = ArgDcaeAppSimConfiguration()
40     }
41
42     fun parseExpectingSuccess(vararg cmdLine: String): DcaeAppSimConfiguration =
43             cut.parse(cmdLine).fold(
44                     { throw AssertionError("Parsing result should be present") },
45                     ::identity
46             )
47
48     fun parseExpectingFailure(vararg cmdLine: String) =
49             cut.parse(cmdLine).fold(
50                     ::identity,
51                     { throw AssertionError("parsing should have failed") }
52             )
53
54     describe("parsing arguments") {
55         lateinit var result: DcaeAppSimConfiguration
56
57         given("all parameters are present in the long form") {
58
59             beforeEachTest {
60                 result = parseExpectingSuccess(
61                         "--listen-port", listenPort,
62                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
63                         "--kafka-topics", kafkaTopics
64                 )
65             }
66
67             it("should set proper port") {
68                 assertThat(result.apiPort).isEqualTo(listenPort.toInt())
69             }
70
71
72             it("should set proper kafka bootstrap 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(
87                         "-p", listenPort,
88                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
89                         "-f", kafkaTopics)
90             }
91
92             it("should set proper port") {
93                 assertThat(result.apiPort).isEqualTo(listenPort.toInt())
94             }
95
96             it("should set proper kafka bootstrap servers") {
97                 assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
98             }
99
100             it("should set proper kafka topics") {
101                 assertThat(result.kafkaTopics).isEqualTo(
102                         setOf("top1", "top2")
103                 )
104             }
105         }
106
107         describe("required parameter is absent") {
108             given("kafka topics are missing") {
109                 it("should throw exception") {
110                     assertThat(parseExpectingFailure(
111                             "-p", listenPort,
112                             "-s", kafkaBootstrapServers
113                     )).isInstanceOf(WrongArgumentError::class.java)
114                 }
115             }
116
117             given("kafka bootstrap servers is missing") {
118                 it("should throw exception") {
119                     assertThat(parseExpectingFailure(
120                             "-p", listenPort,
121                             "-f", kafkaTopics
122                     )).isInstanceOf(WrongArgumentError::class.java)
123                 }
124             }
125
126             given("listen port is missing") {
127                 it("should throw exception") {
128                     assertThat(parseExpectingFailure(
129                             "-p", kafkaTopics,
130                             "-s", kafkaBootstrapServers
131                     )).isInstanceOf(WrongArgumentError::class.java)
132                 }
133             }
134         }
135     }
136 })