ab99f13c682cdc08708f1b917b8b625da0873efc
[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.config.api.model.Routing
28 import org.onap.dcae.collectors.veshv.tests.utils.resourceAsStream
29 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
30 import java.io.StringReader
31 import java.net.InetSocketAddress
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 ip address") {
63                 val input = """{  "collector" : {
64                     "kafkaServers": [
65                       "192.168.255.1:5005",
66                       "192.168.255.26:5006"
67                     ]
68                   }
69                 }"""
70
71                 val config = cut.loadConfig(StringReader(input))
72                 assertThat(config.collector.nonEmpty()).isTrue()
73                 val collector = config.collector.orNull() as PartialCollectorConfig
74                 assertThat(collector.kafkaServers.nonEmpty()).isTrue()
75                 val addresses = collector.kafkaServers.orNull() as List<InetSocketAddress>
76                 assertThat(addresses)
77                         .isEqualTo(listOf(
78                                 InetSocketAddress("192.168.255.1", 5005),
79                                 InetSocketAddress("192.168.255.26", 5006)
80                         ))
81             }
82
83             it("parses routing array with RoutingAdapter") {
84                 val input = """{
85                     "collector" : {
86                         "routing" : [
87                             {
88                               "fromDomain": "perf3gpp",
89                               "toTopic": "HV_VES_PERF3GPP"
90                             }
91                         ]
92                     }
93                 }""".trimIndent()
94                 val config = cut.loadConfig(StringReader(input))
95                 assertThat(config.collector.nonEmpty()).isTrue()
96                 val collector = config.collector.orNull() as PartialCollectorConfig
97                 assertThat(collector.routing.nonEmpty()).isTrue()
98                 val routing = collector.routing.orNull() as Routing
99                 routing.run {
100                     assertThat(routes.size).isEqualTo(1)
101                     assertThat(routes[0].domain).isEqualTo("perf3gpp")
102                     assertThat(routes[0].targetTopic).isEqualTo("HV_VES_PERF3GPP")
103                 }
104             }
105
106             it("parses invalid log level string to empty option") {
107                 val input = """{
108                     "logLevel": something
109                 }""".trimMargin()
110                 val config = cut.loadConfig(input.reader())
111
112                 assertThat(config.logLevel.isEmpty())
113             }
114         }
115
116         describe("complete file loading") {
117             it("loads actual file") {
118                 val config = cut.loadConfig(
119                         javaClass.resourceAsStream("/sampleConfig.json"))
120
121                 assertThat(config).isNotNull
122                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
123
124                 assertThat(config.security.nonEmpty()).isTrue()
125                 val security = config.security.orNull() as PartialSecurityConfig
126                 assertThat(security.keys.nonEmpty()).isTrue()
127
128                 assertThat(config.cbs.nonEmpty()).isTrue()
129                 val cbs = config.cbs.orNull() as PartialCbsConfig
130                 assertThat(cbs.firstRequestDelaySec).isEqualTo(Some(7))
131                 assertThat(cbs.requestIntervalSec).isEqualTo(Some(900))
132
133                 assertThat(config.collector.nonEmpty()).isTrue()
134                 val collector = config.collector.orNull() as PartialCollectorConfig
135                 collector.run {
136                     assertThat(dummyMode).isEqualTo(Some(false))
137                     assertThat(maxRequestSizeBytes).isEqualTo(Some(512000))
138                     assertThat(kafkaServers.nonEmpty()).isTrue()
139                     assertThat(routing.nonEmpty()).isTrue()
140                 }
141
142                 assertThat(config.server.nonEmpty()).isTrue()
143                 val server = config.server.orNull() as PartialServerConfig
144                 server.run {
145                     assertThat(idleTimeoutSec).isEqualTo(Some(1200))
146                     assertThat(listenPort).isEqualTo(Some(6000))
147                     assertThat(maxPayloadSizeBytes).isEqualTo(Some(512000))
148                 }
149             }
150         }
151     }
152 })
153