blob: 7e65a55196ce4f4bdf3447e8ead28d44f4c0a8e5 [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;
5import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
6import net.onrc.onos.core.metrics.OnosMetrics;
7import org.json.JSONArray;
8import org.json.JSONException;
9import org.json.JSONObject;
10import org.junit.After;
11import org.junit.Before;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.powermock.core.classloader.annotations.PrepareForTest;
15import org.powermock.modules.junit4.PowerMockRunner;
16import org.restlet.resource.ClientResource;
17
Ray Milkey14c46512014-07-08 18:30:31 -070018import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.equalTo;
20import static org.hamcrest.Matchers.is;
21import static org.hamcrest.Matchers.notNullValue;
22
23/**
24 * Unit tests for filtering REST APIs for Timer Metrics.
25 */
26
27@RunWith(PowerMockRunner.class)
28@PrepareForTest(PathCalcRuntimeModule.class)
29public class TestRestMetricsFilters extends TestRestMetrics {
30
31 /**
32 * Create the web server and mocks required for
33 * all of the tests.
34 */
35 @Before
36 @SuppressWarnings("ununsed")
37 public void beforeTest() {
38 setRestPort(generateRandomPort());
39 setUp();
40 // Make some test data
41 createMetrics();
42 }
43
44 /**
45 * Remove anything that will interfere with the next test running correctly.
46 * Shuts down the test REST web server and removes the mocks.
47 */
48 @After
49 @SuppressWarnings("unused")
50 public void afterTest() {
51 destroyMetrics();
52 tearDown();
53 }
54
55 // Test data objects
Ray Milkey71cd2c82014-07-16 15:02:33 -070056 private static final OnosMetrics.MetricsComponent COMPONENT =
57 OnosMetrics.registerComponent("MetricsUnitTests");
58 private static final OnosMetrics.MetricsFeature FEATURE =
59 COMPONENT.registerFeature("Filters");
60
Ray Milkey49d67be2014-07-10 13:47:01 -070061 private static final String TIMER1_NAME = "timer1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070062 private static final String TIMER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
63 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070064 "timer1");
65 private static final String TIMER2_NAME = "timer2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070066 private static final String TIMER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
67 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070068 "timer2");
69 private static final String TIMER3_NAME = "timer3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070070 private static final String TIMER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
71 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070072 "timer3");
Ray Milkey14c46512014-07-08 18:30:31 -070073
Ray Milkey49d67be2014-07-10 13:47:01 -070074 private static final String GAUGE1_NAME = "gauge1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070075 private static final String GAUGE1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
76 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070077 "gauge1");
78 private static final String GAUGE2_NAME = "gauge2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070079 private static final String GAUGE2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
80 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070081 "gauge2");
82 private static final String GAUGE3_NAME = "gauge3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070083 private static final String GAUGE3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
84 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070085 "gauge3");
Ray Milkey14c46512014-07-08 18:30:31 -070086
Ray Milkey49d67be2014-07-10 13:47:01 -070087 private static final String COUNTER1_NAME = "counter1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070088 private static final String COUNTER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
89 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070090 "counter1");
91 private static final String COUNTER2_NAME = "counter2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070092 private static final String COUNTER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
93 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070094 "counter2");
95 private static final String COUNTER3_NAME = "counter3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070096 private static final String COUNTER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
97 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070098 "counter3");
Ray Milkey14c46512014-07-08 18:30:31 -070099
Ray Milkey49d67be2014-07-10 13:47:01 -0700100 private static final String METER1_NAME = "meter1";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700101 private static final String METER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
102 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700103 "meter1");
104 private static final String METER2_NAME = "meter2";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700105 private static final String METER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
106 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700107 "meter2");
108 private static final String METER3_NAME = "meter3";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700109 private static final String METER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
110 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700111 "meter3");
Ray Milkey14c46512014-07-08 18:30:31 -0700112
Ray Milkey49d67be2014-07-10 13:47:01 -0700113 private static final String HISTOGRAM1_NAME = "histogram1";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700114 private static final String HISTOGRAM1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
115 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700116 "histogram1");
117 private static final String HISTOGRAM2_NAME = "histogram2";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700118 private static final String HISTOGRAM2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
119 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700120 "histogram2");
121 private static final String HISTOGRAM3_NAME = "histogram3";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700122 private static final String HISTOGRAM3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
123 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700124 "histogram3");
Ray Milkey14c46512014-07-08 18:30:31 -0700125
Ray Milkey128651a2014-07-14 11:24:28 -0700126 private final Gauge<Integer> testGauge = new Gauge<Integer>() {
Ray Milkey14c46512014-07-08 18:30:31 -0700127 @Override
128 public Integer getValue() {
129 return 1;
130 }
131 };
132
133 /**
134 * Creates Metrics objects for test.
135 */
136 private void createMetrics() {
Ray Milkey71cd2c82014-07-16 15:02:33 -0700137 OnosMetrics.createTimer(COMPONENT,
138 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700139 TIMER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700140 OnosMetrics.createTimer(COMPONENT,
141 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700142 TIMER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700143 OnosMetrics.createTimer(COMPONENT,
144 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700145 TIMER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700146
Ray Milkey71cd2c82014-07-16 15:02:33 -0700147 OnosMetrics.createCounter(COMPONENT,
148 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700149 COUNTER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700150 OnosMetrics.createCounter(COMPONENT,
151 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700152 COUNTER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700153 OnosMetrics.createCounter(COMPONENT,
154 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700155 COUNTER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700156
Ray Milkey71cd2c82014-07-16 15:02:33 -0700157 OnosMetrics.createMeter(COMPONENT,
158 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700159 METER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700160 OnosMetrics.createMeter(COMPONENT,
161 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700162 METER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700163 OnosMetrics.createMeter(COMPONENT,
164 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700165 METER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700166
Ray Milkey71cd2c82014-07-16 15:02:33 -0700167 OnosMetrics.createHistogram(COMPONENT,
168 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700169 HISTOGRAM1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700170 OnosMetrics.createHistogram(COMPONENT,
171 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700172 HISTOGRAM2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700173 OnosMetrics.createHistogram(COMPONENT,
174 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700175 HISTOGRAM3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700176
Ray Milkey71cd2c82014-07-16 15:02:33 -0700177 OnosMetrics.registerMetric(COMPONENT,
178 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700179 GAUGE1_NAME,
180 testGauge);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700181 OnosMetrics.registerMetric(COMPONENT,
182 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700183 GAUGE2_NAME,
184 testGauge);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700185 OnosMetrics.registerMetric(COMPONENT,
186 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700187 GAUGE3_NAME,
188 testGauge);
Ray Milkey14c46512014-07-08 18:30:31 -0700189 }
190
191 /**
192 * Removes the Metrics to clean up for the next test run.
193 */
194 private void destroyMetrics() {
Ray Milkey128651a2014-07-14 11:24:28 -0700195 OnosMetrics.removeMatching(MetricFilter.ALL);
Ray Milkey14c46512014-07-08 18:30:31 -0700196 }
197
198 /**
199 * Tests that query of non existant name returns nothing.
200 *
201 * @throws JSONException if any of the JSON processing fails.
202 */
203 @Test
204 public void testFilterMatchesNothing() throws JSONException {
205
206 // Read the metrics from the REST API for the test data
207 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
208 client.addQueryParameter("ids", "xyzzy");
209
210 final JSONObject metrics = getJSONObject(client);
211 assertThat(metrics.length(), is(equalTo(5)));
212
213 // There should be no timers, histograms, gauges, meters or counters
214 checkEmptyLists(metrics, "timers", "histograms", "gauges", "meters", "counters");
215
216 }
217
218 /**
219 * Tests that query of multiple metrics of a single Metric type
220 * returns the proper data.
221 *
222 * @throws JSONException if any of the JSON processing fails.
223 */
224 @Test
225 public void testMultipleFilterSingleType() throws JSONException {
226
227 // Read the metrics from the REST API for the test data
228 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700229 client.addQueryParameter("ids", TIMER1_FULL_NAME + "," + TIMER2_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700230
231 final JSONObject metrics = getJSONObject(client);
232 assertThat(metrics.length(), is(equalTo(5)));
233
234 // There should be 2 timer that match the filter
235 final JSONArray timers = metrics.getJSONArray("timers");
236 assertThat(timers, is(notNullValue()));
237 assertThat(timers.length(), is(2));
238
239 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700240 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700241
242 final JSONObject jsonTimer2 = timers.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700243 assertThat(jsonTimer2.getString("name"), is(equalTo(TIMER2_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700244
245 // There should be no histograms, gauges, meters or counters
246 checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters");
247 }
248
249 /**
250 * Tests that query of a single metric retunrs just that metric.
251 *
252 * @throws JSONException if any of the JSON processing fails.
253 */
254 @Test
255 public void testSingleFilter() throws JSONException {
256
257 // Read the metrics from the REST API for the test data
258 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700259 client.addQueryParameter("ids", TIMER1_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700260
261 final JSONObject metrics = getJSONObject(client);
262 assertThat(metrics.length(), is(equalTo(5)));
263
264 // There should be 1 timer that matches the filter
265 final JSONArray timers = metrics.getJSONArray("timers");
266 assertThat(timers, is(notNullValue()));
267 assertThat(timers.length(), is(1));
268
269 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700270 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700271
272
273 // There should be no histograms, gauges, meters or counters
274 checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters");
275 }
276
277 /**
278 * Tests that query of multiple metrics of multiple metric types returns
279 * the proper data.
280 *
281 * @throws JSONException if any of the JSON processing fails.
282 */
283 @Test
284 public void testMultipleFiltersMultipleTypes() throws JSONException {
285
286 // Read the metrics from the REST API for the test data
287 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700288 client.addQueryParameter("ids",
289 TIMER1_FULL_NAME + "," +
290 GAUGE2_FULL_NAME + "," +
291 HISTOGRAM3_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700292
293 final JSONObject metrics = getJSONObject(client);
294 assertThat(metrics.length(), is(equalTo(5)));
295
296 // There should be 1 timer that matches the filter
297 final JSONArray timers = metrics.getJSONArray("timers");
298 assertThat(timers, is(notNullValue()));
299 assertThat(timers.length(), is(1));
300
301 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700302 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700303
304 // There should be 1 gauge that matches the filter
305 final JSONArray gauges = metrics.getJSONArray("gauges");
306 assertThat(gauges, is(notNullValue()));
307 assertThat(gauges.length(), is(1));
308
309 final JSONObject jsonGauge1 = gauges.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700310 assertThat(jsonGauge1.getString("name"), is(equalTo(GAUGE2_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700311
312 // There should be 1 histogram that matches the filter
313 final JSONArray histograms = metrics.getJSONArray("histograms");
314 assertThat(histograms, is(notNullValue()));
315 assertThat(histograms.length(), is(1));
316
317 final JSONObject jsonHistogram1 = histograms.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700318 assertThat(jsonHistogram1.getString("name"), is(equalTo(HISTOGRAM3_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700319
320 // There should be no meters or counters
321 checkEmptyLists(metrics, "meters", "counters");
322 }
323
324
325}