2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 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.nhaarman.mockito_kotlin.eq
23 import com.nhaarman.mockito_kotlin.mock
24 import com.nhaarman.mockito_kotlin.verify
25 import com.nhaarman.mockito_kotlin.whenever
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.given
28 import org.jetbrains.spek.api.dsl.it
29 import org.mockito.Mockito
30 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
31 import reactor.core.publisher.Mono
32 import reactor.ipc.netty.http.client.HttpClient
33 import reactor.test.StepVerifier
34 import java.time.Duration
36 import kotlin.test.assertEquals
39 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
42 internal object ConsulConfigurationProviderTest : Spek({
44 val httpAdapterMock: HttpAdapter = mock()
45 val firstRequestDelay = Duration.ofMillis(1)
47 given("valid resource url") {
49 val validUrl = "http://valid-url/"
50 val consulConfigProvider = ConsulConfigurationProvider(validUrl, httpAdapterMock, firstRequestDelay)
52 whenever(httpAdapterMock.get(eq(validUrl), Mockito.anyMap()))
53 .thenReturn(Mono.just(constructConsulResponse()))
55 it("should use default configuration at the beginning, " +
56 "then apply received configuration") {
58 StepVerifier.create(consulConfigProvider().take(2))
61 assertEquals("kafka:9092", it.kafkaBootstrapServers)
63 val route1 = it.routing.routes[0]
64 assertEquals(Domain.HVRANMEAS, route1.domain)
65 assertEquals("ves_hvRanMeas", route1.targetTopic)
69 assertEquals("kafka:9093", it.kafkaBootstrapServers)
71 val route1 = it.routing.routes[0]
72 assertEquals(Domain.HEARTBEAT, route1.domain)
73 assertEquals("test-topic-1", route1.targetTopic)
75 val route2 = it.routing.routes[1]
76 assertEquals(Domain.MEASUREMENTS_FOR_VF_SCALING, route2.domain)
77 assertEquals("test-topic-2", route2.targetTopic)
82 given("invalid resource url") {
84 val invalidUrl = "http://invalid-url/"
85 val consulConfigProvider = ConsulConfigurationProvider(invalidUrl, httpAdapterMock, firstRequestDelay)
87 whenever(httpAdapterMock.get(eq(invalidUrl), Mockito.anyMap()))
88 .thenReturn(Mono.error(RuntimeException("Test exception")))
90 it("should use default configuration at the beginning, then should interrupt the flux") {
92 StepVerifier.create(consulConfigProvider())
96 assertEquals("kafka:9092", it.kafkaBootstrapServers)
98 val route1 = it.routing.routes[0]
99 assertEquals(Domain.HVRANMEAS, route1.domain)
100 assertEquals("ves_hvRanMeas", route1.targetTopic)
102 .verifyErrorMessage("Test exception")
107 fun constructConsulResponse(): String {
110 "kafkaBootstrapServers": "kafka:9093",
114 "toTopic": "test-topic-1"
118 "toTopic": "test-topic-2"
123 val encodedValue = String(Base64.getEncoder().encode(config.toByteArray()))
132 "Value": "$encodedValue",
133 "Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"