d5b18e68d42475fd2742eb386ec3c76bc69c19e3
[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.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.utils.logging.LogLevel
28 import java.io.InputStreamReader
29 import java.io.Reader
30 import java.time.Duration
31
32 /**
33  * @author Pawel Biniek <pawel.biniek@nokia.com>
34  * @since February 2019
35  */
36 internal object ConfigurationMergerTest : Spek({
37     describe("Merges partial configurations into one") {
38         it("merges single parameter into empty config") {
39             val actual = PartialConfiguration()
40             val diff = PartialConfiguration(logLevel = Some(LogLevel.INFO))
41
42             val result = ConfigurationMerger().merge(actual, diff)
43
44             assertThat(result.logLevel).isEqualTo(Some(LogLevel.INFO))
45         }
46
47         it("merges single embedded parameter into empty config") {
48             val actual = PartialConfiguration()
49             val serverConfig = PartialServerConfig(listenPort = Some(45))
50             val diff = PartialConfiguration(server = Some(serverConfig))
51
52             val result = ConfigurationMerger().merge(actual, diff)
53
54             assertThat(result.server).isEqualTo(Some(serverConfig))
55         }
56
57         it("merges single parameter into full config") {
58             val actual = FileConfigurationReader().loadConfig(
59                     InputStreamReader(
60                             FileConfigurationReaderTest.javaClass.getResourceAsStream("/sampleConfig.json")) as Reader)
61             val diff = PartialConfiguration(logLevel = Some(LogLevel.INFO))
62
63             val result = ConfigurationMerger().merge(actual, diff)
64
65             assertThat(result.logLevel).isEqualTo(Some(LogLevel.INFO))
66         }
67
68         it("merges single embedded parameter into full config") {
69             val actual = FileConfigurationReader().loadConfig(
70                     InputStreamReader(
71                             FileConfigurationReaderTest.javaClass.getResourceAsStream("/sampleConfig.json")) as Reader)
72             val serverConfig = PartialServerConfig(listenPort = Some(45))
73             val diff = PartialConfiguration(server = Some(serverConfig))
74
75             val result = ConfigurationMerger().merge(actual, diff)
76
77             assertThat(result.server.orNull()?.listenPort).isEqualTo(serverConfig.listenPort)
78             assertThat(result.server.orNull()?.idleTimeoutSec?.isEmpty()).isFalse()
79             assertThat(result.server.orNull()?.idleTimeoutSec).isEqualTo(Some(Duration.ofSeconds(1200)))
80             assertThat(result.server.orNull()?.maxPayloadSizeBytes?.isEmpty()).isFalse()
81             assertThat(result.server.orNull()?.maxPayloadSizeBytes).isEqualTo(Some(512000))
82         }
83
84         it("merges full config into single parameter") {
85             val actual = PartialConfiguration(logLevel = Some(LogLevel.INFO))
86             val diff = FileConfigurationReader().loadConfig(
87                     InputStreamReader(
88                             FileConfigurationReaderTest.javaClass.getResourceAsStream("/sampleConfig.json")) as Reader)
89
90             val result = ConfigurationMerger().merge(actual, diff)
91
92             assertThat(result.logLevel).isEqualTo(Some(LogLevel.ERROR))
93             assertThat(result.server.isEmpty()).isFalse()
94             assertThat(result.server.orNull()?.maxPayloadSizeBytes).isEqualTo(Some(512000))
95             assertThat(result.server.orNull()?.idleTimeoutSec).isEqualTo(Some(Duration.ofSeconds(1200)))
96
97             assertThat(result.security.isEmpty()).isFalse()
98             assertThat(result.cbs.isEmpty()).isFalse()
99         }
100     }
101 })
102