da2bfb4880f8f41d416ad2de01d4982c08324219
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-main / src / test / kotlin / org / onap / dcae / collectors / veshv / main / ArgVesHvConfigurationTest.kt
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.main
21
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
36
37 /**
38  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
39  * @since May 2018
40  */
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
52
53     beforeEachTest {
54         cut = ArgVesHvConfiguration()
55     }
56
57     describe("parsing arguments") {
58         given("all parameters are present in the long form") {
59             lateinit var result: ServerConfiguration
60
61             beforeEachTest {
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
74                 )
75             }
76
77             it("should set proper kafka bootstrap servers") {
78                 assertThat(result.kafkaConfiguration.bootstrapServers).isEqualTo(kafkaBootstrapServers)
79             }
80
81             it("should set proper listen port") {
82                 assertThat(result.serverListenAddress.port).isEqualTo(listenPort.toInt())
83             }
84
85
86             it("should set default listen address") {
87                 assertThat(result.serverListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
88             }
89
90             it("should set proper health check api port") {
91                 assertThat(result.healthCheckApiListenAddress.port).isEqualTo(healthCheckApiPort.toInt())
92             }
93
94             it("should set default health check api address") {
95                 assertThat(result.healthCheckApiListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
96             }
97
98             it("should set proper first consul request delay") {
99                 assertThat(result.configurationProviderParams.firstRequestDelay)
100                         .isEqualTo(Duration.ofSeconds(firstRequestDelay.toLong()))
101             }
102
103             it("should set proper consul request interval") {
104                 assertThat(result.configurationProviderParams.requestInterval)
105                         .isEqualTo(Duration.ofSeconds(requestInterval.toLong()))
106             }
107
108             it("should set proper config url") {
109                 assertThat(result.configurationProviderParams.configurationUrl)
110                         .isEqualTo(configurationUrl)
111             }
112
113             it("should set proper security configuration") {
114                 assertThat(result.securityConfiguration.keys.isEmpty()).isFalse()
115
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())
121
122                 }
123                 keys.trustStorePassword().useChecked {
124                     assertThat(it).isEqualTo(trustStorePassword.toCharArray())
125                 }
126             }
127
128             it("should set proper log level") {
129                 assertThat(result.logLevel).isEqualTo(LogLevel.DEBUG)
130             }
131         }
132
133         describe("required parameter is absent") {
134             on("missing listen port") {
135                 it("should throw exception") {
136                     assertThat(
137                             cut.parseExpectingFailure(
138                                     "--config-url", configurationUrl,
139                                     "--ssl-disable",
140                                     "--first-request-delay", firstRequestDelay,
141                                     "--request-interval", requestInterval
142                             )
143                     ).isInstanceOf(WrongArgumentError::class.java)
144                 }
145             }
146             on("missing configuration url") {
147                 it("should throw exception") {
148                     assertThat(
149                             cut.parseExpectingFailure(
150                                     "--listen-port", listenPort,
151                                     "--ssl-disable",
152                                     "--first-request-delay", firstRequestDelay,
153                                     "--request-interval", requestInterval
154                             )
155                     ).isInstanceOf(WrongArgumentError::class.java)
156                 }
157             }
158         }
159
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
174                     )
175
176                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
177                 }
178             }
179
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,
193                             "--log-level", "1"
194                     )
195
196                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
197                 }
198             }
199         }
200     }
201 })