1aefdb3446e18fdba03125f8cbbbe17a8ace8988
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2019 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.utils
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.Tags
26 import io.micrometer.core.instrument.Timer
27 import io.micrometer.core.instrument.search.RequiredSearch
28 import io.micrometer.prometheus.PrometheusMeterRegistry
29 import org.assertj.core.api.Assertions
30
31
32 fun <T> PrometheusMeterRegistry.verifyGauge(name: String, verifier: (Gauge) -> T) =
33         verifyMeter(findMeter(name), RequiredSearch::gauge, verifier)
34
35 fun <T> PrometheusMeterRegistry.verifyTimer(name: String, verifier: (Timer) -> T) =
36         verifyMeter(findMeter(name), RequiredSearch::timer, verifier)
37
38 fun <T> PrometheusMeterRegistry.verifyCounter(name: String, verifier: (Counter) -> T) =
39         verifyCounter(findMeter(name), verifier)
40
41 fun <T> PrometheusMeterRegistry.verifyCounter(name: String, tags: Tags, verifier: (Counter) -> T) =
42         verifyCounter(findMeter(name).tags(tags), verifier)
43
44 private fun PrometheusMeterRegistry.findMeter(meterName: String) = RequiredSearch.`in`(this).name(meterName)
45
46 private fun <T> verifyCounter(search: RequiredSearch, verifier: (Counter) -> T) =
47         verifyMeter(search, RequiredSearch::counter, verifier)
48
49 private inline fun <M, T> verifyMeter(search: RequiredSearch,
50                                map: (RequiredSearch) -> M,
51                                verifier: (M) -> T) =
52         Try { map(search) }.fold(
53                 { ex -> Assertions.assertThat(ex).doesNotThrowAnyException() },
54                 verifier
55         )