blob: 5ecaf28d446e3244fdcd94c3051423b2d9217345 [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 /**
52 * Tears down graphite metrics reporter instance.
53 */
54 public void tearDown() {
55 gmr.deactivate();
56 }
57
58 /**
59 * Tests whether the containsName method can always return the correct result
60 * with the given metric name and a set of prefixes.
61 */
62 @Test
63 public void testContainsName() {
64 assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES1));
65 assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES3));
66 assertTrue(gmr.containsName(METRIC_NAME1, PREFIXES4));
67 assertTrue(gmr.containsName(METRIC_NAME2, PREFIXES4));
68 assertTrue(gmr.containsName(METRIC_NAME3, PREFIXES4));
69 assertTrue(gmr.containsName(METRIC_NAME4, PREFIXES4));
70 assertFalse(gmr.containsName(METRIC_NAME1, PREFIXES2));
71 }
72
73 /**
74 * Tests whether the filter method can always return the correct result.
75 */
76 @Test
77 public void testFilter() {
78 MetricRegistry filtered;
79 MetricRegistry full = new MetricRegistry();
80 full.meter(METRIC_NAME1);
81 full.meter(METRIC_NAME2);
82 full.meter(METRIC_NAME3);
83 full.meter(METRIC_NAME4);
84
85 gmr.monitorAll = true;
86 filtered = gmr.filter(full);
87
88 assertTrue(filtered.getNames()
89 .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2,
90 METRIC_NAME3, METRIC_NAME4)));
91
92 gmr.monitorAll = false;
93 gmr.metricNames = PREFIXES1;
94 filtered = gmr.filter(full);
95
96 assertTrue(filtered.getNames()
97 .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2)));
98 assertFalse(filtered.getNames()
99 .containsAll(ImmutableSet.of(METRIC_NAME3, METRIC_NAME4)));
100
101 gmr.metricNames = PREFIXES2;
102 filtered = gmr.filter(full);
103
104 assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1)));
105
106 gmr.metricNames = PREFIXES3;
107 filtered = gmr.filter(full);
108
109 assertTrue(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME1)));
110 assertFalse(filtered.getNames().containsAll(ImmutableSet.of(METRIC_NAME2)));
111
112 gmr.metricNames = PREFIXES4;
113 filtered = gmr.filter(full);
114
115 assertTrue(filtered.getNames()
116 .containsAll(ImmutableSet.of(METRIC_NAME1, METRIC_NAME2,
117 METRIC_NAME3, METRIC_NAME4)));
118 }
sangyun-han805219a2016-03-26 12:58:27 +0900119}