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 maxPayloadSizeBytes = Some(defaultMaxPayloadSizeBytes),
80 firstRequestDelaySec = Some(defaultFirstReqDelaySec),
81 requestIntervalSec = Some(defaultRequestIntervalSec),
82 sslDisable = Some(false),
83 keyStoreFile = Some(KEYSTORE),
84 keyStorePassword = Some(KEYSTORE_PASSWORD),
85 trustStoreFile = Some(TRUSTSTORE),
86 trustStorePassword = Some(TRUSTSTORE_PASSWORD),
87 streamPublishers = Some(sampleStreamsDefinition),
88 logLevel = Some(LogLevel.TRACE)
91 it("should create valid configuration") {
92 val result = cut.validate(config)
95 fail("Configuration should have been created successfully")
98 assertThat(it.server.listenPort)
99 .isEqualTo(defaultListenPort)
100 assertThat(it.server.maxPayloadSizeBytes)
101 .isEqualTo(defaultMaxPayloadSizeBytes)
102 assertThat(it.server.idleTimeout)
103 .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
105 val securityKeys = it.security.keys
106 .getOrElse { fail("Should be immutableSecurityKeys") } as SecurityKeys
107 assertThat(securityKeys.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
108 assertThat(securityKeys.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
109 securityKeys.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
110 securityKeys.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
112 assertThat(it.cbs.firstRequestDelay)
113 .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
114 assertThat(it.cbs.requestInterval)
115 .isEqualTo(Duration.ofSeconds(defaultRequestIntervalSec))
117 assertThat(it.collector.routing)
118 .isEqualTo(sampleRouting)
120 assertThat(it.logLevel).isEqualTo(LogLevel.TRACE)
126 describe("validating configuration with security disabled") {
127 val config = partialConfiguration(
128 sslDisable = Some(true),
129 keyStoreFile = Some(""),
130 keyStorePassword = Some(""),
131 trustStoreFile = Some(""),
132 trustStorePassword = Some("")
135 it("should create valid configuration") {
136 val result = cut.validate(config)
139 fail("Configuration should have been created successfully but was $it")
142 assertThat(it.server.idleTimeout)
143 .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
145 assertThat(it.security.keys)
148 assertThat(it.cbs.firstRequestDelay)
149 .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
151 assertThat(it.collector.routing)
152 .isEqualTo(sampleRouting)
158 describe("validating configuration with ssl disable missing") {
159 val config = partialConfiguration(
163 it("should create valid configuration with ssl enabled") {
164 val result = cut.validate(config)
167 fail("Configuration should have been created successfully but was $it")
170 val securityKeys = it.security.keys
171 .getOrElse { fail("Should be immutableSecurityKeys") } as SecurityKeys
172 assertThat(securityKeys.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
173 assertThat(securityKeys.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
174 securityKeys.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
175 securityKeys.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
184 private fun partialConfiguration(listenPort: Option<Int> = Some(defaultListenPort),
185 idleTimeoutSec: Option<Long> = Some(defaultIdleTimeoutSec),
186 maxPayloadSizeBytes: Option<Int> = Some(defaultMaxPayloadSizeBytes),
187 firstReqDelaySec: Option<Long> = Some(defaultFirstReqDelaySec),
188 requestIntervalSec: Option<Long> = Some(defaultRequestIntervalSec),
189 sslDisable: Option<Boolean> = Some(false),
190 keyStoreFile: Option<String> = Some(KEYSTORE),
191 keyStorePassword: Option<String> = Some(KEYSTORE_PASSWORD),
192 trustStoreFile: Option<String> = Some(TRUSTSTORE),
193 trustStorePassword: Option<String> = Some(TRUSTSTORE_PASSWORD),
194 streamPublishers: Option<List<KafkaSink>> = Some(sampleStreamsDefinition),
195 logLevel: Option<LogLevel> = Some(LogLevel.INFO)
196 ) = PartialConfiguration(
197 listenPort = listenPort,
198 idleTimeoutSec = idleTimeoutSec,
199 maxPayloadSizeBytes = maxPayloadSizeBytes,
200 firstRequestDelaySec = firstReqDelaySec,
201 requestIntervalSec = requestIntervalSec,
202 sslDisable = sslDisable,
203 keyStoreFile = keyStoreFile,
204 keyStorePassword = keyStorePassword,
205 trustStoreFile = trustStoreFile,
206 trustStorePassword = trustStorePassword,
207 streamPublishers = streamPublishers,
211 const val defaultListenPort = 1234
212 const val defaultMaxPayloadSizeBytes = 2
213 const val defaultRequestIntervalSec = 3L
214 const val defaultIdleTimeoutSec = 10L
215 const val defaultFirstReqDelaySec = 10L
217 const val KEYSTORE = "test.ks.pkcs12"
218 const val KEYSTORE_PASSWORD = "changeMe"
219 const val TRUSTSTORE = "trust.ks.pkcs12"
220 const val TRUSTSTORE_PASSWORD = "changeMeToo"
222 const val sampleSinkName = "perf3gpp"
224 private val sampleSink = mock<KafkaSink>().also {
225 whenever(it.name()).thenReturn(sampleSinkName)
227 val sampleStreamsDefinition = listOf(sampleSink)
228 val sampleRouting = listOf(Route(sampleSink.name(), sampleSink))