blob: 148fd8590c4b719c6de4cc55b7f88df676c2a9fa [file] [log] [blame]
Ray Milkey26921af2014-06-30 16:27:40 -07001package net.onrc.onos.api.rest;
2
3import com.codahale.metrics.Counter;
4import 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 Counter Metrics.
24 */
25@RunWith(PowerMockRunner.class)
26@PrepareForTest(PathCalcRuntimeModule.class)
27public class TestRestMetricsCounters 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 Counter data objects
Ray Milkey71cd2c82014-07-16 15:02:33 -070051 private static final OnosMetrics.MetricsComponent COMPONENT =
52 OnosMetrics.registerComponent("MetricsUnitTests");
53 private static final OnosMetrics.MetricsFeature FEATURE =
54 COMPONENT.registerFeature("Counters");
55
Ray Milkey26921af2014-06-30 16:27:40 -070056 private static final String COUNTER1_NAME = "COUNTER1";
Ray Milkey71cd2c82014-07-16 15:02:33 -070057 private static final String COUNTER1_FULL_NAME = OnosMetrics.generateName(COMPONENT,
58 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070059 COUNTER1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070060 private static final int COUNTER1_COUNT = 0;
61
62 private static final String COUNTER2_NAME = "COUNTER2";
Ray Milkey71cd2c82014-07-16 15:02:33 -070063 private static final String COUNTER2_FULL_NAME = OnosMetrics.generateName(COMPONENT,
64 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070065 COUNTER2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070066 private static final int COUNTER2_COUNT = -1;
67
68 private static final String COUNTER3_NAME = "COUNTER3";
Ray Milkey71cd2c82014-07-16 15:02:33 -070069 private static final String COUNTER3_FULL_NAME = OnosMetrics.generateName(COMPONENT,
70 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070071 COUNTER3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070072 private static final int COUNTER3_COUNT = 5;
73
74 private final Counter counter1 =
Ray Milkey71cd2c82014-07-16 15:02:33 -070075 OnosMetrics.createCounter(COMPONENT,
76 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070077 COUNTER1_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070078 private final Counter counter2 =
Ray Milkey71cd2c82014-07-16 15:02:33 -070079 OnosMetrics.createCounter(COMPONENT,
80 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070081 COUNTER2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070082 private final Counter counter3 =
Ray Milkey71cd2c82014-07-16 15:02:33 -070083 OnosMetrics.createCounter(COMPONENT,
84 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070085 COUNTER3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070086
87 /**
88 * Create some test data for the tests.
89 */
90 private void fillCounters() {
91 counter1.inc(COUNTER1_COUNT);
92 counter2.inc(COUNTER2_COUNT);
93 counter3.inc(COUNTER3_COUNT);
94 }
95
96 /**
97 * Check that a Counter object has the right contents.
98 *
99 * @param counterContainer JSON for the Counter
100 * @param name name of the Counter
101 * @param count expected count for the Counter
102 * @throws JSONException if any of the JSON fetches fail
103 */
104 private void checkCounter(final JSONObject counterContainer,
105 final String name,
106 final int count) throws JSONException {
107 final String counterName = counterContainer.getString("name");
108 assertThat(counterName, is(notNullValue()));
109 assertThat(counterName, is(equalTo(name)));
110
111 final JSONObject counterObject = counterContainer.getJSONObject("counter");
112 assertThat(counterObject, is(notNullValue()));
113
114 final int counterValue = counterObject.getInt("count");
115 assertThat(counterValue, is(equalTo(count)));
116 }
117
118 /**
119 * Test the REST APIs for Metrics Counter objects.
120 *
121 * @throws JSONException
122 */
123 @Test
124 public void testCounters() throws JSONException {
125
126 fillCounters();
127
128 // Read the metrics from the REST API for the test data
129 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
130
131 final JSONObject metrics = getJSONObject(client);
132 assertThat(metrics.length(), is(equalTo(5)));
133
134 // There should be 3 counters
135 final JSONArray counters = metrics.getJSONArray("counters");
136 assertThat(counters, is(notNullValue()));
137 assertThat(counters.length(), is(3));
138
139 // There should be no timers, gauges, meters or histograms
140 checkEmptyLists(metrics, "timers", "gauges", "meters", "histograms");
141
142 // Check the values for counter 1
143 final JSONObject counter1Container = counters.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700144 checkCounter(counter1Container, COUNTER1_FULL_NAME, COUNTER1_COUNT);
Ray Milkey26921af2014-06-30 16:27:40 -0700145
146 // Check the values for counter 1
147 final JSONObject counter2Container = counters.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700148 checkCounter(counter2Container, COUNTER2_FULL_NAME, COUNTER2_COUNT);
Ray Milkey26921af2014-06-30 16:27:40 -0700149
150 // Check the values for counter 1
151 final JSONObject counter3Container = counters.getJSONObject(2);
Ray Milkey49d67be2014-07-10 13:47:01 -0700152 checkCounter(counter3Container, COUNTER3_FULL_NAME, COUNTER3_COUNT);
Ray Milkey26921af2014-06-30 16:27:40 -0700153
154 }
155
156}