0498344cba5b63f6b15d0282ef801615d08895e6
[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
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.model.ServerConfiguration
31 import java.nio.file.Paths
32 import java.time.Duration
33
34 /**
35  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
36  * @since May 2018
37  */
38 object ArgBasedServerConfigurationTest : Spek({
39     lateinit var cut: ArgBasedServerConfiguration
40     val configurationUrl = "http://test-address/test"
41     val firstRequestDelay = "10"
42     val listenPort = "6969"
43     val pk = Paths.get("/", "etc", "ves", "pk.pem")
44     val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt")
45     val trustCert = Paths.get("/", "etc", "ves", "trusted.crt")
46
47     beforeEachTest {
48         cut = ArgBasedServerConfiguration()
49     }
50
51     fun parse(vararg cmdLine: String): ServerConfiguration =
52             cut.parse(cmdLine).fold(
53                     {throw AssertionError("Parsing result should be present")},
54                     ::identity
55             )
56
57     describe("parsing arguments") {
58         given("all parameters are present in the long form") {
59             lateinit var result: ServerConfiguration
60
61             beforeEachTest {
62                 result = parse("--ssl-disable",
63                         "--listen-port", listenPort,
64                         "--config-url", configurationUrl,
65                         "--first-request-delay", firstRequestDelay,
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.port).isEqualTo(6969)
73             }
74
75             it("should set proper first consul request delay") {
76                 assertThat(result.firstRequestDelay).isEqualTo(Duration.ofSeconds(10))
77             }
78
79             it("should set proper config url") {
80                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
81             }
82
83             it("should set proper security configuration") {
84                 assertThat(result.securityConfiguration).isEqualTo(
85                         SecurityConfiguration(sslDisable = true, privateKey = pk, cert = cert, trustedCert = trustCert)
86                 )
87             }
88
89
90         }
91
92         given("some parameters are present in the short form") {
93             lateinit var result: ServerConfiguration
94
95             beforeEachTest {
96                 result = parse("-p", "666", "-c", configurationUrl, "-d", firstRequestDelay)
97             }
98
99             it("should set proper port") {
100                 assertThat(result.port).isEqualTo(666)
101             }
102
103             it("should set proper first consul request delay") {
104                 assertThat(result.firstRequestDelay).isEqualTo(Duration.ofSeconds(10))
105             }
106
107             it("should set proper config url") {
108                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
109             }
110         }
111
112         given("all optional parameters are absent") {
113             lateinit var result: ServerConfiguration
114
115             beforeEachTest {
116                 result = parse()
117             }
118
119             it("should set default port") {
120                 assertThat(result.port).isEqualTo(DefaultValues.PORT)
121             }
122
123             it("should set default config url") {
124                 assertThat(result.configurationUrl).isEqualTo(DefaultValues.CONFIG_URL)
125             }
126
127             it("should set default first consul request delay") {
128                 assertThat(result.firstRequestDelay)
129                         .isEqualTo(Duration.ofSeconds(DefaultValues.CONSUL_FIRST_REQUEST_DELAY))
130             }
131
132             on("security config") {
133                 val securityConfiguration = result.securityConfiguration
134
135                 it("should set default trust cert file") {
136                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
137                 }
138
139                 it("should set default server cert file") {
140                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
141                 }
142
143                 it("should set default private key file") {
144                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
145                 }
146             }
147         }
148     }
149 })