Jian Li
Committed by Gerrit Code Review

[ONOS-3601] Add more cases in metrics REST API unit test

Change-Id: Ifa0398517d54149822a4c13bc06a3944c909abc6
...@@ -18,10 +18,13 @@ package org.onosproject.rest; ...@@ -18,10 +18,13 @@ package org.onosproject.rest;
18 import com.codahale.metrics.Counter; 18 import com.codahale.metrics.Counter;
19 import com.codahale.metrics.Meter; 19 import com.codahale.metrics.Meter;
20 import com.codahale.metrics.Metric; 20 import com.codahale.metrics.Metric;
21 +import com.codahale.metrics.Timer;
21 import com.eclipsesource.json.JsonArray; 22 import com.eclipsesource.json.JsonArray;
22 import com.eclipsesource.json.JsonObject; 23 import com.eclipsesource.json.JsonObject;
23 import com.google.common.collect.ImmutableMap; 24 import com.google.common.collect.ImmutableMap;
24 import com.sun.jersey.api.client.WebResource; 25 import com.sun.jersey.api.client.WebResource;
26 +import org.hamcrest.Description;
27 +import org.hamcrest.TypeSafeMatcher;
25 import org.junit.After; 28 import org.junit.After;
26 import org.junit.Before; 29 import org.junit.Before;
27 import org.junit.Test; 30 import org.junit.Test;
...@@ -32,6 +35,8 @@ import org.onlab.rest.BaseResource; ...@@ -32,6 +35,8 @@ import org.onlab.rest.BaseResource;
32 import org.onosproject.codec.CodecService; 35 import org.onosproject.codec.CodecService;
33 import org.onosproject.codec.impl.CodecManager; 36 import org.onosproject.codec.impl.CodecManager;
34 37
38 +import java.util.concurrent.TimeUnit;
39 +
35 import static org.easymock.EasyMock.createMock; 40 import static org.easymock.EasyMock.createMock;
36 import static org.easymock.EasyMock.expect; 41 import static org.easymock.EasyMock.expect;
37 import static org.easymock.EasyMock.replay; 42 import static org.easymock.EasyMock.replay;
...@@ -40,6 +45,7 @@ import static org.hamcrest.Matchers.containsString; ...@@ -40,6 +45,7 @@ import static org.hamcrest.Matchers.containsString;
40 import static org.hamcrest.Matchers.is; 45 import static org.hamcrest.Matchers.is;
41 import static org.hamcrest.Matchers.notNullValue; 46 import static org.hamcrest.Matchers.notNullValue;
42 import static org.junit.Assert.assertThat; 47 import static org.junit.Assert.assertThat;
48 +import static org.junit.Assert.assertTrue;
43 49
44 /** 50 /**
45 * Unit tests for Metrics REST APIs. 51 * Unit tests for Metrics REST APIs.
...@@ -73,20 +79,24 @@ public class MetricsResourceTest extends ResourceTest { ...@@ -73,20 +79,24 @@ public class MetricsResourceTest extends ResourceTest {
73 } 79 }
74 80
75 /** 81 /**
76 - * Tests that a fetch of a non-existent object throws an exception. 82 + * Tests GetAllMetrics method.
77 */ 83 */
78 @Test 84 @Test
79 - public void testBadGet() { 85 + public void testGetAllMetrics() {
80 Counter onosCounter = new Counter(); 86 Counter onosCounter = new Counter();
81 onosCounter.inc(); 87 onosCounter.inc();
82 88
83 Meter onosMeter = new Meter(); 89 Meter onosMeter = new Meter();
84 onosMeter.mark(); 90 onosMeter.mark();
85 91
92 + Timer onosTimer = new Timer();
93 + onosTimer.update(1, TimeUnit.MILLISECONDS);
94 +
86 ImmutableMap<String, Metric> metrics = 95 ImmutableMap<String, Metric> metrics =
87 new ImmutableMap.Builder<String, Metric>() 96 new ImmutableMap.Builder<String, Metric>()
88 .put("onosCounter", onosCounter) 97 .put("onosCounter", onosCounter)
89 .put("onosMeter", onosMeter) 98 .put("onosMeter", onosMeter)
99 + .put("onosTimer", onosTimer)
90 .build(); 100 .build();
91 101
92 expect(mockMetricsService.getMetrics()) 102 expect(mockMetricsService.getMetrics())
...@@ -104,6 +114,137 @@ public class MetricsResourceTest extends ResourceTest { ...@@ -104,6 +114,137 @@ public class MetricsResourceTest extends ResourceTest {
104 114
105 JsonArray jsonMetrics = result.get("metrics").asArray(); 115 JsonArray jsonMetrics = result.get("metrics").asArray();
106 assertThat(jsonMetrics, notNullValue()); 116 assertThat(jsonMetrics, notNullValue());
107 - assertThat(jsonMetrics.size(), is(2)); 117 + assertThat(jsonMetrics.size(), is(3));
118 +
119 + assertTrue(matchesMetric(metrics.get("onosCounter")).matchesSafely(jsonMetrics.get(0).asObject()));
120 + assertTrue(matchesMetric(metrics.get("onosMeter")).matchesSafely(jsonMetrics.get(1).asObject()));
121 + assertTrue(matchesMetric(metrics.get("onosTimer")).matchesSafely(jsonMetrics.get(2).asObject()));
122 + }
123 +
124 + /**
125 + * Hamcrest matcher to check that an device representation in JSON matches
126 + * the actual device.
127 + */
128 + public static class MetricJsonMatcher extends TypeSafeMatcher<JsonObject> {
129 + private final Metric metric;
130 + private String reason = "";
131 +
132 + public MetricJsonMatcher(Metric metricValue) {
133 + this.metric = metricValue;
134 + }
135 +
136 + @Override
137 + public boolean matchesSafely(JsonObject jsonObject) {
138 +
139 + JsonObject jsonMetric = jsonObject.get("metric").asObject();
140 + JsonObject jsonCounter;
141 + JsonObject jsonMeter;
142 + JsonObject jsonTimer;
143 + Counter counter;
144 + Meter meter;
145 + Timer timer;
146 +
147 + // check counter metric
148 + if (jsonMetric.get("counter") != null) {
149 + jsonCounter = jsonMetric.get("counter").asObject();
150 + counter = (Counter) metric;
151 + if (jsonCounter.get("counter").asLong() != counter.getCount()) {
152 + reason = "counter " + counter.getCount();
153 + return false;
154 + }
155 + }
156 +
157 + // check meter metric
158 + if (jsonMetric.get("meter") != null) {
159 + jsonMeter = jsonMetric.get("meter").asObject();
160 + meter = (Meter) metric;
161 +
162 + if (jsonMeter.get("counter").asLong() != meter.getCount()) {
163 + reason = "counter " + meter.getCount();
164 + return false;
165 + }
166 +
167 + if (jsonMeter.get("1_min_rate").asDouble() != meter.getOneMinuteRate()) {
168 + reason = "1 minute rate " + meter.getOneMinuteRate();
169 + return false;
170 + }
171 +
172 + if (jsonMeter.get("5_min_rate").asDouble() != meter.getOneMinuteRate()) {
173 + reason = "5 minute rate " + meter.getFiveMinuteRate();
174 + return false;
175 + }
176 +
177 + if (jsonMeter.get("15_min_rate").asDouble() != meter.getFifteenMinuteRate()) {
178 + reason = "15 minute rate " + meter.getFifteenMinuteRate();
179 + return false;
180 + }
181 + }
182 +
183 + if (jsonMetric.get("timer") != null) {
184 + jsonTimer = jsonMetric.get("timer").asObject();
185 + timer = (Timer) metric;
186 +
187 + if (jsonTimer.get("counter").asLong() != timer.getCount()) {
188 + reason = "counter " + timer.getCount();
189 + return false;
190 + }
191 +
192 + if (jsonTimer.get("1_min_rate").asDouble() != timer.getOneMinuteRate()) {
193 + reason = "1 minute rate " + timer.getOneMinuteRate();
194 + return false;
195 + }
196 +
197 + if (jsonTimer.get("5_min_rate").asDouble() != timer.getOneMinuteRate()) {
198 + reason = "5 minute rate " + timer.getFiveMinuteRate();
199 + return false;
200 + }
201 +
202 + if (jsonTimer.get("15_min_rate").asDouble() != timer.getFifteenMinuteRate()) {
203 + reason = "15 minute rate " + timer.getFifteenMinuteRate();
204 + return false;
205 + }
206 +
207 + if (jsonTimer.get("mean").asDouble() != nanoToMs(timer.getSnapshot().getMean())) {
208 + reason = "mean " + timer.getSnapshot().getMean();
209 + return false;
210 + }
211 +
212 + if (jsonTimer.get("min").asDouble() != nanoToMs(timer.getSnapshot().getMin())) {
213 + reason = "min " + timer.getSnapshot().getMin();
214 + return false;
215 + }
216 +
217 + if (jsonTimer.get("max").asDouble() != nanoToMs(timer.getSnapshot().getMax())) {
218 + reason = "max " + timer.getSnapshot().getMax();
219 + return false;
220 + }
221 +
222 + if (jsonTimer.get("stddev").asDouble() != nanoToMs(timer.getSnapshot().getStdDev())) {
223 + reason = "stddev " + timer.getSnapshot().getStdDev();
224 + return false;
225 + }
226 + }
227 +
228 + return true;
229 + }
230 +
231 + @Override
232 + public void describeTo(Description description) {
233 + description.appendText(reason);
234 + }
235 +
236 + private double nanoToMs(double nano) {
237 + return nano / 1_000_000D;
238 + }
239 + }
240 +
241 + /**
242 + * Factory to allocate an metric matcher.
243 + *
244 + * @param metric metric object we are looking for
245 + * @return matcher
246 + */
247 + private static MetricJsonMatcher matchesMetric(Metric metric) {
248 + return new MetricJsonMatcher(metric);
108 } 249 }
109 } 250 }
......