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.Gauge; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 5 | import org.json.JSONArray; |
| 6 | import org.json.JSONException; |
| 7 | import org.json.JSONObject; |
| 8 | import org.junit.After; |
| 9 | import org.junit.Before; |
| 10 | import org.junit.Test; |
| 11 | import org.junit.runner.RunWith; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 12 | import org.powermock.modules.junit4.PowerMockRunner; |
| 13 | import org.restlet.resource.ClientResource; |
| 14 | |
| 15 | import static org.hamcrest.MatcherAssert.assertThat; |
| 16 | import static org.hamcrest.Matchers.equalTo; |
| 17 | import static org.hamcrest.Matchers.is; |
| 18 | import static org.hamcrest.Matchers.notNullValue; |
| 19 | |
| 20 | /** |
| 21 | * Unit tests for REST APIs for Gauges Metrics. |
| 22 | */ |
| 23 | @RunWith(PowerMockRunner.class) |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 24 | public class TestRestMetricsGauges extends TestRestMetrics { |
| 25 | |
| 26 | /** |
| 27 | * Create the web server and mocks required for |
| 28 | * all of the tests. |
| 29 | */ |
| 30 | @Before |
| 31 | @SuppressWarnings("ununsed") |
| 32 | public void beforeTest() { |
| 33 | setRestPort(generateRandomPort()); |
| 34 | setUp(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Remove anything that will interfere with the next test running correctly. |
| 39 | * Shuts down the test REST web server and removes the mocks. |
| 40 | */ |
| 41 | @After |
| 42 | @SuppressWarnings("unused") |
| 43 | public void afterTest() { |
| 44 | tearDown(); |
| 45 | } |
| 46 | |
| 47 | // Test data for Gauges |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 48 | |
| 49 | private static final OnosMetrics.MetricsComponent COMPONENT = |
| 50 | OnosMetrics.registerComponent("MetricsUnitTests"); |
| 51 | private static final OnosMetrics.MetricsFeature FEATURE = |
| 52 | COMPONENT.registerFeature("Gauges"); |
| 53 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 54 | private static final String GAUGE1_NAME = "gauge1"; |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 55 | private static final String GAUGE1_FULL_NAME = |
| 56 | OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE1_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 57 | private static final int GAUGE1_VALUE = 0; |
| 58 | |
| 59 | private static final String GAUGE2_NAME = "gauge2"; |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 60 | private static final String GAUGE2_FULL_NAME = |
| 61 | OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE2_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 62 | private static final int GAUGE2_VALUE = -1; |
| 63 | |
| 64 | private static final String GAUGE3_NAME = "gauge3"; |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 65 | private static final String GAUGE3_FULL_NAME = |
| 66 | OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE3_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 67 | private static final int GAUGE3_VALUE = 123456789; |
| 68 | |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 69 | private final Gauge<Integer> gauge1 = |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 70 | new Gauge<Integer>() { |
| 71 | @Override |
| 72 | public Integer getValue() { |
| 73 | return GAUGE1_VALUE; |
| 74 | } |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 75 | }; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 76 | |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 77 | private final Gauge<Integer> gauge2 = |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 78 | new Gauge<Integer>() { |
| 79 | @Override |
| 80 | public Integer getValue() { |
| 81 | return GAUGE2_VALUE; |
| 82 | } |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 83 | }; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 84 | |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 85 | private final Gauge<Integer> gauge3 = |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 86 | new Gauge<Integer>() { |
| 87 | @Override |
| 88 | public Integer getValue() { |
| 89 | return GAUGE3_VALUE; |
| 90 | } |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Ray Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Registers the test Gauge objects. |
| 95 | */ |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 96 | private void registerGauges() { |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 97 | OnosMetrics.registerMetric(COMPONENT, |
| 98 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 99 | GAUGE1_NAME, |
| 100 | gauge1); |
| 101 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 102 | OnosMetrics.registerMetric(COMPONENT, |
| 103 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 104 | GAUGE2_NAME, |
| 105 | gauge2); |
| 106 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 107 | OnosMetrics.registerMetric(COMPONENT, |
| 108 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 109 | GAUGE3_NAME, |
| 110 | gauge3); |
| 111 | } |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Check that the JSON for a Gauge obect has the correct data values. |
| 115 | * |
| 116 | * @param gaugeContainer JSON object for the Gauge |
| 117 | * @param name expected name of the gauge |
| 118 | * @param gauge Metrics Gauge object that hold the expected value |
| 119 | * @throws JSONException if any JSON operation fails |
| 120 | */ |
| 121 | private void checkGauge(final JSONObject gaugeContainer, |
| 122 | final String name, |
| 123 | final Gauge<Integer> gauge) |
| 124 | throws JSONException { |
| 125 | assertThat(gaugeContainer, is(notNullValue())); |
| 126 | |
| 127 | final String gaugeName = gaugeContainer.getString("name"); |
| 128 | assertThat(gaugeName, is(notNullValue())); |
| 129 | assertThat(gaugeName, is(equalTo(name))); |
| 130 | |
| 131 | final JSONObject gaugeObject = gaugeContainer.getJSONObject("gauge"); |
| 132 | assertThat(gaugeObject, is(notNullValue())); |
| 133 | |
| 134 | final int gaugeValue = gaugeObject.getInt("value"); |
| 135 | assertThat(gaugeValue, is(equalTo(gauge.getValue()))); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Unit test for the Gauges portion of the Metrics REST API. |
| 140 | * |
| 141 | * @throws JSONException if any JSON operation fails |
| 142 | */ |
| 143 | @Test |
Ray Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 144 | public void testGauges() throws JSONException { |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 145 | registerGauges(); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 146 | |
| 147 | // Read the metrics from the REST API for the test data |
| 148 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 149 | |
| 150 | final JSONObject metrics = getJSONObject(client); |
| 151 | assertThat(metrics.length(), is(equalTo(5))); |
| 152 | |
| 153 | // There should be 3 gauges |
| 154 | final JSONArray gauges = metrics.getJSONArray("gauges"); |
| 155 | assertThat(gauges, is(notNullValue())); |
| 156 | assertThat(gauges.length(), is(3)); |
| 157 | |
| 158 | // There should be no timers, meters, histograms or counters |
| 159 | checkEmptyLists(metrics, "timers", "meters", "histograms", "counters"); |
| 160 | |
| 161 | // Check the values for gauge 1 |
| 162 | final JSONObject gauge1Container = gauges.getJSONObject(0); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 163 | checkGauge(gauge1Container, GAUGE1_FULL_NAME, gauge1); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 164 | |
| 165 | // Check the values for gauge 2 |
| 166 | final JSONObject gauge2Container = gauges.getJSONObject(1); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 167 | checkGauge(gauge2Container, GAUGE2_FULL_NAME, gauge2); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 168 | |
| 169 | // Check the values for gauge 3 |
| 170 | final JSONObject gauge3Container = gauges.getJSONObject(2); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 171 | checkGauge(gauge3Container, GAUGE3_FULL_NAME, gauge3); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | } |