0806e8ca623f0b113182c73e88d7be0ad169682f
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.config.impl
21
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
38 import java.io.File
39 import java.time.Duration
40
41
42 internal object ConfigurationValidatorTest : Spek({
43     describe("ConfigurationValidator") {
44         val cut = ConfigurationValidator()
45
46         describe("validating partial configuration with missing fields") {
47             val config = PartialConfiguration(
48                     listenPort = Some(1)
49             )
50
51             it("should return ValidationError") {
52                 val result = cut.validate(config)
53                 assertThat(result.isLeft()).isTrue()
54             }
55         }
56
57         describe("validating configuration with empty log level") {
58             val config = partialConfiguration(
59                     logLevel = None
60             )
61
62             it("should use default log level") {
63                 val result = cut.validate(config)
64                 result.fold(
65                         {
66                             fail("Configuration should have been created successfully")
67                         },
68                         {
69                             assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
70                         }
71                 )
72             }
73         }
74
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)
88             )
89
90             it("should create valid configuration") {
91                 val result = cut.validate(config)
92                 result.fold(
93                         {
94                             fail("Configuration should have been created successfully")
95                         },
96                         {
97                             assertThat(it.server.listenPort)
98                                     .isEqualTo(defaultListenPort)
99                             assertThat(it.server.idleTimeout)
100                                     .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
101
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()) }
108
109                             assertThat(it.cbs.firstRequestDelay)
110                                     .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
111                             assertThat(it.cbs.requestInterval)
112                                     .isEqualTo(Duration.ofSeconds(defaultRequestIntervalSec))
113
114                             assertThat(it.collector.routing)
115                                     .isEqualTo(sampleRouting)
116                             assertThat(it.collector.maxPayloadSizeBytes)
117                                     .isEqualTo(sampleMaxPayloadSize)
118
119                             assertThat(it.logLevel).isEqualTo(LogLevel.TRACE)
120                         }
121                 )
122             }
123         }
124
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("")
132             )
133
134             it("should create valid configuration") {
135                 val result = cut.validate(config)
136                 result.fold(
137                         {
138                             fail("Configuration should have been created successfully but was $it")
139                         },
140                         {
141                             assertThat(it.server.idleTimeout)
142                                     .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
143
144                             assertThat(it.security.keys)
145                                     .isEqualTo(None)
146
147                             assertThat(it.cbs.firstRequestDelay)
148                                     .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
149
150                             assertThat(it.collector.routing)
151                                     .isEqualTo(sampleRouting)
152                         }
153                 )
154             }
155         }
156
157         describe("validating configuration with ssl disable missing") {
158             val config = partialConfiguration(
159                     sslDisable = None
160             )
161
162             it("should create valid configuration with ssl enabled") {
163                 val result = cut.validate(config)
164                 result.fold(
165                         {
166                             fail("Configuration should have been created successfully but was $it")
167                         },
168                         {
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()) }
175                         }
176                 )
177             }
178         }
179
180     }
181 })
182
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,
205         logLevel = logLevel
206 )
207
208 const val defaultListenPort = 1234
209 const val defaultRequestIntervalSec = 3L
210 const val defaultIdleTimeoutSec = 10L
211 const val defaultFirstReqDelaySec = 10L
212
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"
217
218 const val sampleSinkName = "perf3gpp"
219 const val sampleMaxPayloadSize = 1024
220
221 private val sink = mock<KafkaSink>().also {
222     whenever(it.name()).thenReturn(sampleSinkName)
223     whenever(it.maxPayloadSizeBytes()).thenReturn(sampleMaxPayloadSize)
224 }
225
226 private val sampleStreamsDefinition = listOf(sink)
227 private val sampleRouting = listOf(Route(sink.name(), sink))