blob: 5036964a39be89793c443242bdaf160f6bfadd0b [file] [log] [blame]
sangyun-han805219a2016-03-26 12:58:27 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sangyun-han805219a2016-03-26 12:58:27 +09003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.graphitemetrics;
17
sangyun-han805219a2016-03-26 12:58:27 +090018import com.codahale.metrics.MetricRegistry;
sangyun-han805219a2016-03-26 12:58:27 +090019import com.google.common.collect.ImmutableSet;
20import org.junit.Before;
21import org.junit.Test;
sangyun-han805219a2016-03-26 12:58:27 +090022
23import static org.junit.Assert.assertFalse;
24import static org.junit.Assert.assertTrue;
sangyun-han805219a2016-03-26 12:58:27 +090025
26/**
27 * Unit test for metrics reporter of graphite.
28 */
29public class GraphiteMetricsReporterTest {
30
Jian Li55cbd5c2016-04-06 09:50:20 -070031 private DefaultGraphiteMetricsReporter gmr;
sangyun-han805219a2016-03-26 12:58:27 +090032
33 private static final String METRIC_NAME1 = "consistentMap.onos-app-ids.putIfAbsent";
34 private static final String METRIC_NAME2 = "consistentMap.onos-hosts.entrySet";
35 private static final String METRIC_NAME3 = "clusterCommunication.endpoint.*";
36 private static final String METRIC_NAME4 = "atomicCounter.onos-app-id-counter.*";
37
38 private static final String PREFIXES1 = "consistentMap";
39 private static final String PREFIXES2 = "topology";
40 private static final String PREFIXES3 = "consistentMap.onos-app-ids";
41 private static final String PREFIXES4 = "consistentMap, clusterCommunication, atomicCounter";
42
43 /**
44 * Sets up graphite metrics reporter instance.
45 */
46 @Before
47 public void setUp() {
Jian Li55cbd5c2016-04-06 09:50:20 -070048 gmr = new DefaultGraphiteMetricsReporter();
sangyun-han805219a2016-03-26 12:58:27 +090049 }
50
51 /**
sangyun-han805219a2016-03-26 12:58:27 +090052 * Tests whether the containsName method can always return the correct result
53 * with the given metric name and a set of prefixes.
54 */
55 @Test
56 public void testContainsName() {
57 assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES1));
58 assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES3));
59 assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES4));
60 assertTrue(gmr.containsName(METRIC_NAME2, PREFIXES4));
61 assertTrue(gmr.containsName(METRIC_NAME3, PREFIXES4));
62 assertTrue(gmr.containsName(METRIC_NAME4, PREFIXES4));
63 assertFalse(gmr.containsName(METRIC_NAME1, PREFIXES2));
64 }
65
66 /**
67 * Tests whether the filter method can always return the correct result.
68 */
69 @Test
70 public void testFilter() {
71 MetricRegistry filtered;
72 MetricRegistry full = new MetricRegistry();
73 full.meter(METRIC_NAME1);
74 full.meter(METRIC_NAME2);
75 full.meter(METRIC_NAME3);
76 full.meter(METRIC_NAME4);
77
78 gmr.monitorAll = true;
79 filtered = gmr.filter(full);
80
81 assertTrue(filtered.getNames()
82 .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2,
83 METRIC_NAME3, METRIC_NAME4)));
84
85 gmr.monitorAll = false;
86 gmr.metricNames = PREFIXES1;
87 filtered = gmr.filter(full);
88
89 assertTrue(filtered.getNames()
90 .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2)));
91 assertFalse(filtered.getNames()
92 .containsAll(ImmutableSet.of(METRIC_NAME3, METRIC_NAME4)));
93
94 gmr.metricNames = PREFIXES2;
95 filtered = gmr.filter(full);
96
97 assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1)));
98
99 gmr.metricNames = PREFIXES3;
100 filtered = gmr.filter(full);
101
102 assertTrue(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1)));
103 assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME2)));
104
105 gmr.metricNames = PREFIXES4;
106 filtered = gmr.filter(full);
107
108 assertTrue(filtered.getNames()
109 .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2,
110 METRIC_NAME3, METRIC_NAME4)));
111 }
sangyun-han805219a2016-03-26 12:58:27 +0900112}