Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / 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 healthCheckApiPort = "6070"
43     val configurationUrl = "http://test-address/test"
44     val firstRequestDelay = "10"
45     val requestInterval = "5"
46     val listenPort = "6969"
47     val keyStorePassword = "kspass"
48     val trustStorePassword = "tspass"
49
50     beforeEachTest {
51         cut = ArgVesHvConfiguration()
52     }
53
54     describe("parsing arguments") {
55         given("all parameters are present in the long form") {
56             lateinit var result: ServerConfiguration
57
58             beforeEachTest {
59                 result = cut.parseExpectingSuccess(
60                         "--health-check-api-port", healthCheckApiPort,
61                         "--listen-port", listenPort,
62                         "--config-url", configurationUrl,
63                         "--first-request-delay", firstRequestDelay,
64                         "--request-interval", requestInterval,
65                         "--key-store", "/tmp/keys.p12",
66                         "--trust-store", "/tmp/trust.p12",
67                         "--key-store-password", keyStorePassword,
68                         "--trust-store-password", trustStorePassword
69                 )
70             }
71
72             it("should set proper listen port") {
73                 assertThat(result.serverListenAddress.port).isEqualTo(listenPort.toInt())
74             }
75
76
77             it("should set default listen address") {
78                 assertThat(result.serverListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
79             }
80
81             it("should set proper health check api port") {
82                 assertThat(result.healthCheckApiListenAddress.port).isEqualTo(healthCheckApiPort.toInt())
83             }
84
85             it("should set default health check api address") {
86                 assertThat(result.healthCheckApiListenAddress.address.hostAddress).isEqualTo("0.0.0.0")
87             }
88
89             it("should set proper first consul request delay") {
90                 assertThat(result.configurationProviderParams.firstRequestDelay)
91                         .isEqualTo(Duration.ofSeconds(firstRequestDelay.toLong()))
92             }
93
94             it("should set proper consul request interval") {
95                 assertThat(result.configurationProviderParams.requestInterval)
96                         .isEqualTo(Duration.ofSeconds(requestInterval.toLong()))
97             }
98
99             it("should set proper config url") {
100                 assertThat(result.configurationProviderParams.configurationUrl)
101                         .isEqualTo(configurationUrl)
102             }
103
104             it("should set proper security configuration") {
105                 assertThat(result.securityConfiguration.sslDisable).isFalse()
106
107                 val keys = result.securityConfiguration.keys.orNull() as JdkKeys
108                 assertNotNull(keys.keyStore)
109                 assertNotNull(keys.trustStore)
110                 assertThat(keys.keyStorePassword).isEqualTo(keyStorePassword.toCharArray())
111                 assertThat(keys.trustStorePassword).isEqualTo(trustStorePassword.toCharArray())
112             }
113         }
114
115         describe("required parameter is absent") {
116             on("missing listen port") {
117                 it("should throw exception") {
118                     assertThat(cut.parseExpectingFailure(
119                             "--config-url", configurationUrl,
120                             "--ssl-disable",
121                             "--first-request-delay", firstRequestDelay,
122                             "--request-interval", requestInterval)
123                     ).isInstanceOf(WrongArgumentError::class.java)
124                 }
125             }
126             on("missing configuration url") {
127                 it("should throw exception") {
128                     assertThat(cut.parseExpectingFailure(
129                             "--listen-port", listenPort,
130                             "--ssl-disable",
131                             "--first-request-delay", firstRequestDelay,
132                             "--request-interval", requestInterval)
133                     ).isInstanceOf(WrongArgumentError::class.java)
134                 }
135             }
136         }
137     }
138 })