0f8d8d0eda9d64d179b1b7d46cd113c9de445d30
[dcaegen2/collectors/hv-ves.git] /
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.config.api
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.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
37
38 /**
39  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
40  * @since May 2018
41  */
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
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                         "--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
73                 )
74             }
75
76             it("should set proper kafka bootstrap servers") {
77                 assertThat(result.kafkaConfiguration.bootstrapServers).isEqualTo(kafkaBootstrapServers)
78             }
79
80             it("should set proper listen port") {
81                 assertThat(result.serverListenAddress.port).isEqualTo(listenPort.toInt())
82             }
83
84
85             it("should set default listen address") {
86                 assertThat(result.serverListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
87             }
88
89             it("should set proper health check api port") {
90                 assertThat(result.healthCheckApiListenAddress.port).isEqualTo(healthCheckApiPort.toInt())
91             }
92
93             it("should set default health check api address") {
94                 assertThat(result.healthCheckApiListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
95             }
96
97             it("should set proper first request delay") {
98                 assertThat(result.configurationProviderParams.firstRequestDelay)
99                         .isEqualTo(Duration.ofSeconds(firstRequestDelay.toLong()))
100             }
101
102             it("should set proper request interval") {
103                 assertThat(result.configurationProviderParams.requestInterval)
104                         .isEqualTo(Duration.ofSeconds(requestInterval.toLong()))
105             }
106
107             it("should set proper security configuration") {
108                 assertThat(result.securityConfiguration.keys.isEmpty()).isFalse()
109
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())
115
116                 }
117                 keys.trustStorePassword().useChecked {
118                     assertThat(it).isEqualTo(trustStorePassword.toCharArray())
119                 }
120             }
121
122             it("should set proper log level") {
123                 assertThat(result.logLevel).isEqualTo(LogLevel.DEBUG)
124             }
125         }
126
127         describe("required parameter is absent") {
128             on("missing listen port") {
129                 it("should throw exception") {
130                     assertThat(
131                             cut.parseExpectingFailure(
132                                     "--ssl-disable",
133                                     "--first-request-delay", firstRequestDelay,
134                                     "--request-interval", requestInterval
135                             )
136                     ).isInstanceOf(WrongArgumentError::class.java)
137                 }
138             }
139             on("missing configuration url") {
140                 it("should throw exception") {
141                     assertThat(
142                             cut.parseExpectingFailure(
143                                     "--listen-port", listenPort,
144                                     "--ssl-disable",
145                                     "--first-request-delay", firstRequestDelay,
146                                     "--request-interval", requestInterval
147                             )
148                     ).isInstanceOf(WrongArgumentError::class.java)
149                 }
150             }
151         }
152
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
166                     )
167
168                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
169                 }
170             }
171
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,
184                             "--log-level", "1"
185                     )
186
187                     assertThat(config.logLevel).isEqualTo(LogLevel.INFO)
188                 }
189             }
190         }
191     }
192 })