Ray Milkey | 14c4651 | 2014-07-08 18:30:31 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import com.codahale.metrics.Gauge; |
| 4 | import com.codahale.metrics.MetricFilter; |
| 5 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
| 6 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 7 | import org.json.JSONArray; |
| 8 | import org.json.JSONException; |
| 9 | import org.json.JSONObject; |
| 10 | import org.junit.After; |
| 11 | import org.junit.Before; |
| 12 | import org.junit.Test; |
| 13 | import org.junit.runner.RunWith; |
| 14 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 15 | import org.powermock.modules.junit4.PowerMockRunner; |
| 16 | import org.restlet.resource.ClientResource; |
| 17 | |
| 18 | import static com.codahale.metrics.MetricRegistry.name; |
| 19 | import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | import static org.hamcrest.Matchers.equalTo; |
| 21 | import static org.hamcrest.Matchers.is; |
| 22 | import static org.hamcrest.Matchers.notNullValue; |
| 23 | |
| 24 | /** |
| 25 | * Unit tests for filtering REST APIs for Timer Metrics. |
| 26 | */ |
| 27 | |
| 28 | @RunWith(PowerMockRunner.class) |
| 29 | @PrepareForTest(PathCalcRuntimeModule.class) |
| 30 | public class TestRestMetricsFilters extends TestRestMetrics { |
| 31 | |
| 32 | /** |
| 33 | * Create the web server and mocks required for |
| 34 | * all of the tests. |
| 35 | */ |
| 36 | @Before |
| 37 | @SuppressWarnings("ununsed") |
| 38 | public void beforeTest() { |
| 39 | setRestPort(generateRandomPort()); |
| 40 | setUp(); |
| 41 | // Make some test data |
| 42 | createMetrics(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Remove anything that will interfere with the next test running correctly. |
| 47 | * Shuts down the test REST web server and removes the mocks. |
| 48 | */ |
| 49 | @After |
| 50 | @SuppressWarnings("unused") |
| 51 | public void afterTest() { |
| 52 | destroyMetrics(); |
| 53 | tearDown(); |
| 54 | } |
| 55 | |
| 56 | // Test data objects |
| 57 | private static final String TIMER1_NAME = name("timer1"); |
| 58 | private static final String TIMER2_NAME = name("timer2"); |
| 59 | private static final String TIMER3_NAME = name("timer3"); |
| 60 | |
| 61 | private static final String GAUGE1_NAME = name("gauge1"); |
| 62 | private static final String GAUGE2_NAME = name("gauge2"); |
| 63 | private static final String GAUGE3_NAME = name("gauge3"); |
| 64 | |
| 65 | private static final String COUNTER1_NAME = name("counter1"); |
| 66 | private static final String COUNTER2_NAME = name("counter2"); |
| 67 | private static final String COUNTER3_NAME = name("counter3"); |
| 68 | |
| 69 | private static final String METER1_NAME = name("meter1"); |
| 70 | private static final String METER2_NAME = name("meter2"); |
| 71 | private static final String METER3_NAME = name("meter3"); |
| 72 | |
| 73 | private static final String HISTOGRAM1_NAME = name("histogram1"); |
| 74 | private static final String HISTOGRAM2_NAME = name("histogram2"); |
| 75 | private static final String HISTOGRAM3_NAME = name("histogram3"); |
| 76 | |
| 77 | final Gauge<Integer> testGauge = new Gauge<Integer>() { |
| 78 | @Override |
| 79 | public Integer getValue() { |
| 80 | return 1; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * Creates Metrics objects for test. |
| 86 | */ |
| 87 | private void createMetrics() { |
| 88 | OnosMetrics.getMetricsRegistry().timer(TIMER1_NAME); |
| 89 | OnosMetrics.getMetricsRegistry().timer(TIMER2_NAME); |
| 90 | OnosMetrics.getMetricsRegistry().timer(TIMER3_NAME); |
| 91 | |
| 92 | OnosMetrics.getMetricsRegistry().counter(COUNTER1_NAME); |
| 93 | OnosMetrics.getMetricsRegistry().counter(COUNTER2_NAME); |
| 94 | OnosMetrics.getMetricsRegistry().counter(COUNTER3_NAME); |
| 95 | |
| 96 | OnosMetrics.getMetricsRegistry().meter(METER1_NAME); |
| 97 | OnosMetrics.getMetricsRegistry().meter(METER2_NAME); |
| 98 | OnosMetrics.getMetricsRegistry().meter(METER3_NAME); |
| 99 | |
| 100 | OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM1_NAME); |
| 101 | OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM2_NAME); |
| 102 | OnosMetrics.getMetricsRegistry().histogram(HISTOGRAM3_NAME); |
| 103 | |
| 104 | OnosMetrics.getMetricsRegistry().register(GAUGE1_NAME, testGauge); |
| 105 | OnosMetrics.getMetricsRegistry().register(GAUGE2_NAME, testGauge); |
| 106 | OnosMetrics.getMetricsRegistry().register(GAUGE3_NAME, testGauge); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Removes the Metrics to clean up for the next test run. |
| 111 | */ |
| 112 | private void destroyMetrics() { |
| 113 | OnosMetrics.getMetricsRegistry().removeMatching(MetricFilter.ALL); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests that query of non existant name returns nothing. |
| 118 | * |
| 119 | * @throws JSONException if any of the JSON processing fails. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testFilterMatchesNothing() throws JSONException { |
| 123 | |
| 124 | // Read the metrics from the REST API for the test data |
| 125 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 126 | client.addQueryParameter("ids", "xyzzy"); |
| 127 | |
| 128 | final JSONObject metrics = getJSONObject(client); |
| 129 | assertThat(metrics.length(), is(equalTo(5))); |
| 130 | |
| 131 | // There should be no timers, histograms, gauges, meters or counters |
| 132 | checkEmptyLists(metrics, "timers", "histograms", "gauges", "meters", "counters"); |
| 133 | |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tests that query of multiple metrics of a single Metric type |
| 138 | * returns the proper data. |
| 139 | * |
| 140 | * @throws JSONException if any of the JSON processing fails. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testMultipleFilterSingleType() throws JSONException { |
| 144 | |
| 145 | // Read the metrics from the REST API for the test data |
| 146 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 147 | client.addQueryParameter("ids", "timer1,timer2"); |
| 148 | |
| 149 | final JSONObject metrics = getJSONObject(client); |
| 150 | assertThat(metrics.length(), is(equalTo(5))); |
| 151 | |
| 152 | // There should be 2 timer that match the filter |
| 153 | final JSONArray timers = metrics.getJSONArray("timers"); |
| 154 | assertThat(timers, is(notNullValue())); |
| 155 | assertThat(timers.length(), is(2)); |
| 156 | |
| 157 | final JSONObject jsonTimer1 = timers.getJSONObject(0); |
| 158 | assertThat(jsonTimer1.getString("name"), is(equalTo("timer1"))); |
| 159 | |
| 160 | final JSONObject jsonTimer2 = timers.getJSONObject(1); |
| 161 | assertThat(jsonTimer2.getString("name"), is(equalTo("timer2"))); |
| 162 | |
| 163 | // There should be no histograms, gauges, meters or counters |
| 164 | checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters"); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Tests that query of a single metric retunrs just that metric. |
| 169 | * |
| 170 | * @throws JSONException if any of the JSON processing fails. |
| 171 | */ |
| 172 | @Test |
| 173 | public void testSingleFilter() throws JSONException { |
| 174 | |
| 175 | // Read the metrics from the REST API for the test data |
| 176 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 177 | client.addQueryParameter("ids", "timer1"); |
| 178 | |
| 179 | final JSONObject metrics = getJSONObject(client); |
| 180 | assertThat(metrics.length(), is(equalTo(5))); |
| 181 | |
| 182 | // There should be 1 timer that matches the filter |
| 183 | final JSONArray timers = metrics.getJSONArray("timers"); |
| 184 | assertThat(timers, is(notNullValue())); |
| 185 | assertThat(timers.length(), is(1)); |
| 186 | |
| 187 | final JSONObject jsonTimer1 = timers.getJSONObject(0); |
| 188 | assertThat(jsonTimer1.getString("name"), is(equalTo("timer1"))); |
| 189 | |
| 190 | |
| 191 | // There should be no histograms, gauges, meters or counters |
| 192 | checkEmptyLists(metrics, "histograms", "gauges", "meters", "counters"); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Tests that query of multiple metrics of multiple metric types returns |
| 197 | * the proper data. |
| 198 | * |
| 199 | * @throws JSONException if any of the JSON processing fails. |
| 200 | */ |
| 201 | @Test |
| 202 | public void testMultipleFiltersMultipleTypes() throws JSONException { |
| 203 | |
| 204 | // Read the metrics from the REST API for the test data |
| 205 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 206 | client.addQueryParameter("ids", "timer1,gauge2,histogram3"); |
| 207 | |
| 208 | final JSONObject metrics = getJSONObject(client); |
| 209 | assertThat(metrics.length(), is(equalTo(5))); |
| 210 | |
| 211 | // There should be 1 timer that matches the filter |
| 212 | final JSONArray timers = metrics.getJSONArray("timers"); |
| 213 | assertThat(timers, is(notNullValue())); |
| 214 | assertThat(timers.length(), is(1)); |
| 215 | |
| 216 | final JSONObject jsonTimer1 = timers.getJSONObject(0); |
| 217 | assertThat(jsonTimer1.getString("name"), is(equalTo("timer1"))); |
| 218 | |
| 219 | // There should be 1 gauge that matches the filter |
| 220 | final JSONArray gauges = metrics.getJSONArray("gauges"); |
| 221 | assertThat(gauges, is(notNullValue())); |
| 222 | assertThat(gauges.length(), is(1)); |
| 223 | |
| 224 | final JSONObject jsonGauge1 = gauges.getJSONObject(0); |
| 225 | assertThat(jsonGauge1.getString("name"), is(equalTo("gauge2"))); |
| 226 | |
| 227 | // There should be 1 histogram that matches the filter |
| 228 | final JSONArray histograms = metrics.getJSONArray("histograms"); |
| 229 | assertThat(histograms, is(notNullValue())); |
| 230 | assertThat(histograms.length(), is(1)); |
| 231 | |
| 232 | final JSONObject jsonHistogram1 = histograms.getJSONObject(0); |
| 233 | assertThat(jsonHistogram1.getString("name"), is(equalTo("histogram3"))); |
| 234 | |
| 235 | // There should be no meters or counters |
| 236 | checkEmptyLists(metrics, "meters", "counters"); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | } |