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