Use CBS by means of SDK in place of Consul
[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-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
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 firstRequestDelay = "10"
46     val requestInterval = "5"
47     val listenPort = "6969"
48     val keyStorePassword = "kspass"
49     val trustStorePassword = "tspass"
50     val logLevel = LogLevel.DEBUG.name
51
52     beforeEachTest {
53         cut = ArgVesHvConfiguration()
54     }
55
56     describe("parsing arguments") {
57         given("all parameters are present in the long form") {
58             lateinit var result: ServerConfiguration
59
60             beforeEachTest {
61                 result = cut.parseExpectingSuccess(
62                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
63                         "--health-check-api-port", healthCheckApiPort,
64                         "--listen-port", listenPort,
65                         "--first-request-delay", firstRequestDelay,
66                         "--request-interval", requestInterval,
67                         "--key-store", "/tmp/keys.p12",
68                         "--trust-store", "/tmp/trust.p12",
69                         "--key-store-password", keyStorePassword,
70                         "--trust-store-password", trustStorePassword,
71                         "--log-level", logLevel
72                 )
73             }
74
75             it("should set proper kafka bootstrap servers") {
76                 assertThat(result.kafkaConfiguration.bootstrapServers).isEqualTo(kafkaBootstrapServers)
77             }
78
79             it("should set proper listen port") {
80                 assertThat(result.serverListenAddress.port).isEqualTo(listenPort.toInt())
81             }
82
83
84             it("should set default listen address") {
85                 assertThat(result.serverListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
86             }
87
88             it("should set proper health check api port") {
89                 assertThat(result.healthCheckApiListenAddress.port).isEqualTo(healthCheckApiPort.toInt())
90             }
91
92             it("should set default health check api address") {
93                 assertThat(result.healthCheckApiListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
94             }
95
96             it("should set proper first request delay") {
97                 assertThat(result.configurationProviderParams.firstRequestDelay)
98                         .isEqualTo(Duration.ofSeconds(firstRequestDelay.toLong()))
99             }
100
101             it("should set proper request interval") {
102                 assertThat(result.configurationProviderParams.requestInterval)
103                         .isEqualTo(Duration.ofSeconds(requestInterval.toLong()))
104             }
105
106             it("should set proper security configuration") {
107                 assertThat(result.securityConfiguration.keys.isEmpty()).isFalse()
108
109                 val keys = result.securityConfiguration.keys.orNull() as SecurityKeys
110                 assertNotNull(keys.keyStore())
111                 assertNotNull(keys.trustStore())
112                 keys.keyStorePassword().useChecked {
113                     assertThat(it).isEqualTo(keyStorePassword.toCharArray())
114
115                 }
116                 keys.trustStorePassword().useChecked {
117                     assertThat(it).isEqualTo(trustStorePassword.toCharArray())
118                 }
119             }
120
121             it("should set proper log level") {
122                 assertThat(result.logLevel).isEqualTo(LogLevel.DEBUG)
123             }
124         }
125
126         describe("required parameter is absent") {
127             on("missing listen port") {
128                 it("should throw exception") {
129                     assertThat(
130                             cut.parseExpectingFailure(
131                                     "--ssl-disable",
132                                     "--first-request-delay", firstRequestDelay,
133                                     "--request-interval", requestInterval
134                             )
135                     ).isInstanceOf(WrongArgumentError::class.java)
136                 }
137             }
138             on("missing configuration url") {
139                 it("should throw exception") {
140                     assertThat(
141                             cut.parseExpectingFailure(
142                                     "--listen-port", listenPort,
143                                     "--ssl-disable",
144                                     "--first-request-delay", firstRequestDelay,
145                                     "--request-interval", requestInterval
146                             )
147                     ).isInstanceOf(WrongArgumentError::class.java)
148                 }
149             }
150         }
151
152         describe("correct log level not provided") {
153             on("missing log level") {
154                 it("should set default INFO value") {
155                     val config = cut.parseExpectingSuccess(
156                             "--kafka-bootstrap-servers", kafkaBootstrapServers,
157                             "--health-check-api-port", healthCheckApiPort,
158                             "--listen-port", listenPort,
159                             "--first-request-delay", firstRequestDelay,
160                             "--request-interval", requestInterval,
161                             "--key-store", "/tmp/keys.p12",
162                             "--trust-store", "/tmp/trust.p12",
163                             "--key-store-password", keyStorePassword,
164                             "--trust-store-password", trustStorePassword
165                     )
166
167                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
168                 }
169             }
170
171             on("incorrect log level") {
172                 it("should set default INFO value") {
173                     val config = cut.parseExpectingSuccess(
174                             "--kafka-bootstrap-servers", kafkaBootstrapServers,
175                             "--health-check-api-port", healthCheckApiPort,
176                             "--listen-port", listenPort,
177                             "--first-request-delay", firstRequestDelay,
178                             "--request-interval", requestInterval,
179                             "--key-store", "/tmp/keys.p12",
180                             "--trust-store", "/tmp/trust.p12",
181                             "--key-store-password", keyStorePassword,
182                             "--trust-store-password", trustStorePassword,
183                             "--log-level", "1"
184                     )
185
186                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
187                 }
188             }
189         }
190     }
191 })