6420d84da1ddd386a308136807848ee7776ada9d
[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.ArgBasedClientConfiguration
30 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientConfiguration
31 import org.onap.dcae.collectors.veshv.simulators.xnf.config.DefaultValues
32 import java.nio.file.Paths
33
34
35 object ArgBasedClientConfigurationTest : Spek({
36     lateinit var cut: ArgBasedClientConfiguration
37     val messagesAmount = 3L
38     val vesHost = "localhosting"
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 = ArgBasedClientConfiguration()
45     }
46
47     fun parse(vararg cmdLine: String) = cut.parse(cmdLine)
48
49     describe("parsing arguments") {
50         lateinit var result: ClientConfiguration
51
52         given("all parameters are present in the long form") {
53
54             beforeEachTest {
55                 result = parse("--ves-port", "6969",
56                         "--ves-host", vesHost,
57                         "--messages", messagesAmount.toString(),
58                         "--private-key-file", pk.toFile().absolutePath,
59                         "--cert-file", cert.toFile().absolutePath,
60                         "--trust-cert-file", trustCert.toFile().absolutePath)
61             }
62
63             it("should set proper port") {
64                 assertThat(result.vesPort).isEqualTo(6969)
65             }
66
67
68             it("should set proper config url") {
69                 assertThat(result.messagesAmount).isEqualTo(messagesAmount)
70             }
71
72             it("should set proper security configuration") {
73                 assertThat(result.security).isEqualTo(
74                         SecurityConfiguration(pk, cert, 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", "-m", messagesAmount.toString())
83             }
84
85             it("should set proper port") {
86                 assertThat(result.vesPort).isEqualTo(666)
87             }
88
89             it("should set proper messages amount") {
90                 assertThat(result.messagesAmount).isEqualTo(messagesAmount)
91             }
92         }
93
94         given("all optional parameters are absent") {
95
96             beforeEachTest {
97                 result = parse("-h", "ves-hv", "-p", "666")
98             }
99
100             it("should set default messages amount") {
101                 assertThat(result.messagesAmount).isEqualTo(DefaultValues.MESSAGES_AMOUNT)
102             }
103
104             on("security config") {
105                 val securityConfiguration = result.security
106
107                 it("should set default trust cert file") {
108                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
109                 }
110
111                 it("should set default server cert file") {
112                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
113                 }
114
115                 it("should set default private key file") {
116                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
117                 }
118             }
119         }
120     }
121 })