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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.main
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 import java.time.Duration
36 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
39 object ArgBasedServerConfigurationTest : Spek({
40 lateinit var cut: ArgBasedServerConfiguration
41 val configurationUrl = "http://test-address/test"
42 val listenPort = "6969"
43 val updateInterval = "10"
44 val pk = Paths.get("/", "etc", "ves", "pk.pem")
45 val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt")
46 val trustCert = Paths.get("/", "etc", "ves", "trusted.crt")
49 cut = ArgBasedServerConfiguration()
52 fun parse(vararg cmdLine: String): ServerConfiguration {
53 val result = cut.parse(cmdLine)
54 return when (result) {
55 is Success -> result.value
56 is Failure -> throw AssertionError("Parsing result should be present")
60 describe("parsing arguments") {
61 given("all parameters are present in the long form") {
62 lateinit var result: ServerConfiguration
65 result = parse("--listen-port", listenPort,
66 "--config-url", configurationUrl,
67 "--update-interval", updateInterval,
68 "--private-key-file", pk.toFile().absolutePath,
69 "--cert-file", cert.toFile().absolutePath,
70 "--trust-cert-file", trustCert.toFile().absolutePath)
73 it("should set proper port") {
74 assertThat(result.port).isEqualTo(6969)
77 it("should set update interval") {
78 assertThat(result.configurationUpdateInterval).isEqualTo(Duration.ofSeconds(10))
81 it("should set proper config url") {
82 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
85 it("should set proper security configuration") {
86 assertThat(result.securityConfiguration).isEqualTo(
87 SecurityConfiguration(pk, cert, trustCert)
92 given("some parameters are present in the short form") {
93 lateinit var result: ServerConfiguration
96 result = parse("-p", "666", "-c", configurationUrl)
99 it("should set proper port") {
100 assertThat(result.port).isEqualTo(666)
103 it("should set proper config url") {
104 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
108 given("all optional parameters are absent") {
109 lateinit var result: ServerConfiguration
115 it("should set default port") {
116 assertThat(result.port).isEqualTo(DefaultValues.PORT)
119 it("should set default config url") {
120 assertThat(result.configurationUrl).isEqualTo(DefaultValues.CONFIG_URL)
123 it("should set default update interval") {
124 assertThat(result.configurationUpdateInterval).isEqualTo(Duration.ofSeconds(DefaultValues.UPDATE_INTERVAL))
127 on("security config") {
128 val securityConfiguration = result.securityConfiguration
130 it("should set default trust cert file") {
131 assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
134 it("should set default server cert file") {
135 assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
138 it("should set default private key file") {
139 assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)