blob: 4be1a5cba2a13a14ab3cf655630e4d4146749b47 [file] [log] [blame]
Ray Milkey14c46512014-07-08 18:30:31 -07001package net.onrc.onos.api.rest;
2
3import com.codahale.metrics.Gauge;
4import com.codahale.metrics.MetricFilter;
Ray Milkey14c46512014-07-08 18:30:31 -07005import net.onrc.onos.core.metrics.OnosMetrics;
6import org.json.JSONArray;
7import org.json.JSONException;
8import org.json.JSONObject;
9import org.junit.After;
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
Ray Milkey14c46512014-07-08 18:30:31 -070013import org.powermock.modules.junit4.PowerMockRunner;
14import org.restlet.resource.ClientResource;
15
Ray Milkey14c46512014-07-08 18:30:31 -070016import static org.hamcrest.MatcherAssert.assertThat;
17import static org.hamcrest.Matchers.equalTo;
18import static org.hamcrest.Matchers.is;
19import static org.hamcrest.Matchers.notNullValue;
20
21/**
22 * Unit tests for filtering REST APIs for Timer Metrics.
23 */
24
25@RunWith(PowerMockRunner.class)
Ray Milkey14c46512014-07-08 18:30:31 -070026public class TestRestMetricsFilters extends TestRestMetrics {
27
28 /**
29 * Create the web server and mocks required for
30 * all of the tests.
31 */
32 @Before
33 @SuppressWarnings("ununsed")
34 public void beforeTest() {
35 setRestPort(generateRandomPort());
36 setUp();
37 // Make some test data
38 createMetrics();
39 }
40
41 /**
42 * Remove anything that will interfere with the next test running correctly.
43 * Shuts down the test REST web server and removes the mocks.
44 */
45 @After
46 @SuppressWarnings("unused")
47 public void afterTest() {
48 destroyMetrics();
49 tearDown();
50 }
51
52 // Test data objects
Ray Milkey71cd2c82014-07-16 15:02:33 -070053 private static final OnosMetrics.MetricsComponent COMPONENT =
54 OnosMetrics.registerComponent("MetricsUnitTests");
55 private static final OnosMetrics.MetricsFeature FEATURE =
56 COMPONENT.registerFeature("Filters");
57
Ray Milkey49d67be2014-07-10 13:47:01 -070058 private static final String TIMER1_NAME = "timer1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070059 private static final String TIMER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
60 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070061 "timer1");
62 private static final String TIMER2_NAME = "timer2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070063 private static final String TIMER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
64 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070065 "timer2");
66 private static final String TIMER3_NAME = "timer3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070067 private static final String TIMER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
68 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070069 "timer3");
Ray Milkey14c46512014-07-08 18:30:31 -070070
Ray Milkey49d67be2014-07-10 13:47:01 -070071 private static final String GAUGE1_NAME = "gauge1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070072 private static final String GAUGE1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
73 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070074 "gauge1");
75 private static final String GAUGE2_NAME = "gauge2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070076 private static final String GAUGE2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
77 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070078 "gauge2");
79 private static final String GAUGE3_NAME = "gauge3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070080 private static final String GAUGE3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
81 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070082 "gauge3");
Ray Milkey14c46512014-07-08 18:30:31 -070083
Ray Milkey49d67be2014-07-10 13:47:01 -070084 private static final String COUNTER1_NAME = "counter1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070085 private static final String COUNTER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
86 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070087 "counter1");
88 private static final String COUNTER2_NAME = "counter2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070089 private static final String COUNTER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
90 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070091 "counter2");
92 private static final String COUNTER3_NAME = "counter3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070093 private static final String COUNTER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
94 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070095 "counter3");
Ray Milkey14c46512014-07-08 18:30:31 -070096
Ray Milkey49d67be2014-07-10 13:47:01 -070097 private static final String METER1_NAME = "meter1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070098 private static final String METER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
99 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700100 "meter1");
101 private static final String METER2_NAME = "meter2";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700102 private static final String METER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
103 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700104 "meter2");
105 private static final String METER3_NAME = "meter3";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700106 private static final String METER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
107 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700108 "meter3");
Ray Milkey14c46512014-07-08 18:30:31 -0700109
Ray Milkey49d67be2014-07-10 13:47:01 -0700110 private static final String HISTOGRAM1_NAME = "histogram1";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700111 private static final String HISTOGRAM1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
112 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700113 "histogram1");
114 private static final String HISTOGRAM2_NAME = "histogram2";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700115 private static final String HISTOGRAM2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
116 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700117 "histogram2");
118 private static final String HISTOGRAM3_NAME = "histogram3";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700119 private static final String HISTOGRAM3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
120 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700121 "histogram3");
Ray Milkey14c46512014-07-08 18:30:31 -0700122
Ray Milkey128651a2014-07-14 11:24:28 -0700123 private final Gauge<Integer> testGauge = new Gauge<Integer>() {
Ray Milkey14c46512014-07-08 18:30:31 -0700124 @Override
125 public Integer getValue() {
126 return 1;
127 }
128 };
129
130 /**
131 * Creates Metrics objects for test.
132 */
133 private void createMetrics() {
Ray Milkey71cd2c82014-07-16 15:02:33 -0700134 OnosMetrics.createTimer(COMPONENT,
135 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700136 TIMER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700137 OnosMetrics.createTimer(COMPONENT,
138 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700139 TIMER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700140 OnosMetrics.createTimer(COMPONENT,
141 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700142 TIMER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700143
Ray Milkey71cd2c82014-07-16 15:02:33 -0700144 OnosMetrics.createCounter(COMPONENT,
145 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700146 COUNTER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700147 OnosMetrics.createCounter(COMPONENT,
148 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700149 COUNTER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700150 OnosMetrics.createCounter(COMPONENT,
151 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700152 COUNTER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700153
Ray Milkey71cd2c82014-07-16 15:02:33 -0700154 OnosMetrics.createMeter(COMPONENT,
155 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700156 METER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700157 OnosMetrics.createMeter(COMPONENT,
158 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700159 METER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700160 OnosMetrics.createMeter(COMPONENT,
161 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700162 METER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700163
Ray Milkey71cd2c82014-07-16 15:02:33 -0700164 OnosMetrics.createHistogram(COMPONENT,
165 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700166 HISTOGRAM1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700167 OnosMetrics.createHistogram(COMPONENT,
168 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700169 HISTOGRAM2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700170 OnosMetrics.createHistogram(COMPONENT,
171 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700172 HISTOGRAM3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700173
Ray Milkey71cd2c82014-07-16 15:02:33 -0700174 OnosMetrics.registerMetric(COMPONENT,
175 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700176 GAUGE1_NAME,
177 testGauge);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700178 OnosMetrics.registerMetric(COMPONENT,
179 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700180 GAUGE2_NAME,
181 testGauge);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700182 OnosMetrics.registerMetric(COMPONENT,
183 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700184 GAUGE3_NAME,
185 testGauge);
Ray Milkey14c46512014-07-08 18:30:31 -0700186 }
187
188 /**
189 * Removes the Metrics to clean up for the next test run.
190 */
191 private void destroyMetrics() {
Ray Milkey128651a2014-07-14 11:24:28 -0700192 OnosMetrics.removeMatching(MetricFilter.ALL);
Ray Milkey14c46512014-07-08 18:30:31 -0700193 }
194
195 /**
196 * Tests that query of non existant name returns nothing.
197 *
198 * @throws JSONException if any of the JSON processing fails.
199 */
200 @Test
201 public void testFilterMatchesNothing() throws JSONException {
202
203 // Read the metrics from the REST API for the test data
204 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
205 client.addQueryParameter("ids", "xyzzy");
206
207 final JSONObject metrics = getJSONObject(client);
208 assertThat(metrics.length(), is(equalTo(5)));
209
210 // There should be no timers, histograms, gauges, meters or counters
211 checkEmptyLists(metrics, "timers", "histograms", "gauges", "meters", "counters");
212
213 }
214
215 /**
216 * Tests that query of multiple metrics of a single Metric type
217 * returns the proper data.
218 *
219 * @throws JSONException if any of the JSON processing fails.
220 */
221 @Test
222 public void testMultipleFilterSingleType() throws JSONException {
223
224 // Read the metrics from the REST API for the test data
225 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700226 client.addQueryParameter("ids", TIMER1_FULL_NAME + "," + TIMER2_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700227
228 final JSONObject metrics = getJSONObject(client);
229 assertThat(metrics.length(), is(equalTo(5)));
230
231 // There should be 2 timer that match the filter
232 final JSONArray timers = metrics.getJSONArray("timers");
233 assertThat(timers, is(notNullValue()));
234 assertThat(timers.length(), is(2));
235
236 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700237 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700238
239 final JSONObject jsonTimer2 = timers.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700240 assertThat(jsonTimer2.getString("name"), is(equalTo(TIMER2_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700241
242 // There should be no histograms, gauges, meters or counters
243 checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters");
244 }
245
246 /**
247 * Tests that query of a single metric retunrs just that metric.
248 *
249 * @throws JSONException if any of the JSON processing fails.
250 */
251 @Test
252 public void testSingleFilter() throws JSONException {
253
254 // Read the metrics from the REST API for the test data
255 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700256 client.addQueryParameter("ids", TIMER1_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700257
258 final JSONObject metrics = getJSONObject(client);
259 assertThat(metrics.length(), is(equalTo(5)));
260
261 // There should be 1 timer that matches the filter
262 final JSONArray timers = metrics.getJSONArray("timers");
263 assertThat(timers, is(notNullValue()));
264 assertThat(timers.length(), is(1));
265
266 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700267 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700268
269
270 // There should be no histograms, gauges, meters or counters
271 checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters");
272 }
273
274 /**
275 * Tests that query of multiple metrics of multiple metric types returns
276 * the proper data.
277 *
278 * @throws JSONException if any of the JSON processing fails.
279 */
280 @Test
281 public void testMultipleFiltersMultipleTypes() throws JSONException {
282
283 // Read the metrics from the REST API for the test data
284 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700285 client.addQueryParameter("ids",
286 TIMER1_FULL_NAME + "," +
287 GAUGE2_FULL_NAME + "," +
288 HISTOGRAM3_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700289
290 final JSONObject metrics = getJSONObject(client);
291 assertThat(metrics.length(), is(equalTo(5)));
292
293 // There should be 1 timer that matches the filter
294 final JSONArray timers = metrics.getJSONArray("timers");
295 assertThat(timers, is(notNullValue()));
296 assertThat(timers.length(), is(1));
297
298 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700299 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700300
301 // There should be 1 gauge that matches the filter
302 final JSONArray gauges = metrics.getJSONArray("gauges");
303 assertThat(gauges, is(notNullValue()));
304 assertThat(gauges.length(), is(1));
305
306 final JSONObject jsonGauge1 = gauges.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700307 assertThat(jsonGauge1.getString("name"), is(equalTo(GAUGE2_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700308
309 // There should be 1 histogram that matches the filter
310 final JSONArray histograms = metrics.getJSONArray("histograms");
311 assertThat(histograms, is(notNullValue()));
312 assertThat(histograms.length(), is(1));
313
314 final JSONObject jsonHistogram1 = histograms.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700315 assertThat(jsonHistogram1.getString("name"), is(equalTo(HISTOGRAM3_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700316
317 // There should be no meters or counters
318 checkEmptyLists(metrics, "meters", "counters");
319 }
320
321
322}