blob: 481f86c3755e036ea63956445d4c97669d3c8f8d [file] [log] [blame]
Ray Milkey26921af2014-06-30 16:27:40 -07001package net.onrc.onos.api.rest;
2
3import com.codahale.metrics.Gauge;
Ray Milkey26921af2014-06-30 16:27:40 -07004import net.onrc.onos.core.metrics.OnosMetrics;
5import org.json.JSONArray;
6import org.json.JSONException;
7import org.json.JSONObject;
Ray Milkey26921af2014-06-30 16:27:40 -07008import org.junit.Test;
Ray Milkey26921af2014-06-30 16:27:40 -07009import org.restlet.resource.ClientResource;
10
11import static org.hamcrest.MatcherAssert.assertThat;
12import static org.hamcrest.Matchers.equalTo;
13import static org.hamcrest.Matchers.is;
14import static org.hamcrest.Matchers.notNullValue;
15
16/**
17 * Unit tests for REST APIs for Gauges Metrics.
18 */
Ray Milkey26921af2014-06-30 16:27:40 -070019public class TestRestMetricsGauges extends TestRestMetrics {
20
Ray Milkey26921af2014-06-30 16:27:40 -070021 // Test data for Gauges
Ray Milkey71cd2c82014-07-16 15:02:33 -070022
23 private static final OnosMetrics.MetricsComponent COMPONENT =
24 OnosMetrics.registerComponent("MetricsUnitTests");
25 private static final OnosMetrics.MetricsFeature FEATURE =
26 COMPONENT.registerFeature("Gauges");
27
Ray Milkey26921af2014-06-30 16:27:40 -070028 private static final String GAUGE1_NAME = "gauge1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070029 private static final String GAUGE1_FULL_NAME =
30 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070031 private static final int GAUGE1_VALUE = 0;
32
33 private static final String GAUGE2_NAME = "gauge2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070034 private static final String GAUGE2_FULL_NAME =
35 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070036 private static final int GAUGE2_VALUE = -1;
37
38 private static final String GAUGE3_NAME = "gauge3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070039 private static final String GAUGE3_FULL_NAME =
40 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070041 private static final int GAUGE3_VALUE = 123456789;
42
Ray Milkey49d67be2014-07-10 13:47:01 -070043 private final Gauge<Integer> gauge1 =
Ray Milkey26921af2014-06-30 16:27:40 -070044 new Gauge<Integer>() {
45 @Override
46 public Integer getValue() {
47 return GAUGE1_VALUE;
48 }
Ray Milkey49d67be2014-07-10 13:47:01 -070049 };
Ray Milkey26921af2014-06-30 16:27:40 -070050
Ray Milkey49d67be2014-07-10 13:47:01 -070051 private final Gauge<Integer> gauge2 =
Ray Milkey26921af2014-06-30 16:27:40 -070052 new Gauge<Integer>() {
53 @Override
54 public Integer getValue() {
55 return GAUGE2_VALUE;
56 }
Ray Milkey49d67be2014-07-10 13:47:01 -070057 };
Ray Milkey26921af2014-06-30 16:27:40 -070058
Ray Milkey49d67be2014-07-10 13:47:01 -070059 private final Gauge<Integer> gauge3 =
Ray Milkey26921af2014-06-30 16:27:40 -070060 new Gauge<Integer>() {
61 @Override
62 public Integer getValue() {
63 return GAUGE3_VALUE;
64 }
Ray Milkey49d67be2014-07-10 13:47:01 -070065 };
66
Ray Milkey40eb9c82014-07-18 10:28:11 -070067 /**
68 * Registers the test Gauge objects.
69 */
Ray Milkey49d67be2014-07-10 13:47:01 -070070 private void registerGauges() {
Ray Milkey71cd2c82014-07-16 15:02:33 -070071 OnosMetrics.registerMetric(COMPONENT,
72 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070073 GAUGE1_NAME,
74 gauge1);
75
Ray Milkey71cd2c82014-07-16 15:02:33 -070076 OnosMetrics.registerMetric(COMPONENT,
77 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070078 GAUGE2_NAME,
79 gauge2);
80
Ray Milkey71cd2c82014-07-16 15:02:33 -070081 OnosMetrics.registerMetric(COMPONENT,
82 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070083 GAUGE3_NAME,
84 gauge3);
85 }
Ray Milkey26921af2014-06-30 16:27:40 -070086
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 Milkey40eb9c82014-07-18 10:28:11 -0700118 public void testGauges() throws JSONException {
Ray Milkey49d67be2014-07-10 13:47:01 -0700119 registerGauges();
Ray Milkey26921af2014-06-30 16:27:40 -0700120
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 Milkey49d67be2014-07-10 13:47:01 -0700137 checkGauge(gauge1Container, GAUGE1_FULL_NAME, gauge1);
Ray Milkey26921af2014-06-30 16:27:40 -0700138
139 // Check the values for gauge 2
140 final JSONObject gauge2Container = gauges.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700141 checkGauge(gauge2Container, GAUGE2_FULL_NAME, gauge2);
Ray Milkey26921af2014-06-30 16:27:40 -0700142
143 // Check the values for gauge 3
144 final JSONObject gauge3Container = gauges.getJSONObject(2);
Ray Milkey49d67be2014-07-10 13:47:01 -0700145 checkGauge(gauge3Container, GAUGE3_FULL_NAME, gauge3);
Ray Milkey26921af2014-06-30 16:27:40 -0700146 }
147
148}