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.None
 
  23 import arrow.core.Option
 
  24 import arrow.core.Some
 
  25 import arrow.core.getOrElse
 
  26 import com.nhaarman.mockitokotlin2.mock
 
  27 import com.nhaarman.mockitokotlin2.verify
 
  28 import com.nhaarman.mockitokotlin2.whenever
 
  29 import org.assertj.core.api.Assertions.assertThat
 
  30 import org.assertj.core.api.Assertions.fail
 
  31 import org.assertj.core.api.ObjectAssert
 
  32 import org.jetbrains.spek.api.Spek
 
  33 import org.jetbrains.spek.api.dsl.describe
 
  34 import org.jetbrains.spek.api.dsl.it
 
  35 import org.onap.dcae.collectors.veshv.config.api.model.Routing
 
  36 import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator.Companion.DEFAULT_LOG_LEVEL
 
  37 import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityKeysPaths
 
  38 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
 
  39 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
 
  40 import java.time.Duration
 
  42 internal object ConfigurationValidatorTest : Spek({
 
  43     describe("ConfigurationValidator") {
 
  44         val cut = ConfigurationValidator()
 
  46         describe("validating partial configuration with missing fields") {
 
  47             val config = PartialConfiguration(
 
  48                     Some(PartialServerConfig(listenPort = Some(1)))
 
  51             it("should return ValidationError") {
 
  52                 val result = cut.validate(config)
 
  53                 assertThat(result.isLeft()).isTrue()
 
  57         describe("validating configuration with empty log level") {
 
  58             val config = PartialConfiguration(
 
  59                     Some(PartialServerConfig(
 
  61                             Some(Duration.ofSeconds(2)),
 
  64                     Some(PartialCbsConfig(
 
  65                             Some(Duration.ofSeconds(5)),
 
  66                             Some(Duration.ofSeconds(3))
 
  68                     Some(PartialSecurityConfig(
 
  71                     Some(PartialCollectorConfig(
 
  77             it("should use default log level") {
 
  78                 val result = cut.validate(config)
 
  81                             fail("Configuration should have been created successfully")
 
  84                             assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
 
  90         describe("validating complete configuration") {
 
  91             val idleTimeoutSec = Duration.ofSeconds(10L)
 
  92             val firstReqDelaySec = Duration.ofSeconds(10L)
 
  93             val securityKeys = mock<SecurityKeysPaths>()
 
  94             val immutableSecurityKeys = mock<SecurityKeys>()
 
  95             whenever(securityKeys.asImmutableSecurityKeys()).thenReturn(immutableSecurityKeys)
 
  97             val config = PartialConfiguration(
 
  98                     Some(PartialServerConfig(
 
 100                             Some(idleTimeoutSec),
 
 103                     Some(PartialCbsConfig(
 
 104                             Some(firstReqDelaySec),
 
 105                             Some(Duration.ofSeconds(3))
 
 107                     Some(PartialSecurityConfig(
 
 110                     Some(PartialCollectorConfig(
 
 116             it("should create valid configuration") {
 
 117                 val result = cut.validate(config)
 
 120                             fail("Configuration should have been created successfully")
 
 123                             assertThat(it.server.idleTimeout)
 
 124                                     .isEqualTo(idleTimeoutSec)
 
 126                             verify(securityKeys).asImmutableSecurityKeys()
 
 127                             assertThat(it.security.keys
 
 128                                     .getOrElse { fail("Should be immutableSecurityKeys") })
 
 129                                     .isEqualTo(immutableSecurityKeys)
 
 131                             assertThat(it.cbs.firstRequestDelay)
 
 132                                     .isEqualTo(firstReqDelaySec)
 
 134                             assertThat(it.collector.routing)
 
 135                                     .isEqualTo(emptyRouting)
 
 141         describe("validating configuration with security disabled") {
 
 142             val idleTimeoutSec = Duration.ofSeconds(10)
 
 143             val firstReqDelaySec = Duration.ofSeconds(10)
 
 144             val missingSecurityKeys: Option<SecurityKeysPaths> = None
 
 146             val config = PartialConfiguration(
 
 147                     Some(PartialServerConfig(
 
 149                             Some(idleTimeoutSec),
 
 152                     Some(PartialCbsConfig(
 
 153                             Some(firstReqDelaySec),
 
 154                             Some(Duration.ofSeconds(3))
 
 156                     Some(PartialSecurityConfig(
 
 159                     Some(PartialCollectorConfig(
 
 165             it("should create valid configuration") {
 
 166                 val result = cut.validate(config)
 
 169                             fail("Configuration should have been created successfully but was $it")
 
 172                             assertThat(it.server.idleTimeout)
 
 173                                     .isEqualTo(idleTimeoutSec)
 
 175                             assertThat(it.security.keys)
 
 176                                     .isEqualTo(missingSecurityKeys)
 
 178                             assertThat(it.cbs.firstRequestDelay)
 
 179                                     .isEqualTo(firstReqDelaySec)
 
 181                             assertThat(it.collector.routing)
 
 182                                     .isEqualTo(emptyRouting)
 
 191 val emptyRouting: Routing = emptyList()
 
 192 val someFromEmptyRouting = Some(emptyRouting)