322ec4e888919f424b452658638b0da686a68fe2
[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.eq
23 import com.nhaarman.mockito_kotlin.mock
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.mockito.Mockito
29 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
30 import reactor.core.publisher.Mono
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 httpAdapterMock: HttpAdapter = mock()
43     val firstRequestDelay = Duration.ofMillis(1)
44
45     given("valid resource url") {
46
47         val validUrl = "http://valid-url/"
48         val consulConfigProvider = ConsulConfigurationProvider(validUrl, httpAdapterMock, firstRequestDelay)
49
50         whenever(httpAdapterMock.get(eq(validUrl), Mockito.anyMap()))
51                 .thenReturn(Mono.just(constructConsulResponse()))
52
53         it("should use default configuration at the beginning, " +
54                 "then apply received configuration") {
55
56             StepVerifier.create(consulConfigProvider().take(2))
57                     .consumeNextWith {
58
59                         assertEquals("kafka:9092", it.kafkaBootstrapServers)
60
61                         val route1 = it.routing.routes[0]
62                         assertEquals(Domain.HVRANMEAS, route1.domain)
63                         assertEquals("ves_hvRanMeas", route1.targetTopic)
64                     }
65                     .consumeNextWith {
66
67                         assertEquals("kafka:9093", it.kafkaBootstrapServers)
68
69                         val route1 = it.routing.routes[0]
70                         assertEquals(Domain.HEARTBEAT, route1.domain)
71                         assertEquals("test-topic-1", route1.targetTopic)
72
73                         val route2 = it.routing.routes[1]
74                         assertEquals(Domain.MEASUREMENTS_FOR_VF_SCALING, route2.domain)
75                         assertEquals("test-topic-2", route2.targetTopic)
76
77                     }.verifyComplete()
78         }
79     }
80     given("invalid resource url") {
81
82         val invalidUrl = "http://invalid-url/"
83         val consulConfigProvider = ConsulConfigurationProvider(invalidUrl, httpAdapterMock, firstRequestDelay)
84
85         whenever(httpAdapterMock.get(eq(invalidUrl), Mockito.anyMap()))
86                 .thenReturn(Mono.error(RuntimeException("Test exception")))
87
88         it("should use default configuration at the beginning, then should interrupt the flux") {
89
90             StepVerifier.create(consulConfigProvider())
91                     .consumeNextWith {
92
93
94                         assertEquals("kafka:9092", it.kafkaBootstrapServers)
95
96                         val route1 = it.routing.routes[0]
97                         assertEquals(Domain.HVRANMEAS, route1.domain)
98                         assertEquals("ves_hvRanMeas", route1.targetTopic)
99                     }
100                     .verifyErrorMessage("Test exception")
101         }
102     }
103 })
104
105 fun constructConsulResponse(): String {
106
107     val config = """{
108         "kafkaBootstrapServers": "kafka:9093",
109         "routing": [
110                     {
111                         "fromDomain": 1,
112                         "toTopic": "test-topic-1"
113                     },
114                     {
115                         "fromDomain": 2,
116                         "toTopic": "test-topic-2"
117                 }
118     ]
119     }"""
120
121     val encodedValue = String(Base64.getEncoder().encode(config.toByteArray()))
122
123     return """[
124         {
125             "CreateIndex": 100,
126             "ModifyIndex": 200,
127             "LockIndex": 200,
128             "Key": "zip",
129             "Flags": 0,
130             "Value": "$encodedValue",
131             "Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
132         }
133     ]"""
134 }