Jian Li | 7261c7b | 2016-03-05 00:04:55 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 7261c7b | 2016-03-05 00:04:55 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
Jian Li | c0fe42d | 2016-03-28 15:19:16 -0700 | [diff] [blame] | 16 | package org.onosproject.gangliametrics; |
Jian Li | 7261c7b | 2016-03-05 00:04:55 -0800 | [diff] [blame] | 17 | |
| 18 | import com.codahale.metrics.MetricRegistry; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
| 26 | /** |
| 27 | * Unit test for metrics reporter of ganglia. |
| 28 | */ |
| 29 | public class GangliaMetricsReporterTest { |
| 30 | |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 31 | private DefaultGangliaMetricsReporter gmr; |
Jian Li | 7261c7b | 2016-03-05 00:04:55 -0800 | [diff] [blame] | 32 | |
| 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 | * Initializes ganglia metrics reporter instance. |
| 45 | */ |
| 46 | @Before |
| 47 | public void setUp() { |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 48 | gmr = new DefaultGangliaMetricsReporter(); |
Jian Li | 7261c7b | 2016-03-05 00:04:55 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * 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 | } |
| 112 | } |