2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018-2020 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 com.google.gson.JsonParser
23 import com.nhaarman.mockitokotlin2.any
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.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.config.api.ConfigurationStateListener
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient
35 import reactor.core.publisher.Mono
36 import reactor.test.StepVerifier
37 import java.time.Duration
39 internal object CbsClientAdapterTest : Spek({
41 describe("Config Binding Service Client Adapter") {
43 val cbsClientMock: CbsClient = mock()
44 val configStateListener: ConfigurationStateListener = mock()
46 given("successful client creation") {
47 val cbsClientMono = Mono.just(cbsClientMock)
48 val cut = CbsClientAdapter(cbsClientMono, firstRequestDelay, configStateListener, mdc, retry())
50 on("configurations stream in CBS") {
51 val firstConfigurationContent = "first"
52 val secondConfigurationContent = "second"
53 whenever(cbsClientMock.get(any())).thenReturn(
54 configurationMono(firstConfigurationContent),
55 configurationMono(secondConfigurationContent)
58 it("should return flux of fetched configurations") {
61 cut.configurationUpdates().take(2)
64 .expectNoEvent(firstRequestDelay)
65 .expectNext(configuration(firstConfigurationContent))
66 .expectNext(configuration(secondConfigurationContent))
72 on("exception from CBS client on configuration fetch") {
74 whenever(cbsClientMock.get(any())).thenReturn(
75 Mono.error { sampleException }
78 it("should return error flux") {
79 StepVerifier.create(cut.configurationUpdates())
80 .expectErrorMatches { it === sampleException }
86 given("repeated failure during client creation") {
87 val failedCreationsAmount = 3
88 var currentFailuresCount = 0
89 val cbsClientMono = Mono.fromCallable {
90 currentFailuresCount++
91 if (currentFailuresCount <= failedCreationsAmount) {
98 val cut = CbsClientAdapter(cbsClientMono, firstRequestDelay, configStateListener, mdc,
99 retry(failedCreationsAmount + 1L))
101 on("CBS client creation") {
102 whenever(cbsClientMock.get(any())).thenReturn(configurationMono())
104 it("it should emit configuration after failures") {
106 .withVirtualTime { cut.configurationUpdates().take(1) }
107 .expectSubscription()
108 .expectNoEvent(firstRequestDelay)
109 .thenAwait(firstRequestDelay)
110 .expectNext(configuration())
114 it("should call state listener when retrying") {
115 verify(configStateListener, times(failedCreationsAmount)).retrying()
122 private val firstRequestDelay = Duration.ofSeconds(10)
123 private val sampleException = Exception("Best regards from CBS")
125 private fun configuration(content: String = "whatever") =
126 JsonParser().parse("""{ "content": ${content} }""").asJsonObject
128 private fun configurationMono(content: String = "whatever") = Mono.just(configuration(content))