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.main
22 import org.assertj.core.api.Assertions.assertThat
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.jetbrains.spek.api.dsl.on
28 import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError
29 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
30 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingFailure
31 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingSuccess
32 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
33 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
34 import java.time.Duration
35 import kotlin.test.assertNotNull
38 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
41 object ArgVesHvConfigurationTest : Spek({
42 lateinit var cut: ArgVesHvConfiguration
43 val kafkaBootstrapServers = "dmaap-mr-wro:6666,dmaap-mr-gda:6666"
44 val healthCheckApiPort = "6070"
45 val configurationUrl = "http://test-address/test"
46 val firstRequestDelay = "10"
47 val requestInterval = "5"
48 val listenPort = "6969"
49 val keyStorePassword = "kspass"
50 val trustStorePassword = "tspass"
51 val logLevel = LogLevel.DEBUG.name
54 cut = ArgVesHvConfiguration()
57 describe("parsing arguments") {
58 given("all parameters are present in the long form") {
59 lateinit var result: ServerConfiguration
62 result = cut.parseExpectingSuccess(
63 "--kafka-bootstrap-servers", kafkaBootstrapServers,
64 "--health-check-api-port", healthCheckApiPort,
65 "--listen-port", listenPort,
66 "--config-url", configurationUrl,
67 "--first-request-delay", firstRequestDelay,
68 "--request-interval", requestInterval,
69 "--key-store", "/tmp/keys.p12",
70 "--trust-store", "/tmp/trust.p12",
71 "--key-store-password", keyStorePassword,
72 "--trust-store-password", trustStorePassword,
73 "--log-level", logLevel
77 it("should set proper kafka bootstrap servers") {
78 assertThat(result.kafkaConfiguration.bootstrapServers).isEqualTo(kafkaBootstrapServers)
81 it("should set proper listen port") {
82 assertThat(result.serverListenAddress.port).isEqualTo(listenPort.toInt())
86 it("should set default listen address") {
87 assertThat(result.serverListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
90 it("should set proper health check api port") {
91 assertThat(result.healthCheckApiListenAddress.port).isEqualTo(healthCheckApiPort.toInt())
94 it("should set default health check api address") {
95 assertThat(result.healthCheckApiListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
98 it("should set proper first consul request delay") {
99 assertThat(result.configurationProviderParams.firstRequestDelay)
100 .isEqualTo(Duration.ofSeconds(firstRequestDelay.toLong()))
103 it("should set proper consul request interval") {
104 assertThat(result.configurationProviderParams.requestInterval)
105 .isEqualTo(Duration.ofSeconds(requestInterval.toLong()))
108 it("should set proper config url") {
109 assertThat(result.configurationProviderParams.configurationUrl)
110 .isEqualTo(configurationUrl)
113 it("should set proper security configuration") {
114 assertThat(result.securityConfiguration.keys.isEmpty()).isFalse()
116 val keys = result.securityConfiguration.keys.orNull() as SecurityKeys
117 assertNotNull(keys.keyStore())
118 assertNotNull(keys.trustStore())
119 keys.keyStorePassword().useChecked {
120 assertThat(it).isEqualTo(keyStorePassword.toCharArray())
123 keys.trustStorePassword().useChecked {
124 assertThat(it).isEqualTo(trustStorePassword.toCharArray())
128 it("should set proper log level") {
129 assertThat(result.logLevel).isEqualTo(LogLevel.DEBUG)
133 describe("required parameter is absent") {
134 on("missing listen port") {
135 it("should throw exception") {
137 cut.parseExpectingFailure(
138 "--config-url", configurationUrl,
140 "--first-request-delay", firstRequestDelay,
141 "--request-interval", requestInterval
143 ).isInstanceOf(WrongArgumentError::class.java)
146 on("missing configuration url") {
147 it("should throw exception") {
149 cut.parseExpectingFailure(
150 "--listen-port", listenPort,
152 "--first-request-delay", firstRequestDelay,
153 "--request-interval", requestInterval
155 ).isInstanceOf(WrongArgumentError::class.java)
160 describe("correct log level not provided") {
161 on("missing log level") {
162 it("should set default INFO value") {
163 val config = cut.parseExpectingSuccess(
164 "--kafka-bootstrap-servers", kafkaBootstrapServers,
165 "--health-check-api-port", healthCheckApiPort,
166 "--listen-port", listenPort,
167 "--config-url", configurationUrl,
168 "--first-request-delay", firstRequestDelay,
169 "--request-interval", requestInterval,
170 "--key-store", "/tmp/keys.p12",
171 "--trust-store", "/tmp/trust.p12",
172 "--key-store-password", keyStorePassword,
173 "--trust-store-password", trustStorePassword
176 assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
180 on("incorrect log level") {
181 it("should set default INFO value") {
182 val config = cut.parseExpectingSuccess(
183 "--kafka-bootstrap-servers", kafkaBootstrapServers,
184 "--health-check-api-port", healthCheckApiPort,
185 "--listen-port", listenPort,
186 "--config-url", configurationUrl,
187 "--first-request-delay", firstRequestDelay,
188 "--request-interval", requestInterval,
189 "--key-store", "/tmp/keys.p12",
190 "--trust-store", "/tmp/trust.p12",
191 "--key-store-password", keyStorePassword,
192 "--trust-store-password", trustStorePassword,
196 assertThat(config.logLevel).isEqualTo(LogLevel.INFO)