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 | * Removes anything that will interfere with the next test running |
| 32 | * correctly. |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 33 | * Shuts down the test REST web server and removes the mocks. |
| 34 | */ |
| 35 | @Override |
| 36 | public void tearDown() { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 37 | super.tearDown(); |
| 38 | } |
| 39 | |
| 40 | /** |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame^] | 41 | * Fetches the base URL for Metrics REST APIs. |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 42 | * |
| 43 | * @return base URL |
| 44 | */ |
| 45 | String getBaseRestMetricsUrl() { |
| 46 | return getBaseRestUrl() + "/metrics"; |
| 47 | } |
| 48 | |
| 49 | /** |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame^] | 50 | * 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] | 51 | * 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 Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Tick time of the mock clock, in milliseconds. |
| 69 | */ |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 70 | 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 Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 78 | private final Clock mockClock = new Clock() { |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 79 | 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 Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 90 | |
| 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 Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 99 | } |