Skip to content

Commit

Permalink
chore: add a test to verify issue PactDslJsonBody#eachKeyMappedToAnAr…
Browse files Browse the repository at this point in the history
…rayLike does not work on "nested" property #401
  • Loading branch information
rholshausen committed Jan 17, 2023
1 parent 859fff2 commit ef276f8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package au.com.dius.pact.consumer.dsl

import au.com.dius.pact.core.model.matchingrules.DateMatcher
import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory
import au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
import au.com.dius.pact.core.model.matchingrules.TypeMatcher
import au.com.dius.pact.core.model.matchingrules.ValuesMatcher
import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll

Expand All @@ -23,4 +30,30 @@ class DslSpec extends Specification {
'01/01/2001' | "a.b.c['01/01/2001']"
'a[' | "a.b.c['a[']"
}

@Issue('#401')
def 'eachKeyMappedToAnArrayLike does not work on "nested" property'() {
given:
def body = new PactDslJsonBody()
.date("date", "yyyyMMdd'T'HHmmss")
.stringMatcher("system", ".+", "systemname")
.object("data")
.eachKeyMappedToAnArrayLike("subsystem_name")
.stringType("id","1234567")
.closeArray()
.closeObject()

when:
def result = body.close()

then:
result.body.toString() ==
'{"data":{"subsystem_name":[{"id":"1234567"}]},"date":"20000201T000000","system":"systemname"}'
result.matchers == new MatchingRuleCategory('body', [
'$.date': new MatchingRuleGroup([new DateMatcher("yyyyMMdd'T'HHmmss")]),
'$.system': new MatchingRuleGroup([new RegexMatcher('.+')]),
'$.data': new MatchingRuleGroup([ValuesMatcher.INSTANCE]),
'$.data.*[*].id': new MatchingRuleGroup([TypeMatcher.INSTANCE])
])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import au.com.dius.pact.core.model.PactReaderKt
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.Response
import au.com.dius.pact.core.model.matchingrules.ContentTypeMatcher
import au.com.dius.pact.core.model.matchingrules.DateMatcher
import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory
import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl
import au.com.dius.pact.core.model.matchingrules.MinTypeMatcher
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
import au.com.dius.pact.core.model.matchingrules.TypeMatcher
import au.com.dius.pact.core.model.matchingrules.ValuesMatcher
import spock.lang.Issue
import spock.lang.Specification

class MatchingSpec extends Specification {
Expand Down Expand Up @@ -171,4 +176,48 @@ class MatchingSpec extends Specification {
then:
result.mismatches.empty
}

@Issue("401")
def 'Body Matching - eachKeyMappedToAnArrayLike does not work on "nested" property'() {
given:
bodyContext.matchers
.addRule('$.date', new DateMatcher("yyyyMMdd'T'HHmmss"))
.addRule('$.system', new RegexMatcher(".+"))
.addRule('$.data', ValuesMatcher.INSTANCE)
.addRule('$.data.*[*].id', TypeMatcher.INSTANCE)

def body =
'{\n' +
' "date": "20000131T140000",\n' +
' "system": "systemname",\n' +
' "data": {\n' +
' "subsystem_name": [\n' +
' {\n' +
' "id": "1234567"\n' +
' }\n' +
' ]\n' +
' }\n' +
'}'
def actualBody = '{\n' +
' "date": "19880101T120101",\n' +
' "system_name": "s1",\n' +
' "data": {\n' +
' "sub": [\n' +
' {\n' +
' "hop":"san"\n' +
' }\n' +
' ]\n' +
' }\n' +
'}'
def expected = new Response(200, ['content-type': ['application/json']], OptionalBody.body(body.bytes))
def actual = new Response(200, ['content-type': ['application/json']], OptionalBody.body(actualBody.bytes))

when:
def result = Matching.INSTANCE.matchBody(expected, actual, bodyContext).mismatches

then:
result.size() == 3
result[0].mismatch == 'Actual map is missing the following keys: system'
result[1].mismatch == 'Actual map is missing the following keys: id'
}
}

0 comments on commit ef276f8

Please sign in to comment.