Add log diagnostic context
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-core / src / test / kotlin / org / onap / dcae / collectors / veshv / impl / RouterTest.kt
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
21
22 import arrow.core.None
23 import arrow.core.Some
24 import io.netty.buffer.ByteBufAllocator
25 import org.assertj.core.api.Assertions.assertThat
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.jetbrains.spek.api.dsl.on
30 import org.onap.dcae.collectors.veshv.domain.ByteData
31 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
32 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.HEARTBEAT
33 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.SYSLOG
34 import org.onap.dcae.collectors.veshv.model.ClientContext
35 import org.onap.dcae.collectors.veshv.model.RoutedMessage
36 import org.onap.dcae.collectors.veshv.model.VesMessage
37 import org.onap.dcae.collectors.veshv.model.routing
38 import org.onap.dcae.collectors.veshv.tests.utils.commonHeader
39
40
41 /**
42  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
43  * @since May 2018
44  */
45 object RouterTest : Spek({
46     given("sample configuration") {
47         val config = routing {
48
49             defineRoute {
50                 fromDomain(PERF3GPP.domainName)
51                 toTopic("ves_rtpm")
52                 withFixedPartitioning(2)
53             }
54
55             defineRoute {
56                 fromDomain(SYSLOG.domainName)
57                 toTopic("ves_trace")
58                 withFixedPartitioning()
59             }
60         }.build()
61         val cut = Router(config, ClientContext())
62
63         on("message with existing route (rtpm)") {
64             val message = VesMessage(commonHeader(PERF3GPP), ByteData.EMPTY)
65             val result = cut.findDestination(message)
66
67             it("should have route available") {
68                 assertThat(result).isNotNull()
69             }
70
71             it("should be routed to proper partition") {
72                 assertThat(result.map(RoutedMessage::partition)).isEqualTo(Some(2))
73             }
74
75             it("should be routed to proper topic") {
76                 assertThat(result.map(RoutedMessage::topic)).isEqualTo(Some("ves_rtpm"))
77             }
78
79             it("should be routed with a given message") {
80                 assertThat(result.map(RoutedMessage::message)).isEqualTo(Some(message))
81             }
82         }
83
84         on("message with existing route (trace)") {
85             val message = VesMessage(commonHeader(SYSLOG), ByteData.EMPTY)
86             val result = cut.findDestination(message)
87
88             it("should have route available") {
89                 assertThat(result).isNotNull()
90             }
91
92             it("should be routed to proper partition") {
93                 assertThat(result.map(RoutedMessage::partition)).isEqualTo(Some(0))
94             }
95
96             it("should be routed to proper topic") {
97                 assertThat(result.map(RoutedMessage::topic)).isEqualTo(Some("ves_trace"))
98             }
99
100             it("should be routed with a given message") {
101                 assertThat(result.map(RoutedMessage::message)).isEqualTo(Some(message))
102             }
103         }
104
105         on("message with unknown route") {
106             val message = VesMessage(commonHeader(HEARTBEAT), ByteData.EMPTY)
107             val result = cut.findDestination(message)
108
109             it("should not have route available") {
110                 assertThat(result).isEqualTo(None)
111             }
112         }
113     }
114 })