beb5df615e2ad0f487eee69fb63c77599f964bbe
[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 com.nhaarman.mockitokotlin2.mock
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.time.Duration
36
37 internal object ConfigurationValidatorTest : Spek({
38     describe("ConfigurationValidator") {
39         val cut = ConfigurationValidator()
40
41         describe("validating partial configuration with missing fields") {
42             val config = PartialConfiguration(
43                     Some(PartialServerConfig(listenPort = Some(1)))
44             )
45
46             it("should return ValidationError") {
47                 val result = cut.validate(config)
48                 assertThat(result.isLeft()).isTrue()
49             }
50         }
51
52         describe("validating configuration with empty log level") {
53             val config = PartialConfiguration(
54                     Some(PartialServerConfig(
55                             Some(1),
56                             Some(2),
57                             Some(3)
58                     )),
59                     Some(PartialCbsConfig(
60                             Some(5),
61                             Some(3)
62                     )),
63                     Some(PartialSecurityConfig(
64                             Some(mock())
65                     )),
66                     Some(PartialCollectorConfig(
67                             Some(true),
68                             Some(4),
69                             Some(emptyList()),
70                             Some(routing { }.build())
71                     )),
72                     None
73             )
74
75             it("should use default log level") {
76                 val result = cut.validate(config)
77                 result.fold(
78                         {
79                             fail("Configuration should have been created successfully")
80                         },
81                         {
82                             assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
83                         }
84                 )
85             }
86         }
87
88         describe("validating complete configuration") {
89             val idleTimeoutSec = 10
90             val firstReqDelaySec = 10
91             val securityKeys = Some(mock<SecurityKeys>())
92             val routing = routing { }.build()
93
94             val config = PartialConfiguration(
95                     Some(PartialServerConfig(
96                             Some(1),
97                             Some(idleTimeoutSec),
98                             Some(2)
99                     )),
100                     Some(PartialCbsConfig(
101                             Some(firstReqDelaySec),
102                             Some(3)
103                     )),
104                     Some(PartialSecurityConfig(
105                             securityKeys
106                     )),
107                     Some(PartialCollectorConfig(
108                             Some(true),
109                             Some(4),
110                             Some(emptyList()),
111                             Some(routing)
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(Duration.ofSeconds(idleTimeoutSec.toLong()))
125
126                             assertThat(it.security.keys)
127                                     .isEqualTo(securityKeys)
128
129                             assertThat(it.cbs.firstRequestDelay)
130                                     .isEqualTo(Duration.ofSeconds(firstReqDelaySec.toLong()))
131
132                             assertThat(it.collector.routing)
133                                     .isEqualTo(routing)
134                         }
135                 )
136             }
137         }
138
139         describe("validating configuration with security disabled") {
140             val idleTimeoutSec = 10
141             val firstReqDelaySec = 10
142             val securityKeys: Option<SecurityKeys> = None
143             val routing = routing { }.build()
144
145             val config = PartialConfiguration(
146                     Some(PartialServerConfig(
147                             Some(1),
148                             Some(idleTimeoutSec),
149                             Some(2)
150                     )),
151                     Some(PartialCbsConfig(
152                             Some(firstReqDelaySec),
153                             Some(3)
154                     )),
155                     Some(PartialSecurityConfig(
156                             securityKeys
157                     )),
158                     Some(PartialCollectorConfig(
159                             Some(true),
160                             Some(4),
161                             Some(emptyList()),
162                             Some(routing)
163                     )),
164                     Some(LogLevel.INFO)
165             )
166
167             it("should create valid configuration") {
168                 val result = cut.validate(config)
169                 result.fold(
170                         {
171                             fail("Configuration should have been created successfully but was $it")
172                         },
173                         {
174                             assertThat(it.server.idleTimeout)
175                                     .isEqualTo(Duration.ofSeconds(idleTimeoutSec.toLong()))
176
177                             assertThat(it.security.keys)
178                                     .isEqualTo(securityKeys)
179
180                             assertThat(it.cbs.firstRequestDelay)
181                                     .isEqualTo(Duration.ofSeconds(firstReqDelaySec.toLong()))
182
183                             assertThat(it.collector.routing)
184                                     .isEqualTo(routing)
185                         }
186                 )
187             }
188         }
189
190     }
191 })