Custom detekt rule for logger usage check
[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 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
33
34 /**
35  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
36  * @since June 2018
37  */
38 object MicrometerMetricsTest : Spek({
39     val doublePrecision = Percentage.withPercentage(0.5)
40     lateinit var registry: SimpleMeterRegistry
41     lateinit var cut: MicrometerMetrics
42
43     beforeEachTest {
44         registry = SimpleMeterRegistry()
45         cut = MicrometerMetrics(registry)
46     }
47
48     fun registrySearch() = RequiredSearch.`in`(registry)
49
50     fun <M, T> verifyMeter(search: RequiredSearch, map: (RequiredSearch) -> M, verifier: (M) -> T) =
51             Try {
52                 map(search)
53             }.fold(
54                     { ex -> assertThat(ex).doesNotThrowAnyException() },
55                     verifier
56             )
57
58     fun <T> verifyGauge(name: String, verifier: (Gauge) -> T) =
59             verifyMeter(registrySearch().name(name), RequiredSearch::gauge, verifier)
60
61     fun <T> verifyCounter(search: RequiredSearch, verifier: (Counter) -> T) =
62             verifyMeter(search, RequiredSearch::counter, verifier)
63
64     fun <T> verifyCounter(name: String, verifier: (Counter) -> T) =
65             verifyCounter(registrySearch().name(name), verifier)
66
67     fun verifyAllCountersAreUnchangedBut(vararg changedCounters: String) {
68         registry.meters
69                 .filter { it is Counter }
70                 .filterNot { it.id.name in changedCounters }
71                 .forEach { assertThat((it as Counter).count()).isCloseTo(0.0, doublePrecision) }
72     }
73
74     describe("notifyBytesReceived") {
75
76         on("data.received.bytes counter") {
77             val counterName = "data.received.bytes"
78
79             it("should increment counter") {
80                 val bytes = 128
81                 cut.notifyBytesReceived(bytes)
82
83                 verifyCounter(counterName) { counter ->
84                     assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
85                 }
86             }
87
88             it("should leave all other counters unchanged") {
89                 cut.notifyBytesReceived(128)
90                 verifyAllCountersAreUnchangedBut(counterName)
91             }
92         }
93     }
94
95     describe("notifyMessageReceived") {
96         on("messages.received.count counter") {
97             val counterName = "messages.received.count"
98
99             it("should increment counter") {
100                 cut.notifyMessageReceived(777)
101
102                 verifyCounter(counterName) { counter ->
103                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
104                 }
105             }
106         }
107
108         on("messages.received.bytes counter") {
109             val counterName = "messages.received.bytes"
110
111             it("should increment counter") {
112                 val bytes = 888
113                 cut.notifyMessageReceived(bytes)
114
115                 verifyCounter(counterName) { counter ->
116                     assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
117                 }
118             }
119         }
120
121         it("should leave all other counters unchanged") {
122             cut.notifyMessageReceived(128)
123             verifyAllCountersAreUnchangedBut("messages.received.count", "messages.received.bytes")
124         }
125     }
126
127     describe("notifyMessageSent") {
128         val topicName = "dmaap_topic_name"
129         val counterName = "messages.sent.count"
130
131         on("$counterName counter") {
132
133             it("should increment counter") {
134                 cut.notifyMessageSent(topicName)
135
136                 verifyCounter(counterName) { counter ->
137                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
138                 }
139             }
140         }
141
142         on("$counterName[topic=$topicName] counter") {
143
144             it("should increment counter") {
145                 cut.notifyMessageSent(topicName)
146
147                 verifyCounter(registrySearch().name(counterName).tag("topic", topicName)) { counter ->
148                     assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
149                 }
150             }
151         }
152
153         it("should leave all other counters unchanged") {
154             cut.notifyMessageSent(topicName)
155             verifyAllCountersAreUnchangedBut(counterName)
156         }
157     }
158
159     describe("processing gauge") {
160         it("should show difference between sent and received messages") {
161
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)
169                 }
170             }
171
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)
177                 }
178             }
179
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)
186                 }
187             }
188         }
189     }
190
191 })