From ef276f8b40148526f9dad39c5a482e1d2890a1b2 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 17 Jan 2023 14:05:57 +1100 Subject: [PATCH] chore: add a test to verify issue PactDslJsonBody#eachKeyMappedToAnArrayLike does not work on "nested" property #401 --- .../com/dius/pact/consumer/dsl/DslSpec.groovy | 33 +++++++++++++ .../pact/core/matchers/MatchingSpec.groovy | 49 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/DslSpec.groovy b/consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/DslSpec.groovy index a4dc5fb1de..48a8694ad7 100644 --- a/consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/DslSpec.groovy +++ b/consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/DslSpec.groovy @@ -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 @@ -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]) + ]) + } } diff --git a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingSpec.groovy b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingSpec.groovy index be542e3d12..f574ade39a 100644 --- a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingSpec.groovy +++ b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingSpec.groovy @@ -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 { @@ -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' + } }