2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018-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
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.config.api
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.config.api.model.ServerConfiguration
30 import org.onap.dcae.collectors.veshv.config.impl.ArgVesHvConfiguration
31 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingFailure
32 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingSuccess
33 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
34 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
35 import java.time.Duration
36 import kotlin.test.assertNotNull
39 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
42 object ArgVesHvConfigurationTest : Spek({
43 lateinit var cut: ArgVesHvConfiguration
44 val kafkaBootstrapServers = "dmaap-mr-wro:6666,dmaap-mr-gda:6666"
45 val healthCheckApiPort = "6070"
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 "--first-request-delay", firstRequestDelay,
67 "--request-interval", requestInterval,
68 "--key-store", "/tmp/keys.p12",
69 "--trust-store", "/tmp/trust.p12",
70 "--key-store-password", keyStorePassword,
71 "--trust-store-password", trustStorePassword,
72 "--log-level", logLevel
76 it("should set proper kafka bootstrap servers") {
77 assertThat(result.kafkaConfiguration.bootstrapServers).isEqualTo(kafkaBootstrapServers)
80 it("should set proper listen port") {
81 assertThat(result.serverListenAddress.port).isEqualTo(listenPort.toInt())
85 it("should set default listen address") {
86 assertThat(result.serverListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
89 it("should set proper health check api port") {
90 assertThat(result.healthCheckApiListenAddress.port).isEqualTo(healthCheckApiPort.toInt())
93 it("should set default health check api address") {
94 assertThat(result.healthCheckApiListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
97 it("should set proper first request delay") {
98 assertThat(result.configurationProviderParams.firstRequestDelay)
99 .isEqualTo(Duration.ofSeconds(firstRequestDelay.toLong()))
102 it("should set proper request interval") {
103 assertThat(result.configurationProviderParams.requestInterval)
104 .isEqualTo(Duration.ofSeconds(requestInterval.toLong()))
107 it("should set proper security configuration") {
108 assertThat(result.securityConfiguration.keys.isEmpty()).isFalse()
110 val keys = result.securityConfiguration.keys.orNull() as SecurityKeys
111 assertNotNull(keys.keyStore())
112 assertNotNull(keys.trustStore())
113 keys.keyStorePassword().useChecked {
114 assertThat(it).isEqualTo(keyStorePassword.toCharArray())
117 keys.trustStorePassword().useChecked {
118 assertThat(it).isEqualTo(trustStorePassword.toCharArray())
122 it("should set proper log level") {
123 assertThat(result.logLevel).isEqualTo(LogLevel.DEBUG)
127 describe("required parameter is absent") {
128 on("missing listen port") {
129 it("should throw exception") {
131 cut.parseExpectingFailure(
133 "--first-request-delay", firstRequestDelay,
134 "--request-interval", requestInterval
136 ).isInstanceOf(WrongArgumentError::class.java)
139 on("missing configuration url") {
140 it("should throw exception") {
142 cut.parseExpectingFailure(
143 "--listen-port", listenPort,
145 "--first-request-delay", firstRequestDelay,
146 "--request-interval", requestInterval
148 ).isInstanceOf(WrongArgumentError::class.java)
153 describe("correct log level not provided") {
154 on("missing log level") {
155 it("should set default INFO value") {
156 val config = cut.parseExpectingSuccess(
157 "--kafka-bootstrap-servers", kafkaBootstrapServers,
158 "--health-check-api-port", healthCheckApiPort,
159 "--listen-port", listenPort,
160 "--first-request-delay", firstRequestDelay,
161 "--request-interval", requestInterval,
162 "--key-store", "/tmp/keys.p12",
163 "--trust-store", "/tmp/trust.p12",
164 "--key-store-password", keyStorePassword,
165 "--trust-store-password", trustStorePassword
168 assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
172 on("incorrect log level") {
173 it("should set default INFO value") {
174 val config = cut.parseExpectingSuccess(
175 "--kafka-bootstrap-servers", kafkaBootstrapServers,
176 "--health-check-api-port", healthCheckApiPort,
177 "--listen-port", listenPort,
178 "--first-request-delay", firstRequestDelay,
179 "--request-interval", requestInterval,
180 "--key-store", "/tmp/keys.p12",
181 "--trust-store", "/tmp/trust.p12",
182 "--key-store-password", keyStorePassword,
183 "--trust-store-password", trustStorePassword,
187 assertThat(config.logLevel).isEqualTo(LogLevel.INFO)