blob: caded727be9d7063ce78fad31ade7d6e4894694a [file] [log] [blame]
Ray Milkey14c46512014-07-08 18:30:31 -07001package net.onrc.onos.api.rest;
2
3import com.codahale.metrics.Gauge;
Ray Milkey14c46512014-07-08 18:30:31 -07004import net.onrc.onos.core.metrics.OnosMetrics;
5import org.json.JSONArray;
6import org.json.JSONException;
7import org.json.JSONObject;
Ray Milkey14c46512014-07-08 18:30:31 -07008import org.junit.Before;
9import org.junit.Test;
Ray Milkey14c46512014-07-08 18:30:31 -070010import org.restlet.resource.ClientResource;
11
Ray Milkey14c46512014-07-08 18:30:31 -070012import static org.hamcrest.MatcherAssert.assertThat;
13import static org.hamcrest.Matchers.equalTo;
14import static org.hamcrest.Matchers.is;
15import static org.hamcrest.Matchers.notNullValue;
16
17/**
18 * Unit tests for filtering REST APIs for Timer Metrics.
19 */
Ray Milkey14c46512014-07-08 18:30:31 -070020public class TestRestMetricsFilters extends TestRestMetrics {
21
22 /**
23 * Create the web server and mocks required for
24 * all of the tests.
25 */
26 @Before
27 @SuppressWarnings("ununsed")
28 public void beforeTest() {
Ray Milkey14c46512014-07-08 18:30:31 -070029 // Make some test data
30 createMetrics();
31 }
32
Ray Milkey14c46512014-07-08 18:30:31 -070033
34 // Test data objects
Ray Milkey71cd2c82014-07-16 15:02:33 -070035 private static final OnosMetrics.MetricsComponent COMPONENT =
36 OnosMetrics.registerComponent("MetricsUnitTests");
37 private static final OnosMetrics.MetricsFeature FEATURE =
38 COMPONENT.registerFeature("Filters");
39
Ray Milkey49d67be2014-07-10 13:47:01 -070040 private static final String TIMER1_NAME = "timer1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070041 private static final String TIMER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
42 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070043 "timer1");
44 private static final String TIMER2_NAME = "timer2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070045 private static final String TIMER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
46 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070047 "timer2");
48 private static final String TIMER3_NAME = "timer3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070049 private static final String TIMER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
50 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070051 "timer3");
Ray Milkey14c46512014-07-08 18:30:31 -070052
Ray Milkey49d67be2014-07-10 13:47:01 -070053 private static final String GAUGE1_NAME = "gauge1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070054 private static final String GAUGE1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
55 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070056 "gauge1");
57 private static final String GAUGE2_NAME = "gauge2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070058 private static final String GAUGE2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
59 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070060 "gauge2");
61 private static final String GAUGE3_NAME = "gauge3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070062 private static final String GAUGE3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
63 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070064 "gauge3");
Ray Milkey14c46512014-07-08 18:30:31 -070065
Ray Milkey49d67be2014-07-10 13:47:01 -070066 private static final String COUNTER1_NAME = "counter1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070067 private static final String COUNTER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
68 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070069 "counter1");
70 private static final String COUNTER2_NAME = "counter2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070071 private static final String COUNTER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
72 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070073 "counter2");
74 private static final String COUNTER3_NAME = "counter3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070075 private static final String COUNTER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
76 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070077 "counter3");
Ray Milkey14c46512014-07-08 18:30:31 -070078
Ray Milkey49d67be2014-07-10 13:47:01 -070079 private static final String METER1_NAME = "meter1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070080 private static final String METER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
81 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070082 "meter1");
83 private static final String METER2_NAME = "meter2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070084 private static final String METER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
85 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070086 "meter2");
87 private static final String METER3_NAME = "meter3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070088 private static final String METER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
89 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070090 "meter3");
Ray Milkey14c46512014-07-08 18:30:31 -070091
Ray Milkey49d67be2014-07-10 13:47:01 -070092 private static final String HISTOGRAM1_NAME = "histogram1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070093 private static final String HISTOGRAM1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
94 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070095 "histogram1");
96 private static final String HISTOGRAM2_NAME = "histogram2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070097 private static final String HISTOGRAM2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
98 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070099 "histogram2");
100 private static final String HISTOGRAM3_NAME = "histogram3";
Ray Milkey71cd2c82014-07-16 15:02:33 -0700101 private static final String HISTOGRAM3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
102 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700103 "histogram3");
Ray Milkey14c46512014-07-08 18:30:31 -0700104
Ray Milkey128651a2014-07-14 11:24:28 -0700105 private final Gauge<Integer> testGauge = new Gauge<Integer>() {
Ray Milkey14c46512014-07-08 18:30:31 -0700106 @Override
107 public Integer getValue() {
108 return 1;
109 }
110 };
111
112 /**
113 * Creates Metrics objects for test.
114 */
115 private void createMetrics() {
Ray Milkey71cd2c82014-07-16 15:02:33 -0700116 OnosMetrics.createTimer(COMPONENT,
117 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700118 TIMER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700119 OnosMetrics.createTimer(COMPONENT,
120 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700121 TIMER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700122 OnosMetrics.createTimer(COMPONENT,
123 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700124 TIMER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700125
Ray Milkey71cd2c82014-07-16 15:02:33 -0700126 OnosMetrics.createCounter(COMPONENT,
127 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700128 COUNTER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700129 OnosMetrics.createCounter(COMPONENT,
130 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700131 COUNTER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700132 OnosMetrics.createCounter(COMPONENT,
133 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700134 COUNTER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700135
Ray Milkey71cd2c82014-07-16 15:02:33 -0700136 OnosMetrics.createMeter(COMPONENT,
137 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700138 METER1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700139 OnosMetrics.createMeter(COMPONENT,
140 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700141 METER2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700142 OnosMetrics.createMeter(COMPONENT,
143 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700144 METER3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700145
Ray Milkey71cd2c82014-07-16 15:02:33 -0700146 OnosMetrics.createHistogram(COMPONENT,
147 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700148 HISTOGRAM1_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700149 OnosMetrics.createHistogram(COMPONENT,
150 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700151 HISTOGRAM2_NAME);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700152 OnosMetrics.createHistogram(COMPONENT,
153 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700154 HISTOGRAM3_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700155
Ray Milkey71cd2c82014-07-16 15:02:33 -0700156 OnosMetrics.registerMetric(COMPONENT,
157 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700158 GAUGE1_NAME,
159 testGauge);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700160 OnosMetrics.registerMetric(COMPONENT,
161 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700162 GAUGE2_NAME,
163 testGauge);
Ray Milkey71cd2c82014-07-16 15:02:33 -0700164 OnosMetrics.registerMetric(COMPONENT,
165 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700166 GAUGE3_NAME,
167 testGauge);
Ray Milkey14c46512014-07-08 18:30:31 -0700168 }
169
170 /**
Ray Milkey14c46512014-07-08 18:30:31 -0700171 * Tests that query of non existant name returns nothing.
172 *
173 * @throws JSONException if any of the JSON processing fails.
174 */
175 @Test
176 public void testFilterMatchesNothing() throws JSONException {
177
178 // Read the metrics from the REST API for the test data
179 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
180 client.addQueryParameter("ids", "xyzzy");
181
182 final JSONObject metrics = getJSONObject(client);
183 assertThat(metrics.length(), is(equalTo(5)));
184
185 // There should be no timers, histograms, gauges, meters or counters
186 checkEmptyLists(metrics, "timers", "histograms", "gauges", "meters", "counters");
187
188 }
189
190 /**
191 * Tests that query of multiple metrics of a single Metric type
192 * returns the proper data.
193 *
194 * @throws JSONException if any of the JSON processing fails.
195 */
196 @Test
197 public void testMultipleFilterSingleType() throws JSONException {
198
199 // Read the metrics from the REST API for the test data
200 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700201 client.addQueryParameter("ids", TIMER1_FULL_NAME + "," + TIMER2_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700202
203 final JSONObject metrics = getJSONObject(client);
204 assertThat(metrics.length(), is(equalTo(5)));
205
206 // There should be 2 timer that match the filter
207 final JSONArray timers = metrics.getJSONArray("timers");
208 assertThat(timers, is(notNullValue()));
209 assertThat(timers.length(), is(2));
210
211 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700212 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700213
214 final JSONObject jsonTimer2 = timers.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700215 assertThat(jsonTimer2.getString("name"), is(equalTo(TIMER2_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700216
217 // There should be no histograms, gauges, meters or counters
218 checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters");
219 }
220
221 /**
222 * Tests that query of a single metric retunrs just that metric.
223 *
224 * @throws JSONException if any of the JSON processing fails.
225 */
226 @Test
227 public void testSingleFilter() throws JSONException {
228
229 // Read the metrics from the REST API for the test data
230 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700231 client.addQueryParameter("ids", TIMER1_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700232
233 final JSONObject metrics = getJSONObject(client);
234 assertThat(metrics.length(), is(equalTo(5)));
235
236 // There should be 1 timer that matches the filter
237 final JSONArray timers = metrics.getJSONArray("timers");
238 assertThat(timers, is(notNullValue()));
239 assertThat(timers.length(), is(1));
240
241 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700242 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700243
244
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 multiple metrics of multiple metric types returns
251 * the proper data.
252 *
253 * @throws JSONException if any of the JSON processing fails.
254 */
255 @Test
256 public void testMultipleFiltersMultipleTypes() throws JSONException {
257
258 // Read the metrics from the REST API for the test data
259 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
Ray Milkey49d67be2014-07-10 13:47:01 -0700260 client.addQueryParameter("ids",
261 TIMER1_FULL_NAME + "," +
262 GAUGE2_FULL_NAME + "," +
263 HISTOGRAM3_FULL_NAME);
Ray Milkey14c46512014-07-08 18:30:31 -0700264
265 final JSONObject metrics = getJSONObject(client);
266 assertThat(metrics.length(), is(equalTo(5)));
267
268 // There should be 1 timer that matches the filter
269 final JSONArray timers = metrics.getJSONArray("timers");
270 assertThat(timers, is(notNullValue()));
271 assertThat(timers.length(), is(1));
272
273 final JSONObject jsonTimer1 = timers.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700274 assertThat(jsonTimer1.getString("name"), is(equalTo(TIMER1_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700275
276 // There should be 1 gauge that matches the filter
277 final JSONArray gauges = metrics.getJSONArray("gauges");
278 assertThat(gauges, is(notNullValue()));
279 assertThat(gauges.length(), is(1));
280
281 final JSONObject jsonGauge1 = gauges.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700282 assertThat(jsonGauge1.getString("name"), is(equalTo(GAUGE2_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700283
284 // There should be 1 histogram that matches the filter
285 final JSONArray histograms = metrics.getJSONArray("histograms");
286 assertThat(histograms, is(notNullValue()));
287 assertThat(histograms.length(), is(1));
288
289 final JSONObject jsonHistogram1 = histograms.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700290 assertThat(jsonHistogram1.getString("name"), is(equalTo(HISTOGRAM3_FULL_NAME)));
Ray Milkey14c46512014-07-08 18:30:31 -0700291
292 // There should be no meters or counters
293 checkEmptyLists(metrics, "meters", "counters");
294 }
295
296
297}