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.whenever
28 import org.assertj.core.api.Assertions.assertThat
29 import org.assertj.core.api.Assertions.fail
30 import org.jetbrains.spek.api.Spek
31 import org.jetbrains.spek.api.dsl.describe
32 import org.jetbrains.spek.api.dsl.it
33 import org.onap.dcae.collectors.veshv.config.api.model.Route
34 import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator.Companion.DEFAULT_LOG_LEVEL
35 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
36 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSink
37 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
39 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(
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(
62 it("should use default log level") {
63 val result = cut.validate(config)
66 fail("Configuration should have been created successfully")
69 assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
75 describe("validating complete configuration") {
76 val config = PartialConfiguration(
77 listenPort = Some(defaultListenPort),
78 idleTimeoutSec = Some(defaultIdleTimeoutSec),
79 firstRequestDelaySec = Some(defaultFirstReqDelaySec),
80 requestIntervalSec = Some(defaultRequestIntervalSec),
81 sslDisable = Some(false),
82 keyStoreFile = Some(KEYSTORE),
83 keyStorePassword = Some(KEYSTORE_PASSWORD),
84 trustStoreFile = Some(TRUSTSTORE),
85 trustStorePassword = Some(TRUSTSTORE_PASSWORD),
86 streamPublishers = Some(sampleStreamsDefinition),
87 logLevel = Some(LogLevel.TRACE)
90 it("should create valid configuration") {
91 val result = cut.validate(config)
94 fail("Configuration should have been created successfully")
97 assertThat(it.server.listenPort)
98 .isEqualTo(defaultListenPort)
99 assertThat(it.server.idleTimeout)
100 .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
102 val securityKeys = it.security.keys
103 .getOrElse { fail("Should be immutableSecurityKeys") } as SecurityKeys
104 assertThat(securityKeys.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
105 assertThat(securityKeys.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
106 securityKeys.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
107 securityKeys.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
109 assertThat(it.cbs.firstRequestDelay)
110 .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
111 assertThat(it.cbs.requestInterval)
112 .isEqualTo(Duration.ofSeconds(defaultRequestIntervalSec))
114 assertThat(it.collector.routing)
115 .isEqualTo(sampleRouting)
116 assertThat(it.collector.maxPayloadSizeBytes)
117 .isEqualTo(sampleMaxPayloadSize)
119 assertThat(it.logLevel).isEqualTo(LogLevel.TRACE)
125 describe("validating configuration with security disabled") {
126 val config = partialConfiguration(
127 sslDisable = Some(true),
128 keyStoreFile = Some(""),
129 keyStorePassword = Some(""),
130 trustStoreFile = Some(""),
131 trustStorePassword = Some("")
134 it("should create valid configuration") {
135 val result = cut.validate(config)
138 fail("Configuration should have been created successfully but was $it")
141 assertThat(it.server.idleTimeout)
142 .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
144 assertThat(it.security.keys)
147 assertThat(it.cbs.firstRequestDelay)
148 .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
150 assertThat(it.collector.routing)
151 .isEqualTo(sampleRouting)
157 describe("validating configuration with ssl disable missing") {
158 val config = partialConfiguration(
162 it("should create valid configuration with ssl enabled") {
163 val result = cut.validate(config)
166 fail("Configuration should have been created successfully but was $it")
169 val securityKeys = it.security.keys
170 .getOrElse { fail("Should be immutableSecurityKeys") } as SecurityKeys
171 assertThat(securityKeys.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
172 assertThat(securityKeys.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
173 securityKeys.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
174 securityKeys.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
183 private fun partialConfiguration(listenPort: Option<Int> = Some(defaultListenPort),
184 idleTimeoutSec: Option<Long> = Some(defaultIdleTimeoutSec),
185 firstReqDelaySec: Option<Long> = Some(defaultFirstReqDelaySec),
186 requestIntervalSec: Option<Long> = Some(defaultRequestIntervalSec),
187 sslDisable: Option<Boolean> = Some(false),
188 keyStoreFile: Option<String> = Some(KEYSTORE),
189 keyStorePassword: Option<String> = Some(KEYSTORE_PASSWORD),
190 trustStoreFile: Option<String> = Some(TRUSTSTORE),
191 trustStorePassword: Option<String> = Some(TRUSTSTORE_PASSWORD),
192 streamPublishers: Option<List<KafkaSink>> = Some(sampleStreamsDefinition),
193 logLevel: Option<LogLevel> = Some(LogLevel.INFO)
194 ) = PartialConfiguration(
195 listenPort = listenPort,
196 idleTimeoutSec = idleTimeoutSec,
197 firstRequestDelaySec = firstReqDelaySec,
198 requestIntervalSec = requestIntervalSec,
199 sslDisable = sslDisable,
200 keyStoreFile = keyStoreFile,
201 keyStorePassword = keyStorePassword,
202 trustStoreFile = trustStoreFile,
203 trustStorePassword = trustStorePassword,
204 streamPublishers = streamPublishers,
208 const val defaultListenPort = 1234
209 const val defaultRequestIntervalSec = 3L
210 const val defaultIdleTimeoutSec = 10L
211 const val defaultFirstReqDelaySec = 10L
213 const val KEYSTORE = "test.ks.pkcs12"
214 const val KEYSTORE_PASSWORD = "changeMe"
215 const val TRUSTSTORE = "trust.ks.pkcs12"
216 const val TRUSTSTORE_PASSWORD = "changeMeToo"
218 const val sampleSinkName = "perf3gpp"
219 const val sampleMaxPayloadSize = 1024
221 private val sink = mock<KafkaSink>().also {
222 whenever(it.name()).thenReturn(sampleSinkName)
223 whenever(it.maxPayloadSizeBytes()).thenReturn(sampleMaxPayloadSize)
226 private val sampleStreamsDefinition = listOf(sink)
227 private val sampleRouting = listOf(Route(sink.name(), sink))