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