9dddeca915b94695ef3cbafa51646f56b4a73c62
[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 java.time.Duration
34 import kotlin.test.assertNotNull
35
36 /**
37  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
38  * @since May 2018
39  */
40 object ArgVesHvConfigurationTest : Spek({
41     lateinit var cut: ArgVesHvConfiguration
42     val kafkaBootstrapServers = "dmaap-mr-wro:6666,dmaap-mr-gda:6666"
43     val healthCheckApiPort = "6070"
44     val configurationUrl = "http://test-address/test"
45     val firstRequestDelay = "10"
46     val requestInterval = "5"
47     val listenPort = "6969"
48     val keyStorePassword = "kspass"
49     val trustStorePassword = "tspass"
50
51     beforeEachTest {
52         cut = ArgVesHvConfiguration()
53     }
54
55     describe("parsing arguments") {
56         given("all parameters are present in the long form") {
57             lateinit var result: ServerConfiguration
58
59             beforeEachTest {
60                 result = cut.parseExpectingSuccess(
61                         "--kafka-bootstrap-servers", kafkaBootstrapServers,
62                         "--health-check-api-port", healthCheckApiPort,
63                         "--listen-port", listenPort,
64                         "--config-url", configurationUrl,
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                 )
72             }
73
74             it("should set proper kafka bootstrap servers") {
75                 assertThat(result.kafkaConfiguration.bootstrapServers).isEqualTo(kafkaBootstrapServers)
76             }
77
78             it("should set proper listen port") {
79                 assertThat(result.serverListenAddress.port).isEqualTo(listenPort.toInt())
80             }
81
82
83             it("should set default listen address") {
84                 assertThat(result.serverListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
85             }
86
87             it("should set proper health check api port") {
88                 assertThat(result.healthCheckApiListenAddress.port).isEqualTo(healthCheckApiPort.toInt())
89             }
90
91             it("should set default health check api address") {
92                 assertThat(result.healthCheckApiListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
93             }
94
95             it("should set proper first consul request delay") {
96                 assertThat(result.configurationProviderParams.firstRequestDelay)
97                         .isEqualTo(Duration.ofSeconds(firstRequestDelay.toLong()))
98             }
99
100             it("should set proper consul request interval") {
101                 assertThat(result.configurationProviderParams.requestInterval)
102                         .isEqualTo(Duration.ofSeconds(requestInterval.toLong()))
103             }
104
105             it("should set proper config url") {
106                 assertThat(result.configurationProviderParams.configurationUrl)
107                         .isEqualTo(configurationUrl)
108             }
109
110             it("should set proper security configuration") {
111                 assertThat(result.securityConfiguration.sslDisable).isFalse()
112
113                 val keys = result.securityConfiguration.keys.orNull() as JdkKeys
114                 assertNotNull(keys.keyStore)
115                 assertNotNull(keys.trustStore)
116                 assertThat(keys.keyStorePassword).isEqualTo(keyStorePassword.toCharArray())
117                 assertThat(keys.trustStorePassword).isEqualTo(trustStorePassword.toCharArray())
118             }
119         }
120
121         describe("required parameter is absent") {
122             on("missing listen port") {
123                 it("should throw exception") {
124                     assertThat(cut.parseExpectingFailure(
125                             "--config-url", configurationUrl,
126                             "--ssl-disable",
127                             "--first-request-delay", firstRequestDelay,
128                             "--request-interval", requestInterval)
129                     ).isInstanceOf(WrongArgumentError::class.java)
130                 }
131             }
132             on("missing configuration url") {
133                 it("should throw exception") {
134                     assertThat(cut.parseExpectingFailure(
135                             "--listen-port", listenPort,
136                             "--ssl-disable",
137                             "--first-request-delay", firstRequestDelay,
138                             "--request-interval", requestInterval)
139                     ).isInstanceOf(WrongArgumentError::class.java)
140                 }
141             }
142         }
143     }
144 })