2feca410c52cf469f5fb8ea5d2aefc3af807987e
[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.PERF3GPP
29 import org.onap.dcae.collectors.veshv.model.CollectorConfiguration
30 import org.onap.dcae.collectors.veshv.tests.fakes.NoOpSink
31 import org.onap.dcae.collectors.veshv.tests.fakes.FakeMetrics
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.invalidWireFrame
37 import org.onap.dcae.collectors.veshv.tests.utils.vesEvent
38 import org.onap.dcae.collectors.veshv.tests.utils.vesWireFrameMessage
39 import kotlin.test.fail
40
41 object MetricsSpecification : Spek({
42     debugRx(false)
43
44     describe("Bytes received metrics") {
45         it("should sum up all bytes received") {
46             val sut = vesHvWithNoOpSink()
47             val vesWireFrameMessage = vesWireFrameMessage()
48             val invalidWireFrame = invalidWireFrame()
49
50             val bytesSent = invalidWireFrame.readableBytes() +
51                     vesWireFrameMessage.readableBytes()
52             sut.handleConnection(
53                     vesWireFrameMessage,
54                     invalidWireFrame
55             )
56
57             val metrics = sut.metrics
58             assertThat(metrics.bytesReceived)
59                     .describedAs("bytesReceived metric")
60                     .isEqualTo(bytesSent)
61         }
62     }
63
64     describe("Messages received metrics") {
65         it("should sum up all received messages bytes") {
66             val sut = vesHvWithNoOpSink()
67             val firstVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(10)))
68             val secondVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(40)))
69             val firstVesMessage = vesWireFrameMessage(firstVesEvent)
70             val secondVesMessage = vesWireFrameMessage(secondVesEvent)
71
72             val serializedMessagesSize = firstVesEvent.serializedSize + secondVesEvent.serializedSize
73             sut.handleConnection(
74                     firstVesMessage,
75                     secondVesMessage
76             )
77
78             val metrics = sut.metrics
79             assertThat(metrics.messageBytesReceived)
80                     .describedAs("messageBytesReceived metric")
81                     .isEqualTo(serializedMessagesSize)
82         }
83     }
84
85     describe("Messages sent metrics") {
86         it("should gather info for each topic separately") {
87             val sut = vesHvWithNoOpSink(twoDomainsToOneTopicConfiguration)
88
89             sut.handleConnection(
90                     vesWireFrameMessage(PERF3GPP),
91                     vesWireFrameMessage(PERF3GPP),
92                     vesWireFrameMessage(VesEventDomain.MEASUREMENT)
93             )
94
95             val metrics = sut.metrics
96             assertThat(metrics.messageSentCount)
97                     .describedAs("messageSentCount metric")
98                     .isEqualTo(3)
99             assertThat(messagesOnTopic(metrics, PERF3GPP_TOPIC))
100                     .describedAs("messagesSentToTopic $PERF3GPP_TOPIC metric")
101                     .isEqualTo(2)
102             assertThat(messagesOnTopic(metrics, MEASUREMENTS_FOR_VF_SCALING_TOPIC))
103                     .describedAs("messagesSentToTopic $MEASUREMENTS_FOR_VF_SCALING_TOPIC metric")
104                     .isEqualTo(1)
105         }
106     }
107 })
108
109 private fun messagesOnTopic(metrics: FakeMetrics, topic: String) =
110         metrics.messagesSentToTopic.get(topic) ?: fail("No messages were sent to topic $topic")