bb0bfe1df98ea2da8259005f036afaa0eed9cb3b
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 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.kafkaconsumer.config
21
22 import org.assertj.core.api.Assertions
23 import org.jetbrains.spek.api.Spek
24 import org.jetbrains.spek.api.dsl.describe
25 import org.jetbrains.spek.api.dsl.given
26 import org.jetbrains.spek.api.dsl.it
27 import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError
28 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingFailure
29 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingSuccess
30
31 private const val listenPort = "1234"
32 private const val kafkaBootstrapServers = "localhost:1234,10.1.14.10:8090"
33 private const val T1 = "boring_topic"
34 private const val T2 = "exciting_topic"
35 private const val kafkaTopicsString = "$T1,$T2"
36 private val PARSED_TOPICS_SET = setOf(T1, T2)
37
38 internal object ArgKafkaConsumerConfigurationTest : Spek({
39     lateinit var cut: ArgKafkaConsumerConfiguration
40
41     beforeEachTest {
42         cut = ArgKafkaConsumerConfiguration()
43     }
44
45     describe("parsing arguments") {
46         lateinit var result: KafkaConsumerConfiguration
47
48         given("all parameters are present in the long form") {
49
50             beforeEachTest {
51                 result = cut.parseExpectingSuccess(
52                         "--listen-port", listenPort,
53                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
54                         "--kafka-topics", kafkaTopicsString,
55                         "--disable-processing"
56                 )
57             }
58
59             it("should set proper port") {
60                 Assertions.assertThat(result.apiAddress.port).isEqualTo(listenPort.toInt())
61             }
62
63             it("should set proper kafka bootstrap servers") {
64                 Assertions.assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
65             }
66
67             it("should set proper kafka topics") {
68                 Assertions.assertThat(result.kafkaTopics).isEqualTo(PARSED_TOPICS_SET)
69             }
70
71             it("should disable processing") {
72                 Assertions.assertThat(result.disableProcessing).isTrue()
73             }
74         }
75
76         given("some parameters are present in the short form") {
77
78             beforeEachTest {
79                 result = cut.parseExpectingSuccess(
80                         "--listen-port", listenPort,
81                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
82                         "--kafka-topics", kafkaTopicsString)
83             }
84
85             it("should set proper port") {
86                 Assertions.assertThat(result.apiAddress.port).isEqualTo(listenPort.toInt())
87             }
88
89             it("should set proper kafka bootstrap servers") {
90                 Assertions.assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
91             }
92
93             it("should set proper kafka topics") {
94                 Assertions.assertThat(result.kafkaTopics).isEqualTo(PARSED_TOPICS_SET)
95             }
96         }
97
98         given("some missing disable-processing flag") {
99             beforeEachTest {
100                 result = cut.parseExpectingSuccess(
101                         "-p", listenPort,
102                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
103                         "-f", kafkaTopicsString)
104             }
105
106             it("should NOT disable processing") {
107                 Assertions.assertThat(result.disableProcessing).isFalse()
108             }
109         }
110
111         describe("required parameter is absent") {
112             given("kafka topics are missing") {
113                 it("should throw exception") {
114                     Assertions.assertThat(cut.parseExpectingFailure(
115                             "-p", listenPort,
116                             "-T1", kafkaBootstrapServers
117                     )).isInstanceOf(WrongArgumentError::class.java)
118                 }
119             }
120
121             given("kafka bootstrap servers is missing") {
122                 it("should throw exception") {
123                     Assertions.assertThat(cut.parseExpectingFailure(
124                             "-p", listenPort,
125                             "-f", kafkaTopicsString
126                     )).isInstanceOf(WrongArgumentError::class.java)
127                 }
128             }
129
130             given("listen port is missing") {
131                 it("should throw exception") {
132                     Assertions.assertThat(cut.parseExpectingFailure(
133                             "-p", listenPort,
134                             "-T1", kafkaBootstrapServers
135                     )).isInstanceOf(WrongArgumentError::class.java)
136                 }
137             }
138         }
139     }
140 })