Enable setting log level from command line
[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.domain.JdkKeys
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.commandline.WrongArgumentError
33 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
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.sslDisable).isFalse()
115
116                 val keys = result.securityConfiguration.keys.orNull() as JdkKeys
117                 assertNotNull(keys.keyStore)
118                 assertNotNull(keys.trustStore)
119                 assertThat(keys.keyStorePassword).isEqualTo(keyStorePassword.toCharArray())
120                 assertThat(keys.trustStorePassword).isEqualTo(trustStorePassword.toCharArray())
121             }
122
123             it("should set proper log level") {
124                 assertThat(result.logLevel).isEqualTo(LogLevel.DEBUG)
125             }
126         }
127
128         describe("required parameter is absent") {
129             on("missing listen port") {
130                 it("should throw exception") {
131                     assertThat(
132                         cut.parseExpectingFailure(
133                             "--config-url", configurationUrl,
134                             "--ssl-disable",
135                             "--first-request-delay", firstRequestDelay,
136                             "--request-interval", requestInterval
137                         )
138                     ).isInstanceOf(WrongArgumentError::class.java)
139                 }
140             }
141             on("missing configuration url") {
142                 it("should throw exception") {
143                     assertThat(
144                         cut.parseExpectingFailure(
145                             "--listen-port", listenPort,
146                             "--ssl-disable",
147                             "--first-request-delay", firstRequestDelay,
148                             "--request-interval", requestInterval
149                         )
150                     ).isInstanceOf(WrongArgumentError::class.java)
151                 }
152             }
153         }
154
155         describe("correct log level not provided") {
156             on("missing log level") {
157                 it("should set default INFO value") {
158                     val config = cut.parseExpectingSuccess(
159                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
160                         "--health-check-api-port", healthCheckApiPort,
161                         "--listen-port", listenPort,
162                         "--config-url", configurationUrl,
163                         "--first-request-delay", firstRequestDelay,
164                         "--request-interval", requestInterval,
165                         "--key-store", "/tmp/keys.p12",
166                         "--trust-store", "/tmp/trust.p12",
167                         "--key-store-password", keyStorePassword,
168                         "--trust-store-password", trustStorePassword
169                     )
170
171                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
172                 }
173             }
174
175             on("incorrect log level") {
176                 it("should set default INFO value") {
177                     val config = cut.parseExpectingSuccess(
178                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
179                         "--health-check-api-port", healthCheckApiPort,
180                         "--listen-port", listenPort,
181                         "--config-url", configurationUrl,
182                         "--first-request-delay", firstRequestDelay,
183                         "--request-interval", requestInterval,
184                         "--key-store", "/tmp/keys.p12",
185                         "--trust-store", "/tmp/trust.p12",
186                         "--key-store-password", keyStorePassword,
187                         "--trust-store-password", trustStorePassword,
188                         "--log-level", "1"
189                     )
190
191                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
192                 }
193             }
194         }
195     }
196 })