blob: 6796578c088ae860be81c0e4b56a993e4da7e054 [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 * Removes anything that will interfere with the next test running
32 * correctly.
Ray Milkey26921af2014-06-30 16:27:40 -070033 * Shuts down the test REST web server and removes the mocks.
34 */
35 @Override
36 public void tearDown() {
Ray Milkey26921af2014-06-30 16:27:40 -070037 super.tearDown();
38 }
39
40 /**
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070041 * Fetches the base URL for Metrics REST APIs.
Ray Milkey26921af2014-06-30 16:27:40 -070042 *
43 * @return base URL
44 */
45 String getBaseRestMetricsUrl() {
46 return getBaseRestUrl() + "/metrics";
47 }
48
49 /**
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070050 * Checks that the given list of elements in a JSON object are all 0 length
Ray Milkey26921af2014-06-30 16:27:40 -070051 * arrays.
52 *
53 * @param elementNames names of top level elements to check
54 * @param jsonObject top level JSON object
55 * @throws JSONException if JSON fetching throws an error
56 */
57 public void checkEmptyLists(final JSONObject jsonObject,
58 final String ... elementNames)
59 throws JSONException {
60 for (final String elementName : elementNames) {
61 final JSONArray element = jsonObject.getJSONArray(elementName);
62 assertThat(element, is(notNullValue()));
63 assertThat(element.length(), is(0));
64 }
65 }
66
Ray Milkey40eb9c82014-07-18 10:28:11 -070067 /**
68 * Tick time of the mock clock, in milliseconds.
69 */
Ray Milkey26921af2014-06-30 16:27:40 -070070 public static final int MOCK_CLOCK_MILISECONDS_PER_TICK = 50;
71
72 /**
73 * Mock clock used for Timer and Meter tests to give known time values for
74 * test data. Each simulated tick increments the time by
75 * MOCK_CLOCK_MILISECONDS_PER_TICK which is currently defined for 50
76 * millisecond ticks.
77 */
Ray Milkey40eb9c82014-07-18 10:28:11 -070078 private final Clock mockClock = new Clock() {
Ray Milkey26921af2014-06-30 16:27:40 -070079 private long currentTime = 0;
80
81 @Override
82 public long getTick() {
83 final long tickInNanoseconds =
84 TimeUnit.NANOSECONDS.convert(MOCK_CLOCK_MILISECONDS_PER_TICK,
85 TimeUnit.MILLISECONDS);
86 currentTime = currentTime + tickInNanoseconds;
87 return currentTime;
88 }
89 };
Ray Milkey40eb9c82014-07-18 10:28:11 -070090
91 /**
92 * Fetches a mock clock that can be used for Timers and Meters.
93 *
94 * @return Mock clock base on 50 msec ticks
95 */
96 public Clock getMockClock() {
97 return mockClock;
98 }
Ray Milkey26921af2014-06-30 16:27:40 -070099}