919f22c10b6273633c6a7ef924ed11d7b759e824
[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 arrow.core.getOrElse
24 import org.assertj.core.api.Assertions.assertThat
25 import org.jetbrains.spek.api.Spek
26 import org.jetbrains.spek.api.dsl.describe
27 import org.jetbrains.spek.api.dsl.it
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.time.Duration
32 import kotlin.test.fail
33
34 /**
35  * @author Pawel Biniek <pawel.biniek@nokia.com>
36  * @since February 2019
37  */
38 internal object JsonConfigurationParserTest : Spek({
39     describe("A configuration parser utility") {
40         val cut = JsonConfigurationParser()
41
42         describe("partial configuration parsing") {
43             it("parses enumerations") {
44                 val input = """{"logLevel":"ERROR"}"""
45
46                 val config = cut.parse(StringReader(input))
47                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
48             }
49
50             it("parses simple structure and creates correct objects") {
51                 val input = """{
52                     "server.listenPort" : 12003,
53                     "cbs.firstRequestDelaySec": 10
54                 }
55                 """.trimIndent()
56                 val config = cut.parse(StringReader(input))
57                 assertThat(config.listenPort).isEqualTo(Some(12003))
58                 assertThat(config.firstRequestDelaySec).isEqualTo(Some(10L))
59             }
60
61             it("parses disabled security configuration") {
62                 val input = """{
63                     "security.sslDisable": true
64                 }""".trimIndent()
65                 val config = cut.parse(StringReader(input))
66
67                 assertThat(config.sslDisable.getOrElse { fail("Should be Some") }).isTrue()
68             }
69
70             it("parses invalid log level string to empty option") {
71                 val input = """{
72                     "logLevel": something
73                 }""".trimMargin()
74                 val config = cut.parse(input.reader())
75
76                 assertThat(config.logLevel.isEmpty())
77             }
78         }
79
80         describe("complete json parsing") {
81             it("parses actual json") {
82                 val config = cut.parse(
83                         javaClass.resourceAsStream("/sampleConfig.json"))
84
85                 assertThat(config).isNotNull
86                 assertThat(config.logLevel).isEqualTo(Some(LogLevel.ERROR))
87
88                 assertThat(config.listenPort).isEqualTo(Some(6000))
89                 assertThat(config.idleTimeoutSec).isEqualTo(Some(1200L))
90
91                 assertThat(config.firstRequestDelaySec).isEqualTo(Some(7L))
92                 assertThat(config.requestIntervalSec).isEqualTo(Some(900L))
93
94                 assertThat(config.sslDisable).isEqualTo(Some(false))
95                 assertThat(config.keyStoreFile).isEqualTo(Some("test.ks.pkcs12"))
96                 assertThat(config.keyStorePassword).isEqualTo(Some("changeMe"))
97                 assertThat(config.trustStoreFile).isEqualTo(Some("trust.ks.pkcs12"))
98                 assertThat(config.trustStorePassword).isEqualTo(Some("changeMeToo"))
99             }
100         }
101     }
102 })