e43acfa3f655a117760d0632e63c443dbec3d43b
[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 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
35 import java.io.File
36 import java.time.Duration
37
38
39 internal object ConfigurationValidatorTest : Spek({
40     describe("ConfigurationValidator") {
41         val cut = ConfigurationValidator()
42
43         describe("validating partial configuration with missing fields") {
44             val config = PartialConfiguration(
45                     listenPort = Some(1)
46             )
47
48             it("should return ValidationError") {
49                 val result = cut.validate(config)
50                 assertThat(result.isLeft()).isTrue()
51             }
52         }
53
54         describe("validating configuration with empty log level") {
55             val config = partialConfiguration(
56                     logLevel = None
57             )
58
59             it("should use default log level") {
60                 val result = cut.validate(config)
61                 result.fold(
62                         {
63                             fail("Configuration should have been created successfully")
64                         },
65                         {
66                             assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
67                         }
68                 )
69             }
70         }
71
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)
86             )
87
88             it("should create valid configuration") {
89                 val result = cut.validate(config)
90                 result.fold(
91                         {
92                             fail("Configuration should have been created successfully")
93                         },
94                         {
95                             assertThat(it.server.listenPort).isEqualTo(defaultListenPort)
96                             assertThat(it.server.idleTimeout).isEqualTo(defaultIdleTimeoutSec)
97                             assertThat(it.server.maxPayloadSizeBytes).isEqualTo(defaultMaxPayloadSizeBytes)
98
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()) }
105
106                             assertThat(it.cbs.firstRequestDelay).isEqualTo(defaultFirstReqDelaySec)
107                             assertThat(it.cbs.requestInterval).isEqualTo(defaultRequestIntervalSec)
108
109                             assertThat(it.collector.routing).isEqualTo(emptyRouting)
110                             assertThat(it.logLevel).isEqualTo(LogLevel.TRACE)
111                         }
112                 )
113             }
114         }
115
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("")
123             )
124
125             it("should create valid configuration") {
126                 val result = cut.validate(config)
127                 result.fold(
128                         {
129                             fail("Configuration should have been created successfully but was $it")
130                         },
131                         {
132                             assertThat(it.server.idleTimeout)
133                                     .isEqualTo(defaultIdleTimeoutSec)
134
135                             assertThat(it.security.keys)
136                                     .isEqualTo(None)
137
138                             assertThat(it.cbs.firstRequestDelay)
139                                     .isEqualTo(defaultFirstReqDelaySec)
140
141                             assertThat(it.collector.routing)
142                                     .isEqualTo(emptyRouting)
143                         }
144                 )
145             }
146         }
147
148         describe("validating configuration with ssl disable missing") {
149             val config = partialConfiguration(
150                     sslDisable = None
151             )
152
153             it("should create valid configuration with ssl enabled") {
154                 val result = cut.validate(config)
155                 result.fold(
156                         {
157                             fail("Configuration should have been created successfully but was $it")
158                         },
159                         {
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()) }
166                         }
167                 )
168             }
169         }
170
171     }
172 })
173
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)
186 ) =
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,
198                 routing = routing,
199                 logLevel = logLevel
200         )
201
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)
207
208 val KEYSTORE = "test.ks.pkcs12"
209 val KEYSTORE_PASSWORD = "changeMe"
210 val TRUSTSTORE = "trust.ks.pkcs12"
211 val TRUSTSTORE_PASSWORD = "changeMeToo"
212
213 val emptyRouting: Routing = emptyList()