66326ddc3fda99c6458a136490767ab807785673
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-main / src / test / kotlin / org / onap / dcae / collectors / veshv / main / MicrometerMetricsTest.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.main
21
22 import arrow.core.Try
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 io.micrometer.prometheus.PrometheusConfig
28 import io.micrometer.prometheus.PrometheusMeterRegistry
29 import org.assertj.core.api.Assertions.assertThat
30 import org.assertj.core.data.Percentage
31 import org.jetbrains.spek.api.Spek
32 import org.jetbrains.spek.api.dsl.describe
33 import org.jetbrains.spek.api.dsl.it
34 import org.jetbrains.spek.api.dsl.on
35 import org.onap.dcae.collectors.veshv.healthcheck.ports.PrometheusMetricsProvider
36 import org.onap.dcae.collectors.veshv.main.metrics.MicrometerMetrics
37 import org.onap.dcae.collectors.veshv.main.metrics.MicrometerMetrics.Companion.PREFIX
38
39 /**
40  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
41  * @since June 2018
42  */
43 object MicrometerMetricsTest : Spek({
44     val doublePrecision = Percentage.withPercentage(0.5)
45     lateinit var registry: PrometheusMeterRegistry
46     lateinit var cut: MicrometerMetrics
47
48     beforeEachTest {
49         registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
50         cut = MicrometerMetrics(registry)
51     }
52
53     fun registrySearch() = RequiredSearch.`in`(registry)
54
55     fun <M, T> verifyMeter(search: RequiredSearch, map: (RequiredSearch) -> M, verifier: (M) -> T) =
56             Try {
57                 map(search)
58             }.fold(
59                     { ex -> assertThat(ex).doesNotThrowAnyException() },
60                     verifier
61             )
62
63     fun <T> verifyGauge(name: String, verifier: (Gauge) -> T) =
64             verifyMeter(registrySearch().name(name), RequiredSearch::gauge, verifier)
65
66     fun <T> verifyCounter(search: RequiredSearch, verifier: (Counter) -> T) =
67             verifyMeter(search, RequiredSearch::counter, verifier)
68
69     fun <T> verifyCounter(name: String, verifier: (Counter) -> T) =
70             verifyCounter(registrySearch().name(name), verifier)
71
72     fun verifyAllCountersAreUnchangedBut(vararg changedCounters: String) {
73         registry.meters
74                 .filter { it is Counter }
75                 .map { it as Counter }
76                 .filterNot { it.id.name in changedCounters }
77                 .forEach {
78                     assertThat(it.count()).describedAs(it.id.toString()).isCloseTo(0.0, doublePrecision)
79                 }
80     }
81
82     describe("notifyBytesReceived") {
83
84         on("$PREFIX.data.received.bytes counter") {
85             val counterName = "$PREFIX.data.received.bytes"
86
87             it("should increment counter") {
88                 val bytes = 128
89                 cut.notifyBytesReceived(bytes)
90
91                 verifyCounter(counterName) { counter ->
92                     assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
93                 }
94             }
95
96             it("should leave all other counters unchanged") {
97                 cut.notifyBytesReceived(128)
98                 verifyAllCountersAreUnchangedBut(counterName)
99             }
100         }
101     }
102
103     describe("notifyMessageReceived") {
104         on("$PREFIX.messages.received.count counter") {
105             val counterName = "$PREFIX.messages.received.count"
106
107             it("should increment counter") {
108                 cut.notifyMessageReceived(777)
109
110                 verifyCounter(counterName) { counter ->
111                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
112                 }
113             }
114         }
115
116         on("$PREFIX.messages.received.bytes counter") {
117             val counterName = "$PREFIX.messages.received.bytes"
118
119             it("should increment counter") {
120                 val bytes = 888
121                 cut.notifyMessageReceived(bytes)
122
123                 verifyCounter(counterName) { counter ->
124                     assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
125                 }
126             }
127         }
128
129         it("should leave all other counters unchanged") {
130             cut.notifyMessageReceived(128)
131             verifyAllCountersAreUnchangedBut("$PREFIX.messages.received.count", "$PREFIX.messages.received.bytes")
132         }
133     }
134
135     describe("notifyMessageSent") {
136         val topicName1 = "PERF3GPP"
137         val topicName2 = "CALLTRACE"
138
139         on("$PREFIX.messages.sent.count counter") {
140             val counterName = "$PREFIX.messages.sent.count"
141
142             it("should increment counter") {
143                 cut.notifyMessageSent(topicName1)
144
145                 verifyCounter(counterName) { counter ->
146                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
147                 }
148                 verifyAllCountersAreUnchangedBut(counterName, "$PREFIX.messages.sent.topic.count")
149             }
150         }
151
152         on("$PREFIX.messages.sent.topic.count counter") {
153             val counterName = "$PREFIX.messages.sent.topic.count"
154             it("should handle counters for different topics") {
155                 cut.notifyMessageSent(topicName1)
156                 cut.notifyMessageSent(topicName2)
157                 cut.notifyMessageSent(topicName2)
158
159                 verifyCounter(registrySearch().name(counterName).tag("topic", topicName1)) { counter ->
160                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
161                 }
162
163                 verifyCounter(registrySearch().name(counterName).tag("topic", topicName2)) { counter ->
164                     assertThat(counter.count()).isCloseTo(2.0, doublePrecision)
165                 }
166             }
167         }
168     }
169
170     describe("processing gauge") {
171         it("should show difference between sent and received messages") {
172
173             on("positive difference") {
174                 cut.notifyMessageReceived(128)
175                 cut.notifyMessageReceived(256)
176                 cut.notifyMessageReceived(256)
177                 cut.notifyMessageSent("perf3gpp")
178                 verifyGauge("messages.processing.count") { gauge ->
179                     assertThat(gauge.value()).isCloseTo(2.0, doublePrecision)
180                 }
181             }
182
183             on("zero difference") {
184                 cut.notifyMessageReceived(128)
185                 cut.notifyMessageSent("perf3gpp")
186                 verifyGauge("messages.processing.count") { gauge ->
187                     assertThat(gauge.value()).isCloseTo(0.0, doublePrecision)
188                 }
189             }
190
191             on("negative difference") {
192                 cut.notifyMessageReceived(128)
193                 cut.notifyMessageSent("fault")
194                 cut.notifyMessageSent("perf3gpp")
195                 verifyGauge("messages.processing.count") { gauge ->
196                     assertThat(gauge.value()).isCloseTo(0.0, doublePrecision)
197                 }
198             }
199         }
200     }
201
202 })