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
 
  35  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
 
  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")
 
  46         cut = ArgBasedServerConfiguration()
 
  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")
 
  57     describe("parsing arguments") {
 
  58         given("all parameters are present in the long form") {
 
  59             lateinit var result: ServerConfiguration
 
  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)
 
  69             it("should set proper port") {
 
  70                 assertThat(result.port).isEqualTo(6969)
 
  73             it("should set proper config url") {
 
  74                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
 
  77             it("should set proper security configuration") {
 
  78                 assertThat(result.securityConfiguration).isEqualTo(
 
  79                         SecurityConfiguration(pk, cert, trustCert)
 
  84         given("some parameters are present in the short form") {
 
  85             lateinit var result: ServerConfiguration
 
  88                 result = parse("-p", "666", "-c", configurationUrl)
 
  91             it("should set proper port") {
 
  92                 assertThat(result.port).isEqualTo(666)
 
  95             it("should set proper config url") {
 
  96                 assertThat(result.configurationUrl).isEqualTo(configurationUrl)
 
 100         given("all optional parameters are absent") {
 
 101             lateinit var result: ServerConfiguration
 
 107             it("should set default port") {
 
 108                 assertThat(result.port).isEqualTo(DefaultValues.PORT)
 
 111             it("should set default config url") {
 
 112                 assertThat(result.configurationUrl).isEqualTo(DefaultValues.CONFIG_URL)
 
 115             on("security config") {
 
 116                 val securityConfiguration = result.securityConfiguration
 
 118                 it("should set default trust cert file") {
 
 119                     assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
 
 122                 it("should set default server cert file") {
 
 123                     assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
 
 126                 it("should set default private key file") {
 
 127                     assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)