a14801da107ecc0fa73aef507e27dbaa31ff167d
[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.Failure
23 import arrow.core.Success
24 import org.assertj.core.api.Assertions.assertThat
25 import org.jetbrains.spek.api.Spek
26 import org.jetbrains.spek.api.dsl.describe
27 import org.jetbrains.spek.api.dsl.given
28 import org.jetbrains.spek.api.dsl.it
29 import org.jetbrains.spek.api.dsl.on
30 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
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 listenPort = "6969"
43     val updateInterval = "10"
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         val result = cut.parse(cmdLine)
54         return when (result) {
55             is Success -> result.value
56             is Failure -> throw AssertionError("Parsing result should be present")
57         }
58     }
59
60     describe("parsing arguments") {
61         given("all parameters are present in the long form") {
62             lateinit var result: ServerConfiguration
63
64             beforeEachTest {
65                 result = parse("--listen-port", listenPort,
66                         "--config-url", configurationUrl,
67                         "--update-interval", updateInterval,
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 update interval") {
78                 assertThat(result.configurationUpdateInterval).isEqualTo(Duration.ofSeconds(10))
79             }
80
81             it("should set proper config url") {
82                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
83             }
84
85             it("should set proper security configuration") {
86                 assertThat(result.securityConfiguration).isEqualTo(
87                         SecurityConfiguration(pk, cert, trustCert)
88                 )
89             }
90         }
91
92         given("some parameters are present in the short form") {
93             lateinit var result: ServerConfiguration
94
95             beforeEachTest {
96                 result = parse("-p", "666", "-c", configurationUrl)
97             }
98
99             it("should set proper port") {
100                 assertThat(result.port).isEqualTo(666)
101             }
102
103             it("should set proper config url") {
104                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
105             }
106         }
107
108         given("all optional parameters are absent") {
109             lateinit var result: ServerConfiguration
110
111             beforeEachTest {
112                 result = parse()
113             }
114
115             it("should set default port") {
116                 assertThat(result.port).isEqualTo(DefaultValues.PORT)
117             }
118
119             it("should set default config url") {
120                 assertThat(result.configurationUrl).isEqualTo(DefaultValues.CONFIG_URL)
121             }
122
123             it("should set default update interval") {
124                 assertThat(result.configurationUpdateInterval).isEqualTo(Duration.ofSeconds(DefaultValues.UPDATE_INTERVAL))
125             }
126
127             on("security config") {
128                 val securityConfiguration = result.securityConfiguration
129
130                 it("should set default trust cert file") {
131                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
132                 }
133
134                 it("should set default server cert file") {
135                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
136                 }
137
138                 it("should set default private key file") {
139                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
140                 }
141             }
142         }
143     }
144 })