Upgrade hv-ves, reactor, protobuf and sdk versions
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-configuration / src / test / kotlin / org / onap / dcae / collectors / veshv / config / impl / CbsClientAdapterTest.kt
1 /*
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
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 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
38
39 internal object CbsClientAdapterTest : Spek({
40
41     describe("Config Binding Service Client Adapter") {
42
43         val cbsClientMock: CbsClient = mock()
44         val configStateListener: ConfigurationStateListener = mock()
45
46         given("successful client creation") {
47             val cbsClientMono = Mono.just(cbsClientMock)
48             val cut = CbsClientAdapter(cbsClientMono, firstRequestDelay, configStateListener, mdc, retry())
49
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)
56                 )
57
58                 it("should return flux of fetched configurations") {
59                     StepVerifier
60                             .withVirtualTime {
61                                 cut.configurationUpdates().take(2)
62                             }
63                             .expectSubscription()
64                             .expectNoEvent(firstRequestDelay)
65                             .expectNext(configuration(firstConfigurationContent))
66                             .expectNext(configuration(secondConfigurationContent))
67                             .verifyComplete()
68                 }
69             }
70
71
72             on("exception from CBS client on configuration fetch") {
73
74                 whenever(cbsClientMock.get(any())).thenReturn(
75                         Mono.error { sampleException }
76                 )
77
78                 it("should return error flux") {
79                     StepVerifier.create(cut.configurationUpdates())
80                             .expectErrorMatches { it === sampleException }
81                             .verify()
82                 }
83             }
84         }
85
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) {
92                     throw sampleException
93                 } else {
94                     cbsClientMock
95                 }
96             }
97
98             val cut = CbsClientAdapter(cbsClientMono, firstRequestDelay, configStateListener, mdc,
99                     retry(failedCreationsAmount + 1L))
100
101             on("CBS client creation") {
102                 whenever(cbsClientMock.get(any())).thenReturn(configurationMono())
103
104                 it("it should emit configuration after failures") {
105                     StepVerifier
106                             .withVirtualTime { cut.configurationUpdates().take(1) }
107                             .expectSubscription()
108                             .expectNoEvent(firstRequestDelay)
109                             .thenAwait(firstRequestDelay)
110                             .expectNext(configuration())
111                             .verifyComplete()
112                 }
113
114                 it("should call state listener when retrying") {
115                     verify(configStateListener, times(failedCreationsAmount)).retrying()
116                 }
117             }
118         }
119     }
120 })
121
122 private val firstRequestDelay = Duration.ofSeconds(10)
123 private val sampleException = Exception("Best regards from CBS")
124
125 private fun configuration(content: String = "whatever") =
126         JsonParser().parse("""{ "content": ${content} }""").asJsonObject
127
128 private fun configurationMono(content: String = "whatever") = Mono.just(configuration(content))