Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import com.codahale.metrics.Clock; |
Ray Milkey | a34ed04 | 2014-07-17 16:19:52 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.metrics.web.MetricsWebRoutable; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 5 | import org.json.JSONArray; |
| 6 | import org.json.JSONException; |
| 7 | import org.json.JSONObject; |
| 8 | |
| 9 | import java.util.concurrent.TimeUnit; |
| 10 | |
| 11 | import static org.hamcrest.MatcherAssert.assertThat; |
| 12 | import static org.hamcrest.Matchers.is; |
| 13 | import 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 | */ |
| 20 | public class TestRestMetrics extends TestRest { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 21 | /** |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 22 | * Creates the web server and mocks required for the metrics tests. |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 23 | */ |
| 24 | @Override |
| 25 | public void setUp() { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 26 | addRestlet(new MetricsWebRoutable()); |
| 27 | super.setUp(); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | /** |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 31 | * Fetches the base URL for Metrics REST APIs. |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 32 | * |
| 33 | * @return base URL |
| 34 | */ |
| 35 | String getBaseRestMetricsUrl() { |
| 36 | return getBaseRestUrl() + "/metrics"; |
| 37 | } |
| 38 | |
| 39 | /** |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 40 | * Checks that the given list of elements in a JSON object are all 0 length |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 41 | * 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 Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Tick time of the mock clock, in milliseconds. |
| 59 | */ |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 60 | 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 Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 68 | private final Clock mockClock = new Clock() { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 69 | 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 Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 80 | |
| 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 Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 89 | } |