blob: 88b459b2a5bf49274579555df8d48b60594ebf08 [file] [log] [blame]
Ray Milkey26921af2014-06-30 16:27:40 -07001package net.onrc.onos.api.rest;
2
3import com.codahale.metrics.Meter;
Ray Milkey26921af2014-06-30 16:27:40 -07004import net.onrc.onos.core.metrics.OnosMetrics;
5import org.json.JSONArray;
6import org.json.JSONException;
7import org.json.JSONObject;
8import org.junit.After;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
Ray Milkey26921af2014-06-30 16:27:40 -070012import org.powermock.modules.junit4.PowerMockRunner;
13import org.restlet.resource.ClientResource;
14
Ray Milkey26921af2014-06-30 16:27:40 -070015import static org.hamcrest.MatcherAssert.assertThat;
16import static org.hamcrest.Matchers.both;
17import static org.hamcrest.Matchers.equalTo;
18import static org.hamcrest.Matchers.greaterThanOrEqualTo;
19import static org.hamcrest.Matchers.is;
20import static org.hamcrest.Matchers.lessThanOrEqualTo;
21import static org.hamcrest.Matchers.notNullValue;
22
23/**
24 * Unit tests for REST APIs for Meter Metrics.
25 */
26@RunWith(PowerMockRunner.class)
Ray Milkey26921af2014-06-30 16:27:40 -070027public 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 Milkey71cd2c82014-07-16 15:02:33 -070052
53 private static final OnosMetrics.MetricsComponent COMPONENT =
54 OnosMetrics.registerComponent("MetricsUnitTest");
55 private static final OnosMetrics.MetricsFeature FEATURE =
56 COMPONENT.registerFeature("Meters");
57
Ray Milkey26921af2014-06-30 16:27:40 -070058 private static final String METER1_NAME = "METER1";
Ray Milkey49d67be2014-07-10 13:47:01 -070059 private static final String METER1_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070060 OnosMetrics.generateName(COMPONENT, FEATURE, METER1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070061 private static final int METER1_ITERATIONS = 1;
62
63 private static final String METER2_NAME = "METER2";
Ray Milkey49d67be2014-07-10 13:47:01 -070064 private static final String METER2_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070065 OnosMetrics.generateName(COMPONENT, FEATURE, METER2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070066 private static final int METER2_ITERATIONS = 10;
67
68 private static final String METER3_NAME = "METER3";
Ray Milkey49d67be2014-07-10 13:47:01 -070069 private static final String METER3_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070070 OnosMetrics.generateName(COMPONENT, FEATURE, METER3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070071 private static final int METER3_ITERATIONS = 100;
72
Ray Milkey40eb9c82014-07-18 10:28:11 -070073 private final Meter meter1 = new Meter(getMockClock());
74 private final Meter meter2 = new Meter(getMockClock());
75 private final Meter meter3 = new Meter(getMockClock());
Ray Milkey26921af2014-06-30 16:27:40 -070076
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 Milkey71cd2c82014-07-16 15:02:33 -070098 OnosMetrics.registerMetric(COMPONENT,
99 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700100 METER1_NAME,
101 meter1);
102
Ray Milkey71cd2c82014-07-16 15:02:33 -0700103 OnosMetrics.registerMetric(COMPONENT,
104 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700105 METER2_NAME,
106 meter2);
107
Ray Milkey71cd2c82014-07-16 15:02:33 -0700108 OnosMetrics.registerMetric(COMPONENT,
109 FEATURE,
110 METER3_NAME,
111 meter3);
Ray Milkey26921af2014-06-30 16:27:40 -0700112 }
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 Milkey49d67be2014-07-10 13:47:01 -0700185 checkMeter(meter1, meter1Container, METER1_FULL_NAME, METER1_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700186
187 // Check the values for meter 2
188 final JSONObject meter2Container = meters.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700189 checkMeter(meter2, meter2Container, METER2_FULL_NAME, METER2_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700190
191 // Check the values for meter 3
192 final JSONObject meter3Container = meters.getJSONObject(2);
Ray Milkey49d67be2014-07-10 13:47:01 -0700193 checkMeter(meter3, meter3Container, METER3_FULL_NAME, METER3_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700194
195 }
196
197}
198