Ray Milkey

add unit test for criterion codec

Change-Id: I0797c164dbbc03cd46a9d861e758f85cd27845d6
...@@ -17,9 +17,20 @@ package org.onosproject.codec.impl; ...@@ -17,9 +17,20 @@ package org.onosproject.codec.impl;
17 17
18 import java.util.EnumMap; 18 import java.util.EnumMap;
19 19
20 +import org.junit.Before;
20 import org.junit.Test; 21 import org.junit.Test;
22 +import org.onlab.packet.Ip6Address;
23 +import org.onlab.packet.IpPrefix;
24 +import org.onlab.packet.MacAddress;
25 +import org.onlab.packet.VlanId;
26 +import org.onosproject.codec.CodecContext;
27 +import org.onosproject.codec.JsonCodec;
28 +import org.onosproject.net.PortNumber;
29 +import org.onosproject.net.flow.criteria.Criteria;
21 import org.onosproject.net.flow.criteria.Criterion; 30 import org.onosproject.net.flow.criteria.Criterion;
22 31
32 +import com.fasterxml.jackson.databind.node.ObjectNode;
33 +
23 import static org.onlab.junit.TestUtils.getField; 34 import static org.onlab.junit.TestUtils.getField;
24 import static org.hamcrest.MatcherAssert.assertThat; 35 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.*; 36 import static org.hamcrest.Matchers.*;
...@@ -29,13 +40,32 @@ import static org.hamcrest.Matchers.*; ...@@ -29,13 +40,32 @@ import static org.hamcrest.Matchers.*;
29 */ 40 */
30 public class CriterionCodecTest { 41 public class CriterionCodecTest {
31 42
43 + CodecContext context;
44 + JsonCodec<Criterion> criterionCodec;
45 + final PortNumber port = PortNumber.portNumber(1);
46 + final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
47 + final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
48 + final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
49 +
50 + /**
51 + * Sets up for each test. Creates a context and fetches the criterion
52 + * codec.
53 + */
54 + @Before
55 + public void setUp() {
56 + context = new MockCodecContext();
57 + criterionCodec = context.codec(Criterion.class);
58 + assertThat(criterionCodec, notNullValue());
59 + }
60 +
61 +
32 /** 62 /**
33 * Checks that all criterion types are covered by the codec. 63 * Checks that all criterion types are covered by the codec.
34 */ 64 */
35 @Test 65 @Test
36 public void checkCriterionTypes() throws Exception { 66 public void checkCriterionTypes() throws Exception {
37 - CriterionCodec codec = new CriterionCodec(); 67 + EnumMap<Criterion.Type, Object> formatMap =
38 - EnumMap<Criterion.Type, Object> formatMap = getField(codec, "formatMap"); 68 + getField(criterionCodec, "formatMap");
39 assertThat(formatMap, notNullValue()); 69 assertThat(formatMap, notNullValue());
40 70
41 for (Criterion.Type type : Criterion.Type.values()) { 71 for (Criterion.Type type : Criterion.Type.values()) {
...@@ -43,4 +73,359 @@ public class CriterionCodecTest { ...@@ -43,4 +73,359 @@ public class CriterionCodecTest {
43 formatMap.get(type), notNullValue()); 73 formatMap.get(type), notNullValue());
44 } 74 }
45 } 75 }
76 +
77 + /**
78 + * Tests in port criterion.
79 + */
80 + @Test
81 + public void matchInPortTest() {
82 + Criterion criterion = Criteria.matchInPort(port);
83 + ObjectNode result = criterionCodec.encode(criterion, context);
84 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
85 + assertThat(result.get("port").asLong(), is(port.toLong()));
86 + }
87 +
88 + /**
89 + * Tests in physical port criterion.
90 + */
91 + @Test
92 + public void matchInPhyPortTest() {
93 + Criterion criterion = Criteria.matchInPhyPort(port);
94 + ObjectNode result = criterionCodec.encode(criterion, context);
95 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
96 + assertThat(result.get("port").asLong(), is(port.toLong()));
97 + }
98 +
99 + /**
100 + * Tests metadata criterion.
101 + */
102 + @Test
103 + public void matchMetadataTest() {
104 + Criterion criterion = Criteria.matchMetadata(0xabcdL);
105 + ObjectNode result = criterionCodec.encode(criterion, context);
106 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
107 + assertThat(result.get("metadata").asLong(), is(0xabcdL));
108 + }
109 +
110 + /**
111 + * Tests ethernet destination criterion.
112 + */
113 + @Test
114 + public void matchEthDstTest() {
115 + Criterion criterion = Criteria.matchEthDst(mac1);
116 + ObjectNode result = criterionCodec.encode(criterion, context);
117 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
118 + assertThat(result.get("mac").asText(), is(mac1.toString()));
119 + }
120 +
121 + /**
122 + * Tests ethernet source criterion.
123 + */
124 + @Test
125 + public void matchEthSrcTest() {
126 + Criterion criterion = Criteria.matchEthSrc(mac1);
127 + ObjectNode result = criterionCodec.encode(criterion, context);
128 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
129 + assertThat(result.get("mac").asText(), is(mac1.toString()));
130 + }
131 +
132 + /**
133 + * Tests ethernet type criterion.
134 + */
135 + @Test
136 + public void matchEthTypeTest() {
137 + Criterion criterion = Criteria.matchEthType((short) 3);
138 + ObjectNode result = criterionCodec.encode(criterion, context);
139 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
140 + assertThat(result.get("ethType").asInt(), is(3));
141 + }
142 +
143 + /**
144 + * Tests VLAN Id criterion.
145 + */
146 + @Test
147 + public void matchVlanIdTest() {
148 + Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
149 + ObjectNode result = criterionCodec.encode(criterion, context);
150 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
151 + assertThat((short) result.get("vlanId").asInt(), is(VlanId.ANY.toShort()));
152 + }
153 +
154 + /**
155 + * Tests VLAN PCP criterion.
156 + */
157 + @Test
158 + public void matchVlanPcpTest() {
159 + Criterion criterion = Criteria.matchVlanPcp((byte) 4);
160 + ObjectNode result = criterionCodec.encode(criterion, context);
161 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
162 + assertThat(result.get("priority").asInt(), is(4));
163 + }
164 +
165 + /**
166 + * Tests IP DSCP criterion.
167 + */
168 + @Test
169 + public void matchIPDscpTest() {
170 + Criterion criterion = Criteria.matchIPDscp((byte) 5);
171 + ObjectNode result = criterionCodec.encode(criterion, context);
172 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
173 + assertThat(result.get("ipDscp").asInt(), is(5));
174 + }
175 +
176 + /**
177 + * Tests IP ECN criterion.
178 + */
179 + @Test
180 + public void matchIPEcnTest() {
181 + Criterion criterion = Criteria.matchIPEcn((byte) 2);
182 + ObjectNode result = criterionCodec.encode(criterion, context);
183 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
184 + assertThat(result.get("ipEcn").asInt(), is(2));
185 + }
186 +
187 + /**
188 + * Tests IP protocol criterion.
189 + */
190 + @Test
191 + public void matchIPProtocolTest() {
192 + Criterion criterion = Criteria.matchIPProtocol((byte) 7);
193 + ObjectNode result = criterionCodec.encode(criterion, context);
194 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
195 + assertThat(result.get("protocol").asInt(), is(7));
196 + }
197 +
198 + /**
199 + * Tests IP source criterion.
200 + */
201 + @Test
202 + public void matchIPSrcTest() {
203 + Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
204 + ObjectNode result = criterionCodec.encode(criterion, context);
205 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
206 + assertThat(result.get("ip").asText(), is(ipPrefix4.toString()));
207 + }
208 +
209 + /**
210 + * Tests IP destination criterion.
211 + */
212 + @Test
213 + public void matchIPDstTest() {
214 + Criterion criterion = Criteria.matchIPDst(ipPrefix4);
215 + ObjectNode result = criterionCodec.encode(criterion, context);
216 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
217 + assertThat(result.get("ip").asText(), is(ipPrefix4.toString()));
218 + }
219 +
220 + /**
221 + * Tests source TCP port criterion.
222 + */
223 + @Test
224 + public void matchTcpSrcTest() {
225 + Criterion criterion = Criteria.matchTcpSrc((short) 22);
226 + ObjectNode result = criterionCodec.encode(criterion, context);
227 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
228 + assertThat(result.get("tcpPort").asInt(), is(22));
229 + }
230 +
231 + /**
232 + * Tests destination TCP port criterion.
233 + */
234 + @Test
235 + public void matchTcpDstTest() {
236 + Criterion criterion = Criteria.matchTcpDst((short) 22);
237 + ObjectNode result = criterionCodec.encode(criterion, context);
238 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
239 + assertThat(result.get("tcpPort").asInt(), is(22));
240 + }
241 +
242 + /**
243 + * Tests source UDP port criterion.
244 + */
245 + @Test
246 + public void matchUdpSrcTest() {
247 + Criterion criterion = Criteria.matchUdpSrc((short) 22);
248 + ObjectNode result = criterionCodec.encode(criterion, context);
249 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
250 + assertThat(result.get("udpPort").asInt(), is(22));
251 + }
252 +
253 + /**
254 + * Tests destination UDP criterion.
255 + */
256 + @Test
257 + public void matchUdpDstTest() {
258 + Criterion criterion = Criteria.matchUdpDst((short) 22);
259 + ObjectNode result = criterionCodec.encode(criterion, context);
260 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
261 + assertThat(result.get("udpPort").asInt(), is(22));
262 + }
263 +
264 + /**
265 + * Tests source SCTP criterion.
266 + */
267 + @Test
268 + public void matchSctpSrcTest() {
269 + Criterion criterion = Criteria.matchSctpSrc((short) 22);
270 + ObjectNode result = criterionCodec.encode(criterion, context);
271 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
272 + assertThat(result.get("sctpPort").asInt(), is(22));
273 + }
274 +
275 + /**
276 + * Tests destination SCTP criterion.
277 + */
278 + @Test
279 + public void matchSctpDstTest() {
280 + Criterion criterion = Criteria.matchSctpDst((short) 22);
281 + ObjectNode result = criterionCodec.encode(criterion, context);
282 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
283 + assertThat(result.get("sctpPort").asInt(), is(22));
284 + }
285 +
286 + /**
287 + * Tests ICMP type criterion.
288 + */
289 + @Test
290 + public void matchIcmpTypeTest() {
291 + Criterion criterion = Criteria.matchIcmpType((byte) 6);
292 + ObjectNode result = criterionCodec.encode(criterion, context);
293 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
294 + assertThat(result.get("icmpType").asInt(), is(6));
295 + }
296 +
297 + /**
298 + * Tests ICMP code criterion.
299 + */
300 + @Test
301 + public void matchIcmpCodeTest() {
302 + Criterion criterion = Criteria.matchIcmpCode((byte) 6);
303 + ObjectNode result = criterionCodec.encode(criterion, context);
304 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
305 + assertThat(result.get("icmpCode").asInt(), is(6));
306 + }
307 +
308 + /**
309 + * Tests IPv6 source criterion.
310 + */
311 + @Test
312 + public void matchIPv6SrcTest() {
313 + Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
314 + ObjectNode result = criterionCodec.encode(criterion, context);
315 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
316 + assertThat(result.get("ip").asText(), is(ipPrefix6.toString()));
317 + }
318 +
319 + /**
320 + * Tests IPv6 destination criterion.
321 + */
322 + @Test
323 + public void matchIPv6DstTest() {
324 + Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
325 + ObjectNode result = criterionCodec.encode(criterion, context);
326 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
327 + assertThat(result.get("ip").asText(), is(ipPrefix6.toString()));
328 + }
329 +
330 + /**
331 + * Tests IPv6 flow label criterion.
332 + */
333 + @Test
334 + public void matchIPv6FlowLabelTest() {
335 + Criterion criterion = Criteria.matchIPv6FlowLabel(7);
336 + ObjectNode result = criterionCodec.encode(criterion, context);
337 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
338 + assertThat(result.get("flowLabel").asInt(), is(7));
339 + }
340 +
341 + /**
342 + * Tests ICMP v6 type criterion.
343 + */
344 + @Test
345 + public void matchIcmpv6TypeTest() {
346 + Criterion criterion = Criteria.matchIcmpv6Type((byte) 15);
347 + ObjectNode result = criterionCodec.encode(criterion, context);
348 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
349 + assertThat(result.get("icmpv6Type").asInt(), is(15));
350 + }
351 +
352 + /**
353 + * Tests ICMP v6 code criterion.
354 + */
355 + @Test
356 + public void matchIcmpv6CodeTest() {
357 + Criterion criterion = Criteria.matchIcmpv6Code((byte) 17);
358 + ObjectNode result = criterionCodec.encode(criterion, context);
359 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
360 + assertThat(result.get("icmpv6Code").asInt(), is(17));
361 + }
362 +
363 + /**
364 + * Tests IPV6 target address criterion.
365 + */
366 + @Test
367 + public void matchIPv6NDTargetAddressTest() {
368 + Criterion criterion =
369 + Criteria.matchIPv6NDTargetAddress(
370 + Ip6Address.valueOf("1111:2222::"));
371 + ObjectNode result = criterionCodec.encode(criterion, context);
372 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
373 + assertThat(result.get("targetAddress").asText(), is("1111:2222::"));
374 + }
375 +
376 + /**
377 + * Tests IPV6 SLL criterion.
378 + */
379 + @Test
380 + public void matchIPv6NDSourceLinkLayerAddressTest() {
381 + Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
382 + ObjectNode result = criterionCodec.encode(criterion, context);
383 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
384 + assertThat(result.get("mac").asText(), is(mac1.toString()));
385 + }
386 +
387 + /**
388 + * Tests IPV6 TLL criterion.
389 + */
390 + @Test
391 + public void matchIPv6NDTargetLinkLayerAddressTest() {
392 + Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
393 + ObjectNode result = criterionCodec.encode(criterion, context);
394 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
395 + assertThat(result.get("mac").asText(), is(mac1.toString()));
396 + }
397 +
398 + /**
399 + * Tests MPLS label criterion.
400 + */
401 + @Test
402 + public void matchMplsLabelTest() {
403 + Criterion criterion = Criteria.matchMplsLabel(88);
404 + ObjectNode result = criterionCodec.encode(criterion, context);
405 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
406 + assertThat(result.get("label").asInt(), is(88));
407 + }
408 +
409 + /**
410 + * Tests lambda criterion.
411 + */
412 + @Test
413 + public void matchLambdaTest() {
414 + Criterion criterion = Criteria.matchLambda((short) 9);
415 + ObjectNode result = criterionCodec.encode(criterion, context);
416 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
417 + assertThat(result.get("lambda").asInt(), is(9));
418 + }
419 +
420 + /**
421 + * Tests optical signal type criterion.
422 + */
423 + @Test
424 + public void matchOpticalSignalTypeTest() {
425 + Criterion criterion = Criteria.matchOpticalSignalType((short) 11);
426 + ObjectNode result = criterionCodec.encode(criterion, context);
427 + assertThat(result.get("type").textValue(), is(criterion.type().toString()));
428 + assertThat(result.get("signalType").asInt(), is(11));
429 + }
430 +
46 } 431 }
......