dd1908481224e67e0c07bb4435c3aeacf5c28337
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
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.impl.adapters
21
22 import com.nhaarman.mockito_kotlin.mock
23 import com.nhaarman.mockito_kotlin.verify
24 import com.nhaarman.mockito_kotlin.whenever
25 import org.jetbrains.spek.api.Spek
26 import org.jetbrains.spek.api.dsl.given
27 import org.jetbrains.spek.api.dsl.it
28 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
29 import reactor.core.publisher.Mono
30 import reactor.ipc.netty.http.client.HttpClient
31 import reactor.test.StepVerifier
32 import java.time.Duration
33 import java.util.*
34 import kotlin.test.assertEquals
35
36 /**
37  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
38  * @since May 2018
39  */
40 internal object ConsulConfigurationProviderTest : Spek({
41
42     val updateInterval = Duration.ofMillis(1)
43     val httpAdapterMock: HttpAdapter = mock()
44
45     given("valid resource url") {
46
47         val validUrl = "http://valid-url/"
48         val consulConfigProvider = ConsulConfigurationProvider(validUrl, updateInterval, httpAdapterMock)
49
50         whenever(httpAdapterMock.get(validUrl)).thenReturn(Mono.just(constructConsulResponse()))
51
52         it("should use default configuration at the beginning, " +
53                 "then apply received configuration") {
54
55             StepVerifier.create(consulConfigProvider().take(2))
56                     .consumeNextWith {
57
58                         assertEquals("kafka:9092", it.kafkaBootstrapServers)
59
60                         val route1 = it.routing.routes[0]
61                         assertEquals(Domain.HVRANMEAS, route1.domain)
62                         assertEquals("ves_hvRanMeas", route1.targetTopic)
63                     }
64                     .consumeNextWith {
65
66                         assertEquals("kafka:9093", it.kafkaBootstrapServers)
67
68                         val route1 = it.routing.routes[0]
69                         assertEquals(Domain.HEARTBEAT, route1.domain)
70                         assertEquals("test-topic-1", route1.targetTopic)
71
72                         val route2 = it.routing.routes[1]
73                         assertEquals(Domain.MEASUREMENTS_FOR_VF_SCALING, route2.domain)
74                         assertEquals("test-topic-2", route2.targetTopic)
75
76                     }.verifyComplete()
77         }
78     }
79     given("invalid resource url") {
80
81         val invalidUrl = "http://invalid-url/"
82         val consulConfigProvider = ConsulConfigurationProvider(invalidUrl, updateInterval, httpAdapterMock)
83
84         whenever(httpAdapterMock.get(invalidUrl)).thenReturn(Mono.error(RuntimeException("Test exception")))
85
86         it("should use default configuration at the beginning, then should interrupt the flux") {
87
88             StepVerifier.create(consulConfigProvider())
89                     .consumeNextWith {
90
91
92                         assertEquals("kafka:9092", it.kafkaBootstrapServers)
93
94                         val route1 = it.routing.routes[0]
95                         assertEquals(Domain.HVRANMEAS, route1.domain)
96                         assertEquals("ves_hvRanMeas", route1.targetTopic)
97                     }
98                     .verifyErrorMessage("Test exception")
99         }
100     }
101 })
102
103 fun constructConsulResponse(): String {
104
105     val config = """{
106         "kafkaBootstrapServers": "kafka:9093",
107         "routing": [
108                     {
109                         "fromDomain": 1,
110                         "toTopic": "test-topic-1"
111                     },
112                     {
113                         "fromDomain": 2,
114                         "toTopic": "test-topic-2"
115                 }
116     ]
117     }"""
118
119     val encodedValue = String(Base64.getEncoder().encode(config.toByteArray()))
120
121     return """[
122         {
123             "CreateIndex": 100,
124             "ModifyIndex": 200,
125             "LockIndex": 200,
126             "Key": "zip",
127             "Flags": 0,
128             "Value": "$encodedValue",
129             "Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
130         }
131     ]"""
132 }