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.Counter; |
| 4 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
| 5 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 6 | import org.json.JSONArray; |
| 7 | import org.json.JSONException; |
| 8 | import org.json.JSONObject; |
| 9 | import org.junit.After; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | import org.junit.runner.RunWith; |
| 13 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 14 | import org.powermock.modules.junit4.PowerMockRunner; |
| 15 | import org.restlet.resource.ClientResource; |
| 16 | |
| 17 | import static org.hamcrest.MatcherAssert.assertThat; |
| 18 | import static org.hamcrest.Matchers.equalTo; |
| 19 | import static org.hamcrest.Matchers.is; |
| 20 | import static org.hamcrest.Matchers.notNullValue; |
| 21 | |
| 22 | /** |
| 23 | * Unit tests for REST APIs for Counter Metrics. |
| 24 | */ |
| 25 | @RunWith(PowerMockRunner.class) |
| 26 | @PrepareForTest(PathCalcRuntimeModule.class) |
| 27 | public class TestRestMetricsCounters extends TestRestMetrics { |
| 28 | |
| 29 | /** |
| 30 | * Create the web server and mocks required for |
| 31 | * all of the tests. |
| 32 | */ |
| 33 | @Before |
| 34 | @SuppressWarnings("ununsed") |
| 35 | public void beforeTest() { |
| 36 | setRestPort(generateRandomPort()); |
| 37 | setUp(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Remove anything that will interfere with the next test running correctly. |
| 42 | * Shuts down the test REST web server and removes the mocks. |
| 43 | */ |
| 44 | @After |
| 45 | @SuppressWarnings("unused") |
| 46 | public void afterTest() { |
| 47 | tearDown(); |
| 48 | } |
| 49 | |
| 50 | // Test Counter data objects |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame^] | 51 | private static final OnosMetrics.MetricsComponent COMPONENT = |
| 52 | OnosMetrics.registerComponent("MetricsUnitTests"); |
| 53 | private static final OnosMetrics.MetricsFeature FEATURE = |
| 54 | COMPONENT.registerFeature("Counters"); |
| 55 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 56 | private static final String COUNTER1_NAME = "COUNTER1"; |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame^] | 57 | private static final String COUNTER1_FULL_NAME = OnosMetrics.generateName(COMPONENT, |
| 58 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 59 | COUNTER1_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 60 | private static final int COUNTER1_COUNT = 0; |
| 61 | |
| 62 | private static final String COUNTER2_NAME = "COUNTER2"; |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame^] | 63 | private static final String COUNTER2_FULL_NAME = OnosMetrics.generateName(COMPONENT, |
| 64 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 65 | COUNTER2_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 66 | private static final int COUNTER2_COUNT = -1; |
| 67 | |
| 68 | private static final String COUNTER3_NAME = "COUNTER3"; |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame^] | 69 | private static final String COUNTER3_FULL_NAME = OnosMetrics.generateName(COMPONENT, |
| 70 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 71 | COUNTER3_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 72 | private static final int COUNTER3_COUNT = 5; |
| 73 | |
| 74 | private final Counter counter1 = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame^] | 75 | OnosMetrics.createCounter(COMPONENT, |
| 76 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 77 | COUNTER1_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 78 | private final Counter counter2 = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame^] | 79 | OnosMetrics.createCounter(COMPONENT, |
| 80 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 81 | COUNTER2_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 82 | private final Counter counter3 = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame^] | 83 | OnosMetrics.createCounter(COMPONENT, |
| 84 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 85 | COUNTER3_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Create some test data for the tests. |
| 89 | */ |
| 90 | private void fillCounters() { |
| 91 | counter1.inc(COUNTER1_COUNT); |
| 92 | counter2.inc(COUNTER2_COUNT); |
| 93 | counter3.inc(COUNTER3_COUNT); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Check that a Counter object has the right contents. |
| 98 | * |
| 99 | * @param counterContainer JSON for the Counter |
| 100 | * @param name name of the Counter |
| 101 | * @param count expected count for the Counter |
| 102 | * @throws JSONException if any of the JSON fetches fail |
| 103 | */ |
| 104 | private void checkCounter(final JSONObject counterContainer, |
| 105 | final String name, |
| 106 | final int count) throws JSONException { |
| 107 | final String counterName = counterContainer.getString("name"); |
| 108 | assertThat(counterName, is(notNullValue())); |
| 109 | assertThat(counterName, is(equalTo(name))); |
| 110 | |
| 111 | final JSONObject counterObject = counterContainer.getJSONObject("counter"); |
| 112 | assertThat(counterObject, is(notNullValue())); |
| 113 | |
| 114 | final int counterValue = counterObject.getInt("count"); |
| 115 | assertThat(counterValue, is(equalTo(count))); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Test the REST APIs for Metrics Counter objects. |
| 120 | * |
| 121 | * @throws JSONException |
| 122 | */ |
| 123 | @Test |
| 124 | public void testCounters() throws JSONException { |
| 125 | |
| 126 | fillCounters(); |
| 127 | |
| 128 | // Read the metrics from the REST API for the test data |
| 129 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 130 | |
| 131 | final JSONObject metrics = getJSONObject(client); |
| 132 | assertThat(metrics.length(), is(equalTo(5))); |
| 133 | |
| 134 | // There should be 3 counters |
| 135 | final JSONArray counters = metrics.getJSONArray("counters"); |
| 136 | assertThat(counters, is(notNullValue())); |
| 137 | assertThat(counters.length(), is(3)); |
| 138 | |
| 139 | // There should be no timers, gauges, meters or histograms |
| 140 | checkEmptyLists(metrics, "timers", "gauges", "meters", "histograms"); |
| 141 | |
| 142 | // Check the values for counter 1 |
| 143 | final JSONObject counter1Container = counters.getJSONObject(0); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 144 | checkCounter(counter1Container, COUNTER1_FULL_NAME, COUNTER1_COUNT); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 145 | |
| 146 | // Check the values for counter 1 |
| 147 | final JSONObject counter2Container = counters.getJSONObject(1); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 148 | checkCounter(counter2Container, COUNTER2_FULL_NAME, COUNTER2_COUNT); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 149 | |
| 150 | // Check the values for counter 1 |
| 151 | final JSONObject counter3Container = counters.getJSONObject(2); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 152 | checkCounter(counter3Container, COUNTER3_FULL_NAME, COUNTER3_COUNT); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 153 | |
| 154 | } |
| 155 | |
| 156 | } |