Create component tests skeleton for metrics 48/74648/2
authorFilip Krzywka <filip.krzywka@nokia.com>
Thu, 13 Dec 2018 10:22:29 +0000 (11:22 +0100)
committerJakub Dudycz <jakub.dudycz@nokia.com>
Fri, 14 Dec 2018 09:58:22 +0000 (10:58 +0100)
Change-Id: I5acc919171953a4bb428585a7b8420b3223aa556
Issue-ID: DCAEGEN2-1032
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
sources/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/MetricsSpecification.kt [new file with mode: 0644]
sources/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/Sut.kt
sources/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/fakes/metrics.kt
sources/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/fakes/sink.kt
sources/hv-collector-test-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/tests/utils/messages.kt

diff --git a/sources/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/MetricsSpecification.kt b/sources/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/MetricsSpecification.kt
new file mode 100644 (file)
index 0000000..2feca41
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * ============LICENSE_START=======================================================
+ * dcaegen2-collectors-veshv
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcae.collectors.veshv.tests.component
+
+import com.google.protobuf.ByteString
+import org.assertj.core.api.Assertions.assertThat
+import org.jetbrains.spek.api.Spek
+import org.jetbrains.spek.api.dsl.describe
+import org.jetbrains.spek.api.dsl.it
+import org.onap.dcae.collectors.veshv.domain.VesEventDomain
+import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
+import org.onap.dcae.collectors.veshv.model.CollectorConfiguration
+import org.onap.dcae.collectors.veshv.tests.fakes.NoOpSink
+import org.onap.dcae.collectors.veshv.tests.fakes.FakeMetrics
+import org.onap.dcae.collectors.veshv.tests.fakes.MEASUREMENTS_FOR_VF_SCALING_TOPIC
+import org.onap.dcae.collectors.veshv.tests.fakes.PERF3GPP_TOPIC
+import org.onap.dcae.collectors.veshv.tests.fakes.basicConfiguration
+import org.onap.dcae.collectors.veshv.tests.fakes.twoDomainsToOneTopicConfiguration
+import org.onap.dcae.collectors.veshv.tests.utils.invalidWireFrame
+import org.onap.dcae.collectors.veshv.tests.utils.vesEvent
+import org.onap.dcae.collectors.veshv.tests.utils.vesWireFrameMessage
+import kotlin.test.fail
+
+object MetricsSpecification : Spek({
+    debugRx(false)
+
+    describe("Bytes received metrics") {
+        it("should sum up all bytes received") {
+            val sut = vesHvWithNoOpSink()
+            val vesWireFrameMessage = vesWireFrameMessage()
+            val invalidWireFrame = invalidWireFrame()
+
+            val bytesSent = invalidWireFrame.readableBytes() +
+                    vesWireFrameMessage.readableBytes()
+            sut.handleConnection(
+                    vesWireFrameMessage,
+                    invalidWireFrame
+            )
+
+            val metrics = sut.metrics
+            assertThat(metrics.bytesReceived)
+                    .describedAs("bytesReceived metric")
+                    .isEqualTo(bytesSent)
+        }
+    }
+
+    describe("Messages received metrics") {
+        it("should sum up all received messages bytes") {
+            val sut = vesHvWithNoOpSink()
+            val firstVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(10)))
+            val secondVesEvent = vesEvent(eventFields = ByteString.copyFrom(ByteArray(40)))
+            val firstVesMessage = vesWireFrameMessage(firstVesEvent)
+            val secondVesMessage = vesWireFrameMessage(secondVesEvent)
+
+            val serializedMessagesSize = firstVesEvent.serializedSize + secondVesEvent.serializedSize
+            sut.handleConnection(
+                    firstVesMessage,
+                    secondVesMessage
+            )
+
+            val metrics = sut.metrics
+            assertThat(metrics.messageBytesReceived)
+                    .describedAs("messageBytesReceived metric")
+                    .isEqualTo(serializedMessagesSize)
+        }
+    }
+
+    describe("Messages sent metrics") {
+        it("should gather info for each topic separately") {
+            val sut = vesHvWithNoOpSink(twoDomainsToOneTopicConfiguration)
+
+            sut.handleConnection(
+                    vesWireFrameMessage(PERF3GPP),
+                    vesWireFrameMessage(PERF3GPP),
+                    vesWireFrameMessage(VesEventDomain.MEASUREMENT)
+            )
+
+            val metrics = sut.metrics
+            assertThat(metrics.messageSentCount)
+                    .describedAs("messageSentCount metric")
+                    .isEqualTo(3)
+            assertThat(messagesOnTopic(metrics, PERF3GPP_TOPIC))
+                    .describedAs("messagesSentToTopic $PERF3GPP_TOPIC metric")
+                    .isEqualTo(2)
+            assertThat(messagesOnTopic(metrics, MEASUREMENTS_FOR_VF_SCALING_TOPIC))
+                    .describedAs("messagesSentToTopic $MEASUREMENTS_FOR_VF_SCALING_TOPIC metric")
+                    .isEqualTo(1)
+        }
+    }
+})
+
+private fun messagesOnTopic(metrics: FakeMetrics, topic: String) =
+        metrics.messagesSentToTopic.get(topic) ?: fail("No messages were sent to topic $topic")
\ No newline at end of file
index ce242e0..0c1b589 100644 (file)
@@ -28,11 +28,9 @@ import org.onap.dcae.collectors.veshv.boundary.Sink
 import org.onap.dcae.collectors.veshv.boundary.SinkProvider
 import org.onap.dcae.collectors.veshv.factory.CollectorFactory
 import org.onap.dcae.collectors.veshv.model.ClientContext
+import org.onap.dcae.collectors.veshv.model.CollectorConfiguration
 import org.onap.dcae.collectors.veshv.model.RoutedMessage
-import org.onap.dcae.collectors.veshv.tests.fakes.FakeConfigurationProvider
-import org.onap.dcae.collectors.veshv.tests.fakes.FakeHealthState
-import org.onap.dcae.collectors.veshv.tests.fakes.FakeMetrics
-import org.onap.dcae.collectors.veshv.tests.fakes.StoringSink
+import org.onap.dcae.collectors.veshv.tests.fakes.*
 import reactor.core.publisher.Flux
 import java.time.Duration
 
@@ -43,9 +41,9 @@ import java.time.Duration
 class Sut(sink: Sink = StoringSink()) {
     val configurationProvider = FakeConfigurationProvider()
     val healthStateProvider = FakeHealthState()
-
     val alloc: ByteBufAllocator = UnpooledByteBufAllocator.DEFAULT
-    private val metrics = FakeMetrics()
+    val metrics = FakeMetrics()
+
     private val collectorFactory = CollectorFactory(
             configurationProvider,
             SinkProvider.just(sink),
@@ -55,15 +53,27 @@ class Sut(sink: Sink = StoringSink()) {
     private val collectorProvider = collectorFactory.createVesHvCollectorProvider()
 
     val collector: Collector
-        get() = collectorProvider(ClientContext(alloc)).getOrElse{ throw IllegalStateException("Collector not available.") }
+        get() = collectorProvider(ClientContext(alloc)).getOrElse {
+            throw IllegalStateException("Collector not available.")
+        }
 
     companion object {
         const val MAX_PAYLOAD_SIZE_BYTES = 1024
     }
-
 }
 
+private val timeout = Duration.ofSeconds(10)
+
 fun Sut.handleConnection(sink: StoringSink, vararg packets: ByteBuf): List<RoutedMessage> {
-    collector.handleConnection(Flux.fromArray(packets)).block(Duration.ofSeconds(10))
+    collector.handleConnection(Flux.fromArray(packets)).block(timeout)
     return sink.sentMessages
 }
+
+fun Sut.handleConnection(vararg packets: ByteBuf) {
+    collector.handleConnection(Flux.fromArray(packets)).block(timeout)
+}
+
+fun vesHvWithNoOpSink(collectorConfiguration: CollectorConfiguration = basicConfiguration): Sut =
+        Sut(NoOpSink()).apply {
+            configurationProvider.updateConfiguration(collectorConfiguration)
+        }
index f3fc238..dd09805 100644 (file)
 package org.onap.dcae.collectors.veshv.tests.fakes
 
 import org.onap.dcae.collectors.veshv.boundary.Metrics
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.atomic.AtomicInteger
 
 /**
  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
  * @since June 2018
  */
 class FakeMetrics : Metrics {
+    var bytesReceived: Int = 0
+
+    var messageBytesReceived: Int = 0
+
+    var messageSentCount: Int = 0
+    val messagesSentToTopic: MutableMap<String, Int> = ConcurrentHashMap()
+
     override fun notifyBytesReceived(size: Int) {
+        bytesReceived += size
     }
 
     override fun notifyMessageReceived(size: Int) {
+        messageBytesReceived += size
     }
 
     override fun notifyMessageSent(topic: String) {
+        messageSentCount++
+        messagesSentToTopic.compute(topic, { k, v -> messagesSentToTopic.get(k)?.inc() ?: 1 })
     }
 }
\ No newline at end of file
index db7777c..01e1166 100644 (file)
@@ -27,6 +27,7 @@ import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.RESERVED
 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.OTHER
+import org.onap.ves.VesEventOuterClass.VesEvent
 
 import java.util.UUID.randomUUID
 
@@ -42,11 +43,15 @@ private fun ByteBuf.writeValidWireFrameHeaders() {
 }
 
 fun vesWireFrameMessage(domain: VesEventDomain = OTHER,
-                        id: String = randomUUID().toString()): ByteBuf =
+                        id: String = randomUUID().toString(),
+                        eventFields: ByteString = ByteString.EMPTY): ByteBuf =
+        vesWireFrameMessage(vesEvent(domain, id, eventFields))
+
+fun vesWireFrameMessage(vesEvent: VesEvent) =
         allocator.buffer().run {
             writeValidWireFrameHeaders()
 
-            val gpb = vesEvent(domain, id).toByteString().asReadOnlyByteBuffer()
+            val gpb = vesEvent.toByteString().asReadOnlyByteBuffer()
             writeInt(gpb.limit())  // ves event size in bytes
             writeBytes(gpb)  // ves event as GPB bytes
         }
@@ -70,16 +75,9 @@ fun invalidWireFrame(): ByteBuf = allocator.buffer().run {
 }
 
 fun vesMessageWithPayloadOfSize(payloadSizeBytes: Int, domain: VesEventDomain = PERF3GPP): ByteBuf =
-        allocator.buffer().run {
-            writeValidWireFrameHeaders()
-
-            val gpb = vesEvent(
-                    domain = domain,
-                    eventFields = ByteString.copyFrom(ByteArray(payloadSizeBytes))
-            ).toByteString().asReadOnlyByteBuffer()
-
-            writeInt(gpb.limit())  // ves event size in bytes
-            writeBytes(gpb)  // ves event as GPB bytes
-        }
+        vesWireFrameMessage(
+                domain = domain,
+                eventFields = ByteString.copyFrom(ByteArray(payloadSizeBytes))
+        )