blob: da046fd3d9379118ab88d5086d0ceb0d7e4e22e2 [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.intent.runtime.PathCalcRuntimeModule;
5import net.onrc.onos.core.metrics.OnosMetrics;
6import org.json.JSONArray;
7import org.json.JSONException;
8import org.json.JSONObject;
9import org.junit.After;
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13import org.powermock.core.classloader.annotations.PrepareForTest;
14import org.powermock.modules.junit4.PowerMockRunner;
15import org.restlet.resource.ClientResource;
16
Ray Milkey26921af2014-06-30 16:27:40 -070017import static org.hamcrest.MatcherAssert.assertThat;
18import static org.hamcrest.Matchers.both;
19import static org.hamcrest.Matchers.equalTo;
20import static org.hamcrest.Matchers.greaterThanOrEqualTo;
21import static org.hamcrest.Matchers.is;
22import static org.hamcrest.Matchers.lessThanOrEqualTo;
23import static org.hamcrest.Matchers.notNullValue;
24
25/**
26 * Unit tests for REST APIs for Meter Metrics.
27 */
28@RunWith(PowerMockRunner.class)
29@PrepareForTest(PathCalcRuntimeModule.class)
30public class TestRestMetricsMeters 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 }
42
43 /**
44 * Remove anything that will interfere with the next test running correctly.
45 * Shuts down the test REST web server and removes the mocks.
46 */
47 @After
48 @SuppressWarnings("unused")
49 public void afterTest() {
50 tearDown();
51 }
52
53 // Test data for Meters
54
Ray Milkey71cd2c82014-07-16 15:02:33 -070055
56 private static final OnosMetrics.MetricsComponent COMPONENT =
57 OnosMetrics.registerComponent("MetricsUnitTest");
58 private static final OnosMetrics.MetricsFeature FEATURE =
59 COMPONENT.registerFeature("Meters");
60
Ray Milkey26921af2014-06-30 16:27:40 -070061 private static final String METER1_NAME = "METER1";
Ray Milkey49d67be2014-07-10 13:47:01 -070062 private static final String METER1_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070063 OnosMetrics.generateName(COMPONENT, FEATURE, METER1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070064 private static final int METER1_ITERATIONS = 1;
65
66 private static final String METER2_NAME = "METER2";
Ray Milkey49d67be2014-07-10 13:47:01 -070067 private static final String METER2_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070068 OnosMetrics.generateName(COMPONENT, FEATURE, METER2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070069 private static final int METER2_ITERATIONS = 10;
70
71 private static final String METER3_NAME = "METER3";
Ray Milkey49d67be2014-07-10 13:47:01 -070072 private static final String METER3_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070073 OnosMetrics.generateName(COMPONENT, FEATURE, METER3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070074 private static final int METER3_ITERATIONS = 100;
75
76 private final Meter meter1 = new Meter(mockClock);
77 private final Meter meter2 = new Meter(mockClock);
78 private final Meter meter3 = new Meter(mockClock);
79
80 /**
81 * Fill in test data for a given Meter.
82 *
83 * @param meter Meter object to fill in
84 * @param iterations How many times to mark the meter
85 */
86 private void fillMeter(final Meter meter,
87 final long iterations) {
88 for (int i = 0; i < iterations; i++) {
89 meter.mark();
90 }
91 }
92
93 /**
94 * Fill in test data in the test Meters.
95 */
96 private void fillMeters() {
97 fillMeter(meter1, METER1_ITERATIONS);
98 fillMeter(meter2, METER2_ITERATIONS);
99 fillMeter(meter3, METER3_ITERATIONS);
100
Ray Milkey71cd2c82014-07-16 15:02:33 -0700101 OnosMetrics.registerMetric(COMPONENT,
102 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700103 METER1_NAME,
104 meter1);
105
Ray Milkey71cd2c82014-07-16 15:02:33 -0700106 OnosMetrics.registerMetric(COMPONENT,
107 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700108 METER2_NAME,
109 meter2);
110
Ray Milkey71cd2c82014-07-16 15:02:33 -0700111 OnosMetrics.registerMetric(COMPONENT,
112 FEATURE,
113 METER3_NAME,
114 meter3);
Ray Milkey26921af2014-06-30 16:27:40 -0700115 }
116
117 /**
118 * Check if the data in a Meter matches the expected values.
119 *
120 * @param meter the meter object that this JSON representation should match
121 * @param meterContainer JSON object for the Meter
122 * @param name name of the Meter
123 * @param iterations count of marks that should be in the Meter
124 * @throws JSONException if any of the JSON processing fails
125 */
126 private void checkMeter(final Meter meter,
127 final JSONObject meterContainer,
128 final String name,
129 final int iterations)
130 throws JSONException {
131 final String meterName = meterContainer.getString("name");
132 assertThat(meterName, is(notNullValue()));
133 assertThat(meterName, is(equalTo(name)));
134
135 final JSONObject meterObject = meterContainer.getJSONObject("meter");
136 assertThat(meterObject, is(notNullValue()));
137
138 final int meterCount = meterObject.getInt("count");
139 assertThat(meterCount, is(equalTo(iterations)));
140
141 final double m15Rate = meterObject.getDouble("m15_rate");
142 assertThat(m15Rate, is(equalTo(meter.getFifteenMinuteRate())));
143
144 final double m5Rate = meterObject.getDouble("m5_rate");
145 assertThat(m5Rate, is(equalTo(meter.getFiveMinuteRate())));
146
147 final double m1Rate = meterObject.getDouble("m1_rate");
148 assertThat(m1Rate, is(equalTo(meter.getOneMinuteRate())));
149
150 // mean should be between 0.0 and the longest rate. Since all the rates
151 // are the same because of the mocked clock, use the 15 minute rate as
152 // the max - all the rates should be the same.
153 final double meanRate = meterObject.getDouble("mean_rate");
154 assertThat(meanRate,
155 is(both(greaterThanOrEqualTo(0.0)).
156 and(lessThanOrEqualTo(m15Rate))));
157
158 final String units = meterObject.getString("units");
159 assertThat(units, is(equalTo("events/second")));
160 }
161
162 /**
163 * UNIT test for the Metrics REST API for Meters.
164 *
165 * @throws JSONException if any JSON processing fails
166 */
167 @Test
168 public void testMeters() throws JSONException {
169
170 fillMeters();
171
172 // Read the metrics from the REST API for the test data
173 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
174
175 final JSONObject metrics = getJSONObject(client);
176 assertThat(metrics.length(), is(equalTo(5)));
177
178 // There should be 3 meters
179 final JSONArray meters = metrics.getJSONArray("meters");
180 assertThat(meters, is(notNullValue()));
181 assertThat(meters.length(), is(3));
182
183 // There should be no timers, gauges, histograms or counters
184 checkEmptyLists(metrics, "timers", "gauges", "histograms", "counters");
185
186 // Check the values for meter 1
187 final JSONObject meter1Container = meters.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700188 checkMeter(meter1, meter1Container, METER1_FULL_NAME, METER1_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700189
190 // Check the values for meter 2
191 final JSONObject meter2Container = meters.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700192 checkMeter(meter2, meter2Container, METER2_FULL_NAME, METER2_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700193
194 // Check the values for meter 3
195 final JSONObject meter3Container = meters.getJSONObject(2);
Ray Milkey49d67be2014-07-10 13:47:01 -0700196 checkMeter(meter3, meter3Container, METER3_FULL_NAME, METER3_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700197
198 }
199
200}
201