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
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.tests.utils
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
32 fun <T> PrometheusMeterRegistry.verifyGauge(name: String, verifier: (Gauge) -> T) =
33 verifyMeter(findMeter(name), RequiredSearch::gauge, verifier)
35 fun <T> PrometheusMeterRegistry.verifyTimer(name: String, verifier: (Timer) -> T) =
36 verifyMeter(findMeter(name), RequiredSearch::timer, verifier)
38 fun <T> PrometheusMeterRegistry.verifyCounter(name: String, verifier: (Counter) -> T) =
39 verifyCounter(findMeter(name), verifier)
41 fun <T> PrometheusMeterRegistry.verifyCounter(name: String, tags: Tags, verifier: (Counter) -> T) =
42 verifyCounter(findMeter(name).tags(tags), verifier)
44 private fun PrometheusMeterRegistry.findMeter(meterName: String) = RequiredSearch.`in`(this).name(meterName)
46 private fun <T> verifyCounter(search: RequiredSearch, verifier: (Counter) -> T) =
47 verifyMeter(search, RequiredSearch::counter, verifier)
49 private inline fun <M, T> verifyMeter(search: RequiredSearch,
50 map: (RequiredSearch) -> M,
52 Try { map(search) }.fold(
53 { ex -> Assertions.assertThat(ex).doesNotThrowAnyException() },