1b2dbc2b955be02aaba4fd9477fa9c55dbb1d9bd
[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.Option
23 import com.google.gson.JsonParser
24 import com.nhaarman.mockitokotlin2.any
25 import com.nhaarman.mockitokotlin2.mock
26 import com.nhaarman.mockitokotlin2.whenever
27 import org.jetbrains.spek.api.Spek
28 import org.jetbrains.spek.api.dsl.describe
29 import org.jetbrains.spek.api.dsl.given
30 import org.jetbrains.spek.api.dsl.it
31 import org.jetbrains.spek.api.dsl.on
32 import org.onap.dcae.collectors.veshv.config.api.model.*
33 import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityConfiguration
34 import org.onap.dcae.collectors.veshv.tests.utils.absoluteResourcePath
35 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient
37 import reactor.core.publisher.Mono
38 import reactor.test.StepVerifier
39 import java.time.Duration
40
41
42 internal object ConfigurationModuleIT : Spek({
43     describe("configuration module") {
44         val cbsClientMock = mock<CbsClient>()
45         val configStateListenerMock = mock<ConfigurationStateListener>()
46         val sut = ConfigurationModule(configStateListenerMock, Mono.just(cbsClientMock))
47         val configPath = javaClass.absoluteResourcePath("/insecureSampleConfig.json")
48
49         given("sample configuration in file: $configPath") {
50             val arguments = arrayOf(
51                     "--configuration-file",
52                     configPath,
53                     "--health-check-api-port",
54                     "6062")
55             on("configuration changes in Config Binding Service") {
56                 whenever(cbsClientMock.get(any()))
57                         .thenReturn(
58                                 Mono.just(configurationJsonWithIntervalChanged),
59                                 Mono.just(configurationJsonWithIntervalChangedAgain),
60                                 Mono.just(configurationJsonWithIntervalRestored)
61                         )
62                 it("should wait $firstRequestDelayFromFile s as provided in configuration file and later" +
63                         " fetch configurations in intervals specified within them") {
64                     StepVerifier
65                             .withVirtualTime {
66                                 sut.hvVesConfigurationUpdates(arguments, sampleMdc)
67                                         .take(3)
68                             }
69                             .expectSubscription()
70                             .expectNoEvent(firstRequestDelayFromFile)
71                             .expectNext(configurationWithIntervalChanged)
72                             .expectNoEvent(requestIntervalFromCBS)
73                             .expectNext(configurationWithIntervalChangedAgain)
74                             .expectNoEvent(anotherRequestIntervalFromCBS)
75                             .expectNext(configurationWithIntervalRestored)
76                             .verifyComplete()
77                 }
78             }
79         }
80     }
81 })
82
83 private val firstRequestDelayFromFile = Duration.ofSeconds(3)
84 private val firstRequestDelayFromCBS = Duration.ofSeconds(999)
85 private val requestIntervalFromCBS = Duration.ofSeconds(10)
86 private val anotherRequestIntervalFromCBS = Duration.ofSeconds(20)
87
88 private val sampleMdc = { mapOf("k" to "v") }
89 private val emptyRouting = listOf<Route>()
90
91 private val configurationJsonWithIntervalChanged = JsonParser().parse("""{
92     "cbs.requestIntervalSec": ${requestIntervalFromCBS.seconds}
93 }""").asJsonObject
94
95 private val configurationJsonWithIntervalChangedAgain = JsonParser().parse("""{
96     "cbs.firstRequestDelaySec": ${firstRequestDelayFromCBS.seconds},
97     "cbs.requestIntervalSec": ${anotherRequestIntervalFromCBS.seconds}
98 }""").asJsonObject
99
100 private val configurationJsonWithIntervalRestored = JsonParser().parse("""{
101     "cbs.requestIntervalSec": ${requestIntervalFromCBS.seconds}
102 }""").asJsonObject
103
104 private val configurationWithIntervalChanged =
105         hvVesConfiguration(firstRequestDelayFromFile, requestIntervalFromCBS)
106
107 private val configurationWithIntervalChangedAgain =
108         hvVesConfiguration(firstRequestDelayFromCBS, anotherRequestIntervalFromCBS)
109
110 private val configurationWithIntervalRestored =
111         hvVesConfiguration(firstRequestDelayFromFile, requestIntervalFromCBS)
112
113 private fun hvVesConfiguration(firstRequestDelay: Duration, requestInterval: Duration): HvVesConfiguration {
114     return HvVesConfiguration(
115             ServerConfiguration(6061, Duration.ofSeconds(60)),
116             CbsConfiguration(firstRequestDelay, requestInterval),
117             SecurityConfiguration(Option.empty()),
118             CollectorConfiguration(emptyRouting, 1024 * 1024),
119             LogLevel.DEBUG)
120 }