blob: 205f527fc029f6ff979ef91bb04c9012b8cbb40d [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;
8import org.junit.After;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
Ray Milkey26921af2014-06-30 16:27:40 -070012import org.powermock.modules.junit4.PowerMockRunner;
13import org.restlet.resource.ClientResource;
14
15import static org.hamcrest.MatcherAssert.assertThat;
16import static org.hamcrest.Matchers.equalTo;
17import static org.hamcrest.Matchers.is;
18import static org.hamcrest.Matchers.notNullValue;
19
20/**
21 * Unit tests for REST APIs for Gauges Metrics.
22 */
23@RunWith(PowerMockRunner.class)
Ray Milkey26921af2014-06-30 16:27:40 -070024public 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 Milkey71cd2c82014-07-16 15:02:33 -070048
49 private static final OnosMetrics.MetricsComponent COMPONENT =
50 OnosMetrics.registerComponent("MetricsUnitTests");
51 private static final OnosMetrics.MetricsFeature FEATURE =
52 COMPONENT.registerFeature("Gauges");
53
Ray Milkey26921af2014-06-30 16:27:40 -070054 private static final String GAUGE1_NAME = "gauge1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070055 private static final String GAUGE1_FULL_NAME =
56 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070057 private static final int GAUGE1_VALUE = 0;
58
59 private static final String GAUGE2_NAME = "gauge2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070060 private static final String GAUGE2_FULL_NAME =
61 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070062 private static final int GAUGE2_VALUE = -1;
63
64 private static final String GAUGE3_NAME = "gauge3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070065 private static final String GAUGE3_FULL_NAME =
66 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070067 private static final int GAUGE3_VALUE = 123456789;
68
Ray Milkey49d67be2014-07-10 13:47:01 -070069 private final Gauge<Integer> gauge1 =
Ray Milkey26921af2014-06-30 16:27:40 -070070 new Gauge<Integer>() {
71 @Override
72 public Integer getValue() {
73 return GAUGE1_VALUE;
74 }
Ray Milkey49d67be2014-07-10 13:47:01 -070075 };
Ray Milkey26921af2014-06-30 16:27:40 -070076
Ray Milkey49d67be2014-07-10 13:47:01 -070077 private final Gauge<Integer> gauge2 =
Ray Milkey26921af2014-06-30 16:27:40 -070078 new Gauge<Integer>() {
79 @Override
80 public Integer getValue() {
81 return GAUGE2_VALUE;
82 }
Ray Milkey49d67be2014-07-10 13:47:01 -070083 };
Ray Milkey26921af2014-06-30 16:27:40 -070084
Ray Milkey49d67be2014-07-10 13:47:01 -070085 private final Gauge<Integer> gauge3 =
Ray Milkey26921af2014-06-30 16:27:40 -070086 new Gauge<Integer>() {
87 @Override
88 public Integer getValue() {
89 return GAUGE3_VALUE;
90 }
Ray Milkey49d67be2014-07-10 13:47:01 -070091 };
92
Ray Milkey40eb9c82014-07-18 10:28:11 -070093 /**
94 * Registers the test Gauge objects.
95 */
Ray Milkey49d67be2014-07-10 13:47:01 -070096 private void registerGauges() {
Ray Milkey71cd2c82014-07-16 15:02:33 -070097 OnosMetrics.registerMetric(COMPONENT,
98 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070099 GAUGE1_NAME,
100 gauge1);
101
Ray Milkey71cd2c82014-07-16 15:02:33 -0700102 OnosMetrics.registerMetric(COMPONENT,
103 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700104 GAUGE2_NAME,
105 gauge2);
106
Ray Milkey71cd2c82014-07-16 15:02:33 -0700107 OnosMetrics.registerMetric(COMPONENT,
108 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -0700109 GAUGE3_NAME,
110 gauge3);
111 }
Ray Milkey26921af2014-06-30 16:27:40 -0700112
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 Milkey40eb9c82014-07-18 10:28:11 -0700144 public void testGauges() throws JSONException {
Ray Milkey49d67be2014-07-10 13:47:01 -0700145 registerGauges();
Ray Milkey26921af2014-06-30 16:27:40 -0700146
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 Milkey49d67be2014-07-10 13:47:01 -0700163 checkGauge(gauge1Container, GAUGE1_FULL_NAME, gauge1);
Ray Milkey26921af2014-06-30 16:27:40 -0700164
165 // Check the values for gauge 2
166 final JSONObject gauge2Container = gauges.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700167 checkGauge(gauge2Container, GAUGE2_FULL_NAME, gauge2);
Ray Milkey26921af2014-06-30 16:27:40 -0700168
169 // Check the values for gauge 3
170 final JSONObject gauge3Container = gauges.getJSONObject(2);
Ray Milkey49d67be2014-07-10 13:47:01 -0700171 checkGauge(gauge3Container, GAUGE3_FULL_NAME, gauge3);
Ray Milkey26921af2014-06-30 16:27:40 -0700172 }
173
174}