f04636e9ee9433405ba11fd2c82244d400b62a6f
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
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
21 package org.onap.dcaegen2.services.sdk.standardization.moher.metrics.impl;
22
23 import io.micrometer.core.instrument.Counter;
24 import io.micrometer.prometheus.PrometheusMeterRegistry;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api.Metrics;
28 import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api.MetricsFactory;
29 import reactor.test.StepVerifier;
30
31 import java.time.Duration;
32
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34
35 class MetricsIT {
36
37     private static final String COUNTER_NAME = "Duplicates_Amount";
38     private static final String GAUGE_NAME = "gauge_Duplicates_Amount";
39     private static final String TIMER_NAME = "Duplicates_Amount_timer";
40     private static final String SUMMARY_NAME = "Duplicates_Amount_summary";
41     private static final Duration INTERVAL = Duration.ofMillis(100);
42
43     private PrometheusMeterRegistry defaultRegistry;
44     private Metrics cut;
45
46     @BeforeEach
47     void setup() {
48         defaultRegistry = MetricsFactory.createDefaultRegistry();
49         cut = MetricsFactory.createMetrics(defaultRegistry);
50     }
51
52     @Test
53     void metrics_givenDefaultRegistry_shouldReturnCreatedMeters() {
54
55         Counter counter = defaultRegistry.counter(COUNTER_NAME);
56         defaultRegistry.gauge(GAUGE_NAME, counter.count());
57         defaultRegistry.timer(TIMER_NAME);
58         defaultRegistry.summary(SUMMARY_NAME);
59
60         StepVerifier.create(cut.collect())
61                 .expectNextMatches((collectedMetrics) ->
62                         collectedMetrics.contains(COUNTER_NAME) &&
63                                 collectedMetrics.contains(GAUGE_NAME) &&
64                                 collectedMetrics.contains(TIMER_NAME) &&
65                                 collectedMetrics.contains(SUMMARY_NAME)
66                 )
67                 .verifyComplete();
68     }
69
70     @Test
71     void metrics_givenDefaultRegistry_shouldReturnCreatedMetersInIntervals() {
72
73         Counter counter = defaultRegistry.counter(COUNTER_NAME);
74
75         StepVerifier.create(
76                 cut.collect(INTERVAL).take(2)
77         )
78                 .consumeNextWith((collectedMetrics) -> {
79                     assertTrue(collectedMetrics.contains(COUNTER_NAME));
80                     counter.increment();
81                 })
82                 .thenAwait(INTERVAL)
83                 .expectNextMatches((collectedMetrics) ->
84                         collectedMetrics.contains(COUNTER_NAME + "_total 1.0"))
85                 .verifyComplete();
86     }
87 }