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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.simulators.dcaeapp.config
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
32 internal class ArgBasedDcaeAppSimConfigurationTest : Spek({
34 lateinit var cut: ArgBasedDcaeAppSimConfiguration
35 val kafkaBootstrapServers = "localhosting:123,localhostinger:12345"
36 val kafkaTopics = "top1,top2"
39 cut = ArgBasedDcaeAppSimConfiguration()
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")
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
58 describe("parsing arguments") {
59 lateinit var result: DcaeAppSimConfiguration
61 given("all parameters are present in the long form") {
64 result = parseExpectingSuccess("--listen-port", "6969",
65 "--kafka-bootstrap-servers", kafkaBootstrapServers,
66 "--kafka-topics", kafkaTopics
70 it("should set proper port") {
71 assertThat(result.apiPort).isEqualTo(6969)
75 it("should set proper kafka boostrap servers") {
76 assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
79 it("should set proper kafka topics") {
80 assertThat(result.kafkaTopics).isEqualTo(
86 given("some parameters are present in the short form") {
89 result = parseExpectingSuccess("-p", "666",
90 "--kafka-bootstrap-servers", kafkaBootstrapServers,
94 it("should set proper port") {
95 assertThat(result.apiPort).isEqualTo(666)
98 it("should set proper kafka boostrap servers") {
99 assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
102 it("should set proper kafka topics") {
103 assertThat(result.kafkaTopics).isEqualTo(
104 setOf("top1", "top2")
109 given("all optional parameters are absent") {
112 result = parseExpectingSuccess("-s", kafkaBootstrapServers, "-f", kafkaTopics)
115 it("should set default port") {
116 assertThat(result.apiPort).isEqualTo(DefaultValues.API_PORT)
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)
128 given("kafka bootstrap servers are missing") {
129 it("should throw exception") {
130 assertThat(parseExpectingFailure("-f", kafkaTopics))
131 .isInstanceOf(WrongArgumentException::class.java)