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 org.assertj.core.api.Assertions.assertThat
27 import org.assertj.core.api.Assertions.fail
28 import org.jetbrains.spek.api.Spek
29 import org.jetbrains.spek.api.dsl.describe
30 import org.jetbrains.spek.api.dsl.it
31 import org.onap.dcae.collectors.veshv.config.api.model.Routing
32 import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator.Companion.DEFAULT_LOG_LEVEL
33 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
34 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
36 import java.time.Duration
39 internal object ConfigurationValidatorTest : Spek({
40 describe("ConfigurationValidator") {
41 val cut = ConfigurationValidator()
43 describe("validating partial configuration with missing fields") {
44 val config = PartialConfiguration(
48 it("should return ValidationError") {
49 val result = cut.validate(config)
50 assertThat(result.isLeft()).isTrue()
54 describe("validating configuration with empty log level") {
55 val config = partialConfiguration(
59 it("should use default log level") {
60 val result = cut.validate(config)
63 fail("Configuration should have been created successfully")
66 assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
72 describe("validating complete configuration") {
73 val config = PartialConfiguration(
74 listenPort = Some(defaultListenPort),
75 idleTimeoutSec = Some(defaultIdleTimeoutSec),
76 maxPayloadSizeBytes = Some(defaultMaxPayloadSizeBytes),
77 firstRequestDelaySec = Some(defaultFirstReqDelaySec),
78 requestIntervalSec = Some(defaultRequestIntervalSec),
79 sslDisable = Some(false),
80 keyStoreFile = Some(KEYSTORE),
81 keyStorePassword = Some(KEYSTORE_PASSWORD),
82 trustStoreFile = Some(TRUSTSTORE),
83 trustStorePassword = Some(TRUSTSTORE_PASSWORD),
84 routing = Some(emptyRouting),
85 logLevel = Some(LogLevel.TRACE)
88 it("should create valid configuration") {
89 val result = cut.validate(config)
92 fail("Configuration should have been created successfully")
95 assertThat(it.server.listenPort).isEqualTo(defaultListenPort)
96 assertThat(it.server.idleTimeout).isEqualTo(defaultIdleTimeoutSec)
97 assertThat(it.server.maxPayloadSizeBytes).isEqualTo(defaultMaxPayloadSizeBytes)
99 val securityKeys = it.security.keys
100 .getOrElse { fail("Should be immutableSecurityKeys") } as SecurityKeys
101 assertThat(securityKeys.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
102 assertThat(securityKeys.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
103 securityKeys.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
104 securityKeys.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
106 assertThat(it.cbs.firstRequestDelay).isEqualTo(defaultFirstReqDelaySec)
107 assertThat(it.cbs.requestInterval).isEqualTo(defaultRequestIntervalSec)
109 assertThat(it.collector.routing).isEqualTo(emptyRouting)
110 assertThat(it.logLevel).isEqualTo(LogLevel.TRACE)
116 describe("validating configuration with security disabled") {
117 val config = partialConfiguration(
118 sslDisable = Some(true),
119 keyStoreFile = Some(""),
120 keyStorePassword = Some(""),
121 trustStoreFile = Some(""),
122 trustStorePassword = Some("")
125 it("should create valid configuration") {
126 val result = cut.validate(config)
129 fail("Configuration should have been created successfully but was $it")
132 assertThat(it.server.idleTimeout)
133 .isEqualTo(defaultIdleTimeoutSec)
135 assertThat(it.security.keys)
138 assertThat(it.cbs.firstRequestDelay)
139 .isEqualTo(defaultFirstReqDelaySec)
141 assertThat(it.collector.routing)
142 .isEqualTo(emptyRouting)
148 describe("validating configuration with ssl disable missing") {
149 val config = partialConfiguration(
153 it("should create valid configuration with ssl enabled") {
154 val result = cut.validate(config)
157 fail("Configuration should have been created successfully but was $it")
160 val securityKeys = it.security.keys
161 .getOrElse { fail("Should be immutableSecurityKeys") } as SecurityKeys
162 assertThat(securityKeys.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
163 assertThat(securityKeys.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
164 securityKeys.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
165 securityKeys.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
174 private fun partialConfiguration(listenPort: Option<Int> = Some(defaultListenPort),
175 idleTimeoutSec: Option<Duration> = Some(defaultIdleTimeoutSec),
176 maxPayloadSizeBytes: Option<Int> = Some(defaultMaxPayloadSizeBytes),
177 firstReqDelaySec: Option<Duration> = Some(defaultFirstReqDelaySec),
178 requestIntervalSec: Option<Duration> = Some(defaultRequestIntervalSec),
179 sslDisable: Option<Boolean> = Some(false),
180 keyStoreFile: Option<String> = Some(KEYSTORE),
181 keyStorePassword: Option<String> = Some(KEYSTORE_PASSWORD),
182 trustStoreFile: Option<String> = Some(TRUSTSTORE),
183 trustStorePassword: Option<String> = Some(TRUSTSTORE_PASSWORD),
184 routing: Option<Routing> = Some(emptyRouting),
185 logLevel: Option<LogLevel> = Some(LogLevel.INFO)
187 PartialConfiguration(
188 listenPort = listenPort,
189 idleTimeoutSec = idleTimeoutSec,
190 maxPayloadSizeBytes = maxPayloadSizeBytes,
191 firstRequestDelaySec = firstReqDelaySec,
192 requestIntervalSec = requestIntervalSec,
193 sslDisable = sslDisable,
194 keyStoreFile = keyStoreFile,
195 keyStorePassword = keyStorePassword,
196 trustStoreFile = trustStoreFile,
197 trustStorePassword = trustStorePassword,
202 val defaultListenPort = 1234
203 val defaultRequestIntervalSec = Duration.ofSeconds(3)
204 val defaultMaxPayloadSizeBytes = 2
205 val defaultIdleTimeoutSec = Duration.ofSeconds(10L)
206 val defaultFirstReqDelaySec = Duration.ofSeconds(10L)
208 val KEYSTORE = "test.ks.pkcs12"
209 val KEYSTORE_PASSWORD = "changeMe"
210 val TRUSTSTORE = "trust.ks.pkcs12"
211 val TRUSTSTORE_PASSWORD = "changeMeToo"
213 val emptyRouting: Routing = emptyList()