6da605f4c3324aba8ea2f46ca43028a17617e7d1
[dcaegen2/collectors/hv-ves.git] /
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 arrow.core.identity
23 import org.assertj.core.api.Assertions.assertThat
24 import org.jetbrains.spek.api.Spek
25 import org.jetbrains.spek.api.dsl.describe
26 import org.jetbrains.spek.api.dsl.given
27 import org.jetbrains.spek.api.dsl.it
28 import org.jetbrains.spek.api.dsl.on
29 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
30 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
31 import java.nio.file.Paths
32 import java.time.Duration
33
34 /**
35  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
36  * @since May 2018
37  */
38 object ArgBasedServerConfigurationTest : Spek({
39     lateinit var cut: ArgBasedServerConfiguration
40     val configurationUrl = "http://test-address/test"
41     val firstRequestDelay = "10"
42     val requestInterval = "5"
43     val listenPort = "6969"
44     val pk = Paths.get("/", "etc", "ves", "pk.pem")
45     val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt")
46     val trustCert = Paths.get("/", "etc", "ves", "trusted.crt")
47
48     beforeEachTest {
49         cut = ArgBasedServerConfiguration()
50     }
51
52     fun parse(vararg cmdLine: String): ServerConfiguration =
53             cut.parse(cmdLine).fold(
54                     {throw AssertionError("Parsing result should be present")},
55                     ::identity
56             )
57
58     describe("parsing arguments") {
59         given("all parameters are present in the long form") {
60             lateinit var result: ServerConfiguration
61
62             beforeEachTest {
63                 result = parse("--ssl-disable",
64                         "--listen-port", listenPort,
65                         "--config-url", configurationUrl,
66                         "--first-request-delay", firstRequestDelay,
67                         "--request-interval", requestInterval,
68                         "--private-key-file", pk.toFile().absolutePath,
69                         "--cert-file", cert.toFile().absolutePath,
70                         "--trust-cert-file", trustCert.toFile().absolutePath)
71             }
72
73             it("should set proper port") {
74                 assertThat(result.port).isEqualTo(6969)
75             }
76
77             it("should set proper first consul request delay") {
78                 assertThat(result.configurationProviderParams.firstRequestDelay)
79                         .isEqualTo(Duration.ofSeconds(10))
80             }
81
82             it("should set proper consul request interval") {
83                 assertThat(result.configurationProviderParams.requestInterval)
84                         .isEqualTo(Duration.ofSeconds(5))
85             }
86
87             it("should set proper config url") {
88                 assertThat(result.configurationProviderParams.configurationUrl)
89                         .isEqualTo(configurationUrl)
90             }
91
92             it("should set proper security configuration") {
93                 assertThat(result.securityConfiguration).isEqualTo(
94                         SecurityConfiguration(sslDisable = true, privateKey = pk, cert = cert, trustedCert = trustCert)
95                 )
96             }
97         }
98
99         given("some parameters are present in the short form") {
100             lateinit var result: ServerConfiguration
101
102             beforeEachTest {
103                 result = parse("-p", "666", "-c", configurationUrl, "-d", firstRequestDelay)
104             }
105
106             it("should set proper port") {
107                 assertThat(result.port).isEqualTo(666)
108             }
109
110             it("should set proper first consul request delay") {
111                 assertThat(result.configurationProviderParams.firstRequestDelay)
112                         .isEqualTo(Duration.ofSeconds(10))
113             }
114
115             it("should set proper config url") {
116                 assertThat(result.configurationProviderParams.configurationUrl)
117                         .isEqualTo(configurationUrl)
118             }
119         }
120
121         given("all optional parameters are absent") {
122             lateinit var result: ServerConfiguration
123
124             beforeEachTest {
125                 result = parse()
126             }
127
128             it("should set default port") {
129                 assertThat(result.port).isEqualTo(DefaultValues.PORT)
130             }
131
132             it("should set default config url") {
133                 assertThat(result.configurationProviderParams.configurationUrl)
134                         .isEqualTo(DefaultValues.CONFIG_URL)
135             }
136
137             it("should set default first consul request delay") {
138                 assertThat(result.configurationProviderParams.firstRequestDelay)
139                         .isEqualTo(Duration.ofSeconds(DefaultValues.CONSUL_FIRST_REQUEST_DELAY))
140             }
141
142             it("should set default consul request interval") {
143                 assertThat(result.configurationProviderParams.requestInterval)
144                         .isEqualTo(Duration.ofSeconds(DefaultValues.CONSUL_REQUEST_INTERVAL))
145             }
146
147             on("security config") {
148                 val securityConfiguration = result.securityConfiguration
149
150                 it("should set default trust cert file") {
151                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
152                 }
153
154                 it("should set default server cert file") {
155                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
156                 }
157
158                 it("should set default private key file") {
159                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
160                 }
161             }
162         }
163     }
164 })