aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/test/MetricMock.java
blob: 8dd023b5605e897ece7ef29bf03d8e5275d16b99 (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
46
47
48
49
50
51
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.test;

import com.yahoo.jdisc.Metric;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Simple mock for use whne testing metrics.
 *
 * @author bjorncs
 */
public class MetricMock implements Metric {
    public static class SimpleMetricContext implements Context {
        public final Map<String, String> dimensions;

        @SuppressWarnings("unchecked")
        SimpleMetricContext(Map<String, ?> dimensions) {
            this.dimensions = (Map<String, String>) dimensions;
        }
    }

    public static class Invocation {
        public final Number val;
        public final Context ctx;
        public Invocation(Number val, Context ctx) {
            this.val = val;
            this.ctx = ctx;
        }
    }

    private final Map<String, Invocation> addInvocations = new ConcurrentHashMap<>();

    public Map<String, Invocation> innvocations() {
        return addInvocations;
    }

    @Override
    public void add(String key, Number val, Context ctx) {
        addInvocations.put(key, new Invocation(val, ctx));
    }

    @Override
    public void set(String key, Number val, Context ctx) { addInvocations.put(key, new Invocation(val, ctx)); }

    @Override
    public Context createContext(Map<String, ?> properties) {
        return new SimpleMetricContext(properties);
    }
}