blob: c8d5d8e3de99ad6339155c54d97c84a45f34ae81 [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;
Ray Milkey26921af2014-06-30 16:27:40 -07008import org.junit.Test;
Ray Milkey26921af2014-06-30 16:27:40 -07009import org.restlet.resource.ClientResource;
10
Ray Milkey26921af2014-06-30 16:27:40 -070011import static org.hamcrest.MatcherAssert.assertThat;
12import static org.hamcrest.Matchers.both;
13import static org.hamcrest.Matchers.equalTo;
14import static org.hamcrest.Matchers.greaterThanOrEqualTo;
15import static org.hamcrest.Matchers.is;
16import static org.hamcrest.Matchers.lessThanOrEqualTo;
17import static org.hamcrest.Matchers.notNullValue;
18
19/**
20 * Unit tests for REST APIs for Meter Metrics.
21 */
Ray Milkey26921af2014-06-30 16:27:40 -070022public class TestRestMetricsMeters extends TestRestMetrics {
23
Ray Milkey26921af2014-06-30 16:27:40 -070024 // Test data for Meters
25
Ray Milkey71cd2c82014-07-16 15:02:33 -070026 private static final OnosMetrics.MetricsComponent COMPONENT =
27 OnosMetrics.registerComponent("MetricsUnitTest");
28 private static final OnosMetrics.MetricsFeature FEATURE =
29 COMPONENT.registerFeature("Meters");
30
Ray Milkey26921af2014-06-30 16:27:40 -070031 private static final String METER1_NAME = "METER1";
Ray Milkey49d67be2014-07-10 13:47:01 -070032 private static final String METER1_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070033 OnosMetrics.generateName(COMPONENT, FEATURE, METER1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070034 private static final int METER1_ITERATIONS = 1;
35
36 private static final String METER2_NAME = "METER2";
Ray Milkey49d67be2014-07-10 13:47:01 -070037 private static final String METER2_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070038 OnosMetrics.generateName(COMPONENT, FEATURE, METER2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070039 private static final int METER2_ITERATIONS = 10;
40
41 private static final String METER3_NAME = "METER3";
Ray Milkey49d67be2014-07-10 13:47:01 -070042 private static final String METER3_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070043 OnosMetrics.generateName(COMPONENT, FEATURE, METER3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070044 private static final int METER3_ITERATIONS = 100;
45
Ray Milkey40eb9c82014-07-18 10:28:11 -070046 private final Meter meter1 = new Meter(getMockClock());
47 private final Meter meter2 = new Meter(getMockClock());
48 private final Meter meter3 = new Meter(getMockClock());
Ray Milkey26921af2014-06-30 16:27:40 -070049
50 /**
51 * Fill in test data for a given Meter.
52 *
53 * @param meter Meter object to fill in
54 * @param iterations How many times to mark the meter
55 */
56 private void fillMeter(final Meter meter,
57 final long iterations) {
58 for (int i = 0; i < iterations; i++) {
59 meter.mark();
60 }
61 }
62
63 /**
64 * Fill in test data in the test Meters.
65 */
66 private void fillMeters() {
67 fillMeter(meter1, METER1_ITERATIONS);
68 fillMeter(meter2, METER2_ITERATIONS);
69 fillMeter(meter3, METER3_ITERATIONS);
70
Ray Milkey71cd2c82014-07-16 15:02:33 -070071 OnosMetrics.registerMetric(COMPONENT,
72 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070073 METER1_NAME,
74 meter1);
75
Ray Milkey71cd2c82014-07-16 15:02:33 -070076 OnosMetrics.registerMetric(COMPONENT,
77 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070078 METER2_NAME,
79 meter2);
80
Ray Milkey71cd2c82014-07-16 15:02:33 -070081 OnosMetrics.registerMetric(COMPONENT,
82 FEATURE,
83 METER3_NAME,
84 meter3);
Ray Milkey26921af2014-06-30 16:27:40 -070085 }
86
87 /**
88 * Check if the data in a Meter matches the expected values.
89 *
90 * @param meter the meter object that this JSON representation should match
91 * @param meterContainer JSON object for the Meter
92 * @param name name of the Meter
93 * @param iterations count of marks that should be in the Meter
94 * @throws JSONException if any of the JSON processing fails
95 */
96 private void checkMeter(final Meter meter,
97 final JSONObject meterContainer,
98 final String name,
99 final int iterations)
100 throws JSONException {
101 final String meterName = meterContainer.getString("name");
102 assertThat(meterName, is(notNullValue()));
103 assertThat(meterName, is(equalTo(name)));
104
105 final JSONObject meterObject = meterContainer.getJSONObject("meter");
106 assertThat(meterObject, is(notNullValue()));
107
108 final int meterCount = meterObject.getInt("count");
109 assertThat(meterCount, is(equalTo(iterations)));
110
111 final double m15Rate = meterObject.getDouble("m15_rate");
112 assertThat(m15Rate, is(equalTo(meter.getFifteenMinuteRate())));
113
114 final double m5Rate = meterObject.getDouble("m5_rate");
115 assertThat(m5Rate, is(equalTo(meter.getFiveMinuteRate())));
116
117 final double m1Rate = meterObject.getDouble("m1_rate");
118 assertThat(m1Rate, is(equalTo(meter.getOneMinuteRate())));
119
120 // mean should be between 0.0 and the longest rate. Since all the rates
121 // are the same because of the mocked clock, use the 15 minute rate as
122 // the max - all the rates should be the same.
123 final double meanRate = meterObject.getDouble("mean_rate");
124 assertThat(meanRate,
125 is(both(greaterThanOrEqualTo(0.0)).
126 and(lessThanOrEqualTo(m15Rate))));
127
128 final String units = meterObject.getString("units");
129 assertThat(units, is(equalTo("events/second")));
130 }
131
132 /**
133 * UNIT test for the Metrics REST API for Meters.
134 *
135 * @throws JSONException if any JSON processing fails
136 */
137 @Test
138 public void testMeters() throws JSONException {
139
140 fillMeters();
141
142 // Read the metrics from the REST API for the test data
143 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
144
145 final JSONObject metrics = getJSONObject(client);
146 assertThat(metrics.length(), is(equalTo(5)));
147
148 // There should be 3 meters
149 final JSONArray meters = metrics.getJSONArray("meters");
150 assertThat(meters, is(notNullValue()));
151 assertThat(meters.length(), is(3));
152
153 // There should be no timers, gauges, histograms or counters
154 checkEmptyLists(metrics, "timers", "gauges", "histograms", "counters");
155
156 // Check the values for meter 1
157 final JSONObject meter1Container = meters.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700158 checkMeter(meter1, meter1Container, METER1_FULL_NAME, METER1_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700159
160 // Check the values for meter 2
161 final JSONObject meter2Container = meters.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700162 checkMeter(meter2, meter2Container, METER2_FULL_NAME, METER2_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700163
164 // Check the values for meter 3
165 final JSONObject meter3Container = meters.getJSONObject(2);
Ray Milkey49d67be2014-07-10 13:47:01 -0700166 checkMeter(meter3, meter3Container, METER3_FULL_NAME, METER3_ITERATIONS);
Ray Milkey26921af2014-06-30 16:27:40 -0700167
168 }
169
170}
171