Configure xnf simulator api listen port
[dcaegen2/collectors/hv-ves.git] / hv-collector-xnf-simulator / src / test / kotlin / org / onap / dcae / collectors / veshv / main / config / ArgConfigurationProviderTest.kt
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.assertThat
24 import org.jetbrains.spek.api.Spek
25 import org.jetbrains.spek.api.dsl.describe
26 import org.jetbrains.spek.api.dsl.given
27 import org.jetbrains.spek.api.dsl.it
28 import org.jetbrains.spek.api.dsl.on
29 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
30 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgConfigurationProvider
31 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgConfigurationProvider.DefaultValues
32 import org.onap.dcae.collectors.veshv.simulators.xnf.config.SimulatorConfiguration
33 import java.nio.file.Paths
34 import kotlin.test.assertTrue
35
36
37 object ArgConfigurationProviderTest : Spek({
38     lateinit var cut: ArgConfigurationProvider
39     val vesHost = "localhosting"
40     val pk = Paths.get("/", "etc", "ves", "pk.pem")
41     val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt")
42     val trustCert = Paths.get("/", "etc", "ves", "trusted.crt")
43
44     beforeEachTest {
45         cut = ArgConfigurationProvider()
46     }
47
48     fun parse(vararg cmdLine: String): SimulatorConfiguration =
49             cut.parse(cmdLine).fold(
50                     { throw AssertionError("Parsing result should be present") },
51                     ::identity
52             )
53
54     describe("parsing arguments") {
55         lateinit var result: SimulatorConfiguration
56
57         given("all parameters are present in the long form") {
58
59             beforeEachTest {
60                 result = parse("--ssl-disable",
61                         "--ves-port", "6969",
62                         "--ves-host", vesHost,
63                         "--private-key-file", pk.toFile().absolutePath,
64                         "--cert-file", cert.toFile().absolutePath,
65                         "--trust-cert-file", trustCert.toFile().absolutePath)
66             }
67
68             it("should set proper port") {
69                 assertThat(result.vesPort).isEqualTo(6969)
70             }
71
72             it("should set proper security configuration") {
73                 assertThat(result.security).isEqualTo(
74                         SecurityConfiguration(sslDisable = true, privateKey = pk, cert = cert, trustedCert = trustCert)
75                 )
76             }
77         }
78
79         given("some parameters are present in the short form") {
80
81             beforeEachTest {
82                 result = parse("-h", "ves-hv", "--ves-port", "666")
83             }
84
85             it("should set proper port") {
86                 assertThat(result.vesPort).isEqualTo(666)
87             }
88         }
89
90         given("all optional parameters are absent") {
91
92             beforeEachTest {
93                 result = parse("-h", "ves-hv", "-v", "666")
94             }
95
96             on("security config") {
97                 val securityConfiguration = result.security
98
99                 it("should set default trust cert file") {
100                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
101                 }
102
103                 it("should set default server cert file") {
104                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
105                 }
106
107                 it("should set default private key file") {
108                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
109                 }
110             }
111         }
112
113         given("disabled ssl certs together with all other parameters") {
114             beforeEachTest {
115                 result = parse("--ssl-disable",
116                         "--ves-port", "888",
117                         "--ves-host", vesHost,
118                         "--private-key-file", pk.toFile().absolutePath,
119                         "--cert-file", cert.toFile().absolutePath,
120                         "--trust-cert-file", trustCert.toFile().absolutePath)
121             }
122
123             on("security config") {
124                 val securityConfiguration = result.security
125
126                 it("should set ssl disable to true") {
127                     assertTrue(securityConfiguration.sslDisable)
128                 }
129
130                 it("should set proper security configuration") {
131                     assertThat(securityConfiguration).isEqualTo(
132                             SecurityConfiguration(
133                                     sslDisable = true,
134                                     privateKey = pk,
135                                     cert = cert,
136                                     trustedCert = trustCert)
137                     )
138                 }
139             }
140         }
141     }
142 })