4c0224e31a35bd3e334c5db9390a00e9835abfbb
[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 org.assertj.core.api.Assertions.assertThat
23 import org.jetbrains.spek.api.Spek
24 import org.jetbrains.spek.api.dsl.given
25 import org.jetbrains.spek.api.dsl.it
26 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
27 import org.onap.dcae.collectors.veshv.domain.ServerConfiguration
28 import java.nio.file.Paths
29
30 /**
31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32  * @since May 2018
33  */
34 object ArgBasedServerConfigurationTest : Spek({
35     lateinit var cut: ArgBasedServerConfiguration
36     val configurationUrl = "http://test-address/test"
37     val pk = Paths.get("/", "etc", "ves", "pk.pem")
38     val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt")
39     val trustCert = Paths.get("/", "etc", "ves", "trusted.crt")
40
41     beforeEachTest {
42         cut = ArgBasedServerConfiguration()
43     }
44
45     fun parse(vararg cmdLine: String) = cut.parse(cmdLine)
46
47     given("all parameters are present in the long form") {
48         lateinit var result: ServerConfiguration
49
50         beforeEachTest {
51             result = parse("--listen-port", "6969",
52                     "--config-url", configurationUrl,
53                     "--private-key-file", pk.toFile().absolutePath,
54                     "--cert-file", cert.toFile().absolutePath,
55                     "--trust-cert-file", trustCert.toFile().absolutePath)
56         }
57
58         it("should set proper port") {
59             assertThat(result.port).isEqualTo(6969)
60         }
61
62
63         it("should set proper config url") {
64             assertThat(result.configurationUrl).isEqualTo(configurationUrl)
65         }
66
67         it("should set proper security configuration") {
68             assertThat(result.securityConfiguration).isEqualTo(
69                     SecurityConfiguration(pk, cert, trustCert)
70             )
71         }
72     }
73
74     given("some parameters are present in the short form") {
75         lateinit var result: ServerConfiguration
76
77         beforeEachTest {
78             result = parse("-p", "666", "-c", configurationUrl)
79         }
80
81         it("should set proper port") {
82             assertThat(result.port).isEqualTo(666)
83         }
84
85         it("should set proper config url") {
86             assertThat(result.configurationUrl).isEqualTo(configurationUrl)
87         }
88     }
89
90     given("all optional parameters are absent") {
91         lateinit var result: ServerConfiguration
92
93         beforeEachTest {
94             result = parse()
95         }
96
97         it("should set default port") {
98             assertThat(result.port).isEqualTo(DefaultValues.PORT)
99         }
100
101         it("should set default config url") {
102             assertThat(result.configurationUrl).isEqualTo(DefaultValues.CONFIG_URL)
103         }
104     }
105 })