2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018-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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.config.impl
22 import arrow.core.Some
23 import com.google.gson.JsonParser
24 import com.nhaarman.mockitokotlin2.mock
25 import com.nhaarman.mockitokotlin2.times
26 import com.nhaarman.mockitokotlin2.verify
27 import com.nhaarman.mockitokotlin2.whenever
28 import org.assertj.core.api.Assertions.assertThat
29 import org.jetbrains.spek.api.Spek
30 import org.jetbrains.spek.api.dsl.describe
31 import org.jetbrains.spek.api.dsl.given
32 import org.jetbrains.spek.api.dsl.it
33 import org.jetbrains.spek.api.dsl.on
34 import org.onap.dcae.collectors.veshv.config.api.ConfigurationStateListener
35 import org.onap.dcaegen2.services.sdk.model.streams.ImmutableAafCredentials
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient
37 import reactor.core.publisher.Flux
38 import reactor.core.publisher.Mono
39 import reactor.retry.Retry
40 import reactor.test.StepVerifier
41 import java.time.Duration
44 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
47 internal object CbsConfigurationProviderTest : Spek({
49 describe("Configuration provider") {
51 val cbsClientAdapter: CbsClientAdapter = mock()
52 val configStateListener: ConfigurationStateListener = mock()
54 given("configuration is never in cbs") {
55 val cbsClientMock: CbsClient = mock()
56 val configProvider = constructConfigurationProvider(
57 constructCbsClientAdapter(cbsClientMock, configStateListener),
61 on("waiting for configuration") {
62 val waitTime = Duration.ofMillis(100)
64 it("should not get it") {
65 StepVerifier.create(configProvider().take(1))
66 .expectNoEvent(waitTime)
71 given("valid configuration from cbs") {
72 val configProvider = constructConfigurationProvider(cbsClientAdapter, configStateListener)
74 on("new configuration") {
75 whenever(cbsClientAdapter.configurationUpdates())
76 .thenReturn(Flux.just(validConfiguration))
77 it("should use received configuration") {
79 StepVerifier.create(configProvider().take(1))
81 assertThat(it.requestIntervalSec).isEqualTo(Some(5L))
82 assertThat(it.listenPort).isEqualTo(Some(6061))
83 assertThat(it.idleTimeoutSec).isEqualTo(Some(60L))
85 val sinks = it.streamPublishers.orNull()!!
89 assertThat(sink1.name()).isEqualTo(PERF3GPP_REGIONAL)
90 assertThat(sink1.aafCredentials()).isEqualTo(aafCredentials1)
91 assertThat(sink1.bootstrapServers())
92 .isEqualTo("dmaap-mr-kafka-0.regional:6060,dmaap-mr-kafka-1.regional:6060")
93 assertThat(sink1.topicName()).isEqualTo("REG_HVVES_PERF3GPP")
95 assertThat(sink2.name()).isEqualTo(PERF3GPP_CENTRAL)
96 assertThat(sink2.aafCredentials()).isEqualTo(aafCredentials2)
97 assertThat(sink2.bootstrapServers())
98 .isEqualTo("dmaap-mr-kafka-0.central:6060,dmaap-mr-kafka-1.central:6060")
99 assertThat(sink2.topicName()).isEqualTo("CEN_HVVES_PERF3GPP")
105 given("invalid configuration from cbs") {
106 val iterationCount = 3L
107 val configProvider = constructConfigurationProvider(
108 cbsClientAdapter, configStateListener, iterationCount
111 on("new configuration") {
112 whenever(cbsClientAdapter.configurationUpdates())
113 .thenReturn(Flux.just(invalidConfiguration))
115 it("should interrupt the flux") {
117 .create(configProvider())
121 it("should call state listener when retrying") {
122 verify(configStateListener, times(iterationCount.toInt())).retrying()
131 private const val PERF3GPP_REGIONAL = "perf3gpp_regional"
132 private const val PERF3GPP_CENTRAL = "perf3gpp_central"
134 private val aafCredentials1 = ImmutableAafCredentials.builder()
136 .password("very secure password")
139 private val aafCredentials2 = ImmutableAafCredentials.builder()
140 .username("other_client")
141 .password("another very secure password")
144 private val validConfiguration = JsonParser().parse("""
146 "server.listenPort": 6061,
147 "server.idleTimeoutSec": 60,
148 "cbs.requestIntervalSec": 5,
149 "streams_publishes": {
150 "$PERF3GPP_REGIONAL": {
153 "username": "client",
154 "password": "very secure password"
157 "bootstrap_servers": "dmaap-mr-kafka-0.regional:6060,dmaap-mr-kafka-1.regional:6060",
158 "topic_name": "REG_HVVES_PERF3GPP"
161 "$PERF3GPP_CENTRAL": {
164 "username": "other_client",
165 "password": "another very secure password"
168 "bootstrap_servers": "dmaap-mr-kafka-0.central:6060,dmaap-mr-kafka-1.central:6060",
169 "topic_name": "CEN_HVVES_PERF3GPP"
175 private val invalidConfiguration = JsonParser().parse("""
177 "streams_publishes": {
178 "$PERF3GPP_REGIONAL": {
182 "password": "very secure password"
185 "servers": "dmaap-mr-kafka-0.regional:6060,dmaap-mr-kafka-1.regional:6060",
186 "name": "REG_HVVES_PERF3GPP"
192 private val firstRequestDelay = Duration.ofMillis(1)
193 private val configParser = JsonConfigurationParser()
195 private fun constructCbsClientAdapter(cbsClientMock: CbsClient, configStateListener: ConfigurationStateListener) =
196 CbsClientAdapter(Mono.just(cbsClientMock), firstRequestDelay, configStateListener, mdc, retry())
198 private fun constructConfigurationProvider(cbsClientAdapter: CbsClientAdapter,
199 configurationStateListener: ConfigurationStateListener,
200 iterationCount: Long = 1) = CbsConfigurationProvider(
203 configurationStateListener,
205 retry(iterationCount)