682bec5a9193d2cd4e14df7a5ada5e12baf370ea
[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.verify
28 import com.nhaarman.mockitokotlin2.whenever
29 import org.assertj.core.api.Assertions.assertThat
30 import org.assertj.core.api.Assertions.fail
31 import org.assertj.core.api.ObjectAssert
32 import org.jetbrains.spek.api.Spek
33 import org.jetbrains.spek.api.dsl.describe
34 import org.jetbrains.spek.api.dsl.it
35 import org.onap.dcae.collectors.veshv.config.api.model.Routing
36 import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator.Companion.DEFAULT_LOG_LEVEL
37 import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityKeysPaths
38 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
39 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
40 import java.time.Duration
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                     Some(PartialServerConfig(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                     Some(PartialServerConfig(
60                             Some(1),
61                             Some(Duration.ofSeconds(2)),
62                             Some(3)
63                     )),
64                     Some(PartialCbsConfig(
65                             Some(Duration.ofSeconds(5)),
66                             Some(Duration.ofSeconds(3))
67                     )),
68                     Some(PartialSecurityConfig(
69                             Some(mock())
70                     )),
71                     Some(PartialCollectorConfig(
72                             someFromEmptyRouting
73                     )),
74                     None
75             )
76
77             it("should use default log level") {
78                 val result = cut.validate(config)
79                 result.fold(
80                         {
81                             fail("Configuration should have been created successfully")
82                         },
83                         {
84                             assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
85                         }
86                 )
87             }
88         }
89
90         describe("validating complete configuration") {
91             val idleTimeoutSec = Duration.ofSeconds(10L)
92             val firstReqDelaySec = Duration.ofSeconds(10L)
93             val securityKeys = mock<SecurityKeysPaths>()
94             val immutableSecurityKeys = mock<SecurityKeys>()
95             whenever(securityKeys.asImmutableSecurityKeys()).thenReturn(immutableSecurityKeys)
96
97             val config = PartialConfiguration(
98                     Some(PartialServerConfig(
99                             Some(1),
100                             Some(idleTimeoutSec),
101                             Some(2)
102                     )),
103                     Some(PartialCbsConfig(
104                             Some(firstReqDelaySec),
105                             Some(Duration.ofSeconds(3))
106                     )),
107                     Some(PartialSecurityConfig(
108                             Some(securityKeys)
109                     )),
110                     Some(PartialCollectorConfig(
111                             someFromEmptyRouting
112                     )),
113                     Some(LogLevel.INFO)
114             )
115
116             it("should create valid configuration") {
117                 val result = cut.validate(config)
118                 result.fold(
119                         {
120                             fail("Configuration should have been created successfully")
121                         },
122                         {
123                             assertThat(it.server.idleTimeout)
124                                     .isEqualTo(idleTimeoutSec)
125
126                             verify(securityKeys).asImmutableSecurityKeys()
127                             assertThat(it.security.keys
128                                     .getOrElse { fail("Should be immutableSecurityKeys") })
129                                     .isEqualTo(immutableSecurityKeys)
130
131                             assertThat(it.cbs.firstRequestDelay)
132                                     .isEqualTo(firstReqDelaySec)
133
134                             assertThat(it.collector.routing)
135                                     .isEqualTo(emptyRouting)
136                         }
137                 )
138             }
139         }
140
141         describe("validating configuration with security disabled") {
142             val idleTimeoutSec = Duration.ofSeconds(10)
143             val firstReqDelaySec = Duration.ofSeconds(10)
144             val missingSecurityKeys: Option<SecurityKeysPaths> = None
145
146             val config = PartialConfiguration(
147                     Some(PartialServerConfig(
148                             Some(1),
149                             Some(idleTimeoutSec),
150                             Some(2)
151                     )),
152                     Some(PartialCbsConfig(
153                             Some(firstReqDelaySec),
154                             Some(Duration.ofSeconds(3))
155                     )),
156                     Some(PartialSecurityConfig(
157                             missingSecurityKeys
158                     )),
159                     Some(PartialCollectorConfig(
160                             someFromEmptyRouting
161                     )),
162                     Some(LogLevel.INFO)
163             )
164
165             it("should create valid configuration") {
166                 val result = cut.validate(config)
167                 result.fold(
168                         {
169                             fail("Configuration should have been created successfully but was $it")
170                         },
171                         {
172                             assertThat(it.server.idleTimeout)
173                                     .isEqualTo(idleTimeoutSec)
174
175                             assertThat(it.security.keys)
176                                     .isEqualTo(missingSecurityKeys)
177
178                             assertThat(it.cbs.firstRequestDelay)
179                                     .isEqualTo(firstReqDelaySec)
180
181                             assertThat(it.collector.routing)
182                                     .isEqualTo(emptyRouting)
183                         }
184                 )
185             }
186         }
187
188     }
189 })
190
191 val emptyRouting: Routing = emptyList()
192 val someFromEmptyRouting = Some(emptyRouting)