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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  20 package org.onap.dcae.collectors.veshv.main
 
  23 import io.micrometer.core.instrument.Counter
 
  24 import io.micrometer.core.instrument.Gauge
 
  25 import io.micrometer.core.instrument.search.RequiredSearch
 
  26 import io.micrometer.core.instrument.simple.SimpleMeterRegistry
 
  27 import org.assertj.core.api.Assertions.assertThat
 
  28 import org.assertj.core.data.Percentage
 
  29 import org.jetbrains.spek.api.Spek
 
  30 import org.jetbrains.spek.api.dsl.describe
 
  31 import org.jetbrains.spek.api.dsl.it
 
  32 import org.jetbrains.spek.api.dsl.on
 
  35  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
 
  38 object MicrometerMetricsTest : Spek({
 
  39     val doublePrecision = Percentage.withPercentage(0.5)
 
  40     lateinit var registry: SimpleMeterRegistry
 
  41     lateinit var cut: MicrometerMetrics
 
  44         registry = SimpleMeterRegistry()
 
  45         cut = MicrometerMetrics(registry)
 
  48     fun registrySearch() = RequiredSearch.`in`(registry)
 
  50     fun <M, T> verifyMeter(search: RequiredSearch, map: (RequiredSearch) -> M, verifier: (M) -> T) =
 
  54                     { ex -> assertThat(ex).doesNotThrowAnyException() },
 
  58     fun <T> verifyGauge(name: String, verifier: (Gauge) -> T) =
 
  59             verifyMeter(registrySearch().name(name), RequiredSearch::gauge, verifier)
 
  61     fun <T> verifyCounter(search: RequiredSearch, verifier: (Counter) -> T) =
 
  62             verifyMeter(search, RequiredSearch::counter, verifier)
 
  64     fun <T> verifyCounter(name: String, verifier: (Counter) -> T) =
 
  65             verifyCounter(registrySearch().name(name), verifier)
 
  67     fun verifyAllCountersAreUnchangedBut(vararg changedCounters: String) {
 
  69                 .filter { it is Counter }
 
  70                 .filterNot { it.id.name in changedCounters }
 
  71                 .forEach { assertThat((it as Counter).count()).isCloseTo(0.0, doublePrecision) }
 
  74     describe("notifyBytesReceived") {
 
  76         on("data.received.bytes counter") {
 
  77             val counterName = "data.received.bytes"
 
  79             it("should increment counter") {
 
  81                 cut.notifyBytesReceived(bytes)
 
  83                 verifyCounter(counterName) { counter ->
 
  84                     assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
 
  88             it("should leave all other counters unchanged") {
 
  89                 cut.notifyBytesReceived(128)
 
  90                 verifyAllCountersAreUnchangedBut(counterName)
 
  95     describe("notifyMessageReceived") {
 
  96         on("messages.received.count counter") {
 
  97             val counterName = "messages.received.count"
 
  99             it("should increment counter") {
 
 100                 cut.notifyMessageReceived(777)
 
 102                 verifyCounter(counterName) { counter ->
 
 103                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
 
 108         on("messages.received.bytes counter") {
 
 109             val counterName = "messages.received.bytes"
 
 111             it("should increment counter") {
 
 113                 cut.notifyMessageReceived(bytes)
 
 115                 verifyCounter(counterName) { counter ->
 
 116                     assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
 
 121         it("should leave all other counters unchanged") {
 
 122             cut.notifyMessageReceived(128)
 
 123             verifyAllCountersAreUnchangedBut("messages.received.count", "messages.received.bytes")
 
 127     describe("notifyMessageSent") {
 
 128         val topicName = "dmaap_topic_name"
 
 129         val counterName = "messages.sent.count"
 
 131         on("$counterName counter") {
 
 133             it("should increment counter") {
 
 134                 cut.notifyMessageSent(topicName)
 
 136                 verifyCounter(counterName) { counter ->
 
 137                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
 
 142         on("$counterName[topic=$topicName] counter") {
 
 144             it("should increment counter") {
 
 145                 cut.notifyMessageSent(topicName)
 
 147                 verifyCounter(registrySearch().name(counterName).tag("topic", topicName)) { counter ->
 
 148                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
 
 153         it("should leave all other counters unchanged") {
 
 154             cut.notifyMessageSent(topicName)
 
 155             verifyAllCountersAreUnchangedBut(counterName)
 
 159     describe("processing gauge") {
 
 160         it("should show difference between sent and received messages") {
 
 162             on("positive difference") {
 
 163                 cut.notifyMessageReceived(128)
 
 164                 cut.notifyMessageReceived(256)
 
 165                 cut.notifyMessageReceived(256)
 
 166                 cut.notifyMessageSent("perf3gpp")
 
 167                 verifyGauge("messages.processing.count") { gauge ->
 
 168                     assertThat(gauge.value()).isCloseTo(2.0, doublePrecision)
 
 172             on("zero difference") {
 
 173                 cut.notifyMessageReceived(128)
 
 174                 cut.notifyMessageSent("perf3gpp")
 
 175                 verifyGauge("messages.processing.count") { gauge ->
 
 176                     assertThat(gauge.value()).isCloseTo(0.0, doublePrecision)
 
 180             on("negative difference") {
 
 181                 cut.notifyMessageReceived(128)
 
 182                 cut.notifyMessageSent("fault")
 
 183                 cut.notifyMessageSent("perf3gpp")
 
 184                 verifyGauge("messages.processing.count") { gauge ->
 
 185                     assertThat(gauge.value()).isCloseTo(0.0, doublePrecision)