blob: c5d0e2fdd544731f70143d3b42f78e07f26c892a [file] [log] [blame]
Ray Milkey26921af2014-06-30 16:27:40 -07001package net.onrc.onos.api.rest;
2
3import com.codahale.metrics.Clock;
Ray Milkeya34ed042014-07-17 16:19:52 -07004import net.onrc.onos.core.metrics.web.MetricsWebRoutable;
Ray Milkey26921af2014-06-30 16:27:40 -07005import org.json.JSONArray;
6import org.json.JSONException;
7import org.json.JSONObject;
8
9import java.util.concurrent.TimeUnit;
10
11import static org.hamcrest.MatcherAssert.assertThat;
12import static org.hamcrest.Matchers.is;
13import static org.hamcrest.Matchers.notNullValue;
14
15/**
16 * Test harness for Metrics based REST API tests. This class maintains the
17 * web server and mocks required for testing metrics APIs. REST API tests
18 * for metrics should inherit from this class.
19 */
20public class TestRestMetrics extends TestRest {
Ray Milkey26921af2014-06-30 16:27:40 -070021 /**
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070022 * Creates the web server and mocks required for the metrics tests.
Ray Milkey26921af2014-06-30 16:27:40 -070023 */
24 @Override
25 public void setUp() {
Ray Milkey26921af2014-06-30 16:27:40 -070026 addRestlet(new MetricsWebRoutable());
27 super.setUp();
Ray Milkey26921af2014-06-30 16:27:40 -070028 }
29
30 /**
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070031 * Fetches the base URL for Metrics REST APIs.
Ray Milkey26921af2014-06-30 16:27:40 -070032 *
33 * @return base URL
34 */
35 String getBaseRestMetricsUrl() {
36 return getBaseRestUrl() + "/metrics";
37 }
38
39 /**
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070040 * Checks that the given list of elements in a JSON object are all 0 length
Ray Milkey26921af2014-06-30 16:27:40 -070041 * arrays.
42 *
43 * @param elementNames names of top level elements to check
44 * @param jsonObject top level JSON object
45 * @throws JSONException if JSON fetching throws an error
46 */
47 public void checkEmptyLists(final JSONObject jsonObject,
48 final String ... elementNames)
49 throws JSONException {
50 for (final String elementName : elementNames) {
51 final JSONArray element = jsonObject.getJSONArray(elementName);
52 assertThat(element, is(notNullValue()));
53 assertThat(element.length(), is(0));
54 }
55 }
56
Ray Milkey40eb9c82014-07-18 10:28:11 -070057 /**
58 * Tick time of the mock clock, in milliseconds.
59 */
Ray Milkey26921af2014-06-30 16:27:40 -070060 public static final int MOCK_CLOCK_MILISECONDS_PER_TICK = 50;
61
62 /**
63 * Mock clock used for Timer and Meter tests to give known time values for
64 * test data. Each simulated tick increments the time by
65 * MOCK_CLOCK_MILISECONDS_PER_TICK which is currently defined for 50
66 * millisecond ticks.
67 */
Ray Milkey40eb9c82014-07-18 10:28:11 -070068 private final Clock mockClock = new Clock() {
Ray Milkey26921af2014-06-30 16:27:40 -070069 private long currentTime = 0;
70
71 @Override
72 public long getTick() {
73 final long tickInNanoseconds =
74 TimeUnit.NANOSECONDS.convert(MOCK_CLOCK_MILISECONDS_PER_TICK,
75 TimeUnit.MILLISECONDS);
76 currentTime = currentTime + tickInNanoseconds;
77 return currentTime;
78 }
79 };
Ray Milkey40eb9c82014-07-18 10:28:11 -070080
81 /**
82 * Fetches a mock clock that can be used for Timers and Meters.
83 *
84 * @return Mock clock base on 50 msec ticks
85 */
86 public Clock getMockClock() {
87 return mockClock;
88 }
Ray Milkey26921af2014-06-30 16:27:40 -070089}