e3a20c703bf298edebe39d87aab63af6ff163760
[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.config
21
22 import arrow.core.identity
23 import org.assertj.core.api.Assertions
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.simulators.xnf.config.ArgXnfSimulatorConfiguration
32 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgXnfSimulatorConfiguration.DefaultValues
33 import org.onap.dcae.collectors.veshv.simulators.xnf.config.SimulatorConfiguration
34 import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentError
35 import java.nio.file.Paths
36 import kotlin.test.assertTrue
37
38
39 object ArgXnfSimulatorConfiurationTest : Spek({
40     lateinit var cut: ArgXnfSimulatorConfiguration
41     val listenPort = "4321"
42     val vesHost = "localhost"
43     val vesPort = "1234"
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 = ArgXnfSimulatorConfiguration()
50     }
51
52     fun parse(vararg cmdLine: String): SimulatorConfiguration =
53             cut.parse(cmdLine).fold(
54                     { throw AssertionError("Parsing result should be present") },
55                     ::identity
56             )
57
58     fun parseExpectingFailure(vararg cmdLine: String) =
59             cut.parse(cmdLine).fold(
60                     ::identity,
61                     { throw AssertionError("parsing should have failed") }
62             )
63
64     describe("parsing arguments") {
65         lateinit var result: SimulatorConfiguration
66
67         given("all parameters are present in the long form") {
68
69             beforeEachTest {
70                 result = parse("--ssl-disable",
71                         "--listen-port", listenPort,
72                         "--ves-host", vesHost,
73                         "--ves-port", vesPort,
74                         "--private-key-file", pk.toFile().absolutePath,
75                         "--cert-file", cert.toFile().absolutePath,
76                         "--trust-cert-file", trustCert.toFile().absolutePath)
77             }
78
79             it("should set proper listen port") {
80                 assertThat(result.listenPort).isEqualTo(listenPort.toInt())
81             }
82
83             it("should set proper ves host") {
84                 assertThat(result.vesHost).isEqualTo(vesHost)
85             }
86
87             it("should set proper ves port") {
88                 assertThat(result.vesPort).isEqualTo(vesPort.toInt())
89             }
90
91             it("should set proper security configuration") {
92                 assertThat(result.security).isEqualTo(
93                         SecurityConfiguration(sslDisable = true, privateKey = pk, cert = cert, trustedCert = trustCert)
94                 )
95             }
96         }
97
98         given("some parameters are present in the short form") {
99
100             beforeEachTest {
101                 result = parse("-p", listenPort, "-h", vesHost, "--ves-port", vesPort)
102             }
103
104             it("should set proper listen port") {
105                 assertThat(result.listenPort).isEqualTo(listenPort.toInt())
106             }
107
108             it("should set proper ves host") {
109                 assertThat(result.vesHost).isEqualTo(vesHost)
110             }
111
112             it("should set proper ves port") {
113                 assertThat(result.vesPort).isEqualTo(vesPort.toInt())
114             }
115         }
116
117         given("all optional parameters are absent") {
118
119             beforeEachTest {
120                 result = parse("-p", listenPort, "-h", vesHost, "-v", vesPort)
121             }
122
123             on("security config") {
124                 val securityConfiguration = result.security
125
126                 it("should set default trust cert file") {
127                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
128                 }
129
130                 it("should set default server cert file") {
131                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
132                 }
133
134                 it("should set default private key file") {
135                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
136                 }
137             }
138         }
139
140         given("disabled ssl certs together with all other parameters") {
141             beforeEachTest {
142                 result = parse("--ssl-disable",
143                         "--listen-port", listenPort,
144                         "--ves-port", "888",
145                         "--ves-host", vesHost,
146                         "--private-key-file", pk.toFile().absolutePath,
147                         "--cert-file", cert.toFile().absolutePath,
148                         "--trust-cert-file", trustCert.toFile().absolutePath)
149             }
150
151             on("security config") {
152                 val securityConfiguration = result.security
153
154                 it("should set ssl disable to true") {
155                     assertTrue(securityConfiguration.sslDisable)
156                 }
157
158                 it("should set proper security configuration") {
159                     assertThat(securityConfiguration).isEqualTo(
160                             SecurityConfiguration(
161                                     sslDisable = true,
162                                     privateKey = pk,
163                                     cert = cert,
164                                     trustedCert = trustCert)
165                     )
166                 }
167             }
168         }
169
170         describe("required parameter is absent") {
171             given("ves port is missing") {
172                 it("should throw exception") {
173                     assertThat(parseExpectingFailure("-p", listenPort, "-h", vesHost))
174                             .isInstanceOf(WrongArgumentError::class.java)
175                 }
176             }
177
178             given("ves host is missing") {
179                 it("should throw exception") {
180                     assertThat(parseExpectingFailure("-p", listenPort, "-v", vesPort))
181                             .isInstanceOf(WrongArgumentError::class.java)
182                 }
183             }
184
185             given("listen port is missing") {
186                 it("should throw exception") {
187                     assertThat(parseExpectingFailure("-h", vesHost, "-v", vesPort))
188                             .isInstanceOf(WrongArgumentError::class.java)
189                 }
190             }
191         }
192     }
193 })