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.impl.adapters
22 import com.google.gson.JsonParser
23 import com.nhaarman.mockitokotlin2.any
24 import com.nhaarman.mockitokotlin2.eq
25 import com.nhaarman.mockitokotlin2.mock
26 import com.nhaarman.mockitokotlin2.whenever
27 import org.assertj.core.api.Assertions.assertThat
28 import org.jetbrains.spek.api.Spek
29 import org.jetbrains.spek.api.dsl.describe
30 import org.jetbrains.spek.api.dsl.given
31 import org.jetbrains.spek.api.dsl.it
32 import org.jetbrains.spek.api.dsl.on
33 import org.onap.dcae.collectors.veshv.healthcheck.api.HealthDescription
34 import org.onap.dcae.collectors.veshv.healthcheck.api.HealthState
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 org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParsers
38 import reactor.core.publisher.Flux
39 import reactor.core.publisher.Mono
40 import reactor.retry.Retry
41 import reactor.test.StepVerifier
42 import java.time.Duration
45 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
48 internal object ConfigurationProviderImplTest : Spek({
50 describe("Configuration provider") {
52 val cbsClient: CbsClient = mock()
53 val cbsClientMock: Mono<CbsClient> = Mono.just(cbsClient)
54 val healthStateProvider = HealthState.INSTANCE
56 given("configuration is never in cbs") {
57 val configProvider = constructConfigurationProvider(cbsClientMock, healthStateProvider)
59 on("waiting for configuration") {
60 val waitTime = Duration.ofMillis(100)
62 it("should not get it") {
63 StepVerifier.create(configProvider().take(1))
64 .expectNoEvent(waitTime)
69 given("valid configuration from cbs") {
70 val configProvider = constructConfigurationProvider(cbsClientMock, healthStateProvider)
72 on("new configuration") {
73 whenever(cbsClient.updates(any(), eq(firstRequestDelay), eq(requestInterval)))
74 .thenReturn(Flux.just(validConfiguration))
75 it("should use received configuration") {
77 StepVerifier.create(configProvider().take(1))
79 val receivedSink1 = it.elementAt(0)
80 val receivedSink2 = it.elementAt(1)
82 assertThat(receivedSink1.aafCredentials()).isEqualTo(aafCredentials1)
83 assertThat(receivedSink1.bootstrapServers())
84 .isEqualTo("dmaap-mr-kafka-0.regional:6060,dmaap-mr-kafka-1.regional:6060")
85 assertThat(receivedSink1.topicName()).isEqualTo("REG_HVVES_PERF3GPP")
87 assertThat(receivedSink2.aafCredentials()).isEqualTo(aafCredentials2)
88 assertThat(receivedSink2.bootstrapServers())
89 .isEqualTo("dmaap-mr-kafka-0.central:6060,dmaap-mr-kafka-1.central:6060")
90 assertThat(receivedSink2.topicName()).isEqualTo("CEN_HVVES_PERF3GPP")
96 given("invalid configuration from cbs") {
97 val iterationCount = 3L
98 val configProvider = constructConfigurationProvider(
99 cbsClientMock, healthStateProvider, iterationCount
102 on("new configuration") {
103 whenever(cbsClient.updates(any(), eq(firstRequestDelay), eq(requestInterval)))
104 .thenReturn(Flux.just(invalidConfiguration))
106 it("should interrupt the flux") {
107 StepVerifier.create(configProvider())
111 it("should update the health state") {
112 StepVerifier.create(healthStateProvider().take(iterationCount))
113 .expectNextCount(iterationCount - 1)
114 .expectNext(HealthDescription.RETRYING_FOR_DYNAMIC_CONFIGURATION)
123 private val aafCredentials1 = ImmutableAafCredentials.builder()
125 .password("very secure password")
128 private val aafCredentials2 = ImmutableAafCredentials.builder()
129 .username("other_client")
130 .password("another very secure password")
133 private val validConfiguration = JsonParser().parse("""
135 "streams_publishes": {
136 "perf3gpp_regional": {
139 "username": "client",
140 "password": "very secure password"
143 "bootstrap_servers": "dmaap-mr-kafka-0.regional:6060,dmaap-mr-kafka-1.regional:6060",
144 "topic_name": "REG_HVVES_PERF3GPP"
147 "perf3gpp_central": {
150 "username": "other_client",
151 "password": "another very secure password"
154 "bootstrap_servers": "dmaap-mr-kafka-0.central:6060,dmaap-mr-kafka-1.central:6060",
155 "topic_name": "CEN_HVVES_PERF3GPP"
161 private val invalidConfiguration = JsonParser().parse("""
163 "streams_publishes": {
164 "perf3gpp_regional": {
167 "username": "client",
168 "password": "very secure password"
171 "bootstrap_servers": "dmaap-mr-kafka-0.regional:6060,dmaap-mr-kafka-1.regional:6060",
172 "popic_name": "REG_HVVES_PERF3GPP"
178 private val firstRequestDelay = Duration.ofMillis(1)
179 private val requestInterval = Duration.ofMillis(1)
180 private val streamParser = StreamFromGsonParsers.kafkaSinkParser()
182 private fun constructConfigurationProvider(cbsClientMono: Mono<CbsClient>,
183 healthState: HealthState,
184 iterationCount: Long = 1
185 ): ConfigurationProviderImpl {
187 val retry = Retry.onlyIf<Any> { it.iteration() <= iterationCount }.fixedBackoff(Duration.ofNanos(1))
189 return ConfigurationProviderImpl(