Add metrics for dropped messages
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-ct / src / test / kotlin / org / onap / dcae / collectors / veshv / tests / component / MetricsSpecification.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.tests.component
21
22 import com.google.protobuf.ByteString
23 import org.assertj.core.api.Assertions.assertThat
24 import org.jetbrains.spek.api.Spek
25 import org.jetbrains.spek.api.dsl.describe
26 import org.jetbrains.spek.api.dsl.it
27 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
28 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.HEARTBEAT
29 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
30 import org.onap.dcae.collectors.veshv.model.MessageDropCause.INVALID_MESSAGE
31 import org.onap.dcae.collectors.veshv.model.MessageDropCause.ROUTE_NOT_FOUND
32 import org.onap.dcae.collectors.veshv.tests.fakes.MEASUREMENTS_FOR_VF_SCALING_TOPIC
33 import org.onap.dcae.collectors.veshv.tests.fakes.PERF3GPP_TOPIC
34 import org.onap.dcae.collectors.veshv.tests.fakes.basicConfiguration
35 import org.onap.dcae.collectors.veshv.tests.fakes.twoDomainsToOneTopicConfiguration
36 import org.onap.dcae.collectors.veshv.tests.utils.*
37
38 object MetricsSpecification : Spek({
39     debugRx(false)
40
41     describe("Bytes received metrics") {
42         it("should sum up all bytes received") {
43             val sut = vesHvWithNoOpSink()
44             val vesWireFrameMessage = vesWireFrameMessage()
45             val invalidWireFrame = messageWithInvalidWireFrameHeader()
46
47             val bytesSent = invalidWireFrame.readableBytes() +
48                     vesWireFrameMessage.readableBytes()
49             sut.handleConnection(
50                     vesWireFrameMessage,
51                     invalidWireFrame
52             )
53
54             val metrics = sut.metrics
55             assertThat(metrics.bytesReceived)
56                     .describedAs("bytesReceived metric")
57                     .isEqualTo(bytesSent)
58         }
59     }
60
61     describe("Messages received metrics") {
62         it("should sum up all received messages bytes") {
63             val sut = vesHvWithNoOpSink()
64             val firstVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(10)))
65             val secondVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(40)))
66             val firstVesMessage = vesWireFrameMessage(firstVesEvent)
67             val secondVesMessage = vesWireFrameMessage(secondVesEvent)
68
69             val serializedMessagesSize = firstVesEvent.serializedSize + secondVesEvent.serializedSize
70             sut.handleConnection(
71                     firstVesMessage,
72                     secondVesMessage
73             )
74
75             val metrics = sut.metrics
76             assertThat(metrics.messageBytesReceived)
77                     .describedAs("messageBytesReceived metric")
78                     .isEqualTo(serializedMessagesSize)
79         }
80     }
81
82     describe("Messages sent metrics") {
83         it("should gather info for each topic separately") {
84             val sut = vesHvWithNoOpSink(twoDomainsToOneTopicConfiguration)
85
86             sut.handleConnection(
87                     vesWireFrameMessage(PERF3GPP),
88                     vesWireFrameMessage(PERF3GPP),
89                     vesWireFrameMessage(VesEventDomain.MEASUREMENT)
90             )
91
92             val metrics = sut.metrics
93             assertThat(metrics.messagesSentCount)
94                     .describedAs("messagesSentCount metric")
95                     .isEqualTo(3)
96             assertThat(metrics.messagesOnTopic(PERF3GPP_TOPIC))
97                     .describedAs("messagesSentToTopic $PERF3GPP_TOPIC metric")
98                     .isEqualTo(2)
99             assertThat(metrics.messagesOnTopic(MEASUREMENTS_FOR_VF_SCALING_TOPIC))
100                     .describedAs("messagesSentToTopic $MEASUREMENTS_FOR_VF_SCALING_TOPIC metric")
101                     .isEqualTo(1)
102         }
103     }
104
105     describe("Messages dropped metrics") {
106         it("should gather metrics for invalid messages") {
107             val sut = vesHvWithNoOpSink(basicConfiguration)
108
109             sut.handleConnection(
110                     messageWithInvalidWireFrameHeader(),
111                     wireFrameMessageWithInvalidPayload(),
112                     vesWireFrameMessage(domain = PERF3GPP),
113                     messageWithInvalidListenerVersion()
114             )
115
116             val metrics = sut.metrics
117             assertThat(metrics.messagesDropped(INVALID_MESSAGE))
118                     .describedAs("messagesDroppedCause $INVALID_MESSAGE metric")
119                     .isEqualTo(3)
120         }
121
122         it("should gather metrics for route not found") {
123             val sut = vesHvWithNoOpSink(basicConfiguration)
124
125             sut.handleConnection(
126                     vesWireFrameMessage(domain = PERF3GPP),
127                     vesWireFrameMessage(domain = HEARTBEAT)
128             )
129
130             val metrics = sut.metrics
131             assertThat(metrics.messagesDropped(ROUTE_NOT_FOUND))
132                     .describedAs("messagesDroppedCause $ROUTE_NOT_FOUND metric")
133                     .isEqualTo(1)
134         }
135
136         it("should gather summed metrics for dropped messages"){
137             val sut = vesHvWithNoOpSink(basicConfiguration)
138
139             sut.handleConnection(
140                     vesWireFrameMessage(domain = PERF3GPP),
141                     vesWireFrameMessage(domain = HEARTBEAT),
142                     wireFrameMessageWithInvalidPayload()
143             )
144
145             val metrics = sut.metrics
146             assertThat(metrics.messagesDroppedCount)
147                     .describedAs("messagesDroppedCount metric")
148                     .isEqualTo(2)
149         }
150     }
151 })