55da7d0235381035dd09d7ffd9b2e0f77cac2e6d
[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.api
21
22 import arrow.core.Some
23 import org.jetbrains.spek.api.Spek
24 import org.assertj.core.api.Assertions.assertThat
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.config.impl.FileConfigurationReader
29 import org.onap.dcae.collectors.veshv.config.impl.PartialCbsConfig
30 import org.onap.dcae.collectors.veshv.config.impl.PartialKafkaConfig
31 import org.onap.dcae.collectors.veshv.config.impl.PartialSecurityConfig
32 import org.onap.dcae.collectors.veshv.config.impl.PartialServerConfig
33 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
34 import java.io.InputStreamReader
35 import java.io.StringReader
36 import java.net.InetSocketAddress
37
38 /**
39  * @author Pawel Biniek <pawel.biniek@nokia.com>
40  * @since February 2019
41  */
42 internal object FileConfigurationReaderTest : Spek({
43     describe("A configuration loader utility") {
44
45         describe("partial configuration loading") {
46             it("parses enumerations") {
47                 val input = """{"logLevel":"ERROR"}"""
48
49                 val config = FileConfigurationReader().loadConfig(StringReader(input))
50                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
51             }
52
53             it("parses simple structure") {
54                 val input = """{
55                 "server" : {
56                     "healthCheckApiPort" : 12002,
57                     "listenPort" : 12003
58                 }
59             }
60             """.trimIndent()
61                 val config = FileConfigurationReader().loadConfig(StringReader(input))
62                 assertThat(config.server.nonEmpty()).isTrue()
63                 assertThat(config.server.orNull()?.healthCheckApiPort).isEqualTo(Some(12002))
64                 assertThat(config.server.orNull()?.listenPort).isEqualTo(Some(12003))
65             }
66
67             it("parses ip address") {
68                 val input = """{  "kafka" : {
69                     "kafkaServers": [
70                       "192.168.255.1:5005",
71                       "192.168.255.26:5006"
72                     ]
73                   }
74                 }"""
75
76                 val config = FileConfigurationReader().loadConfig(StringReader(input))
77                 assertThat(config.kafka.nonEmpty()).isTrue()
78                 val kafka = config.kafka.orNull() as PartialKafkaConfig
79                 assertThat(kafka.kafkaServers.nonEmpty()).isTrue()
80                 val addresses = kafka.kafkaServers.orNull() as Array<InetSocketAddress>
81                 assertThat(addresses)
82                         .isEqualTo(arrayOf(
83                                 InetSocketAddress("192.168.255.1", 5005),
84                                 InetSocketAddress("192.168.255.26", 5006)
85                         ))
86             }
87
88             it("parses routing array with RoutingAdapter") {
89                 val input = """{
90                     "kafka" : {
91                         "routing" : [
92                             {
93                               "fromDomain": "perf3gpp",
94                               "toTopic": "HV_VES_PERF3GPP"
95                             }
96                         ]
97                     }
98                 }""".trimIndent()
99                 val config = FileConfigurationReader().loadConfig(StringReader(input))
100                 assertThat(config.kafka.nonEmpty()).isTrue()
101                 val kafka = config.kafka.orNull() as PartialKafkaConfig
102                 assertThat(kafka.routing.nonEmpty()).isTrue()
103                 val routing = kafka.routing.orNull() as Routing
104                 routing.run {
105                     assertThat(routes.size).isEqualTo(1)
106                     assertThat(routes[0].domain).isEqualTo("perf3gpp")
107                     assertThat(routes[0].targetTopic).isEqualTo("HV_VES_PERF3GPP")
108                 }
109             }
110         }
111
112         describe("complete file loading") {
113             it("loads actual file") {
114                 val config = FileConfigurationReader().loadConfig(
115                         InputStreamReader(
116                                 FileConfigurationReaderTest.javaClass.getResourceAsStream("/sampleConfig.json")))
117                 assertThat(config).isNotNull
118                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
119
120                 assertThat(config.security.nonEmpty()).isTrue()
121                 val security = config.security.orNull() as PartialSecurityConfig
122                 assertThat(security.sslDisable.orNull()).isFalse()
123                 assertThat(security.keys.nonEmpty()).isTrue()
124
125                 assertThat(config.cbs.nonEmpty()).isTrue()
126                 val cbs = config.cbs.orNull() as PartialCbsConfig
127                 assertThat(cbs.firstRequestDelaySec).isEqualTo(Some(7))
128                 assertThat(cbs.requestIntervalSec).isEqualTo(Some(900))
129
130                 assertThat(config.kafka.nonEmpty()).isTrue()
131                 val kafka = config.kafka.orNull() as PartialKafkaConfig
132                 assertThat(kafka.kafkaServers.nonEmpty()).isTrue()
133                 assertThat(kafka.routing.nonEmpty()).isTrue()
134
135                 assertThat(config.server.nonEmpty()).isTrue()
136                 val server = config.server.orNull() as PartialServerConfig
137                 server.run {
138                     assertThat(dummyMode).isEqualTo(Some(false))
139                     assertThat(healthCheckApiPort).isEqualTo(Some(5000))
140                     assertThat(idleTimeoutSec).isEqualTo(Some(1200))
141                     assertThat(listenPort).isEqualTo(Some(6000))
142                     assertThat(maximumPayloadSizeBytes).isEqualTo(Some(512000))
143                 }
144             }
145         }
146     }
147 })