9f5c37e1d9df0e6c5a4449d8a07115fedb76d824
[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.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.messageWithInvalidListenerVersion
37 import org.onap.dcae.collectors.veshv.tests.utils.messageWithInvalidWireFrameHeader
38 import org.onap.dcae.collectors.veshv.tests.utils.vesEvent
39 import org.onap.dcae.collectors.veshv.tests.utils.vesWireFrameMessage
40 import org.onap.dcae.collectors.veshv.tests.utils.wireFrameMessageWithInvalidPayload
41 import java.time.Duration
42
43 object MetricsSpecification : Spek({
44     debugRx(false)
45
46     describe("Bytes received metrics") {
47         it("should sum up all bytes received") {
48             val sut = vesHvWithNoOpSink()
49             val vesWireFrameMessage = vesWireFrameMessage()
50             val invalidWireFrame = messageWithInvalidWireFrameHeader()
51
52             val bytesSent = invalidWireFrame.readableBytes() +
53                     vesWireFrameMessage.readableBytes()
54             sut.handleConnection(
55                     vesWireFrameMessage,
56                     invalidWireFrame
57             )
58
59             val metrics = sut.metrics
60             assertThat(metrics.bytesReceived)
61                     .describedAs("bytesReceived metric")
62                     .isEqualTo(bytesSent)
63         }
64     }
65
66     describe("Messages received metrics") {
67         it("should sum up all received messages bytes") {
68             val sut = vesHvWithNoOpSink()
69             val firstVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(10)))
70             val secondVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(40)))
71             val firstVesMessage = vesWireFrameMessage(firstVesEvent)
72             val secondVesMessage = vesWireFrameMessage(secondVesEvent)
73
74             val serializedMessagesSize = firstVesEvent.serializedSize + secondVesEvent.serializedSize
75             sut.handleConnection(
76                     firstVesMessage,
77                     secondVesMessage
78             )
79
80             val metrics = sut.metrics
81             assertThat(metrics.messageBytesReceived)
82                     .describedAs("messageBytesReceived metric")
83                     .isEqualTo(serializedMessagesSize)
84         }
85     }
86
87     describe("Messages sent metrics") {
88         it("should gather info for each topic separately") {
89             val sut = vesHvWithNoOpSink(twoDomainsToOneTopicConfiguration)
90
91             sut.handleConnection(
92                     vesWireFrameMessage(PERF3GPP),
93                     vesWireFrameMessage(PERF3GPP),
94                     vesWireFrameMessage(VesEventDomain.MEASUREMENT)
95             )
96
97             val metrics = sut.metrics
98             assertThat(metrics.messagesSentCount)
99                     .describedAs("messagesSentCount metric")
100                     .isEqualTo(3)
101             assertThat(metrics.messagesOnTopic(PERF3GPP_TOPIC))
102                     .describedAs("messagesSentToTopic $PERF3GPP_TOPIC metric")
103                     .isEqualTo(2)
104             assertThat(metrics.messagesOnTopic(MEASUREMENTS_FOR_VF_SCALING_TOPIC))
105                     .describedAs("messagesSentToTopic $MEASUREMENTS_FOR_VF_SCALING_TOPIC metric")
106                     .isEqualTo(1)
107         }
108     }
109
110     describe("Processing time") {
111         it("should gather processing time metric") {
112             val delay = Duration.ofMillis(10)
113             val sut = vesHvWithDelayingSink(delay)
114
115             sut.handleConnection(vesWireFrameMessage(PERF3GPP))
116
117
118             val metrics = sut.metrics
119             assertThat(metrics.lastProcessingTimeMicros)
120                     .describedAs("processingTime metric")
121                     .isGreaterThanOrEqualTo(delay.toNanos().toDouble() / 1000.0)
122         }
123     }
124
125     describe("Messages dropped metrics") {
126         it("should gather metrics for invalid messages") {
127             val sut = vesHvWithNoOpSink(basicConfiguration)
128
129             sut.handleConnection(
130                     messageWithInvalidWireFrameHeader(),
131                     wireFrameMessageWithInvalidPayload(),
132                     vesWireFrameMessage(domain = PERF3GPP),
133                     messageWithInvalidListenerVersion()
134             )
135
136             val metrics = sut.metrics
137             assertThat(metrics.messagesDropped(INVALID_MESSAGE))
138                     .describedAs("messagesDroppedCause $INVALID_MESSAGE metric")
139                     .isEqualTo(3)
140         }
141
142         it("should gather metrics for route not found") {
143             val sut = vesHvWithNoOpSink(basicConfiguration)
144
145             sut.handleConnection(
146                     vesWireFrameMessage(domain = PERF3GPP),
147                     vesWireFrameMessage(domain = HEARTBEAT)
148             )
149
150             val metrics = sut.metrics
151             assertThat(metrics.messagesDropped(ROUTE_NOT_FOUND))
152                     .describedAs("messagesDroppedCause $ROUTE_NOT_FOUND metric")
153                     .isEqualTo(1)
154         }
155
156         it("should gather summed metrics for dropped messages"){
157             val sut = vesHvWithNoOpSink(basicConfiguration)
158
159             sut.handleConnection(
160                     vesWireFrameMessage(domain = PERF3GPP),
161                     vesWireFrameMessage(domain = HEARTBEAT),
162                     wireFrameMessageWithInvalidPayload()
163             )
164
165             val metrics = sut.metrics
166             assertThat(metrics.messagesDroppedCount)
167                     .describedAs("messagesDroppedCount metric")
168                     .isEqualTo(2)
169         }
170     }
171 })