4c2425bc80a8ea1ec3d4a29be2c82647212aa1e0
[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.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.model.ServerConfiguration
32 import java.nio.file.Paths
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 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 = ArgBasedServerConfiguration()
47     }
48
49     fun parse(vararg cmdLine: String): ServerConfiguration {
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         given("all parameters are present in the long form") {
59             lateinit var result: ServerConfiguration
60
61             beforeEachTest {
62                 result = parse("--listen-port", "6969",
63                         "--config-url", configurationUrl,
64                         "--private-key-file", pk.toFile().absolutePath,
65                         "--cert-file", cert.toFile().absolutePath,
66                         "--trust-cert-file", trustCert.toFile().absolutePath)
67             }
68
69             it("should set proper port") {
70                 assertThat(result.port).isEqualTo(6969)
71             }
72
73             it("should set proper config url") {
74                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
75             }
76
77             it("should set proper security configuration") {
78                 assertThat(result.securityConfiguration).isEqualTo(
79                         SecurityConfiguration(pk, cert, trustCert)
80                 )
81             }
82         }
83
84         given("some parameters are present in the short form") {
85             lateinit var result: ServerConfiguration
86
87             beforeEachTest {
88                 result = parse("-p", "666", "-c", configurationUrl)
89             }
90
91             it("should set proper port") {
92                 assertThat(result.port).isEqualTo(666)
93             }
94
95             it("should set proper config url") {
96                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
97             }
98         }
99
100         given("all optional parameters are absent") {
101             lateinit var result: ServerConfiguration
102
103             beforeEachTest {
104                 result = parse()
105             }
106
107             it("should set default port") {
108                 assertThat(result.port).isEqualTo(DefaultValues.PORT)
109             }
110
111             it("should set default config url") {
112                 assertThat(result.configurationUrl).isEqualTo(DefaultValues.CONFIG_URL)
113             }
114
115             on("security config") {
116                 val securityConfiguration = result.securityConfiguration
117
118                 it("should set default trust cert file") {
119                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
120                 }
121
122                 it("should set default server cert file") {
123                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
124                 }
125
126                 it("should set default private key file") {
127                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
128                 }
129             }
130         }
131     }
132 })