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