@@ -5,6 +5,8 @@ import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl
5
5
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
6
6
import spock.lang.Issue
7
7
import spock.lang.Specification
8
+ import spock.lang.Unroll
9
+ import spock.util.environment.RestoreSystemProperties
8
10
9
11
@SuppressWarnings ([' LineLength' , ' PrivateFieldCouldBeFinal' ])
10
12
class XmlBodyMatcherSpec extends Specification {
@@ -14,6 +16,8 @@ class XmlBodyMatcherSpec extends Specification {
14
16
private XmlBodyMatcher matcher
15
17
16
18
def setup () {
19
+ System . clearProperty(' pact.matching.xml.namespace-aware' )
20
+
17
21
matcher = new XmlBodyMatcher ()
18
22
matchers = new MatchingRulesImpl ()
19
23
expectedBody = OptionalBody . missing()
@@ -340,4 +344,135 @@ class XmlBodyMatcherSpec extends Specification {
340
344
matcher. matchBody(expectedBody, actualBody, false , matchers). empty
341
345
}
342
346
347
+ @Unroll
348
+ def ' matching XML bodies - with different namespace declarations' () {
349
+ given :
350
+ actualBody = OptionalBody . body(actual. bytes)
351
+ expectedBody = OptionalBody . body(expected. bytes)
352
+
353
+ expect :
354
+ matcher. matchBody(expectedBody, actualBody, false , matchers). empty
355
+
356
+ where :
357
+ actual | expected
358
+ ' <blah xmlns="urn:ns"/>' | ' <blah xmlns="urn:ns"/>'
359
+ ' <blah xmlns="urn:ns"/>' | ' <b:blah xmlns:b="urn:ns"/>'
360
+ ' <a:blah xmlns:a="urn:ns"/>' | ' <blah xmlns="urn:ns"/>'
361
+ ' <a:blah xmlns:a="urn:ns"/>' | ' <b:blah xmlns:b="urn:ns"/>'
362
+ }
363
+
364
+ @Unroll
365
+ def ' matching XML bodies - with different namespace declarations - and have child elements' () {
366
+ given :
367
+ actualBody = OptionalBody . body(actual. bytes)
368
+ expectedBody = OptionalBody . body(expected. bytes)
369
+
370
+ expect :
371
+ matcher. matchBody(expectedBody, actualBody, false , matchers). empty
372
+
373
+ where :
374
+ actual | expected
375
+ ' <foo xmlns="urn:ns"><item/></foo>' | ' <foo xmlns="urn:ns"><item/></foo>'
376
+ ' <foo xmlns="urn:ns"><item/></foo>' | ' <b:foo xmlns:b="urn:ns"><b:item/></b:foo>'
377
+ ' <a:foo xmlns:a="urn:ns"><a:item/></a:foo>' | ' <foo xmlns="urn:ns"><item/></foo>'
378
+ ' <a:foo xmlns:a="urn:ns"><a:item/></a:foo>' | ' <b:foo xmlns:b="urn:ns"><b:item/></b:foo>'
379
+ ' <a:foo xmlns:a="urn:ns"><a2:item xmlns:a2="urn:ns"/></a:foo>' | ' <b:foo xmlns:b="urn:ns"><b:item/></b:foo>'
380
+ }
381
+
382
+ def ' matching XML bodies - returns a mismatch - when different namespaces are used' () {
383
+ given :
384
+ actualBody = OptionalBody . body(' <blah xmlns="urn:other"/>' . bytes)
385
+ expectedBody = OptionalBody . body(' <blah xmlns="urn:ns"/>' . bytes)
386
+
387
+ when :
388
+ def mismatches = matcher. matchBody(expectedBody, actualBody, false , matchers)
389
+
390
+ then :
391
+ ! mismatches. empty
392
+ mismatches* . mismatch == [' Expected element {urn:ns}blah but received {urn:other}blah' ]
393
+ mismatches* . path == [' $.blah' ]
394
+ }
395
+
396
+ def ' matching XML bodies - returns a mismatch - when expected namespace is not used' () {
397
+ given :
398
+ actualBody = OptionalBody . body(' <blah/>' . bytes)
399
+ expectedBody = OptionalBody . body(' <blah xmlns="urn:ns"/>' . bytes)
400
+
401
+ when :
402
+ def mismatches = matcher. matchBody(expectedBody, actualBody, false , matchers)
403
+
404
+ then :
405
+ ! mismatches. empty
406
+ mismatches* . mismatch == [' Expected element {urn:ns}blah but received blah' ]
407
+ mismatches* . path == [' $.blah' ]
408
+ }
409
+
410
+ def ' matching XML bodies - returns a mismatch - when allowUnexpectedKeys is true - and no namespace is expected' () {
411
+ given :
412
+ actualBody = OptionalBody . body(' <blah xmlns="urn:ns"/>' . bytes)
413
+ expectedBody = OptionalBody . body(' <blah/>' . bytes)
414
+
415
+ when :
416
+ def mismatches = matcher. matchBody(expectedBody, actualBody, true , matchers)
417
+
418
+ then :
419
+ ! mismatches. empty
420
+ mismatches* . mismatch == [' Expected element blah but received {urn:ns}blah' ]
421
+ mismatches* . path == [' $.blah' ]
422
+ }
423
+
424
+ @RestoreSystemProperties
425
+ def ' matching XML bodies - when allowUnexpectedKeys is true - and namespace-aware matching disabled - and no namespace is expected' () {
426
+ given :
427
+ System . setProperty(' pact.matching.xml.namespace-aware' , ' false' )
428
+ actualBody = OptionalBody . body(' <blah xmlns="urn:ns"/>' . bytes)
429
+ expectedBody = OptionalBody . body(' <blah/>' . bytes)
430
+
431
+ expect :
432
+ matcher. matchBody(expectedBody, actualBody, true , matchers). empty
433
+ }
434
+
435
+ def ' matching XML bodies - when attribute uses different prefix' () {
436
+ given :
437
+ actualBody = OptionalBody . body(' <foo xmlns:a="urn:ns" a:something="100"/>' . bytes)
438
+ expectedBody = OptionalBody . body(' <foo xmlns:b="urn:ns" b:something="100"/>' . bytes)
439
+
440
+ expect :
441
+ matcher. matchBody(expectedBody, actualBody, true , matchers). empty
442
+ }
443
+
444
+ def ' matching XML bodies - returns a mismatch - when attribute uses different namespace' () {
445
+ given :
446
+ actualBody = OptionalBody . body(' <foo xmlns:ns="urn:a" ns:something="100"/>' . bytes)
447
+ expectedBody = OptionalBody . body(' <foo xmlns:ns="urn:b" ns:something="100"/>' . bytes)
448
+
449
+ when :
450
+ def mismatches = matcher. matchBody(expectedBody, actualBody, false , matchers)
451
+
452
+ then :
453
+ ! mismatches. empty
454
+ mismatches* . mismatch == [' Expected {urn:b}something=\' 100\' but was missing' ]
455
+ mismatches* . path == [' $.foo.@ns:something' ]
456
+ }
457
+
458
+ def ' matching XML bodies - with namespaces and a matcher defined - delegate to matcher for attribute' () {
459
+ given :
460
+ actualBody = OptionalBody . body(' <foo xmlns:a="urn:ns" a:something="100"/>' . bytes)
461
+ expectedBody = OptionalBody . body(' <foo xmlns:b="urn:ns" b:something="101"/>' . bytes)
462
+ matchers. addCategory(' body' ). addRule(" \$ .foo['@b:something']" , new RegexMatcher (' \\ d+' ))
463
+
464
+ expect :
465
+ matcher. matchBody(expectedBody, actualBody, false , matchers). empty
466
+ }
467
+
468
+ def ' matching XML bodies - with namespaces and a matcher defined - delegate to the matcher' () {
469
+ given :
470
+ actualBody = OptionalBody . body(' <ns:foo xmlns:ns="urn:ns"><ns:something>100</ns:something></ns:foo>' . bytes)
471
+ expectedBody = OptionalBody . body(' <foo xmlns="urn:ns"><something>101</something></foo>' . bytes)
472
+ matchers. addCategory(' body' ). addRule(" \$ .foo.something" , new RegexMatcher (' \\ d+' ))
473
+
474
+ expect :
475
+ matcher. matchBody(expectedBody, actualBody, false , matchers). empty
476
+ }
477
+
343
478
}
0 commit comments