Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import com.codahale.metrics.Meter; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 5 | import org.json.JSONArray; |
| 6 | import org.json.JSONException; |
| 7 | import org.json.JSONObject; |
| 8 | import org.junit.After; |
| 9 | import org.junit.Before; |
| 10 | import org.junit.Test; |
| 11 | import org.junit.runner.RunWith; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 12 | import org.powermock.modules.junit4.PowerMockRunner; |
| 13 | import org.restlet.resource.ClientResource; |
| 14 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 15 | import static org.hamcrest.MatcherAssert.assertThat; |
| 16 | import static org.hamcrest.Matchers.both; |
| 17 | import static org.hamcrest.Matchers.equalTo; |
| 18 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 19 | import static org.hamcrest.Matchers.is; |
| 20 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 21 | import static org.hamcrest.Matchers.notNullValue; |
| 22 | |
| 23 | /** |
| 24 | * Unit tests for REST APIs for Meter Metrics. |
| 25 | */ |
| 26 | @RunWith(PowerMockRunner.class) |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 27 | public class TestRestMetricsMeters extends TestRestMetrics { |
| 28 | |
| 29 | /** |
| 30 | * Create the web server and mocks required for |
| 31 | * all of the tests. |
| 32 | */ |
| 33 | @Before |
| 34 | @SuppressWarnings("ununsed") |
| 35 | public void beforeTest() { |
| 36 | setRestPort(generateRandomPort()); |
| 37 | setUp(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Remove anything that will interfere with the next test running correctly. |
| 42 | * Shuts down the test REST web server and removes the mocks. |
| 43 | */ |
| 44 | @After |
| 45 | @SuppressWarnings("unused") |
| 46 | public void afterTest() { |
| 47 | tearDown(); |
| 48 | } |
| 49 | |
| 50 | // Test data for Meters |
| 51 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 52 | |
| 53 | private static final OnosMetrics.MetricsComponent COMPONENT = |
| 54 | OnosMetrics.registerComponent("MetricsUnitTest"); |
| 55 | private static final OnosMetrics.MetricsFeature FEATURE = |
| 56 | COMPONENT.registerFeature("Meters"); |
| 57 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 58 | private static final String METER1_NAME = "METER1"; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 59 | private static final String METER1_FULL_NAME = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 60 | OnosMetrics.generateName(COMPONENT, FEATURE, METER1_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 61 | private static final int METER1_ITERATIONS = 1; |
| 62 | |
| 63 | private static final String METER2_NAME = "METER2"; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 64 | private static final String METER2_FULL_NAME = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 65 | OnosMetrics.generateName(COMPONENT, FEATURE, METER2_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 66 | private static final int METER2_ITERATIONS = 10; |
| 67 | |
| 68 | private static final String METER3_NAME = "METER3"; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 69 | private static final String METER3_FULL_NAME = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 70 | OnosMetrics.generateName(COMPONENT, FEATURE, METER3_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 71 | private static final int METER3_ITERATIONS = 100; |
| 72 | |
Ray Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 73 | private final Meter meter1 = new Meter(getMockClock()); |
| 74 | private final Meter meter2 = new Meter(getMockClock()); |
| 75 | private final Meter meter3 = new Meter(getMockClock()); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Fill in test data for a given Meter. |
| 79 | * |
| 80 | * @param meter Meter object to fill in |
| 81 | * @param iterations How many times to mark the meter |
| 82 | */ |
| 83 | private void fillMeter(final Meter meter, |
| 84 | final long iterations) { |
| 85 | for (int i = 0; i < iterations; i++) { |
| 86 | meter.mark(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Fill in test data in the test Meters. |
| 92 | */ |
| 93 | private void fillMeters() { |
| 94 | fillMeter(meter1, METER1_ITERATIONS); |
| 95 | fillMeter(meter2, METER2_ITERATIONS); |
| 96 | fillMeter(meter3, METER3_ITERATIONS); |
| 97 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 98 | OnosMetrics.registerMetric(COMPONENT, |
| 99 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 100 | METER1_NAME, |
| 101 | meter1); |
| 102 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 103 | OnosMetrics.registerMetric(COMPONENT, |
| 104 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 105 | METER2_NAME, |
| 106 | meter2); |
| 107 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 108 | OnosMetrics.registerMetric(COMPONENT, |
| 109 | FEATURE, |
| 110 | METER3_NAME, |
| 111 | meter3); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Check if the data in a Meter matches the expected values. |
| 116 | * |
| 117 | * @param meter the meter object that this JSON representation should match |
| 118 | * @param meterContainer JSON object for the Meter |
| 119 | * @param name name of the Meter |
| 120 | * @param iterations count of marks that should be in the Meter |
| 121 | * @throws JSONException if any of the JSON processing fails |
| 122 | */ |
| 123 | private void checkMeter(final Meter meter, |
| 124 | final JSONObject meterContainer, |
| 125 | final String name, |
| 126 | final int iterations) |
| 127 | throws JSONException { |
| 128 | final String meterName = meterContainer.getString("name"); |
| 129 | assertThat(meterName, is(notNullValue())); |
| 130 | assertThat(meterName, is(equalTo(name))); |
| 131 | |
| 132 | final JSONObject meterObject = meterContainer.getJSONObject("meter"); |
| 133 | assertThat(meterObject, is(notNullValue())); |
| 134 | |
| 135 | final int meterCount = meterObject.getInt("count"); |
| 136 | assertThat(meterCount, is(equalTo(iterations))); |
| 137 | |
| 138 | final double m15Rate = meterObject.getDouble("m15_rate"); |
| 139 | assertThat(m15Rate, is(equalTo(meter.getFifteenMinuteRate()))); |
| 140 | |
| 141 | final double m5Rate = meterObject.getDouble("m5_rate"); |
| 142 | assertThat(m5Rate, is(equalTo(meter.getFiveMinuteRate()))); |
| 143 | |
| 144 | final double m1Rate = meterObject.getDouble("m1_rate"); |
| 145 | assertThat(m1Rate, is(equalTo(meter.getOneMinuteRate()))); |
| 146 | |
| 147 | // mean should be between 0.0 and the longest rate. Since all the rates |
| 148 | // are the same because of the mocked clock, use the 15 minute rate as |
| 149 | // the max - all the rates should be the same. |
| 150 | final double meanRate = meterObject.getDouble("mean_rate"); |
| 151 | assertThat(meanRate, |
| 152 | is(both(greaterThanOrEqualTo(0.0)). |
| 153 | and(lessThanOrEqualTo(m15Rate)))); |
| 154 | |
| 155 | final String units = meterObject.getString("units"); |
| 156 | assertThat(units, is(equalTo("events/second"))); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * UNIT test for the Metrics REST API for Meters. |
| 161 | * |
| 162 | * @throws JSONException if any JSON processing fails |
| 163 | */ |
| 164 | @Test |
| 165 | public void testMeters() throws JSONException { |
| 166 | |
| 167 | fillMeters(); |
| 168 | |
| 169 | // Read the metrics from the REST API for the test data |
| 170 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 171 | |
| 172 | final JSONObject metrics = getJSONObject(client); |
| 173 | assertThat(metrics.length(), is(equalTo(5))); |
| 174 | |
| 175 | // There should be 3 meters |
| 176 | final JSONArray meters = metrics.getJSONArray("meters"); |
| 177 | assertThat(meters, is(notNullValue())); |
| 178 | assertThat(meters.length(), is(3)); |
| 179 | |
| 180 | // There should be no timers, gauges, histograms or counters |
| 181 | checkEmptyLists(metrics, "timers", "gauges", "histograms", "counters"); |
| 182 | |
| 183 | // Check the values for meter 1 |
| 184 | final JSONObject meter1Container = meters.getJSONObject(0); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 185 | checkMeter(meter1, meter1Container, METER1_FULL_NAME, METER1_ITERATIONS); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 186 | |
| 187 | // Check the values for meter 2 |
| 188 | final JSONObject meter2Container = meters.getJSONObject(1); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 189 | checkMeter(meter2, meter2Container, METER2_FULL_NAME, METER2_ITERATIONS); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 190 | |
| 191 | // Check the values for meter 3 |
| 192 | final JSONObject meter3Container = meters.getJSONObject(2); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 193 | checkMeter(meter3, meter3Container, METER3_FULL_NAME, METER3_ITERATIONS); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 194 | |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |