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