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,18 +25,442 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -25,18 +25,442 @@ 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 + /**
45 + * Factory to allocate an criterion matcher.
46 + *
47 + * @param criterion criterion object we are looking for
48 + * @return matcher
49 + */
50 + public static CriterionJsonMatcher matchesCriterion(Criterion criterion) {
51 + return new CriterionJsonMatcher(criterion);
52 + }
53 +
54 + /**
55 + * Matches a port criterion object.
56 + *
57 + * @param criterion criterion to match
58 + * @return true if the JSON matches the criterion, false otherwise.
59 + */
60 + private boolean matchCriterion(Criteria.PortCriterion criterion) {
61 + final long port = criterion.port().toLong();
62 + final long jsonPort = jsonCriterion.get("port").asLong();
63 + if (port != jsonPort) {
64 + description.appendText("port was " + Long.toString(jsonPort));
65 + return false;
66 + }
67 + return true;
68 + }
69 +
70 + /**
71 + * Matches a metadata criterion object.
72 + *
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();
78 + final long jsonMetadata = jsonCriterion.get("metadata").asLong();
79 + if (metadata != jsonMetadata) {
80 + description.appendText("metadata was "
81 + + Long.toString(jsonMetadata));
82 + return false;
83 + }
84 + return true;
85 + }
86 +
87 + /**
88 + * Matches an eth criterion object.
89 + *
90 + * @param criterion criterion to match
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();
95 + final String jsonMac = jsonCriterion.get("mac").textValue();
96 + if (!mac.equals(jsonMac)) {
97 + description.appendText("mac was " + jsonMac);
98 + return false;
99 + }
100 + return true;
101 + }
102 +
103 + /**
104 + * Matches an eth type criterion object.
105 + *
106 + * @param criterion criterion to match
107 + * @return true if the JSON matches the criterion, false otherwise.
108 + */
109 + private boolean matchCriterion(Criteria.EthTypeCriterion criterion) {
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));
115 + return false;
116 + }
117 + return true;
118 + }
119 +
120 + /**
121 + * Matches a VLAN ID criterion object.
122 + *
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();
128 + final short jsonVlanId = jsonCriterion.get("vlanId").shortValue();
129 + if (vlanId != jsonVlanId) {
130 + description.appendText("vlanId was " + Short.toString(jsonVlanId));
131 + return false;
132 + }
133 + return true;
134 + }
135 +
136 + /**
137 + * Matches a VLAN PCP criterion object.
138 + *
139 + * @param criterion criterion to match
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();
146 + if (priority != jsonPriority) {
147 + description.appendText("priority was " + Byte.toString(jsonPriority));
148 + return false;
149 + }
150 + return true;
151 + }
152 +
153 + /**
154 + * Matches an IP DSCP criterion object.
155 + *
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();
161 + final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue();
162 + if (ipDscp != jsonIpDscp) {
163 + description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp));
164 + return false;
165 + }
166 + return true;
167 + }
168 +
169 + /**
170 + * Matches an IP ECN criterion object.
171 + *
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();
177 + final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue();
178 + if (ipEcn != jsonIpEcn) {
179 + description.appendText("IP ECN was " + Byte.toString(jsonIpEcn));
180 + return false;
181 + }
182 + return true;
183 + }
184 +
185 + /**
186 + * Matches an IP protocol criterion object.
187 + *
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();
193 + final short jsonProtocol = jsonCriterion.get("protocol").shortValue();
194 + if (protocol != jsonProtocol) {
195 + description.appendText("protocol was "
196 + + Short.toString(jsonProtocol));
197 + return false;
198 + }
199 + return true;
200 + }
201 +
202 + /**
203 + * Matches an IP address criterion object.
204 + *
205 + * @param criterion criterion to match
206 + * @return true if the JSON matches the criterion, false otherwise.
207 + */
208 + private boolean matchCriterion(Criteria.IPCriterion criterion) {
209 + final String ip = criterion.ip().toString();
210 + final String jsonIp = jsonCriterion.get("ip").textValue();
211 + if (!ip.equals(jsonIp)) {
212 + description.appendText("ip was " + jsonIp);
213 + return false;
214 + }
215 + return true;
216 + }
217 +
218 + /**
219 + * Matches a TCP port criterion object.
220 + *
221 + * @param criterion criterion to match
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();
226 + final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue();
227 + if (tcpPort != jsonTcpPort) {
228 + description.appendText("tcp port was "
229 + + Integer.toString(jsonTcpPort));
230 + return false;
231 + }
232 + return true;
233 + }
234 +
235 + /**
236 + * Matches a UDP port criterion object.
237 + *
238 + * @param criterion criterion to match
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();
243 + final int jsonUdpPort = jsonCriterion.get("udpPort").intValue();
244 + if (udpPort != jsonUdpPort) {
245 + description.appendText("udp port was "
246 + + Integer.toString(jsonUdpPort));
247 + return false;
248 + }
249 + return true;
250 + }
251 +
252 + /**
253 + * Matches an SCTP port criterion object.
254 + *
255 + * @param criterion criterion to match
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();
260 + final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue();
261 + if (sctpPort != jsonSctpPort) {
262 + description.appendText("sctp port was "
263 + + Integer.toString(jsonSctpPort));
264 + return false;
265 + }
266 + return true;
267 + }
268 +
269 + /**
270 + * Matches an ICMP type criterion object.
271 + *
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();
277 + final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue();
278 + if (icmpType != jsonIcmpType) {
279 + description.appendText("icmp type was "
280 + + Short.toString(jsonIcmpType));
281 + return false;
282 + }
283 + return true;
284 + }
285 +
286 + /**
287 + * Matches an ICMP code criterion object.
288 + *
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();
294 + final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue();
295 + if (icmpCode != jsonIcmpCode) {
296 + description.appendText("icmp code was "
297 + + Short.toString(jsonIcmpCode));
298 + return false;
299 + }
300 + return true;
301 + }
302 +
303 + /**
304 + * Matches an IPV6 flow label criterion object.
305 + *
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();
311 + final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue();
312 + if (flowLabel != jsonFlowLabel) {
313 + description.appendText("IPv6 flow label was "
314 + + Integer.toString(jsonFlowLabel));
315 + return false;
316 + }
317 + return true;
318 + }
319 +
320 + /**
321 + * Matches an ICMP V6 type criterion object.
322 + *
323 + * @param criterion criterion to match
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();
330 + if (icmpv6Type != jsonIcmpv6Type) {
331 + description.appendText("icmpv6 type was "
332 + + Short.toString(jsonIcmpv6Type));
333 + return false;
334 + }
335 + return true;
336 + }
337 +
338 + /**
339 + * Matches an IPV6 code criterion object.
340 + *
341 + * @param criterion criterion to match
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();
348 + if (icmpv6Code != jsonIcmpv6Code) {
349 + description.appendText("icmpv6 code was "
350 + + Short.toString(jsonIcmpv6Code));
351 + return false;
352 + }
353 + return true;
354 + }
355 +
356 + /**
357 + * Matches an IPV6 ND target criterion object.
358 + *
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) {
363 + final String targetAddress =
364 + criterion.targetAddress().toString();
365 + final String jsonTargetAddress =
366 + jsonCriterion.get("targetAddress").textValue();
367 + if (!targetAddress.equals(jsonTargetAddress)) {
368 + description.appendText("target address was " +
369 + jsonTargetAddress);
370 + return false;
371 + }
372 + return true;
373 + }
374 +
375 + /**
376 + * Matches an IPV6 ND link layer criterion object.
377 + *
378 + * @param criterion criterion to match
379 + * @return true if the JSON matches the criterion, false otherwise.
380 + */
381 + private boolean matchCriterion(Criteria.IPv6NDLinkLayerAddressCriterion criterion) {
382 + final String llAddress =
383 + criterion.mac().toString();
384 + final String jsonLlAddress =
385 + jsonCriterion.get("mac").textValue();
386 + if (!llAddress.equals(jsonLlAddress)) {
387 + description.appendText("mac was " + jsonLlAddress);
388 + return false;
389 + }
390 + return true;
391 + }
392 +
393 + /**
394 + * Matches an MPLS label criterion object.
395 + *
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();
401 + final int jsonLabel = jsonCriterion.get("label").intValue();
402 + if (label != jsonLabel) {
403 + description.appendText("label was " + Integer.toString(jsonLabel));
404 + return false;
405 + }
406 + return true;
407 + }
408 +
409 + /**
410 + * Matches an IPV6 exthdr criterion object.
411 + *
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();
417 + final int jsonExthdrFlags =
418 + jsonCriterion.get("exthdrFlags").intValue();
419 + if (exthdrFlags != jsonExthdrFlags) {
420 + description.appendText("exthdrFlags was "
421 + + Long.toHexString(jsonExthdrFlags));
422 + return false;
423 + }
424 + return true;
425 + }
426 +
427 + /**
428 + * Matches a lambda criterion object.
429 + *
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();
435 + final int jsonLambda = jsonCriterion.get("lambda").intValue();
436 + if (lambda != jsonLambda) {
437 + description.appendText("lambda was " + Integer.toString(lambda));
438 + return false;
439 + }
440 + return true;
441 + }
442 +
443 + /**
444 + * Matches an optical signal type criterion object.
445 + *
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();
451 + final short jsonSignalType = jsonCriterion.get("signalType").shortValue();
452 + if (signalType != jsonSignalType) {
453 + description.appendText("signal type was " + Short.toString(signalType));
454 + return false;
455 + }
456 + return true;
457 + }
458 +
37 @Override 459 @Override
38 public boolean matchesSafely(JsonNode jsonCriterion, 460 public boolean matchesSafely(JsonNode jsonCriterion,
39 Description description) { 461 Description description) {
462 + this.description = description;
463 + this.jsonCriterion = jsonCriterion;
40 final String type = criterion.type().name(); 464 final String type = criterion.type().name();
41 final String jsonType = jsonCriterion.get("type").asText(); 465 final String jsonType = jsonCriterion.get("type").asText();
42 if (!type.equals(jsonType)) { 466 if (!type.equals(jsonType)) {
...@@ -48,285 +472,88 @@ public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNo ...@@ -48,285 +472,88 @@ public final class CriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNo
48 472
49 case IN_PORT: 473 case IN_PORT:
50 case IN_PHY_PORT: 474 case IN_PHY_PORT:
51 - final Criteria.PortCriterion portCriterion = 475 + return matchCriterion((Criteria.PortCriterion) criterion);
52 - (Criteria.PortCriterion) criterion;
53 - final long port = portCriterion.port().toLong();
54 - final long jsonPort = jsonCriterion.get("port").asLong();
55 - if (port != jsonPort) {
56 - description.appendText("port was " + Long.toString(jsonPort));
57 - return false;
58 - }
59 - break;
60 476
61 case METADATA: 477 case METADATA:
62 - final Criteria.MetadataCriterion metadataCriterion = 478 + return matchCriterion((Criteria.MetadataCriterion) criterion);
63 - (Criteria.MetadataCriterion) criterion;
64 - final long metadata = metadataCriterion.metadata();
65 - final long jsonMetadata = jsonCriterion.get("metadata").asLong();
66 - if (metadata != jsonMetadata) {
67 - description.appendText("metadata was " + Long.toString(jsonMetadata));
68 - return false;
69 - }
70 - break;
71 479
72 case ETH_DST: 480 case ETH_DST:
73 case ETH_SRC: 481 case ETH_SRC:
74 - final Criteria.EthCriterion ethCriterion = 482 + return matchCriterion((Criteria.EthCriterion) criterion);
75 - (Criteria.EthCriterion) criterion;
76 - final String mac = ethCriterion.mac().toString();
77 - final String jsonMac = jsonCriterion.get("mac").textValue();
78 - if (!mac.equals(jsonMac)) {
79 - description.appendText("mac was " + jsonMac);
80 - return false;
81 - }
82 - break;
83 483
84 case ETH_TYPE: 484 case ETH_TYPE:
85 - final Criteria.EthTypeCriterion ethTypeCriterion = 485 + return matchCriterion((Criteria.EthTypeCriterion) criterion);
86 - (Criteria.EthTypeCriterion) criterion;
87 - final String ethType =
88 - Long.toHexString(ethTypeCriterion.ethType());
89 - final String jsonEthType = jsonCriterion.get("ethType").textValue();
90 - if (!ethType.equals(jsonEthType)) {
91 - description.appendText("ethType was " + jsonEthType);
92 - return false;
93 - }
94 - break;
95 486
96 case VLAN_VID: 487 case VLAN_VID:
97 - final Criteria.VlanIdCriterion vlanIdCriterion = 488 + return matchCriterion((Criteria.VlanIdCriterion) criterion);
98 - (Criteria.VlanIdCriterion) criterion;
99 - final short vlanId = vlanIdCriterion.vlanId().toShort();
100 - final short jsonVlanId = jsonCriterion.get("vlanId").shortValue();
101 - if (vlanId != jsonVlanId) {
102 - description.appendText("vlanId was " + Short.toString(jsonVlanId));
103 - return false;
104 - }
105 - break;
106 489
107 case VLAN_PCP: 490 case VLAN_PCP:
108 - final Criteria.VlanPcpCriterion vlanPcpCriterion = 491 + return matchCriterion((Criteria.VlanPcpCriterion) criterion);
109 - (Criteria.VlanPcpCriterion) criterion;
110 - final byte priority = vlanPcpCriterion.priority();
111 - final byte jsonPriority = (byte) jsonCriterion.get("priority").shortValue();
112 - if (priority != jsonPriority) {
113 - description.appendText("priority was " + Byte.toString(jsonPriority));
114 - return false;
115 - }
116 - break;
117 492
118 case IP_DSCP: 493 case IP_DSCP:
119 - final Criteria.IPDscpCriterion ipDscpCriterion = 494 + return matchCriterion((Criteria.IPDscpCriterion) criterion);
120 - (Criteria.IPDscpCriterion) criterion;
121 - final byte ipDscp = ipDscpCriterion.ipDscp();
122 - final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue();
123 - if (ipDscp != jsonIpDscp) {
124 - description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp));
125 - return false;
126 - }
127 - break;
128 495
129 case IP_ECN: 496 case IP_ECN:
130 - final Criteria.IPEcnCriterion ipEcnCriterion = 497 + return matchCriterion((Criteria.IPEcnCriterion) criterion);
131 - (Criteria.IPEcnCriterion) criterion;
132 - final byte ipEcn = ipEcnCriterion.ipEcn();
133 - final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue();
134 - if (ipEcn != jsonIpEcn) {
135 - description.appendText("IP ECN was " + Byte.toString(jsonIpEcn));
136 - return false;
137 - }
138 - break;
139 498
140 case IP_PROTO: 499 case IP_PROTO:
141 - final Criteria.IPProtocolCriterion iPProtocolCriterion = 500 + return matchCriterion((Criteria.IPProtocolCriterion) criterion);
142 - (Criteria.IPProtocolCriterion) criterion;
143 - final short protocol = iPProtocolCriterion.protocol();
144 - final short jsonProtocol = jsonCriterion.get("protocol").shortValue();
145 - if (protocol != jsonProtocol) {
146 - description.appendText("protocol was " + Short.toString(jsonProtocol));
147 - return false;
148 - }
149 - break;
150 501
151 case IPV4_SRC: 502 case IPV4_SRC:
152 case IPV4_DST: 503 case IPV4_DST:
153 case IPV6_SRC: 504 case IPV6_SRC:
154 case IPV6_DST: 505 case IPV6_DST:
155 - final Criteria.IPCriterion ipCriterion = 506 + return matchCriterion((Criteria.IPCriterion) criterion);
156 - (Criteria.IPCriterion) criterion;
157 - final String ip = ipCriterion.ip().toString();
158 - final String jsonIp = jsonCriterion.get("ip").textValue();
159 - if (!ip.equals(jsonIp)) {
160 - description.appendText("ip was " + jsonIp);
161 - return false;
162 - }
163 - break;
164 507
165 case TCP_SRC: 508 case TCP_SRC:
166 case TCP_DST: 509 case TCP_DST:
167 - final Criteria.TcpPortCriterion tcpPortCriterion = 510 + return matchCriterion((Criteria.TcpPortCriterion) criterion);
168 - (Criteria.TcpPortCriterion) criterion;
169 - final int tcpPort = tcpPortCriterion.tcpPort();
170 - final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue();
171 - if (tcpPort != jsonTcpPort) {
172 - description.appendText("tcp port was " + Integer.toString(jsonTcpPort));
173 - return false;
174 - }
175 - break;
176 511
177 case UDP_SRC: 512 case UDP_SRC:
178 case UDP_DST: 513 case UDP_DST:
179 - final Criteria.UdpPortCriterion udpPortCriterion = 514 + return matchCriterion((Criteria.UdpPortCriterion) criterion);
180 - (Criteria.UdpPortCriterion) criterion;
181 - final int udpPort = udpPortCriterion.udpPort();
182 - final int jsonUdpPort = jsonCriterion.get("udpPort").intValue();
183 - if (udpPort != jsonUdpPort) {
184 - description.appendText("udp port was " + Integer.toString(jsonUdpPort));
185 - return false;
186 - }
187 - break;
188 515
189 case SCTP_SRC: 516 case SCTP_SRC:
190 case SCTP_DST: 517 case SCTP_DST:
191 - final Criteria.SctpPortCriterion sctpPortCriterion = 518 + return matchCriterion((Criteria.SctpPortCriterion) criterion);
192 - (Criteria.SctpPortCriterion) criterion;
193 - final int sctpPort = sctpPortCriterion.sctpPort();
194 - final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue();
195 - if (sctpPort != jsonSctpPort) {
196 - description.appendText("sctp port was " + Integer.toString(jsonSctpPort));
197 - return false;
198 - }
199 - break;
200 519
201 case ICMPV4_TYPE: 520 case ICMPV4_TYPE:
202 - final Criteria.IcmpTypeCriterion icmpTypeCriterion = 521 + return matchCriterion((Criteria.IcmpTypeCriterion) criterion);
203 - (Criteria.IcmpTypeCriterion) criterion;
204 - final short icmpType = icmpTypeCriterion.icmpType();
205 - final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue();
206 - if (icmpType != jsonIcmpType) {
207 - description.appendText("icmp type was " + Short.toString(jsonIcmpType));
208 - return false;
209 - }
210 - break;
211 522
212 case ICMPV4_CODE: 523 case ICMPV4_CODE:
213 - final Criteria.IcmpCodeCriterion icmpCodeCriterion = 524 + return matchCriterion((Criteria.IcmpCodeCriterion) criterion);
214 - (Criteria.IcmpCodeCriterion) criterion;
215 - final short icmpCode = icmpCodeCriterion.icmpCode();
216 - final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue();
217 - if (icmpCode != jsonIcmpCode) {
218 - description.appendText("icmp code was " + Short.toString(jsonIcmpCode));
219 - return false;
220 - }
221 - break;
222 525
223 case IPV6_FLABEL: 526 case IPV6_FLABEL:
224 - final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion = 527 + return matchCriterion((Criteria.IPv6FlowLabelCriterion) criterion);
225 - (Criteria.IPv6FlowLabelCriterion) criterion;
226 - final int flowLabel = ipv6FlowLabelCriterion.flowLabel();
227 - final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue();
228 - if (flowLabel != jsonFlowLabel) {
229 - description.appendText("IPv6 flow label was " + Integer.toString(jsonFlowLabel));
230 - return false;
231 - }
232 - break;
233 528
234 case ICMPV6_TYPE: 529 case ICMPV6_TYPE:
235 - final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion = 530 + return matchCriterion((Criteria.Icmpv6TypeCriterion) criterion);
236 - (Criteria.Icmpv6TypeCriterion) criterion;
237 - final short icmpv6Type = icmpv6TypeCriterion.icmpv6Type();
238 - final short jsonIcmpv6Type = jsonCriterion.get("icmpv6Type").shortValue();
239 - if (icmpv6Type != jsonIcmpv6Type) {
240 - description.appendText("icmpv6 type was " + Short.toString(jsonIcmpv6Type));
241 - return false;
242 - }
243 - break;
244 531
245 case ICMPV6_CODE: 532 case ICMPV6_CODE:
246 - final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion = 533 + return matchCriterion((Criteria.Icmpv6CodeCriterion) criterion);
247 - (Criteria.Icmpv6CodeCriterion) criterion;
248 - final short icmpv6Code = icmpv6CodeCriterion.icmpv6Code();
249 - final short jsonIcmpv6Code = jsonCriterion.get("icmpv6Code").shortValue();
250 - if (icmpv6Code != jsonIcmpv6Code) {
251 - description.appendText("icmpv6 code was " + Short.toString(jsonIcmpv6Code));
252 - return false;
253 - }
254 - break;
255 534
256 case IPV6_ND_TARGET: 535 case IPV6_ND_TARGET:
257 - final Criteria.IPv6NDTargetAddressCriterion 536 + return matchCriterion(
258 - ipv6NDTargetAddressCriterion = 537 + (Criteria.IPv6NDTargetAddressCriterion) criterion);
259 - (Criteria.IPv6NDTargetAddressCriterion) criterion;
260 - final String targetAddress =
261 - ipv6NDTargetAddressCriterion.targetAddress().toString();
262 - final String jsonTargetAddress =
263 - jsonCriterion.get("targetAddress").textValue();
264 - if (!targetAddress.equals(jsonTargetAddress)) {
265 - description.appendText("target address was " +
266 - jsonTargetAddress);
267 - return false;
268 - }
269 - break;
270 538
271 case IPV6_ND_SLL: 539 case IPV6_ND_SLL:
272 case IPV6_ND_TLL: 540 case IPV6_ND_TLL:
273 - final Criteria.IPv6NDLinkLayerAddressCriterion 541 + return matchCriterion(
274 - ipv6NDLinkLayerAddressCriterion = 542 + (Criteria.IPv6NDLinkLayerAddressCriterion) criterion);
275 - (Criteria.IPv6NDLinkLayerAddressCriterion) criterion;
276 - final String llAddress =
277 - ipv6NDLinkLayerAddressCriterion.mac().toString();
278 - final String jsonLlAddress =
279 - jsonCriterion.get("mac").textValue();
280 - if (!llAddress.equals(jsonLlAddress)) {
281 - description.appendText("mac was " + jsonLlAddress);
282 - return false;
283 - }
284 - break;
285 543
286 case MPLS_LABEL: 544 case MPLS_LABEL:
287 - final Criteria.MplsCriterion mplsCriterion = 545 + return matchCriterion((Criteria.MplsCriterion) criterion);
288 - (Criteria.MplsCriterion) criterion;
289 - final int label = mplsCriterion.label();
290 - final int jsonLabel = jsonCriterion.get("label").intValue();
291 - if (label != jsonLabel) {
292 - description.appendText("label was " + Integer.toString(jsonLabel));
293 - return false;
294 - }
295 - break;
296 546
297 case IPV6_EXTHDR: 547 case IPV6_EXTHDR:
298 - final Criteria.IPv6ExthdrFlagsCriterion exthdrCriterion = 548 + return matchCriterion(
299 - (Criteria.IPv6ExthdrFlagsCriterion) criterion; 549 + (Criteria.IPv6ExthdrFlagsCriterion) criterion);
300 - final int exthdrFlags = exthdrCriterion.exthdrFlags();
301 - final int jsonExthdrFlags =
302 - jsonCriterion.get("exthdrFlags").intValue();
303 - if (exthdrFlags != jsonExthdrFlags) {
304 - description.appendText("exthdrFlags was " + Long.toHexString(jsonExthdrFlags));
305 - return false;
306 - }
307 - break;
308 550
309 case OCH_SIGID: 551 case OCH_SIGID:
310 - final Criteria.LambdaCriterion lambdaCriterion = 552 + return matchCriterion((Criteria.LambdaCriterion) criterion);
311 - (Criteria.LambdaCriterion) criterion;
312 - final int lambda = lambdaCriterion.lambda();
313 - final int jsonLambda = jsonCriterion.get("lambda").intValue();
314 - if (lambda != jsonLambda) {
315 - description.appendText("lambda was " + Integer.toString(lambda));
316 - return false;
317 - }
318 - break;
319 553
320 case OCH_SIGTYPE: 554 case OCH_SIGTYPE:
321 - final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion = 555 + return matchCriterion(
322 - (Criteria.OpticalSignalTypeCriterion) criterion; 556 + (Criteria.OpticalSignalTypeCriterion) criterion);
323 - final short signalType = opticalSignalTypeCriterion.signalType();
324 - final short jsonSignalType = jsonCriterion.get("signalType").shortValue();
325 - if (signalType != jsonSignalType) {
326 - description.appendText("signal type was " + Short.toString(signalType));
327 - return false;
328 - }
329 - break;
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 }
......