blob: b6c5cab2d2029dcdf5c8b51fe05edd05214773f1 [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.intent.runtime.PathCalcRuntimeModule;
5import net.onrc.onos.core.metrics.OnosMetrics;
6import org.json.JSONArray;
7import org.json.JSONException;
8import org.json.JSONObject;
9import org.junit.After;
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13import org.powermock.core.classloader.annotations.PrepareForTest;
14import org.powermock.modules.junit4.PowerMockRunner;
15import org.restlet.resource.ClientResource;
16
17import static org.hamcrest.MatcherAssert.assertThat;
18import static org.hamcrest.Matchers.equalTo;
19import static org.hamcrest.Matchers.is;
20import static org.hamcrest.Matchers.notNullValue;
21
22/**
23 * Unit tests for REST APIs for Gauges Metrics.
24 */
25@RunWith(PowerMockRunner.class)
26@PrepareForTest(PathCalcRuntimeModule.class)
27public class TestRestMetricsGauges 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 data for Gauges
Ray Milkey71cd2c82014-07-16 15:02:33 -070051
52 private static final OnosMetrics.MetricsComponent COMPONENT =
53 OnosMetrics.registerComponent("MetricsUnitTests");
54 private static final OnosMetrics.MetricsFeature FEATURE =
55 COMPONENT.registerFeature("Gauges");
56
Ray Milkey26921af2014-06-30 16:27:40 -070057 private static final String GAUGE1_NAME = "gauge1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070058 private static final String GAUGE1_FULL_NAME =
59 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070060 private static final int GAUGE1_VALUE = 0;
61
62 private static final String GAUGE2_NAME = "gauge2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070063 private static final String GAUGE2_FULL_NAME =
64 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070065 private static final int GAUGE2_VALUE = -1;
66
67 private static final String GAUGE3_NAME = "gauge3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070068 private static final String GAUGE3_FULL_NAME =
69 OnosMetrics.generateName(COMPONENT, FEATURE, GAUGE3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070070 private static final int GAUGE3_VALUE = 123456789;
71
Ray Milkey49d67be2014-07-10 13:47:01 -070072 private final Gauge<Integer> gauge1 =
Ray Milkey26921af2014-06-30 16:27:40 -070073 new Gauge<Integer>() {
74 @Override
75 public Integer getValue() {
76 return GAUGE1_VALUE;
77 }
Ray Milkey49d67be2014-07-10 13:47:01 -070078 };
Ray Milkey26921af2014-06-30 16:27:40 -070079
Ray Milkey49d67be2014-07-10 13:47:01 -070080 private final Gauge<Integer> gauge2 =
Ray Milkey26921af2014-06-30 16:27:40 -070081 new Gauge<Integer>() {
82 @Override
83 public Integer getValue() {
84 return GAUGE2_VALUE;
85 }
Ray Milkey49d67be2014-07-10 13:47:01 -070086 };
Ray Milkey26921af2014-06-30 16:27:40 -070087
Ray Milkey49d67be2014-07-10 13:47:01 -070088 private final Gauge<Integer> gauge3 =
Ray Milkey26921af2014-06-30 16:27:40 -070089 new Gauge<Integer>() {
90 @Override
91 public Integer getValue() {
92 return GAUGE3_VALUE;
93 }
Ray Milkey49d67be2014-07-10 13:47:01 -070094 };
95
96 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
144 public void testGauges() throws Exception {
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}