blob: c3fd01c1783408263476dd916c6268580ab2e2c5 [file] [log] [blame]
Ray Milkey26921af2014-06-30 16:27:40 -07001package net.onrc.onos.api.rest;
2
3import com.codahale.metrics.Histogram;
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 java.util.Arrays;
16
17import static org.hamcrest.MatcherAssert.assertThat;
18import static org.hamcrest.Matchers.both;
19import static org.hamcrest.Matchers.equalTo;
20import static org.hamcrest.Matchers.greaterThanOrEqualTo;
21import static org.hamcrest.Matchers.is;
22import static org.hamcrest.Matchers.lessThanOrEqualTo;
23import static org.hamcrest.Matchers.notNullValue;
24
25/**
26 * Unit tests for REST APIs for Histogram Metrics.
27 */
Ray Milkey26921af2014-06-30 16:27:40 -070028@RunWith(PowerMockRunner.class)
Ray Milkey26921af2014-06-30 16:27:40 -070029public class TestRestMetricsHistograms extends TestRestMetrics {
30
31 /**
32 * Create the web server and mocks required for
33 * all of the tests.
34 */
35 @Before
36 @SuppressWarnings("ununsed")
37 public void beforeTest() {
38 setRestPort(generateRandomPort());
39 setUp();
40 }
41
42 /**
43 * Remove anything that will interfere with the next test running correctly.
44 * Shuts down the test REST web server and removes the mocks.
45 */
46 @After
47 @SuppressWarnings("unused")
48 public void afterTest() {
49 tearDown();
50 }
51
52 // Test data for Histograms
53
Ray Milkey71cd2c82014-07-16 15:02:33 -070054 private static final OnosMetrics.MetricsComponent COMPONENT =
55 OnosMetrics.registerComponent("MetricsUnitTests");
56 private static final OnosMetrics.MetricsFeature FEATURE =
57 COMPONENT.registerFeature("Histograms");
58
Ray Milkey26921af2014-06-30 16:27:40 -070059 private static final String HISTOGRAM1_NAME = "HISTOGRAM1";
Ray Milkey49d67be2014-07-10 13:47:01 -070060 private static final String HISTOGRAM1_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070061 OnosMetrics.generateName(COMPONENT, FEATURE, HISTOGRAM1_NAME);
62
Ray Milkey26921af2014-06-30 16:27:40 -070063 private static final int[] HISTOGRAM1_VALUES =
64 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
65
66 private static final String HISTOGRAM2_NAME = "HISTOGRAM2";
Ray Milkey49d67be2014-07-10 13:47:01 -070067 private static final String HISTOGRAM2_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070068 OnosMetrics.generateName(COMPONENT, FEATURE, HISTOGRAM2_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070069 private static final int[] HISTOGRAM2_VALUES =
70 {100, 100, 100, 100, 100, 100, 100};
71
72 private static final String HISTOGRAM3_NAME = "HISTOGRAM3";
Ray Milkey49d67be2014-07-10 13:47:01 -070073 private static final String HISTOGRAM3_FULL_NAME =
Ray Milkey71cd2c82014-07-16 15:02:33 -070074 OnosMetrics.generateName(COMPONENT, FEATURE, HISTOGRAM3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070075 private static final int[] HISTOGRAM3_VALUES =
76 {555};
77
78 private final Histogram histogram1 =
Ray Milkey49d67be2014-07-10 13:47:01 -070079 OnosMetrics.createHistogram(
Ray Milkey71cd2c82014-07-16 15:02:33 -070080 COMPONENT,
81 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070082 HISTOGRAM1_NAME);
83
Ray Milkey26921af2014-06-30 16:27:40 -070084 private final Histogram histogram2 =
Ray Milkey49d67be2014-07-10 13:47:01 -070085 OnosMetrics.createHistogram(
Ray Milkey71cd2c82014-07-16 15:02:33 -070086 COMPONENT,
87 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070088 HISTOGRAM2_NAME);
89
Ray Milkey26921af2014-06-30 16:27:40 -070090 private final Histogram histogram3 =
Ray Milkey49d67be2014-07-10 13:47:01 -070091 OnosMetrics.createHistogram(
Ray Milkey71cd2c82014-07-16 15:02:33 -070092 COMPONENT,
93 FEATURE,
Ray Milkey49d67be2014-07-10 13:47:01 -070094 HISTOGRAM3_NAME);
Ray Milkey26921af2014-06-30 16:27:40 -070095
96 /**
97 * Add each int in an array to a Histogram.
98 *
99 * @param histogram Histogram object to update
100 * @param values list of values to add to the Histogram
101 */
102 private void updateHistogramFromArray(final Histogram histogram,
103 final int[] values) {
104 for (final int value : values) {
105 histogram.update(value);
106 }
107 }
108
109 /**
110 * Initialize all the Histograms.
111 */
112 private void fillHistograms() {
113 updateHistogramFromArray(histogram1, HISTOGRAM1_VALUES);
114 updateHistogramFromArray(histogram2, HISTOGRAM2_VALUES);
115 updateHistogramFromArray(histogram3, HISTOGRAM3_VALUES);
116 }
117
118 /**
119 * Check that a JSON object representing a histogram contains the correct
120 * data.
121 *
122 * @param histogramContainer JSON object for the Histogram
123 * @param name the name of the Histogram
124 * @param values the array of expected values in the histogram
125 * @throws JSONException if any of the JSON processing generates an error
126 */
127 private void checkHistogram(final JSONObject histogramContainer,
128 final String name,
129 final int[] values) throws JSONException {
130
131 // Check that the name is correct
132 final String histogramName = histogramContainer.getString("name");
133 assertThat(histogramName, is(notNullValue()));
134 assertThat(histogramName, is(equalTo(name)));
135
136 // Make sure a histogram is present
137 final JSONObject histogramObject = histogramContainer.getJSONObject("histogram");
138 assertThat(histogramObject, is(notNullValue()));
139
140 // The histogram count should equal the length of the array used to
141 // initialize it.
142 final int histogramCount = histogramObject.getInt("count");
143 assertThat(histogramCount, is(equalTo(values.length)));
144
145 final int[] sortedValues = Arrays.copyOf(values, values.length);
146 Arrays.sort(sortedValues);
147
148 // max should be the largest value from the array
149 final int max = histogramObject.getInt("max");
150 assertThat(max, is(equalTo(sortedValues[sortedValues.length - 1])));
151
152 // min should be the smallest value from the array
153 final int min = histogramObject.getInt("min");
154 assertThat(min, is(equalTo(sortedValues[0])));
155
156 // Each of the probability values should be between the min and the max
157 // value
158 final double p999 = histogramObject.getDouble("p999");
159 assertThat((int) p999,
160 is(both(greaterThanOrEqualTo(min)).and(lessThanOrEqualTo(max))));
161
162 final double p99 = histogramObject.getDouble("p99");
163 assertThat((int) p99,
164 is(both(greaterThanOrEqualTo(min)).and(lessThanOrEqualTo(max))));
165
166 final double p98 = histogramObject.getDouble("p98");
167 assertThat((int) p98,
168 is(both(greaterThanOrEqualTo(min)).and(lessThanOrEqualTo(max))));
169
170 final double p95 = histogramObject.getDouble("p95");
171 assertThat((int) p95,
172 is(both(greaterThanOrEqualTo(min)).and(lessThanOrEqualTo(max))));
173
174 final double p75 = histogramObject.getDouble("p75");
175 assertThat((int) p75,
176 is(both(greaterThanOrEqualTo(min)).and(lessThanOrEqualTo(max))));
177
178 final double p50 = histogramObject.getDouble("p50");
179 assertThat((int) p50,
180 is(both(greaterThanOrEqualTo(min)).and(lessThanOrEqualTo(max))));
181
182 }
183
184 /**
185 * Unit test for REST APIs for Histogram Metrics.
186 *
187 * @throws JSONException if any JSON processing causes an error
188 */
189 @Test
190 public void testHistograms() throws JSONException {
191
192 fillHistograms();
193
194 // Read the metrics from the REST API for the test data
195 final ClientResource client = new ClientResource(getBaseRestMetricsUrl());
196
197 final JSONObject metrics = getJSONObject(client);
198 assertThat(metrics.length(), is(equalTo(5)));
199
200 // There should be 3 histograms
201 final JSONArray histograms = metrics.getJSONArray("histograms");
202 assertThat(histograms, is(notNullValue()));
203 assertThat(histograms.length(), is(3));
204
205 // There should be no timers, gauges, meters or counters
206 checkEmptyLists(metrics, "timers", "gauges", "meters", "counters");
207
208 // Check the values for histogram 1
209 final JSONObject histogram1Container = histograms.getJSONObject(0);
Ray Milkey49d67be2014-07-10 13:47:01 -0700210 checkHistogram(histogram1Container, HISTOGRAM1_FULL_NAME, HISTOGRAM1_VALUES);
Ray Milkey26921af2014-06-30 16:27:40 -0700211
212 // Check the values for histogram 2
213 final JSONObject histogram2Container = histograms.getJSONObject(1);
Ray Milkey49d67be2014-07-10 13:47:01 -0700214 checkHistogram(histogram2Container, HISTOGRAM2_FULL_NAME, HISTOGRAM2_VALUES);
Ray Milkey26921af2014-06-30 16:27:40 -0700215
216 // Check the values for histogram 3
217 final JSONObject histogram3Container = histograms.getJSONObject(2);
Ray Milkey49d67be2014-07-10 13:47:01 -0700218 checkHistogram(histogram3Container, HISTOGRAM3_FULL_NAME, HISTOGRAM3_VALUES);
Ray Milkey26921af2014-06-30 16:27:40 -0700219
220 }
221
222}
223