12396d2337ec6fecfb3023eea0e03a7340a2ef60
[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.Some
24 import com.nhaarman.mockitokotlin2.mock
25 import org.assertj.core.api.Assertions.assertThat
26 import org.assertj.core.api.Assertions.fail
27 import org.jetbrains.spek.api.Spek
28 import org.jetbrains.spek.api.dsl.describe
29 import org.jetbrains.spek.api.dsl.it
30 import org.onap.dcae.collectors.veshv.config.api.model.routing
31 import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator.Companion.DEFAULT_LOG_LEVEL
32 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
33 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
34 import java.time.Duration
35
36 internal object ConfigurationValidatorTest : Spek({
37     describe("ConfigurationValidator") {
38         val cut = ConfigurationValidator()
39
40         describe("validating partial configuration with missing fields") {
41             val config = PartialConfiguration(
42                     Some(PartialServerConfig(listenPort = Some(1)))
43             )
44
45             it("should return ValidationError") {
46                 val result = cut.validate(config)
47                 assertThat(result.isLeft()).isTrue()
48             }
49         }
50
51         describe("validating configuration with empty log level") {
52             val config = PartialConfiguration(
53                     Some(PartialServerConfig(
54                             Some(1),
55                             Some(2),
56                             Some(3)
57                     )),
58                     Some(PartialCbsConfig(
59                             Some(5),
60                             Some(3)
61                     )),
62                     Some(PartialSecurityConfig(
63                             Some(mock())
64                     )),
65                     Some(PartialCollectorConfig(
66                             Some(true),
67                             Some(4),
68                             Some(emptyList()),
69                             Some(routing { }.build())
70                     )),
71                     None
72             )
73
74             it("should use default log level") {
75                 val result = cut.validate(config)
76                 result.fold(
77                         {
78                             fail("Configuration should have been created successfully")
79                         },
80                         {
81                             assertThat(it.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
82                         }
83                 )
84             }
85         }
86
87         describe("validating complete configuration") {
88             val idleTimeoutSec = 10
89             val firstReqDelaySec = 10
90             val securityKeys = Some(mock<SecurityKeys>())
91             val routing = routing { }.build()
92
93             val config = PartialConfiguration(
94                     Some(PartialServerConfig(
95                             Some(1),
96                             Some(idleTimeoutSec),
97                             Some(2)
98                     )),
99                     Some(PartialCbsConfig(
100                             Some(firstReqDelaySec),
101                             Some(3)
102                     )),
103                     Some(PartialSecurityConfig(
104                             securityKeys
105                     )),
106                     Some(PartialCollectorConfig(
107                             Some(true),
108                             Some(4),
109                             Some(emptyList()),
110                             Some(routing)
111                     )),
112                     Some(LogLevel.INFO)
113             )
114
115             it("should create valid configuration") {
116                 val result = cut.validate(config)
117                 result.fold(
118                         {
119                             fail("Configuration should have been created successfully")
120                         },
121                         {
122                             assertThat(it.server.idleTimeout)
123                                     .isEqualTo(Duration.ofSeconds(idleTimeoutSec.toLong()))
124
125                             assertThat(it.security.keys)
126                                     .isEqualTo(securityKeys)
127
128                             assertThat(it.cbs.firstRequestDelay)
129                                     .isEqualTo(Duration.ofSeconds(firstReqDelaySec.toLong()))
130
131                             assertThat(it.collector.routing)
132                                     .isEqualTo(routing)
133                         }
134                 )
135             }
136         }
137     }
138 })