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