1626c028573b58c80d8f995abc13ce41f480e398
[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.retry.Retry
32 import reactor.test.StepVerifier
33 import java.time.Duration
34 import java.util.*
35 import kotlin.test.assertEquals
36
37 /**
38  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
39  * @since May 2018
40  */
41 internal object ConsulConfigurationProviderTest : Spek({
42
43     val httpAdapterMock: HttpAdapter = mock()
44     val firstRequestDelay = Duration.ofMillis(1)
45     val requestInterval = Duration.ofMillis(1)
46     val retry = Retry.onlyIf<Any> { it.iteration() < 2 }.fixedBackoff(Duration.ofNanos(1))
47
48     given("valid resource url") {
49
50         val validUrl = "http://valid-url/"
51         val consulConfigProvider = ConsulConfigurationProvider(
52                 httpAdapterMock,
53                 validUrl,
54                 firstRequestDelay,
55                 requestInterval,
56                 retry)
57
58         whenever(httpAdapterMock.get(eq(validUrl), Mockito.anyMap()))
59                 .thenReturn(Mono.just(constructConsulResponse()))
60
61         it("should use received configuration") {
62
63             StepVerifier.create(consulConfigProvider().take(1))
64                     .consumeNextWith {
65
66                         assertEquals("kafka:9093", it.kafkaBootstrapServers)
67
68                         val route1 = it.routing.routes[0]
69                         assertEquals(Domain.FAULT, route1.domain)
70                         assertEquals("test-topic-1", route1.targetTopic)
71
72                         val route2 = it.routing.routes[1]
73                         assertEquals(Domain.HEARTBEAT, 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(
83                 httpAdapterMock,
84                 invalidUrl,
85                 firstRequestDelay,
86                 requestInterval,
87                 retry)
88
89         whenever(httpAdapterMock.get(eq(invalidUrl), Mockito.anyMap()))
90                 .thenReturn(Mono.error(RuntimeException("Test exception")))
91
92         it("should interrupt the flux") {
93
94             StepVerifier.create(consulConfigProvider())
95                     .verifyErrorMessage("Test exception")
96         }
97     }
98 })
99
100 fun constructConsulResponse(): String {
101
102     val config = """{
103     "kafkaBootstrapServers": "kafka:9093",
104     "routing": [
105             {
106                 "fromDomain": 1,
107                 "toTopic": "test-topic-1"
108             },
109             {
110                 "fromDomain": 2,
111                 "toTopic": "test-topic-2"
112             }
113     ]
114 }"""
115
116     val encodedValue = String(Base64.getEncoder().encode(config.toByteArray()))
117
118     return """[
119         {
120             "CreateIndex": 100,
121             "ModifyIndex": 200,
122             "LockIndex": 200,
123             "Key": "zip",
124             "Flags": 0,
125             "Value": "$encodedValue",
126             "Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
127         }
128     ]"""
129 }