4084ee8db2aeb385b754c668c4d0310f2c24be5e
[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 ArgBasedDcaeAppSimConfigurationTest : Spek({
32
33     lateinit var cut: ArgBasedDcaeAppSimConfiguration
34     val kafkaBootstrapServers = "localhosting:123,localhostinger:12345"
35     val kafkaTopics = "top1,top2"
36
37     beforeEachTest {
38         cut = ArgBasedDcaeAppSimConfiguration()
39     }
40
41     fun parseExpectingSuccess(vararg cmdLine: String): DcaeAppSimConfiguration =
42             cut.parse(cmdLine).fold(
43                     { throw AssertionError("Parsing result should be present") },
44                     ::identity
45             )
46
47     fun parseExpectingFailure(vararg cmdLine: String) =
48             cut.parse(cmdLine).fold(
49                     ::identity,
50                     { throw AssertionError("parsing should have failed") }
51             )
52
53     describe("parsing arguments") {
54         lateinit var result: DcaeAppSimConfiguration
55
56         given("all parameters are present in the long form") {
57
58             beforeEachTest {
59                 result = parseExpectingSuccess("--listen-port", "6969",
60                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
61                         "--kafka-topics", kafkaTopics
62                 )
63             }
64
65             it("should set proper port") {
66                 assertThat(result.apiPort).isEqualTo(6969)
67             }
68
69
70             it("should set proper kafka bootstrap servers") {
71                 assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
72             }
73
74             it("should set proper kafka topics") {
75                 assertThat(result.kafkaTopics).isEqualTo(
76                         setOf("top1", "top2")
77                 )
78             }
79         }
80
81         given("some parameters are present in the short form") {
82
83             beforeEachTest {
84                 result = parseExpectingSuccess("-p", "666",
85                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
86                         "-f", kafkaTopics)
87             }
88
89             it("should set proper port") {
90                 assertThat(result.apiPort).isEqualTo(666)
91             }
92
93             it("should set proper kafka bootstrap servers") {
94                 assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
95             }
96
97             it("should set proper kafka topics") {
98                 assertThat(result.kafkaTopics).isEqualTo(
99                         setOf("top1", "top2")
100                 )
101             }
102         }
103
104         given("all optional parameters are absent") {
105
106             beforeEachTest {
107                 result = parseExpectingSuccess("-s", kafkaBootstrapServers, "-f", kafkaTopics)
108             }
109
110             it("should set default port") {
111                 assertThat(result.apiPort).isEqualTo(DefaultValues.API_PORT)
112             }
113         }
114
115         describe("required parameter is absent") {
116             given("kafka topics are missing") {
117                 it("should throw exception") {
118                     assertThat(parseExpectingFailure("-s", kafkaBootstrapServers))
119                             .isInstanceOf(WrongArgumentError::class.java)
120                 }
121             }
122
123             given("kafka bootstrap servers are missing") {
124                 it("should throw exception") {
125                     assertThat(parseExpectingFailure("-f", kafkaTopics))
126                             .isInstanceOf(WrongArgumentError::class.java)
127                 }
128             }
129         }
130     }
131 })