55d06cdd48f85d48b6aabd954c2ad2bc6e4e4c81
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-configuration / src / test / kotlin / org / onap / dcae / collectors / veshv / config / impl / ConfigurationValidatorTest.kt
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(Duration.ofSeconds(2)),
57                             Some(3)
58                     )),
59                     Some(PartialCbsConfig(
60                             Some(Duration.ofSeconds(5)),
61                             Some(Duration.ofSeconds(3))
62                     )),
63                     Some(PartialSecurityConfig(
64                             Some(mock())
65                     )),
66                     Some(PartialCollectorConfig(
67                             someFromEmptyRouting
68                     )),
69                     None
70             )
71
72             it("should use default log level") {
73                 val result = cut.validate(config)
74                 result.fold(
75                         {
76                             fail("Configuration should have been created successfully")
77                         },
78                         {
79                             assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
80                         }
81                 )
82             }
83         }
84
85         describe("validating complete configuration") {
86             val idleTimeoutSec = Duration.ofSeconds(10L)
87             val firstReqDelaySec = Duration.ofSeconds(10L)
88             val securityKeys = Some(mock<SecurityKeys>())
89
90             val config = PartialConfiguration(
91                     Some(PartialServerConfig(
92                             Some(1),
93                             Some(idleTimeoutSec),
94                             Some(2)
95                     )),
96                     Some(PartialCbsConfig(
97                             Some(firstReqDelaySec),
98                             Some(Duration.ofSeconds(3))
99                     )),
100                     Some(PartialSecurityConfig(
101                             securityKeys
102                     )),
103                     Some(PartialCollectorConfig(
104                             someFromEmptyRouting
105                     )),
106                     Some(LogLevel.INFO)
107             )
108
109             it("should create valid configuration") {
110                 val result = cut.validate(config)
111                 result.fold(
112                         {
113                             fail("Configuration should have been created successfully")
114                         },
115                         {
116                             assertThat(it.server.idleTimeout)
117                                     .isEqualTo(idleTimeoutSec)
118
119                             assertThat(it.security.keys)
120                                     .isEqualTo(securityKeys)
121
122                             assertThat(it.cbs.firstRequestDelay)
123                                     .isEqualTo(firstReqDelaySec)
124
125                             assertThat(it.collector.routing)
126                                     .isEqualTo(emptyRouting)
127                         }
128                 )
129             }
130         }
131
132         describe("validating configuration with security disabled") {
133             val idleTimeoutSec = Duration.ofSeconds(10)
134             val firstReqDelaySec = Duration.ofSeconds(10)
135             val securityKeys: Option<SecurityKeys> = None
136
137             val config = PartialConfiguration(
138                     Some(PartialServerConfig(
139                             Some(1),
140                             Some(idleTimeoutSec),
141                             Some(2)
142                     )),
143                     Some(PartialCbsConfig(
144                             Some(firstReqDelaySec),
145                             Some(Duration.ofSeconds(3))
146                     )),
147                     Some(PartialSecurityConfig(
148                             securityKeys
149                     )),
150                     Some(PartialCollectorConfig(
151                             someFromEmptyRouting
152                     )),
153                     Some(LogLevel.INFO)
154             )
155
156             it("should create valid configuration") {
157                 val result = cut.validate(config)
158                 result.fold(
159                         {
160                             fail("Configuration should have been created successfully but was $it")
161                         },
162                         {
163                             assertThat(it.server.idleTimeout)
164                                     .isEqualTo(idleTimeoutSec)
165
166                             assertThat(it.security.keys)
167                                     .isEqualTo(securityKeys)
168
169                             assertThat(it.cbs.firstRequestDelay)
170                                     .isEqualTo(firstReqDelaySec)
171
172                             assertThat(it.collector.routing)
173                                     .isEqualTo(emptyRouting)
174                         }
175                 )
176             }
177         }
178
179     }
180 })
181
182 val emptyRouting: Routing = emptyList()
183 val someFromEmptyRouting = Some(emptyRouting)