4e35bfb3af6f3cc2f2d4d3186e95e5d939159129
[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.Some
23 import org.assertj.core.api.Assertions.assertThat
24 import org.jetbrains.spek.api.Spek
25 import org.jetbrains.spek.api.dsl.describe
26 import org.jetbrains.spek.api.dsl.it
27 import org.onap.dcae.collectors.veshv.tests.utils.resourceAsStream
28 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
29 import java.io.StringReader
30 import java.net.InetSocketAddress
31 import java.time.Duration
32
33 /**
34  * @author Pawel Biniek <pawel.biniek@nokia.com>
35  * @since February 2019
36  */
37 internal object FileConfigurationReaderTest : Spek({
38     describe("A configuration loader utility") {
39         val cut = FileConfigurationReader()
40
41         describe("partial configuration loading") {
42             it("parses enumerations") {
43                 val input = """{"logLevel":"ERROR"}"""
44
45                 val config = cut.loadConfig(StringReader(input))
46                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
47             }
48
49             it("parses simple structure") {
50                 val input = """{
51                 "server" : {
52                     "healthCheckApiPort" : 12002,
53                     "listenPort" : 12003
54                 }
55             }
56             """.trimIndent()
57                 val config = cut.loadConfig(StringReader(input))
58                 assertThat(config.server.nonEmpty()).isTrue()
59                 assertThat(config.server.orNull()?.listenPort).isEqualTo(Some(12003))
60             }
61
62             it("parses disabled security configuration") {
63                 val input = """{
64                     "security": {
65                     }
66                 }""".trimIndent()
67                 val config = cut.loadConfig(StringReader(input))
68
69                 assertThat(config.security.nonEmpty()).isTrue()
70                 val security = config.security.orNull() as PartialSecurityConfig
71                 assertThat(security.keys.nonEmpty()).isFalse()
72             }
73
74             it("parses invalid log level string to empty option") {
75                 val input = """{
76                     "logLevel": something
77                 }""".trimMargin()
78                 val config = cut.loadConfig(input.reader())
79
80                 assertThat(config.logLevel.isEmpty())
81             }
82         }
83
84         describe("complete file loading") {
85             it("loads actual file") {
86                 val config = cut.loadConfig(
87                         javaClass.resourceAsStream("/sampleConfig.json"))
88
89                 assertThat(config).isNotNull
90                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
91
92                 assertThat(config.security.nonEmpty()).isTrue()
93                 val security = config.security.orNull() as PartialSecurityConfig
94                 assertThat(security.keys.nonEmpty()).isTrue()
95
96                 assertThat(config.cbs.nonEmpty()).isTrue()
97                 val cbs = config.cbs.orNull() as PartialCbsConfig
98                 assertThat(cbs.firstRequestDelaySec).isEqualTo(Some(Duration.ofSeconds(7)))
99                 assertThat(cbs.requestIntervalSec).isEqualTo(Some(Duration.ofSeconds(900)))
100
101                 assertThat(config.server.nonEmpty()).isTrue()
102                 val server = config.server.orNull() as PartialServerConfig
103                 server.run {
104                     assertThat(idleTimeoutSec).isEqualTo(Some(Duration.ofSeconds(1200)))
105                     assertThat(listenPort).isEqualTo(Some(6000))
106                     assertThat(maxPayloadSizeBytes).isEqualTo(Some(512000))
107                 }
108             }
109         }
110     }
111 })
112