2c49cf9864fb12a90563d8ef294b49a30a759507
[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 arrow.core.identity
25 import org.assertj.core.api.Assertions.assertThat
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.given
29 import org.jetbrains.spek.api.dsl.it
30 import org.jetbrains.spek.api.dsl.on
31 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
32 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
33 import java.nio.file.Paths
34 import java.time.Duration
35
36 /**
37  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
38  * @since May 2018
39  */
40 object ArgBasedServerConfigurationTest : Spek({
41     lateinit var cut: ArgBasedServerConfiguration
42     val configurationUrl = "http://test-address/test"
43     val listenPort = "6969"
44     val updateInterval = "10"
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("--listen-port", listenPort,
65                         "--config-url", configurationUrl,
66                         "--update-interval", updateInterval,
67                         "--private-key-file", pk.toFile().absolutePath,
68                         "--cert-file", cert.toFile().absolutePath,
69                         "--trust-cert-file", trustCert.toFile().absolutePath)
70             }
71
72             it("should set proper port") {
73                 assertThat(result.port).isEqualTo(6969)
74             }
75
76             it("should set update interval") {
77                 assertThat(result.configurationUpdateInterval).isEqualTo(Duration.ofSeconds(10))
78             }
79
80             it("should set proper config url") {
81                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
82             }
83
84             it("should set proper security configuration") {
85                 assertThat(result.securityConfiguration).isEqualTo(
86                         SecurityConfiguration(pk, cert, trustCert)
87                 )
88             }
89         }
90
91         given("some parameters are present in the short form") {
92             lateinit var result: ServerConfiguration
93
94             beforeEachTest {
95                 result = parse("-p", "666", "-c", configurationUrl)
96             }
97
98             it("should set proper port") {
99                 assertThat(result.port).isEqualTo(666)
100             }
101
102             it("should set proper config url") {
103                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
104             }
105         }
106
107         given("all optional parameters are absent") {
108             lateinit var result: ServerConfiguration
109
110             beforeEachTest {
111                 result = parse()
112             }
113
114             it("should set default port") {
115                 assertThat(result.port).isEqualTo(DefaultValues.PORT)
116             }
117
118             it("should set default config url") {
119                 assertThat(result.configurationUrl).isEqualTo(DefaultValues.CONFIG_URL)
120             }
121
122             it("should set default update interval") {
123                 assertThat(result.configurationUpdateInterval).isEqualTo(Duration.ofSeconds(DefaultValues.UPDATE_INTERVAL))
124             }
125
126             on("security config") {
127                 val securityConfiguration = result.securityConfiguration
128
129                 it("should set default trust cert file") {
130                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
131                 }
132
133                 it("should set default server cert file") {
134                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
135                 }
136
137                 it("should set default private key file") {
138                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
139                 }
140             }
141         }
142     }
143 })