Ray Milkey

Refactor criterion codec test and matcher

- codec test now uses the json matcher rather than comparing itself
- refactored the codec matcher to be less ugly
- fixed a bug in how eth types are compared

Change-Id: Iaf9980c52e98518405a23d2cdde7ccd7d4afaa9b
...@@ -34,6 +34,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -34,6 +34,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
34 import static org.onlab.junit.TestUtils.getField; 34 import static org.onlab.junit.TestUtils.getField;
35 import static org.hamcrest.MatcherAssert.assertThat; 35 import static org.hamcrest.MatcherAssert.assertThat;
36 import static org.hamcrest.Matchers.*; 36 import static org.hamcrest.Matchers.*;
37 +import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
37 38
38 /** 39 /**
39 * Unit tests for criterion codec. 40 * Unit tests for criterion codec.
...@@ -81,8 +82,7 @@ public class CriterionCodecTest { ...@@ -81,8 +82,7 @@ public class CriterionCodecTest {
81 public void matchInPortTest() { 82 public void matchInPortTest() {
82 Criterion criterion = Criteria.matchInPort(port); 83 Criterion criterion = Criteria.matchInPort(port);
83 ObjectNode result = criterionCodec.encode(criterion, context); 84 ObjectNode result = criterionCodec.encode(criterion, context);
84 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 85 + assertThat(result, matchesCriterion(criterion));
85 - assertThat(result.get("port").asLong(), is(port.toLong()));
86 } 86 }
87 87
88 /** 88 /**
...@@ -92,8 +92,7 @@ public class CriterionCodecTest { ...@@ -92,8 +92,7 @@ public class CriterionCodecTest {
92 public void matchInPhyPortTest() { 92 public void matchInPhyPortTest() {
93 Criterion criterion = Criteria.matchInPhyPort(port); 93 Criterion criterion = Criteria.matchInPhyPort(port);
94 ObjectNode result = criterionCodec.encode(criterion, context); 94 ObjectNode result = criterionCodec.encode(criterion, context);
95 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 95 + assertThat(result, matchesCriterion(criterion));
96 - assertThat(result.get("port").asLong(), is(port.toLong()));
97 } 96 }
98 97
99 /** 98 /**
...@@ -103,8 +102,7 @@ public class CriterionCodecTest { ...@@ -103,8 +102,7 @@ public class CriterionCodecTest {
103 public void matchMetadataTest() { 102 public void matchMetadataTest() {
104 Criterion criterion = Criteria.matchMetadata(0xabcdL); 103 Criterion criterion = Criteria.matchMetadata(0xabcdL);
105 ObjectNode result = criterionCodec.encode(criterion, context); 104 ObjectNode result = criterionCodec.encode(criterion, context);
106 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 105 + assertThat(result, matchesCriterion(criterion));
107 - assertThat(result.get("metadata").asLong(), is(0xabcdL));
108 } 106 }
109 107
110 /** 108 /**
...@@ -114,8 +112,7 @@ public class CriterionCodecTest { ...@@ -114,8 +112,7 @@ public class CriterionCodecTest {
114 public void matchEthDstTest() { 112 public void matchEthDstTest() {
115 Criterion criterion = Criteria.matchEthDst(mac1); 113 Criterion criterion = Criteria.matchEthDst(mac1);
116 ObjectNode result = criterionCodec.encode(criterion, context); 114 ObjectNode result = criterionCodec.encode(criterion, context);
117 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 115 + assertThat(result, matchesCriterion(criterion));
118 - assertThat(result.get("mac").asText(), is(mac1.toString()));
119 } 116 }
120 117
121 /** 118 /**
...@@ -125,8 +122,7 @@ public class CriterionCodecTest { ...@@ -125,8 +122,7 @@ public class CriterionCodecTest {
125 public void matchEthSrcTest() { 122 public void matchEthSrcTest() {
126 Criterion criterion = Criteria.matchEthSrc(mac1); 123 Criterion criterion = Criteria.matchEthSrc(mac1);
127 ObjectNode result = criterionCodec.encode(criterion, context); 124 ObjectNode result = criterionCodec.encode(criterion, context);
128 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 125 + assertThat(result, matchesCriterion(criterion));
129 - assertThat(result.get("mac").asText(), is(mac1.toString()));
130 } 126 }
131 127
132 /** 128 /**
...@@ -136,8 +132,7 @@ public class CriterionCodecTest { ...@@ -136,8 +132,7 @@ public class CriterionCodecTest {
136 public void matchEthTypeTest() { 132 public void matchEthTypeTest() {
137 Criterion criterion = Criteria.matchEthType((short) 0x8844); 133 Criterion criterion = Criteria.matchEthType((short) 0x8844);
138 ObjectNode result = criterionCodec.encode(criterion, context); 134 ObjectNode result = criterionCodec.encode(criterion, context);
139 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 135 + assertThat(result, matchesCriterion(criterion));
140 - assertThat(result.get("ethType").asInt(), is(0x8844));
141 } 136 }
142 137
143 /** 138 /**
...@@ -147,8 +142,7 @@ public class CriterionCodecTest { ...@@ -147,8 +142,7 @@ public class CriterionCodecTest {
147 public void matchVlanIdTest() { 142 public void matchVlanIdTest() {
148 Criterion criterion = Criteria.matchVlanId(VlanId.ANY); 143 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
149 ObjectNode result = criterionCodec.encode(criterion, context); 144 ObjectNode result = criterionCodec.encode(criterion, context);
150 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 145 + assertThat(result, matchesCriterion(criterion));
151 - assertThat((short) result.get("vlanId").asInt(), is(VlanId.ANY.toShort()));
152 } 146 }
153 147
154 /** 148 /**
...@@ -158,8 +152,7 @@ public class CriterionCodecTest { ...@@ -158,8 +152,7 @@ public class CriterionCodecTest {
158 public void matchVlanPcpTest() { 152 public void matchVlanPcpTest() {
159 Criterion criterion = Criteria.matchVlanPcp((byte) 7); 153 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
160 ObjectNode result = criterionCodec.encode(criterion, context); 154 ObjectNode result = criterionCodec.encode(criterion, context);
161 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 155 + assertThat(result, matchesCriterion(criterion));
162 - assertThat(result.get("priority").asInt(), is(7));
163 } 156 }
164 157
165 /** 158 /**
...@@ -169,8 +162,7 @@ public class CriterionCodecTest { ...@@ -169,8 +162,7 @@ public class CriterionCodecTest {
169 public void matchIPDscpTest() { 162 public void matchIPDscpTest() {
170 Criterion criterion = Criteria.matchIPDscp((byte) 63); 163 Criterion criterion = Criteria.matchIPDscp((byte) 63);
171 ObjectNode result = criterionCodec.encode(criterion, context); 164 ObjectNode result = criterionCodec.encode(criterion, context);
172 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 165 + assertThat(result, matchesCriterion(criterion));
173 - assertThat(result.get("ipDscp").asInt(), is(63));
174 } 166 }
175 167
176 /** 168 /**
...@@ -180,8 +172,7 @@ public class CriterionCodecTest { ...@@ -180,8 +172,7 @@ public class CriterionCodecTest {
180 public void matchIPEcnTest() { 172 public void matchIPEcnTest() {
181 Criterion criterion = Criteria.matchIPEcn((byte) 7); 173 Criterion criterion = Criteria.matchIPEcn((byte) 7);
182 ObjectNode result = criterionCodec.encode(criterion, context); 174 ObjectNode result = criterionCodec.encode(criterion, context);
183 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 175 + assertThat(result, matchesCriterion(criterion));
184 - assertThat(result.get("ipEcn").asInt(), is(7));
185 } 176 }
186 177
187 /** 178 /**
...@@ -191,8 +182,7 @@ public class CriterionCodecTest { ...@@ -191,8 +182,7 @@ public class CriterionCodecTest {
191 public void matchIPProtocolTest() { 182 public void matchIPProtocolTest() {
192 Criterion criterion = Criteria.matchIPProtocol((byte) 250); 183 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
193 ObjectNode result = criterionCodec.encode(criterion, context); 184 ObjectNode result = criterionCodec.encode(criterion, context);
194 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 185 + assertThat(result, matchesCriterion(criterion));
195 - assertThat(result.get("protocol").asInt(), is(250));
196 } 186 }
197 187
198 /** 188 /**
...@@ -202,8 +192,7 @@ public class CriterionCodecTest { ...@@ -202,8 +192,7 @@ public class CriterionCodecTest {
202 public void matchIPSrcTest() { 192 public void matchIPSrcTest() {
203 Criterion criterion = Criteria.matchIPSrc(ipPrefix4); 193 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
204 ObjectNode result = criterionCodec.encode(criterion, context); 194 ObjectNode result = criterionCodec.encode(criterion, context);
205 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 195 + assertThat(result, matchesCriterion(criterion));
206 - assertThat(result.get("ip").asText(), is(ipPrefix4.toString()));
207 } 196 }
208 197
209 /** 198 /**
...@@ -213,8 +202,7 @@ public class CriterionCodecTest { ...@@ -213,8 +202,7 @@ public class CriterionCodecTest {
213 public void matchIPDstTest() { 202 public void matchIPDstTest() {
214 Criterion criterion = Criteria.matchIPDst(ipPrefix4); 203 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
215 ObjectNode result = criterionCodec.encode(criterion, context); 204 ObjectNode result = criterionCodec.encode(criterion, context);
216 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 205 + assertThat(result, matchesCriterion(criterion));
217 - assertThat(result.get("ip").asText(), is(ipPrefix4.toString()));
218 } 206 }
219 207
220 /** 208 /**
...@@ -224,8 +212,7 @@ public class CriterionCodecTest { ...@@ -224,8 +212,7 @@ public class CriterionCodecTest {
224 public void matchTcpSrcTest() { 212 public void matchTcpSrcTest() {
225 Criterion criterion = Criteria.matchTcpSrc((short) 40000); 213 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
226 ObjectNode result = criterionCodec.encode(criterion, context); 214 ObjectNode result = criterionCodec.encode(criterion, context);
227 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 215 + assertThat(result, matchesCriterion(criterion));
228 - assertThat(result.get("tcpPort").asInt(), is(40000));
229 } 216 }
230 217
231 /** 218 /**
...@@ -235,8 +222,7 @@ public class CriterionCodecTest { ...@@ -235,8 +222,7 @@ public class CriterionCodecTest {
235 public void matchTcpDstTest() { 222 public void matchTcpDstTest() {
236 Criterion criterion = Criteria.matchTcpDst((short) 40000); 223 Criterion criterion = Criteria.matchTcpDst((short) 40000);
237 ObjectNode result = criterionCodec.encode(criterion, context); 224 ObjectNode result = criterionCodec.encode(criterion, context);
238 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 225 + assertThat(result, matchesCriterion(criterion));
239 - assertThat(result.get("tcpPort").asInt(), is(40000));
240 } 226 }
241 227
242 /** 228 /**
...@@ -244,10 +230,9 @@ public class CriterionCodecTest { ...@@ -244,10 +230,9 @@ public class CriterionCodecTest {
244 */ 230 */
245 @Test 231 @Test
246 public void matchUdpSrcTest() { 232 public void matchUdpSrcTest() {
247 - Criterion criterion = Criteria.matchUdpSrc((short) 40000); 233 + Criterion criterion = Criteria.matchUdpSrc(40000);
248 ObjectNode result = criterionCodec.encode(criterion, context); 234 ObjectNode result = criterionCodec.encode(criterion, context);
249 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 235 + assertThat(result, matchesCriterion(criterion));
250 - assertThat(result.get("udpPort").asInt(), is(40000));
251 } 236 }
252 237
253 /** 238 /**
...@@ -255,10 +240,9 @@ public class CriterionCodecTest { ...@@ -255,10 +240,9 @@ public class CriterionCodecTest {
255 */ 240 */
256 @Test 241 @Test
257 public void matchUdpDstTest() { 242 public void matchUdpDstTest() {
258 - Criterion criterion = Criteria.matchUdpDst((short) 40000); 243 + Criterion criterion = Criteria.matchUdpDst(40000);
259 ObjectNode result = criterionCodec.encode(criterion, context); 244 ObjectNode result = criterionCodec.encode(criterion, context);
260 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 245 + assertThat(result, matchesCriterion(criterion));
261 - assertThat(result.get("udpPort").asInt(), is(40000));
262 } 246 }
263 247
264 /** 248 /**
...@@ -266,10 +250,9 @@ public class CriterionCodecTest { ...@@ -266,10 +250,9 @@ public class CriterionCodecTest {
266 */ 250 */
267 @Test 251 @Test
268 public void matchSctpSrcTest() { 252 public void matchSctpSrcTest() {
269 - Criterion criterion = Criteria.matchSctpSrc((short) 40000); 253 + Criterion criterion = Criteria.matchSctpSrc(40000);
270 ObjectNode result = criterionCodec.encode(criterion, context); 254 ObjectNode result = criterionCodec.encode(criterion, context);
271 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 255 + assertThat(result, matchesCriterion(criterion));
272 - assertThat(result.get("sctpPort").asInt(), is(40000));
273 } 256 }
274 257
275 /** 258 /**
...@@ -277,10 +260,9 @@ public class CriterionCodecTest { ...@@ -277,10 +260,9 @@ public class CriterionCodecTest {
277 */ 260 */
278 @Test 261 @Test
279 public void matchSctpDstTest() { 262 public void matchSctpDstTest() {
280 - Criterion criterion = Criteria.matchSctpDst((short) 40000); 263 + Criterion criterion = Criteria.matchSctpDst(40000);
281 ObjectNode result = criterionCodec.encode(criterion, context); 264 ObjectNode result = criterionCodec.encode(criterion, context);
282 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 265 + assertThat(result, matchesCriterion(criterion));
283 - assertThat(result.get("sctpPort").asInt(), is(40000));
284 } 266 }
285 267
286 /** 268 /**
...@@ -290,8 +272,7 @@ public class CriterionCodecTest { ...@@ -290,8 +272,7 @@ public class CriterionCodecTest {
290 public void matchIcmpTypeTest() { 272 public void matchIcmpTypeTest() {
291 Criterion criterion = Criteria.matchIcmpType((byte) 250); 273 Criterion criterion = Criteria.matchIcmpType((byte) 250);
292 ObjectNode result = criterionCodec.encode(criterion, context); 274 ObjectNode result = criterionCodec.encode(criterion, context);
293 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 275 + assertThat(result, matchesCriterion(criterion));
294 - assertThat(result.get("icmpType").asInt(), is(250));
295 } 276 }
296 277
297 /** 278 /**
...@@ -301,8 +282,7 @@ public class CriterionCodecTest { ...@@ -301,8 +282,7 @@ public class CriterionCodecTest {
301 public void matchIcmpCodeTest() { 282 public void matchIcmpCodeTest() {
302 Criterion criterion = Criteria.matchIcmpCode((byte) 250); 283 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
303 ObjectNode result = criterionCodec.encode(criterion, context); 284 ObjectNode result = criterionCodec.encode(criterion, context);
304 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 285 + assertThat(result, matchesCriterion(criterion));
305 - assertThat(result.get("icmpCode").asInt(), is(250));
306 } 286 }
307 287
308 /** 288 /**
...@@ -312,8 +292,7 @@ public class CriterionCodecTest { ...@@ -312,8 +292,7 @@ public class CriterionCodecTest {
312 public void matchIPv6SrcTest() { 292 public void matchIPv6SrcTest() {
313 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6); 293 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
314 ObjectNode result = criterionCodec.encode(criterion, context); 294 ObjectNode result = criterionCodec.encode(criterion, context);
315 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 295 + assertThat(result, matchesCriterion(criterion));
316 - assertThat(result.get("ip").asText(), is(ipPrefix6.toString()));
317 } 296 }
318 297
319 /** 298 /**
...@@ -323,8 +302,7 @@ public class CriterionCodecTest { ...@@ -323,8 +302,7 @@ public class CriterionCodecTest {
323 public void matchIPv6DstTest() { 302 public void matchIPv6DstTest() {
324 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6); 303 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
325 ObjectNode result = criterionCodec.encode(criterion, context); 304 ObjectNode result = criterionCodec.encode(criterion, context);
326 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 305 + assertThat(result, matchesCriterion(criterion));
327 - assertThat(result.get("ip").asText(), is(ipPrefix6.toString()));
328 } 306 }
329 307
330 /** 308 /**
...@@ -334,8 +312,7 @@ public class CriterionCodecTest { ...@@ -334,8 +312,7 @@ public class CriterionCodecTest {
334 public void matchIPv6FlowLabelTest() { 312 public void matchIPv6FlowLabelTest() {
335 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe); 313 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
336 ObjectNode result = criterionCodec.encode(criterion, context); 314 ObjectNode result = criterionCodec.encode(criterion, context);
337 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 315 + assertThat(result, matchesCriterion(criterion));
338 - assertThat(result.get("flowLabel").asInt(), is(0xffffe));
339 } 316 }
340 317
341 /** 318 /**
...@@ -345,8 +322,7 @@ public class CriterionCodecTest { ...@@ -345,8 +322,7 @@ public class CriterionCodecTest {
345 public void matchIcmpv6TypeTest() { 322 public void matchIcmpv6TypeTest() {
346 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250); 323 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
347 ObjectNode result = criterionCodec.encode(criterion, context); 324 ObjectNode result = criterionCodec.encode(criterion, context);
348 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 325 + assertThat(result, matchesCriterion(criterion));
349 - assertThat(result.get("icmpv6Type").asInt(), is(250));
350 } 326 }
351 327
352 /** 328 /**
...@@ -356,8 +332,7 @@ public class CriterionCodecTest { ...@@ -356,8 +332,7 @@ public class CriterionCodecTest {
356 public void matchIcmpv6CodeTest() { 332 public void matchIcmpv6CodeTest() {
357 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250); 333 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
358 ObjectNode result = criterionCodec.encode(criterion, context); 334 ObjectNode result = criterionCodec.encode(criterion, context);
359 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 335 + assertThat(result, matchesCriterion(criterion));
360 - assertThat(result.get("icmpv6Code").asInt(), is(250));
361 } 336 }
362 337
363 /** 338 /**
...@@ -369,8 +344,7 @@ public class CriterionCodecTest { ...@@ -369,8 +344,7 @@ public class CriterionCodecTest {
369 Criteria.matchIPv6NDTargetAddress( 344 Criteria.matchIPv6NDTargetAddress(
370 Ip6Address.valueOf("1111:2222::")); 345 Ip6Address.valueOf("1111:2222::"));
371 ObjectNode result = criterionCodec.encode(criterion, context); 346 ObjectNode result = criterionCodec.encode(criterion, context);
372 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 347 + assertThat(result, matchesCriterion(criterion));
373 - assertThat(result.get("targetAddress").asText(), is("1111:2222::"));
374 } 348 }
375 349
376 /** 350 /**
...@@ -380,8 +354,7 @@ public class CriterionCodecTest { ...@@ -380,8 +354,7 @@ public class CriterionCodecTest {
380 public void matchIPv6NDSourceLinkLayerAddressTest() { 354 public void matchIPv6NDSourceLinkLayerAddressTest() {
381 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1); 355 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
382 ObjectNode result = criterionCodec.encode(criterion, context); 356 ObjectNode result = criterionCodec.encode(criterion, context);
383 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 357 + assertThat(result, matchesCriterion(criterion));
384 - assertThat(result.get("mac").asText(), is(mac1.toString()));
385 } 358 }
386 359
387 /** 360 /**
...@@ -391,8 +364,7 @@ public class CriterionCodecTest { ...@@ -391,8 +364,7 @@ public class CriterionCodecTest {
391 public void matchIPv6NDTargetLinkLayerAddressTest() { 364 public void matchIPv6NDTargetLinkLayerAddressTest() {
392 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1); 365 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
393 ObjectNode result = criterionCodec.encode(criterion, context); 366 ObjectNode result = criterionCodec.encode(criterion, context);
394 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 367 + assertThat(result, matchesCriterion(criterion));
395 - assertThat(result.get("mac").asText(), is(mac1.toString()));
396 } 368 }
397 369
398 /** 370 /**
...@@ -402,8 +374,7 @@ public class CriterionCodecTest { ...@@ -402,8 +374,7 @@ public class CriterionCodecTest {
402 public void matchMplsLabelTest() { 374 public void matchMplsLabelTest() {
403 Criterion criterion = Criteria.matchMplsLabel(0xffffe); 375 Criterion criterion = Criteria.matchMplsLabel(0xffffe);
404 ObjectNode result = criterionCodec.encode(criterion, context); 376 ObjectNode result = criterionCodec.encode(criterion, context);
405 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 377 + assertThat(result, matchesCriterion(criterion));
406 - assertThat(result.get("label").asInt(), is(0xffffe));
407 } 378 }
408 379
409 /** 380 /**
...@@ -423,8 +394,8 @@ public class CriterionCodecTest { ...@@ -423,8 +394,8 @@ public class CriterionCodecTest {
423 Criterion.IPv6ExthdrFlags.UNSEQ.getValue(); 394 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
424 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags); 395 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
425 ObjectNode result = criterionCodec.encode(criterion, context); 396 ObjectNode result = criterionCodec.encode(criterion, context);
426 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 397 +
427 - assertThat(result.get("exthdrFlags").asInt(), is(exthdrFlags)); 398 + assertThat(result, matchesCriterion(criterion));
428 } 399 }
429 400
430 /** 401 /**
...@@ -434,8 +405,7 @@ public class CriterionCodecTest { ...@@ -434,8 +405,7 @@ public class CriterionCodecTest {
434 public void matchLambdaTest() { 405 public void matchLambdaTest() {
435 Criterion criterion = Criteria.matchLambda((short) 40000); 406 Criterion criterion = Criteria.matchLambda((short) 40000);
436 ObjectNode result = criterionCodec.encode(criterion, context); 407 ObjectNode result = criterionCodec.encode(criterion, context);
437 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 408 + assertThat(result, matchesCriterion(criterion));
438 - assertThat(result.get("lambda").asInt(), is(40000));
439 } 409 }
440 410
441 /** 411 /**
...@@ -445,8 +415,7 @@ public class CriterionCodecTest { ...@@ -445,8 +415,7 @@ public class CriterionCodecTest {
445 public void matchOpticalSignalTypeTest() { 415 public void matchOpticalSignalTypeTest() {
446 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250); 416 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
447 ObjectNode result = criterionCodec.encode(criterion, context); 417 ObjectNode result = criterionCodec.encode(criterion, context);
448 - assertThat(result.get("type").textValue(), is(criterion.type().toString())); 418 + assertThat(result, matchesCriterion(criterion));
449 - assertThat(result.get("signalType").asInt(), is(250));
450 } 419 }
451 420
452 } 421 }
......
...@@ -25,240 +25,343 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -25,240 +25,343 @@ import com.fasterxml.jackson.databind.JsonNode;
25 /** 25 /**
26 * Hamcrest matcher for criterion objects. 26 * Hamcrest matcher for criterion objects.
27 */ 27 */
28 -public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { 28 +public final class CriterionJsonMatcher extends
29 + TypeSafeDiagnosingMatcher<JsonNode> {
29 30
30 final Criterion criterion; 31 final Criterion criterion;
32 + Description description;
33 + JsonNode jsonCriterion;
31 34
35 + /**
36 + * Constructs a matcher object.
37 + *
38 + * @param criterionValue criterion to match
39 + */
32 private CriterionJsonMatcher(Criterion criterionValue) { 40 private CriterionJsonMatcher(Criterion criterionValue) {
33 criterion = criterionValue; 41 criterion = criterionValue;
34 } 42 }
35 43
36 - // CHECKSTYLE IGNORE MethodLength FOR NEXT 300 LINES 44 + /**
37 - @Override 45 + * Factory to allocate an criterion matcher.
38 - public boolean matchesSafely(JsonNode jsonCriterion, 46 + *
39 - Description description) { 47 + * @param criterion criterion object we are looking for
40 - final String type = criterion.type().name(); 48 + * @return matcher
41 - final String jsonType = jsonCriterion.get("type").asText(); 49 + */
42 - if (!type.equals(jsonType)) { 50 + public static CriterionJsonMatcher matchesCriterion(Criterion criterion) {
43 - description.appendText("type was " + type); 51 + return new CriterionJsonMatcher(criterion);
44 - return false;
45 } 52 }
46 53
47 - switch (criterion.type()) { 54 + /**
48 - 55 + * Matches a port criterion object.
49 - case IN_PORT: 56 + *
50 - case IN_PHY_PORT: 57 + * @param criterion criterion to match
51 - final Criteria.PortCriterion portCriterion = 58 + * @return true if the JSON matches the criterion, false otherwise.
52 - (Criteria.PortCriterion) criterion; 59 + */
53 - final long port = portCriterion.port().toLong(); 60 + private boolean matchCriterion(Criteria.PortCriterion criterion) {
61 + final long port = criterion.port().toLong();
54 final long jsonPort = jsonCriterion.get("port").asLong(); 62 final long jsonPort = jsonCriterion.get("port").asLong();
55 if (port != jsonPort) { 63 if (port != jsonPort) {
56 description.appendText("port was " + Long.toString(jsonPort)); 64 description.appendText("port was " + Long.toString(jsonPort));
57 return false; 65 return false;
58 } 66 }
59 - break; 67 + return true;
68 + }
60 69
61 - case METADATA: 70 + /**
62 - final Criteria.MetadataCriterion metadataCriterion = 71 + * Matches a metadata criterion object.
63 - (Criteria.MetadataCriterion) criterion; 72 + *
64 - final long metadata = metadataCriterion.metadata(); 73 + * @param criterion criterion to match
74 + * @return true if the JSON matches the criterion, false otherwise.
75 + */
76 + private boolean matchCriterion(Criteria.MetadataCriterion criterion) {
77 + final long metadata = criterion.metadata();
65 final long jsonMetadata = jsonCriterion.get("metadata").asLong(); 78 final long jsonMetadata = jsonCriterion.get("metadata").asLong();
66 if (metadata != jsonMetadata) { 79 if (metadata != jsonMetadata) {
67 - description.appendText("metadata was " + Long.toString(jsonMetadata)); 80 + description.appendText("metadata was "
81 + + Long.toString(jsonMetadata));
68 return false; 82 return false;
69 } 83 }
70 - break; 84 + return true;
85 + }
71 86
72 - case ETH_DST: 87 + /**
73 - case ETH_SRC: 88 + * Matches an eth criterion object.
74 - final Criteria.EthCriterion ethCriterion = 89 + *
75 - (Criteria.EthCriterion) criterion; 90 + * @param criterion criterion to match
76 - final String mac = ethCriterion.mac().toString(); 91 + * @return true if the JSON matches the criterion, false otherwise.
92 + */
93 + private boolean matchCriterion(Criteria.EthCriterion criterion) {
94 + final String mac = criterion.mac().toString();
77 final String jsonMac = jsonCriterion.get("mac").textValue(); 95 final String jsonMac = jsonCriterion.get("mac").textValue();
78 if (!mac.equals(jsonMac)) { 96 if (!mac.equals(jsonMac)) {
79 description.appendText("mac was " + jsonMac); 97 description.appendText("mac was " + jsonMac);
80 return false; 98 return false;
81 } 99 }
82 - break; 100 + return true;
101 + }
83 102
84 - case ETH_TYPE: 103 + /**
85 - final Criteria.EthTypeCriterion ethTypeCriterion = 104 + * Matches an eth type criterion object.
86 - (Criteria.EthTypeCriterion) criterion; 105 + *
87 - final String ethType = 106 + * @param criterion criterion to match
88 - Long.toHexString(ethTypeCriterion.ethType()); 107 + * @return true if the JSON matches the criterion, false otherwise.
89 - final String jsonEthType = jsonCriterion.get("ethType").textValue(); 108 + */
90 - if (!ethType.equals(jsonEthType)) { 109 + private boolean matchCriterion(Criteria.EthTypeCriterion criterion) {
91 - description.appendText("ethType was " + jsonEthType); 110 + final int ethType = criterion.ethType();
111 + final int jsonEthType = jsonCriterion.get("ethType").intValue();
112 + if (ethType != jsonEthType) {
113 + description.appendText("ethType was "
114 + + Integer.toString(jsonEthType));
92 return false; 115 return false;
93 } 116 }
94 - break; 117 + return true;
118 + }
95 119
96 - case VLAN_VID: 120 + /**
97 - final Criteria.VlanIdCriterion vlanIdCriterion = 121 + * Matches a VLAN ID criterion object.
98 - (Criteria.VlanIdCriterion) criterion; 122 + *
99 - final short vlanId = vlanIdCriterion.vlanId().toShort(); 123 + * @param criterion criterion to match
124 + * @return true if the JSON matches the criterion, false otherwise.
125 + */
126 + private boolean matchCriterion(Criteria.VlanIdCriterion criterion) {
127 + final short vlanId = criterion.vlanId().toShort();
100 final short jsonVlanId = jsonCriterion.get("vlanId").shortValue(); 128 final short jsonVlanId = jsonCriterion.get("vlanId").shortValue();
101 if (vlanId != jsonVlanId) { 129 if (vlanId != jsonVlanId) {
102 description.appendText("vlanId was " + Short.toString(jsonVlanId)); 130 description.appendText("vlanId was " + Short.toString(jsonVlanId));
103 return false; 131 return false;
104 } 132 }
105 - break; 133 + return true;
134 + }
106 135
107 - case VLAN_PCP: 136 + /**
108 - final Criteria.VlanPcpCriterion vlanPcpCriterion = 137 + * Matches a VLAN PCP criterion object.
109 - (Criteria.VlanPcpCriterion) criterion; 138 + *
110 - final byte priority = vlanPcpCriterion.priority(); 139 + * @param criterion criterion to match
111 - final byte jsonPriority = (byte) jsonCriterion.get("priority").shortValue(); 140 + * @return true if the JSON matches the criterion, false otherwise.
141 + */
142 + private boolean matchCriterion(Criteria.VlanPcpCriterion criterion) {
143 + final byte priority = criterion.priority();
144 + final byte jsonPriority =
145 + (byte) jsonCriterion.get("priority").shortValue();
112 if (priority != jsonPriority) { 146 if (priority != jsonPriority) {
113 description.appendText("priority was " + Byte.toString(jsonPriority)); 147 description.appendText("priority was " + Byte.toString(jsonPriority));
114 return false; 148 return false;
115 } 149 }
116 - break; 150 + return true;
151 + }
117 152
118 - case IP_DSCP: 153 + /**
119 - final Criteria.IPDscpCriterion ipDscpCriterion = 154 + * Matches an IP DSCP criterion object.
120 - (Criteria.IPDscpCriterion) criterion; 155 + *
121 - final byte ipDscp = ipDscpCriterion.ipDscp(); 156 + * @param criterion criterion to match
157 + * @return true if the JSON matches the criterion, false otherwise.
158 + */
159 + private boolean matchCriterion(Criteria.IPDscpCriterion criterion) {
160 + final byte ipDscp = criterion.ipDscp();
122 final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue(); 161 final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue();
123 if (ipDscp != jsonIpDscp) { 162 if (ipDscp != jsonIpDscp) {
124 description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp)); 163 description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp));
125 return false; 164 return false;
126 } 165 }
127 - break; 166 + return true;
167 + }
128 168
129 - case IP_ECN: 169 + /**
130 - final Criteria.IPEcnCriterion ipEcnCriterion = 170 + * Matches an IP ECN criterion object.
131 - (Criteria.IPEcnCriterion) criterion; 171 + *
132 - final byte ipEcn = ipEcnCriterion.ipEcn(); 172 + * @param criterion criterion to match
173 + * @return true if the JSON matches the criterion, false otherwise.
174 + */
175 + private boolean matchCriterion(Criteria.IPEcnCriterion criterion) {
176 + final byte ipEcn = criterion.ipEcn();
133 final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue(); 177 final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue();
134 if (ipEcn != jsonIpEcn) { 178 if (ipEcn != jsonIpEcn) {
135 description.appendText("IP ECN was " + Byte.toString(jsonIpEcn)); 179 description.appendText("IP ECN was " + Byte.toString(jsonIpEcn));
136 return false; 180 return false;
137 } 181 }
138 - break; 182 + return true;
183 + }
139 184
140 - case IP_PROTO: 185 + /**
141 - final Criteria.IPProtocolCriterion iPProtocolCriterion = 186 + * Matches an IP protocol criterion object.
142 - (Criteria.IPProtocolCriterion) criterion; 187 + *
143 - final short protocol = iPProtocolCriterion.protocol(); 188 + * @param criterion criterion to match
189 + * @return true if the JSON matches the criterion, false otherwise.
190 + */
191 + private boolean matchCriterion(Criteria.IPProtocolCriterion criterion) {
192 + final short protocol = criterion.protocol();
144 final short jsonProtocol = jsonCriterion.get("protocol").shortValue(); 193 final short jsonProtocol = jsonCriterion.get("protocol").shortValue();
145 if (protocol != jsonProtocol) { 194 if (protocol != jsonProtocol) {
146 - description.appendText("protocol was " + Short.toString(jsonProtocol)); 195 + description.appendText("protocol was "
196 + + Short.toString(jsonProtocol));
147 return false; 197 return false;
148 } 198 }
149 - break; 199 + return true;
200 + }
150 201
151 - case IPV4_SRC: 202 + /**
152 - case IPV4_DST: 203 + * Matches an IP address criterion object.
153 - case IPV6_SRC: 204 + *
154 - case IPV6_DST: 205 + * @param criterion criterion to match
155 - final Criteria.IPCriterion ipCriterion = 206 + * @return true if the JSON matches the criterion, false otherwise.
156 - (Criteria.IPCriterion) criterion; 207 + */
157 - final String ip = ipCriterion.ip().toString(); 208 + private boolean matchCriterion(Criteria.IPCriterion criterion) {
209 + final String ip = criterion.ip().toString();
158 final String jsonIp = jsonCriterion.get("ip").textValue(); 210 final String jsonIp = jsonCriterion.get("ip").textValue();
159 if (!ip.equals(jsonIp)) { 211 if (!ip.equals(jsonIp)) {
160 description.appendText("ip was " + jsonIp); 212 description.appendText("ip was " + jsonIp);
161 return false; 213 return false;
162 } 214 }
163 - break; 215 + return true;
216 + }
164 217
165 - case TCP_SRC: 218 + /**
166 - case TCP_DST: 219 + * Matches a TCP port criterion object.
167 - final Criteria.TcpPortCriterion tcpPortCriterion = 220 + *
168 - (Criteria.TcpPortCriterion) criterion; 221 + * @param criterion criterion to match
169 - final int tcpPort = tcpPortCriterion.tcpPort(); 222 + * @return true if the JSON matches the criterion, false otherwise.
223 + */
224 + private boolean matchCriterion(Criteria.TcpPortCriterion criterion) {
225 + final int tcpPort = criterion.tcpPort();
170 final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue(); 226 final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue();
171 if (tcpPort != jsonTcpPort) { 227 if (tcpPort != jsonTcpPort) {
172 - description.appendText("tcp port was " + Integer.toString(jsonTcpPort)); 228 + description.appendText("tcp port was "
229 + + Integer.toString(jsonTcpPort));
173 return false; 230 return false;
174 } 231 }
175 - break; 232 + return true;
233 + }
176 234
177 - case UDP_SRC: 235 + /**
178 - case UDP_DST: 236 + * Matches a UDP port criterion object.
179 - final Criteria.UdpPortCriterion udpPortCriterion = 237 + *
180 - (Criteria.UdpPortCriterion) criterion; 238 + * @param criterion criterion to match
181 - final int udpPort = udpPortCriterion.udpPort(); 239 + * @return true if the JSON matches the criterion, false otherwise.
240 + */
241 + private boolean matchCriterion(Criteria.UdpPortCriterion criterion) {
242 + final int udpPort = criterion.udpPort();
182 final int jsonUdpPort = jsonCriterion.get("udpPort").intValue(); 243 final int jsonUdpPort = jsonCriterion.get("udpPort").intValue();
183 if (udpPort != jsonUdpPort) { 244 if (udpPort != jsonUdpPort) {
184 - description.appendText("udp port was " + Integer.toString(jsonUdpPort)); 245 + description.appendText("udp port was "
246 + + Integer.toString(jsonUdpPort));
185 return false; 247 return false;
186 } 248 }
187 - break; 249 + return true;
250 + }
188 251
189 - case SCTP_SRC: 252 + /**
190 - case SCTP_DST: 253 + * Matches an SCTP port criterion object.
191 - final Criteria.SctpPortCriterion sctpPortCriterion = 254 + *
192 - (Criteria.SctpPortCriterion) criterion; 255 + * @param criterion criterion to match
193 - final int sctpPort = sctpPortCriterion.sctpPort(); 256 + * @return true if the JSON matches the criterion, false otherwise.
257 + */
258 + private boolean matchCriterion(Criteria.SctpPortCriterion criterion) {
259 + final int sctpPort = criterion.sctpPort();
194 final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue(); 260 final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue();
195 if (sctpPort != jsonSctpPort) { 261 if (sctpPort != jsonSctpPort) {
196 - description.appendText("sctp port was " + Integer.toString(jsonSctpPort)); 262 + description.appendText("sctp port was "
263 + + Integer.toString(jsonSctpPort));
197 return false; 264 return false;
198 } 265 }
199 - break; 266 + return true;
267 + }
200 268
201 - case ICMPV4_TYPE: 269 + /**
202 - final Criteria.IcmpTypeCriterion icmpTypeCriterion = 270 + * Matches an ICMP type criterion object.
203 - (Criteria.IcmpTypeCriterion) criterion; 271 + *
204 - final short icmpType = icmpTypeCriterion.icmpType(); 272 + * @param criterion criterion to match
273 + * @return true if the JSON matches the criterion, false otherwise.
274 + */
275 + private boolean matchCriterion(Criteria.IcmpTypeCriterion criterion) {
276 + final short icmpType = criterion.icmpType();
205 final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue(); 277 final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue();
206 if (icmpType != jsonIcmpType) { 278 if (icmpType != jsonIcmpType) {
207 - description.appendText("icmp type was " + Short.toString(jsonIcmpType)); 279 + description.appendText("icmp type was "
280 + + Short.toString(jsonIcmpType));
208 return false; 281 return false;
209 } 282 }
210 - break; 283 + return true;
284 + }
211 285
212 - case ICMPV4_CODE: 286 + /**
213 - final Criteria.IcmpCodeCriterion icmpCodeCriterion = 287 + * Matches an ICMP code criterion object.
214 - (Criteria.IcmpCodeCriterion) criterion; 288 + *
215 - final short icmpCode = icmpCodeCriterion.icmpCode(); 289 + * @param criterion criterion to match
290 + * @return true if the JSON matches the criterion, false otherwise.
291 + */
292 + private boolean matchCriterion(Criteria.IcmpCodeCriterion criterion) {
293 + final short icmpCode = criterion.icmpCode();
216 final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue(); 294 final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue();
217 if (icmpCode != jsonIcmpCode) { 295 if (icmpCode != jsonIcmpCode) {
218 - description.appendText("icmp code was " + Short.toString(jsonIcmpCode)); 296 + description.appendText("icmp code was "
297 + + Short.toString(jsonIcmpCode));
219 return false; 298 return false;
220 } 299 }
221 - break; 300 + return true;
301 + }
222 302
223 - case IPV6_FLABEL: 303 + /**
224 - final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion = 304 + * Matches an IPV6 flow label criterion object.
225 - (Criteria.IPv6FlowLabelCriterion) criterion; 305 + *
226 - final int flowLabel = ipv6FlowLabelCriterion.flowLabel(); 306 + * @param criterion criterion to match
307 + * @return true if the JSON matches the criterion, false otherwise.
308 + */
309 + private boolean matchCriterion(Criteria.IPv6FlowLabelCriterion criterion) {
310 + final int flowLabel = criterion.flowLabel();
227 final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue(); 311 final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue();
228 if (flowLabel != jsonFlowLabel) { 312 if (flowLabel != jsonFlowLabel) {
229 - description.appendText("IPv6 flow label was " + Integer.toString(jsonFlowLabel)); 313 + description.appendText("IPv6 flow label was "
314 + + Integer.toString(jsonFlowLabel));
230 return false; 315 return false;
231 } 316 }
232 - break; 317 + return true;
318 + }
233 319
234 - case ICMPV6_TYPE: 320 + /**
235 - final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion = 321 + * Matches an ICMP V6 type criterion object.
236 - (Criteria.Icmpv6TypeCriterion) criterion; 322 + *
237 - final short icmpv6Type = icmpv6TypeCriterion.icmpv6Type(); 323 + * @param criterion criterion to match
238 - final short jsonIcmpv6Type = jsonCriterion.get("icmpv6Type").shortValue(); 324 + * @return true if the JSON matches the criterion, false otherwise.
325 + */
326 + private boolean matchCriterion(Criteria.Icmpv6TypeCriterion criterion) {
327 + final short icmpv6Type = criterion.icmpv6Type();
328 + final short jsonIcmpv6Type =
329 + jsonCriterion.get("icmpv6Type").shortValue();
239 if (icmpv6Type != jsonIcmpv6Type) { 330 if (icmpv6Type != jsonIcmpv6Type) {
240 - description.appendText("icmpv6 type was " + Short.toString(jsonIcmpv6Type)); 331 + description.appendText("icmpv6 type was "
332 + + Short.toString(jsonIcmpv6Type));
241 return false; 333 return false;
242 } 334 }
243 - break; 335 + return true;
336 + }
244 337
245 - case ICMPV6_CODE: 338 + /**
246 - final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion = 339 + * Matches an IPV6 code criterion object.
247 - (Criteria.Icmpv6CodeCriterion) criterion; 340 + *
248 - final short icmpv6Code = icmpv6CodeCriterion.icmpv6Code(); 341 + * @param criterion criterion to match
249 - final short jsonIcmpv6Code = jsonCriterion.get("icmpv6Code").shortValue(); 342 + * @return true if the JSON matches the criterion, false otherwise.
343 + */
344 + private boolean matchCriterion(Criteria.Icmpv6CodeCriterion criterion) {
345 + final short icmpv6Code = criterion.icmpv6Code();
346 + final short jsonIcmpv6Code =
347 + jsonCriterion.get("icmpv6Code").shortValue();
250 if (icmpv6Code != jsonIcmpv6Code) { 348 if (icmpv6Code != jsonIcmpv6Code) {
251 - description.appendText("icmpv6 code was " + Short.toString(jsonIcmpv6Code)); 349 + description.appendText("icmpv6 code was "
350 + + Short.toString(jsonIcmpv6Code));
252 return false; 351 return false;
253 } 352 }
254 - break; 353 + return true;
354 + }
255 355
256 - case IPV6_ND_TARGET: 356 + /**
257 - final Criteria.IPv6NDTargetAddressCriterion 357 + * Matches an IPV6 ND target criterion object.
258 - ipv6NDTargetAddressCriterion = 358 + *
259 - (Criteria.IPv6NDTargetAddressCriterion) criterion; 359 + * @param criterion criterion to match
360 + * @return true if the JSON matches the criterion, false otherwise.
361 + */
362 + private boolean matchCriterion(Criteria.IPv6NDTargetAddressCriterion criterion) {
260 final String targetAddress = 363 final String targetAddress =
261 - ipv6NDTargetAddressCriterion.targetAddress().toString(); 364 + criterion.targetAddress().toString();
262 final String jsonTargetAddress = 365 final String jsonTargetAddress =
263 jsonCriterion.get("targetAddress").textValue(); 366 jsonCriterion.get("targetAddress").textValue();
264 if (!targetAddress.equals(jsonTargetAddress)) { 367 if (!targetAddress.equals(jsonTargetAddress)) {
...@@ -266,67 +369,191 @@ public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNo ...@@ -266,67 +369,191 @@ public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNo
266 jsonTargetAddress); 369 jsonTargetAddress);
267 return false; 370 return false;
268 } 371 }
269 - break; 372 + return true;
373 + }
270 374
271 - case IPV6_ND_SLL: 375 + /**
272 - case IPV6_ND_TLL: 376 + * Matches an IPV6 ND link layer criterion object.
273 - final Criteria.IPv6NDLinkLayerAddressCriterion 377 + *
274 - ipv6NDLinkLayerAddressCriterion = 378 + * @param criterion criterion to match
275 - (Criteria.IPv6NDLinkLayerAddressCriterion) criterion; 379 + * @return true if the JSON matches the criterion, false otherwise.
380 + */
381 + private boolean matchCriterion(Criteria.IPv6NDLinkLayerAddressCriterion criterion) {
276 final String llAddress = 382 final String llAddress =
277 - ipv6NDLinkLayerAddressCriterion.mac().toString(); 383 + criterion.mac().toString();
278 final String jsonLlAddress = 384 final String jsonLlAddress =
279 jsonCriterion.get("mac").textValue(); 385 jsonCriterion.get("mac").textValue();
280 if (!llAddress.equals(jsonLlAddress)) { 386 if (!llAddress.equals(jsonLlAddress)) {
281 description.appendText("mac was " + jsonLlAddress); 387 description.appendText("mac was " + jsonLlAddress);
282 return false; 388 return false;
283 } 389 }
284 - break; 390 + return true;
391 + }
285 392
286 - case MPLS_LABEL: 393 + /**
287 - final Criteria.MplsCriterion mplsCriterion = 394 + * Matches an MPLS label criterion object.
288 - (Criteria.MplsCriterion) criterion; 395 + *
289 - final int label = mplsCriterion.label(); 396 + * @param criterion criterion to match
397 + * @return true if the JSON matches the criterion, false otherwise.
398 + */
399 + private boolean matchCriterion(Criteria.MplsCriterion criterion) {
400 + final int label = criterion.label();
290 final int jsonLabel = jsonCriterion.get("label").intValue(); 401 final int jsonLabel = jsonCriterion.get("label").intValue();
291 if (label != jsonLabel) { 402 if (label != jsonLabel) {
292 description.appendText("label was " + Integer.toString(jsonLabel)); 403 description.appendText("label was " + Integer.toString(jsonLabel));
293 return false; 404 return false;
294 } 405 }
295 - break; 406 + return true;
407 + }
296 408
297 - case IPV6_EXTHDR: 409 + /**
298 - final Criteria.IPv6ExthdrFlagsCriterion exthdrCriterion = 410 + * Matches an IPV6 exthdr criterion object.
299 - (Criteria.IPv6ExthdrFlagsCriterion) criterion; 411 + *
300 - final int exthdrFlags = exthdrCriterion.exthdrFlags(); 412 + * @param criterion criterion to match
413 + * @return true if the JSON matches the criterion, false otherwise.
414 + */
415 + private boolean matchCriterion(Criteria.IPv6ExthdrFlagsCriterion criterion) {
416 + final int exthdrFlags = criterion.exthdrFlags();
301 final int jsonExthdrFlags = 417 final int jsonExthdrFlags =
302 jsonCriterion.get("exthdrFlags").intValue(); 418 jsonCriterion.get("exthdrFlags").intValue();
303 if (exthdrFlags != jsonExthdrFlags) { 419 if (exthdrFlags != jsonExthdrFlags) {
304 - description.appendText("exthdrFlags was " + Long.toHexString(jsonExthdrFlags)); 420 + description.appendText("exthdrFlags was "
421 + + Long.toHexString(jsonExthdrFlags));
305 return false; 422 return false;
306 } 423 }
307 - break; 424 + return true;
425 + }
308 426
309 - case OCH_SIGID: 427 + /**
310 - final Criteria.LambdaCriterion lambdaCriterion = 428 + * Matches a lambda criterion object.
311 - (Criteria.LambdaCriterion) criterion; 429 + *
312 - final int lambda = lambdaCriterion.lambda(); 430 + * @param criterion criterion to match
431 + * @return true if the JSON matches the criterion, false otherwise.
432 + */
433 + private boolean matchCriterion(Criteria.LambdaCriterion criterion) {
434 + final int lambda = criterion.lambda();
313 final int jsonLambda = jsonCriterion.get("lambda").intValue(); 435 final int jsonLambda = jsonCriterion.get("lambda").intValue();
314 if (lambda != jsonLambda) { 436 if (lambda != jsonLambda) {
315 description.appendText("lambda was " + Integer.toString(lambda)); 437 description.appendText("lambda was " + Integer.toString(lambda));
316 return false; 438 return false;
317 } 439 }
318 - break; 440 + return true;
441 + }
319 442
320 - case OCH_SIGTYPE: 443 + /**
321 - final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion = 444 + * Matches an optical signal type criterion object.
322 - (Criteria.OpticalSignalTypeCriterion) criterion; 445 + *
323 - final short signalType = opticalSignalTypeCriterion.signalType(); 446 + * @param criterion criterion to match
447 + * @return true if the JSON matches the criterion, false otherwise.
448 + */
449 + private boolean matchCriterion(Criteria.OpticalSignalTypeCriterion criterion) {
450 + final short signalType = criterion.signalType();
324 final short jsonSignalType = jsonCriterion.get("signalType").shortValue(); 451 final short jsonSignalType = jsonCriterion.get("signalType").shortValue();
325 if (signalType != jsonSignalType) { 452 if (signalType != jsonSignalType) {
326 description.appendText("signal type was " + Short.toString(signalType)); 453 description.appendText("signal type was " + Short.toString(signalType));
327 return false; 454 return false;
328 } 455 }
329 - break; 456 + return true;
457 + }
458 +
459 + @Override
460 + public boolean matchesSafely(JsonNode jsonCriterion,
461 + Description description) {
462 + this.description = description;
463 + this.jsonCriterion = jsonCriterion;
464 + final String type = criterion.type().name();
465 + final String jsonType = jsonCriterion.get("type").asText();
466 + if (!type.equals(jsonType)) {
467 + description.appendText("type was " + type);
468 + return false;
469 + }
470 +
471 + switch (criterion.type()) {
472 +
473 + case IN_PORT:
474 + case IN_PHY_PORT:
475 + return matchCriterion((Criteria.PortCriterion) criterion);
476 +
477 + case METADATA:
478 + return matchCriterion((Criteria.MetadataCriterion) criterion);
479 +
480 + case ETH_DST:
481 + case ETH_SRC:
482 + return matchCriterion((Criteria.EthCriterion) criterion);
483 +
484 + case ETH_TYPE:
485 + return matchCriterion((Criteria.EthTypeCriterion) criterion);
486 +
487 + case VLAN_VID:
488 + return matchCriterion((Criteria.VlanIdCriterion) criterion);
489 +
490 + case VLAN_PCP:
491 + return matchCriterion((Criteria.VlanPcpCriterion) criterion);
492 +
493 + case IP_DSCP:
494 + return matchCriterion((Criteria.IPDscpCriterion) criterion);
495 +
496 + case IP_ECN:
497 + return matchCriterion((Criteria.IPEcnCriterion) criterion);
498 +
499 + case IP_PROTO:
500 + return matchCriterion((Criteria.IPProtocolCriterion) criterion);
501 +
502 + case IPV4_SRC:
503 + case IPV4_DST:
504 + case IPV6_SRC:
505 + case IPV6_DST:
506 + return matchCriterion((Criteria.IPCriterion) criterion);
507 +
508 + case TCP_SRC:
509 + case TCP_DST:
510 + return matchCriterion((Criteria.TcpPortCriterion) criterion);
511 +
512 + case UDP_SRC:
513 + case UDP_DST:
514 + return matchCriterion((Criteria.UdpPortCriterion) criterion);
515 +
516 + case SCTP_SRC:
517 + case SCTP_DST:
518 + return matchCriterion((Criteria.SctpPortCriterion) criterion);
519 +
520 + case ICMPV4_TYPE:
521 + return matchCriterion((Criteria.IcmpTypeCriterion) criterion);
522 +
523 + case ICMPV4_CODE:
524 + return matchCriterion((Criteria.IcmpCodeCriterion) criterion);
525 +
526 + case IPV6_FLABEL:
527 + return matchCriterion((Criteria.IPv6FlowLabelCriterion) criterion);
528 +
529 + case ICMPV6_TYPE:
530 + return matchCriterion((Criteria.Icmpv6TypeCriterion) criterion);
531 +
532 + case ICMPV6_CODE:
533 + return matchCriterion((Criteria.Icmpv6CodeCriterion) criterion);
534 +
535 + case IPV6_ND_TARGET:
536 + return matchCriterion(
537 + (Criteria.IPv6NDTargetAddressCriterion) criterion);
538 +
539 + case IPV6_ND_SLL:
540 + case IPV6_ND_TLL:
541 + return matchCriterion(
542 + (Criteria.IPv6NDLinkLayerAddressCriterion) criterion);
543 +
544 + case MPLS_LABEL:
545 + return matchCriterion((Criteria.MplsCriterion) criterion);
546 +
547 + case IPV6_EXTHDR:
548 + return matchCriterion(
549 + (Criteria.IPv6ExthdrFlagsCriterion) criterion);
550 +
551 + case OCH_SIGID:
552 + return matchCriterion((Criteria.LambdaCriterion) criterion);
553 +
554 + case OCH_SIGTYPE:
555 + return matchCriterion(
556 + (Criteria.OpticalSignalTypeCriterion) criterion);
330 557
331 default: 558 default:
332 // Don't know how to format this type 559 // Don't know how to format this type
...@@ -334,21 +561,10 @@ public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNo ...@@ -334,21 +561,10 @@ public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNo
334 criterion.type()); 561 criterion.type());
335 return false; 562 return false;
336 } 563 }
337 - return true;
338 } 564 }
339 565
340 @Override 566 @Override
341 public void describeTo(Description description) { 567 public void describeTo(Description description) {
342 description.appendText(criterion.toString()); 568 description.appendText(criterion.toString());
343 } 569 }
344 -
345 - /**
346 - * Factory to allocate an criterion matcher.
347 - *
348 - * @param criterion criterion object we are looking for
349 - * @return matcher
350 - */
351 - public static CriterionJsonMatcher matchesCriterion(Criterion criterion) {
352 - return new CriterionJsonMatcher(criterion);
353 - }
354 } 570 }
......