5fa1fd6206212145c5fcb340d8f648202c392ecc
[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                     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)
89             )
90
91             it("should create valid configuration") {
92                 val result = cut.validate(config)
93                 result.fold(
94                         {
95                             fail("Configuration should have been created successfully")
96                         },
97                         {
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))
104
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()) }
111
112                             assertThat(it.cbs.firstRequestDelay)
113                                     .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
114                             assertThat(it.cbs.requestInterval)
115                                     .isEqualTo(Duration.ofSeconds(defaultRequestIntervalSec))
116
117                             assertThat(it.collector.routing)
118                                     .isEqualTo(sampleRouting)
119
120                             assertThat(it.logLevel).isEqualTo(LogLevel.TRACE)
121                         }
122                 )
123             }
124         }
125
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("")
133             )
134
135             it("should create valid configuration") {
136                 val result = cut.validate(config)
137                 result.fold(
138                         {
139                             fail("Configuration should have been created successfully but was $it")
140                         },
141                         {
142                             assertThat(it.server.idleTimeout)
143                                     .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
144
145                             assertThat(it.security.keys)
146                                     .isEqualTo(None)
147
148                             assertThat(it.cbs.firstRequestDelay)
149                                     .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
150
151                             assertThat(it.collector.routing)
152                                     .isEqualTo(sampleRouting)
153                         }
154                 )
155             }
156         }
157
158         describe("validating configuration with ssl disable missing") {
159             val config = partialConfiguration(
160                     sslDisable = None
161             )
162
163             it("should create valid configuration with ssl enabled") {
164                 val result = cut.validate(config)
165                 result.fold(
166                         {
167                             fail("Configuration should have been created successfully but was $it")
168                         },
169                         {
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()) }
176                         }
177                 )
178             }
179         }
180
181     }
182 })
183
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,
208         logLevel = logLevel
209 )
210
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
216
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"
221
222 const val sampleSinkName = "perf3gpp"
223
224 private val sampleSink = mock<KafkaSink>().also {
225     whenever(it.name()).thenReturn(sampleSinkName)
226 }
227 val sampleStreamsDefinition = listOf(sampleSink)
228 val sampleRouting = listOf(Route(sampleSink.name(), sampleSink))