2  * ============LICENSE_START=======================================================
 
   3  * dcaegen2-collectors-veshv
 
   4  * ================================================================================
 
   5  * Copyright (C) 2019 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.config.impl
 
  22 import arrow.core.Some
 
  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.it
 
  27 import org.onap.dcae.collectors.veshv.config.api.model.Routing
 
  28 import org.onap.dcae.collectors.veshv.tests.utils.resourceAsStream
 
  29 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
 
  30 import java.io.StringReader
 
  31 import java.net.InetSocketAddress
 
  34  * @author Pawel Biniek <pawel.biniek@nokia.com>
 
  35  * @since February 2019
 
  37 internal object FileConfigurationReaderTest : Spek({
 
  38     describe("A configuration loader utility") {
 
  39         val cut = FileConfigurationReader()
 
  41         describe("partial configuration loading") {
 
  42             it("parses enumerations") {
 
  43                 val input = """{"logLevel":"ERROR"}"""
 
  45                 val config = cut.loadConfig(StringReader(input))
 
  46                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
 
  49             it("parses simple structure") {
 
  52                     "healthCheckApiPort" : 12002,
 
  57                 val config = cut.loadConfig(StringReader(input))
 
  58                 assertThat(config.server.nonEmpty()).isTrue()
 
  59                 assertThat(config.server.orNull()?.listenPort).isEqualTo(Some(12003))
 
  62             it("parses ip address") {
 
  63                 val input = """{  "collector" : {
 
  71                 val config = cut.loadConfig(StringReader(input))
 
  72                 assertThat(config.collector.nonEmpty()).isTrue()
 
  73                 val collector = config.collector.orNull() as PartialCollectorConfig
 
  74                 assertThat(collector.kafkaServers.nonEmpty()).isTrue()
 
  75                 val addresses = collector.kafkaServers.orNull() as List<InetSocketAddress>
 
  78                                 InetSocketAddress("192.168.255.1", 5005),
 
  79                                 InetSocketAddress("192.168.255.26", 5006)
 
  83             it("parses routing array with RoutingAdapter") {
 
  88                               "fromDomain": "perf3gpp",
 
  89                               "toTopic": "HV_VES_PERF3GPP"
 
  94                 val config = cut.loadConfig(StringReader(input))
 
  95                 assertThat(config.collector.nonEmpty()).isTrue()
 
  96                 val collector = config.collector.orNull() as PartialCollectorConfig
 
  97                 assertThat(collector.routing.nonEmpty()).isTrue()
 
  98                 val routing = collector.routing.orNull() as Routing
 
 100                     assertThat(routes.size).isEqualTo(1)
 
 101                     assertThat(routes[0].domain).isEqualTo("perf3gpp")
 
 102                     assertThat(routes[0].targetTopic).isEqualTo("HV_VES_PERF3GPP")
 
 106             it("parses disabled security configuration") {
 
 111                 val config = cut.loadConfig(StringReader(input))
 
 113                 assertThat(config.security.nonEmpty()).isTrue()
 
 114                 val security = config.security.orNull() as PartialSecurityConfig
 
 115                 assertThat(security.keys.nonEmpty()).isFalse()
 
 118             it("parses invalid log level string to empty option") {
 
 120                     "logLevel": something
 
 122                 val config = cut.loadConfig(input.reader())
 
 124                 assertThat(config.logLevel.isEmpty())
 
 128         describe("complete file loading") {
 
 129             it("loads actual file") {
 
 130                 val config = cut.loadConfig(
 
 131                         javaClass.resourceAsStream("/sampleConfig.json"))
 
 133                 assertThat(config).isNotNull
 
 134                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
 
 136                 assertThat(config.security.nonEmpty()).isTrue()
 
 137                 val security = config.security.orNull() as PartialSecurityConfig
 
 138                 assertThat(security.keys.nonEmpty()).isTrue()
 
 140                 assertThat(config.cbs.nonEmpty()).isTrue()
 
 141                 val cbs = config.cbs.orNull() as PartialCbsConfig
 
 142                 assertThat(cbs.firstRequestDelaySec).isEqualTo(Some(7))
 
 143                 assertThat(cbs.requestIntervalSec).isEqualTo(Some(900))
 
 145                 assertThat(config.collector.nonEmpty()).isTrue()
 
 146                 val collector = config.collector.orNull() as PartialCollectorConfig
 
 148                     assertThat(dummyMode).isEqualTo(Some(false))
 
 149                     assertThat(maxRequestSizeBytes).isEqualTo(Some(512000))
 
 150                     assertThat(kafkaServers.nonEmpty()).isTrue()
 
 151                     assertThat(routing.nonEmpty()).isTrue()
 
 154                 assertThat(config.server.nonEmpty()).isTrue()
 
 155                 val server = config.server.orNull() as PartialServerConfig
 
 157                     assertThat(idleTimeoutSec).isEqualTo(Some(1200))
 
 158                     assertThat(listenPort).isEqualTo(Some(6000))
 
 159                     assertThat(maxPayloadSizeBytes).isEqualTo(Some(512000))