Committed by
Ray Milkey
[ONOS-3601] Add unit test for MetricCodec and correct some typos
Change-Id: I72317b3d2ea8c428bcc24aa215ae97c098a38305
Showing
5 changed files
with
254 additions
and
4 deletions
... | @@ -26,6 +26,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode; | ... | @@ -26,6 +26,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
26 | import org.onosproject.codec.CodecContext; | 26 | import org.onosproject.codec.CodecContext; |
27 | import org.onosproject.codec.JsonCodec; | 27 | import org.onosproject.codec.JsonCodec; |
28 | 28 | ||
29 | +import java.util.concurrent.TimeUnit; | ||
30 | + | ||
29 | import static com.google.common.base.Preconditions.checkNotNull; | 31 | import static com.google.common.base.Preconditions.checkNotNull; |
30 | 32 | ||
31 | /** | 33 | /** |
... | @@ -96,6 +98,6 @@ public class MetricCodec extends JsonCodec<Metric> { | ... | @@ -96,6 +98,6 @@ public class MetricCodec extends JsonCodec<Metric> { |
96 | } | 98 | } |
97 | 99 | ||
98 | private double nanoToMs(double nano) { | 100 | private double nanoToMs(double nano) { |
99 | - return nano / 1_000_000D; | 101 | + return TimeUnit.MILLISECONDS.convert((long) nano, TimeUnit.NANOSECONDS); |
100 | } | 102 | } |
101 | } | 103 | } | ... | ... |
... | @@ -51,7 +51,7 @@ public class MeterCodecTest { | ... | @@ -51,7 +51,7 @@ public class MeterCodecTest { |
51 | final CoreService mockCoreService = createMock(CoreService.class); | 51 | final CoreService mockCoreService = createMock(CoreService.class); |
52 | 52 | ||
53 | /** | 53 | /** |
54 | - * Sets up for each test. Creates a context and fetches the flow rule | 54 | + * Sets up for each test. Creates a context and fetches the meter |
55 | * codec. | 55 | * codec. |
56 | */ | 56 | */ |
57 | @Before | 57 | @Before | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import com.codahale.metrics.Counter; | ||
19 | +import com.codahale.metrics.Meter; | ||
20 | +import com.codahale.metrics.Metric; | ||
21 | +import com.codahale.metrics.Timer; | ||
22 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
23 | +import org.junit.Before; | ||
24 | +import org.junit.Test; | ||
25 | +import org.onosproject.codec.JsonCodec; | ||
26 | +import org.onosproject.core.CoreService; | ||
27 | + | ||
28 | +import java.util.concurrent.TimeUnit; | ||
29 | + | ||
30 | +import static org.easymock.EasyMock.createMock; | ||
31 | +import static org.easymock.EasyMock.replay; | ||
32 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
33 | +import static org.hamcrest.Matchers.notNullValue; | ||
34 | +import static org.onosproject.codec.impl.MetricJsonMatcher.matchesMetric; | ||
35 | + | ||
36 | +/** | ||
37 | + * Unit tests for Metric codec. | ||
38 | + */ | ||
39 | +public class MetricCodecTest { | ||
40 | + | ||
41 | + MockCodecContext context; | ||
42 | + JsonCodec<Metric> metricCodec; | ||
43 | + final CoreService mockCoreService = createMock(CoreService.class); | ||
44 | + | ||
45 | + /** | ||
46 | + * Sets up for each test. Creates a context and fetches the metric codec. | ||
47 | + */ | ||
48 | + @Before | ||
49 | + public void setUp() { | ||
50 | + context = new MockCodecContext(); | ||
51 | + metricCodec = context.codec(Metric.class); | ||
52 | + assertThat(metricCodec, notNullValue()); | ||
53 | + | ||
54 | + replay(mockCoreService); | ||
55 | + context.registerService(CoreService.class, mockCoreService); | ||
56 | + } | ||
57 | + | ||
58 | + /** | ||
59 | + * Tests encoding of a Metric object. | ||
60 | + */ | ||
61 | + @Test | ||
62 | + public void testMetricEncode() { | ||
63 | + Counter counter = new Counter(); | ||
64 | + Meter meter = new Meter(); | ||
65 | + Timer timer = new Timer(); | ||
66 | + | ||
67 | + counter.inc(); | ||
68 | + meter.mark(); | ||
69 | + timer.update(1, TimeUnit.MILLISECONDS); | ||
70 | + | ||
71 | + ObjectNode counterJson = metricCodec.encode(counter, context); | ||
72 | + assertThat(counterJson.get("counter"), matchesMetric(counter)); | ||
73 | + | ||
74 | + ObjectNode meterJson = metricCodec.encode(meter, context); | ||
75 | + assertThat(meterJson.get("meter"), matchesMetric(meter)); | ||
76 | + | ||
77 | + ObjectNode timerJson = metricCodec.encode(timer, context); | ||
78 | + assertThat(timerJson.get("timer"), matchesMetric(timer)); | ||
79 | + } | ||
80 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import com.codahale.metrics.Counter; | ||
19 | +import com.codahale.metrics.Meter; | ||
20 | +import com.codahale.metrics.Metric; | ||
21 | +import com.codahale.metrics.Timer; | ||
22 | +import com.fasterxml.jackson.databind.JsonNode; | ||
23 | +import org.hamcrest.Description; | ||
24 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
25 | + | ||
26 | +import java.util.concurrent.TimeUnit; | ||
27 | + | ||
28 | +/** | ||
29 | + * Hamcrest matcher for metrics. | ||
30 | + */ | ||
31 | +public final class MetricJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { | ||
32 | + | ||
33 | + private final Metric metric; | ||
34 | + | ||
35 | + private MetricJsonMatcher(Metric metric) { | ||
36 | + this.metric = metric; | ||
37 | + } | ||
38 | + | ||
39 | + @Override | ||
40 | + protected boolean matchesSafely(JsonNode jsonMetric, Description description) { | ||
41 | + | ||
42 | + // check counter | ||
43 | + if (metric instanceof Counter) { | ||
44 | + Counter counter = (Counter) metric; | ||
45 | + long jsonCounter = jsonMetric.get("counter").asLong(); | ||
46 | + long counterVal = counter.getCount(); | ||
47 | + if (jsonCounter != counterVal) { | ||
48 | + description.appendText("counter was " + jsonCounter); | ||
49 | + return false; | ||
50 | + } | ||
51 | + } | ||
52 | + | ||
53 | + // check meter | ||
54 | + if (metric instanceof Meter) { | ||
55 | + Meter meter = (Meter) metric; | ||
56 | + | ||
57 | + long jsonCounter = jsonMetric.get("counter").asLong(); | ||
58 | + long counterVal = meter.getCount(); | ||
59 | + if (jsonCounter != counterVal) { | ||
60 | + description.appendText("counter was " + jsonCounter); | ||
61 | + return false; | ||
62 | + } | ||
63 | + | ||
64 | + double jsonOneMinRate = jsonMetric.get("1_min_rate").asDouble(); | ||
65 | + double oneMinRate = meter.getOneMinuteRate(); | ||
66 | + if (jsonOneMinRate != oneMinRate) { | ||
67 | + description.appendText("one minute rate was " + jsonOneMinRate); | ||
68 | + return false; | ||
69 | + } | ||
70 | + | ||
71 | + double jsonFiveMinRate = jsonMetric.get("5_min_rate").asDouble(); | ||
72 | + double fiveMinRate = meter.getFiveMinuteRate(); | ||
73 | + if (jsonFiveMinRate != fiveMinRate) { | ||
74 | + description.appendText("five minute rate was " + jsonFiveMinRate); | ||
75 | + return false; | ||
76 | + } | ||
77 | + | ||
78 | + double jsonFiftMinRate = jsonMetric.get("15_min_rate").asDouble(); | ||
79 | + double fiftMinRate = meter.getFifteenMinuteRate(); | ||
80 | + if (jsonFiftMinRate != fiftMinRate) { | ||
81 | + description.appendText("fifteen minute rate was " + jsonFiftMinRate); | ||
82 | + return false; | ||
83 | + } | ||
84 | + } | ||
85 | + | ||
86 | + // check timer | ||
87 | + if (metric instanceof Timer) { | ||
88 | + Timer timer = (Timer) metric; | ||
89 | + | ||
90 | + long jsonCounter = jsonMetric.get("counter").asLong(); | ||
91 | + long counterVal = timer.getCount(); | ||
92 | + if (jsonCounter != counterVal) { | ||
93 | + description.appendText("counter was " + jsonCounter); | ||
94 | + return false; | ||
95 | + } | ||
96 | + | ||
97 | + double jsonOneMinRate = jsonMetric.get("1_min_rate").asDouble(); | ||
98 | + double oneMinRate = timer.getOneMinuteRate(); | ||
99 | + if (jsonOneMinRate != oneMinRate) { | ||
100 | + description.appendText("one minute rate was " + jsonOneMinRate); | ||
101 | + return false; | ||
102 | + } | ||
103 | + | ||
104 | + double jsonFiveMinRate = jsonMetric.get("5_min_rate").asDouble(); | ||
105 | + double fiveMinRate = timer.getFiveMinuteRate(); | ||
106 | + if (jsonFiveMinRate != fiveMinRate) { | ||
107 | + description.appendText("five minute rate was " + jsonFiveMinRate); | ||
108 | + return false; | ||
109 | + } | ||
110 | + | ||
111 | + double jsonFiftMinRate = jsonMetric.get("15_min_rate").asDouble(); | ||
112 | + double fiftMinRate = timer.getFifteenMinuteRate(); | ||
113 | + if (jsonFiftMinRate != fiftMinRate) { | ||
114 | + description.appendText("fifteen minute rate was " + jsonFiftMinRate); | ||
115 | + return false; | ||
116 | + } | ||
117 | + | ||
118 | + double jsonMean = jsonMetric.get("mean").asDouble(); | ||
119 | + double mean = nanoToMs(timer.getSnapshot().getMean()); | ||
120 | + if (jsonMean != mean) { | ||
121 | + description.appendText("mean was " + jsonMean); | ||
122 | + return false; | ||
123 | + } | ||
124 | + | ||
125 | + double jsonMin = jsonMetric.get("min").asDouble(); | ||
126 | + double min = nanoToMs(timer.getSnapshot().getMin()); | ||
127 | + if (jsonMin != min) { | ||
128 | + description.appendText("json min was " + jsonMin); | ||
129 | + return false; | ||
130 | + } | ||
131 | + | ||
132 | + double jsonMax = jsonMetric.get("max").asDouble(); | ||
133 | + double max = nanoToMs(timer.getSnapshot().getMax()); | ||
134 | + if (jsonMax != max) { | ||
135 | + description.appendText("max was " + jsonMax); | ||
136 | + return false; | ||
137 | + } | ||
138 | + | ||
139 | + double jsonStdDev = jsonMetric.get("stddev").asDouble(); | ||
140 | + double stdDev = nanoToMs(timer.getSnapshot().getStdDev()); | ||
141 | + if (jsonStdDev != stdDev) { | ||
142 | + description.appendText("stddev was " + jsonStdDev); | ||
143 | + return false; | ||
144 | + } | ||
145 | + } | ||
146 | + | ||
147 | + return true; | ||
148 | + } | ||
149 | + | ||
150 | + @Override | ||
151 | + public void describeTo(Description description) { | ||
152 | + description.appendText(metric.toString()); | ||
153 | + } | ||
154 | + | ||
155 | + /** | ||
156 | + * Factory to allocate a metric matcher. | ||
157 | + * | ||
158 | + * @param metric metric object we are looking for | ||
159 | + * @return matcher | ||
160 | + */ | ||
161 | + public static MetricJsonMatcher matchesMetric(Metric metric) { | ||
162 | + return new MetricJsonMatcher(metric); | ||
163 | + } | ||
164 | + | ||
165 | + private double nanoToMs(double nano) { | ||
166 | + return TimeUnit.MILLISECONDS.convert((long) nano, TimeUnit.NANOSECONDS); | ||
167 | + } | ||
168 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -122,8 +122,8 @@ public class MetricsResourceTest extends ResourceTest { | ... | @@ -122,8 +122,8 @@ public class MetricsResourceTest extends ResourceTest { |
122 | } | 122 | } |
123 | 123 | ||
124 | /** | 124 | /** |
125 | - * Hamcrest matcher to check that an device representation in JSON matches | 125 | + * Hamcrest matcher to check that a metric representation in JSON matches |
126 | - * the actual device. | 126 | + * the actual metric. |
127 | */ | 127 | */ |
128 | public static class MetricJsonMatcher extends TypeSafeMatcher<JsonObject> { | 128 | public static class MetricJsonMatcher extends TypeSafeMatcher<JsonObject> { |
129 | private final Metric metric; | 129 | private final Metric metric; | ... | ... |
-
Please register or login to post a comment