8749dc5bd7a57e822d8fa20ae277527bafe88a68
[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 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.simulators.xnf.config.ArgXnfSimulatorConfiguration
30 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgXnfSimulatorConfiguration.DefaultValues
31 import org.onap.dcae.collectors.veshv.simulators.xnf.config.SimulatorConfiguration
32 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingFailure
33 import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingSuccess
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     describe("parsing arguments") {
53         lateinit var result: SimulatorConfiguration
54
55         given("all parameters are present in the long form") {
56
57             beforeEachTest {
58                 result = cut.parseExpectingSuccess("--ssl-disable",
59                         "--listen-port", listenPort,
60                         "--ves-host", vesHost,
61                         "--ves-port", vesPort,
62                         "--private-key-file", pk.toFile().absolutePath,
63                         "--cert-file", cert.toFile().absolutePath,
64                         "--trust-cert-file", trustCert.toFile().absolutePath)
65             }
66
67             it("should set proper listen port") {
68                 assertThat(result.listenPort).isEqualTo(listenPort.toInt())
69             }
70
71             it("should set proper ves host") {
72                 assertThat(result.vesHost).isEqualTo(vesHost)
73             }
74
75             it("should set proper ves port") {
76                 assertThat(result.vesPort).isEqualTo(vesPort.toInt())
77             }
78
79             it("should set proper security configuration") {
80                 assertThat(result.security).isEqualTo(
81                         SecurityConfiguration(sslDisable = true, privateKey = pk, cert = cert, trustedCert = trustCert)
82                 )
83             }
84         }
85
86         given("some parameters are present in the short form") {
87
88             beforeEachTest {
89                 result = cut.parseExpectingSuccess("-p", listenPort, "-h", vesHost, "--ves-port", vesPort)
90             }
91
92             it("should set proper listen port") {
93                 assertThat(result.listenPort).isEqualTo(listenPort.toInt())
94             }
95
96             it("should set proper ves host") {
97                 assertThat(result.vesHost).isEqualTo(vesHost)
98             }
99
100             it("should set proper ves port") {
101                 assertThat(result.vesPort).isEqualTo(vesPort.toInt())
102             }
103         }
104
105         given("all optional parameters are absent") {
106
107             beforeEachTest {
108                 result = cut.parseExpectingSuccess("-p", listenPort, "-h", vesHost, "-v", vesPort)
109             }
110
111             on("security config") {
112                 val securityConfiguration = result.security
113
114                 it("should set default trust cert file") {
115                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
116                 }
117
118                 it("should set default server cert file") {
119                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
120                 }
121
122                 it("should set default private key file") {
123                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
124                 }
125             }
126         }
127
128         given("disabled ssl certs together with all other parameters") {
129             beforeEachTest {
130                 result = cut.parseExpectingSuccess("--ssl-disable",
131                         "--listen-port", listenPort,
132                         "--ves-port", "888",
133                         "--ves-host", vesHost,
134                         "--private-key-file", pk.toFile().absolutePath,
135                         "--cert-file", cert.toFile().absolutePath,
136                         "--trust-cert-file", trustCert.toFile().absolutePath)
137             }
138
139             on("security config") {
140                 val securityConfiguration = result.security
141
142                 it("should set ssl disable to true") {
143                     assertTrue(securityConfiguration.sslDisable)
144                 }
145
146                 it("should set proper security configuration") {
147                     assertThat(securityConfiguration).isEqualTo(
148                             SecurityConfiguration(
149                                     sslDisable = true,
150                                     privateKey = pk,
151                                     cert = cert,
152                                     trustedCert = trustCert)
153                     )
154                 }
155             }
156         }
157
158         describe("required parameter is absent") {
159             given("ves port is missing") {
160                 it("should throw exception") {
161                     assertThat(cut.parseExpectingFailure("-p", listenPort, "-h", vesHost))
162                             .isInstanceOf(WrongArgumentError::class.java)
163                 }
164             }
165
166             given("ves host is missing") {
167                 it("should throw exception") {
168                     assertThat(cut.parseExpectingFailure("-p", listenPort, "-v", vesPort))
169                             .isInstanceOf(WrongArgumentError::class.java)
170                 }
171             }
172
173             given("listen port is missing") {
174                 it("should throw exception") {
175                     assertThat(cut.parseExpectingFailure("-h", vesHost, "-v", vesPort))
176                             .isInstanceOf(WrongArgumentError::class.java)
177                 }
178             }
179         }
180     }
181 })