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