fccd8b5aabbea1a073cf6b443a6a870d573ab993
[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.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.*
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 messagesAmount = 3L
40     val vesHost = "localhosting"
41     val pk = Paths.get("/", "etc", "ves", "pk.pem")
42     val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt")
43     val trustCert = Paths.get("/", "etc", "ves", "trusted.crt")
44
45     beforeEachTest {
46         cut = ArgConfigurationProvider()
47     }
48
49     fun parse(vararg cmdLine: String): SimulatorConfiguration =
50             cut.parse(cmdLine).fold(
51                     { throw AssertionError("Parsing result should be present") },
52                     ::identity
53             )
54
55     describe("parsing arguments") {
56         lateinit var result: SimulatorConfiguration
57
58         given("all parameters are present in the long form") {
59
60             beforeEachTest {
61                 result = parse("--ssl-disable",
62                         "--ves-port", "6969",
63                         "--ves-host", vesHost,
64                         "--messages", messagesAmount.toString(),
65                         "--private-key-file", pk.toFile().absolutePath,
66                         "--cert-file", cert.toFile().absolutePath,
67                         "--trust-cert-file", trustCert.toFile().absolutePath)
68             }
69
70             it("should set proper port") {
71                 assertThat(result.vesPort).isEqualTo(6969)
72             }
73
74
75             it("should set proper config url") {
76                 assertThat(result.messagesAmount).isEqualTo(messagesAmount)
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 = parse("-h", "ves-hv", "--ves-port", "666", "-m", messagesAmount.toString())
90             }
91
92             it("should set proper port") {
93                 assertThat(result.vesPort).isEqualTo(666)
94             }
95
96             it("should set proper messages amount") {
97                 assertThat(result.messagesAmount).isEqualTo(messagesAmount)
98             }
99         }
100
101         given("all optional parameters are absent") {
102
103             beforeEachTest {
104                 result = parse("-h", "ves-hv", "-p", "666")
105             }
106
107             it("should set default messages amount") {
108                 assertThat(result.messagesAmount).isEqualTo(DefaultValues.MESSAGES_AMOUNT)
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 = parse("--ssl-disable",
131                         "--ves-port", "888",
132                         "--ves-host", vesHost,
133                         "--messages", messagesAmount.toString(),
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 })