aboutsummaryrefslogtreecommitdiffstats
path: root/container-disc/src/test/java/com/yahoo/container/jdisc/metric/ForwardingMetricConsumerTest.java
blob: d03689b5540ab65b8f8cbd04087068959ca99c91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.metric;

import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.application.MetricConsumer;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * @author Simon Thoresen Hult
 */
public class ForwardingMetricConsumerTest {

    @Test
    void requireThatAllMethodsAreForwarded() {
        MetricConsumer fooConsumer = Mockito.mock(MetricConsumer.class);
        Metric.Context fooCtx = Mockito.mock(Metric.Context.class);
        Mockito.when(fooConsumer.createContext(Mockito.<Map<String, ?>>any())).thenReturn(fooCtx);

        MetricConsumer barConsumer = Mockito.mock(MetricConsumer.class);
        Metric.Context barCtx = Mockito.mock(Metric.Context.class);
        Mockito.when(barConsumer.createContext(Mockito.<Map<String, ?>>any())).thenReturn(barCtx);

        MetricConsumer fwdConsumer = new ForwardingMetricConsumer(new MetricConsumer[]{fooConsumer, barConsumer});

        Map<String, ?> properties = new HashMap<>();
        Metric.Context ctx = fwdConsumer.createContext(properties);
        assertNotNull(ctx);
        Mockito.verify(fooConsumer, Mockito.times(1)).createContext(properties);
        Mockito.verify(barConsumer, Mockito.times(1)).createContext(properties);

        fwdConsumer.add("a", 69, ctx);
        Mockito.verify(fooConsumer, Mockito.times(1)).add("a", 69, fooCtx);
        Mockito.verify(barConsumer, Mockito.times(1)).add("a", 69, barCtx);

        fwdConsumer.set("b", 96, ctx);
        Mockito.verify(fooConsumer, Mockito.times(1)).set("b", 96, fooCtx);
        Mockito.verify(barConsumer, Mockito.times(1)).set("b", 96, barCtx);
    }
}