1

I have a sample body as below;

[
  "01/01/2019",
  "01/02/2019",
  "01/03/2019"
]

I'd like to validate the following: 1. The response should be an array with at least 1 element 2. Each element should be: 2.1. String 2.2. Should be in this format "DD/MM/YYYY"

It works perfectly fine with the "old" style:

DslPart body = PactDslJsonArray.arrayMinLike(1, 2, PactDslJsonRootValue.stringMatcher(Constants.DATETIMEPATTERN, "01/01/2019"));

This would generate pact file with matching rules as below. Notice the $ and $[*]

...
                "matchingRules": {
                    "body": {
                        "$": {
                            "matchers": [
                                {
                                    "match": "type",
                                    "min": 1
                                }
                            ],
                            "combine": "AND"
                        },
                        "$[*]": {
                            "matchers": [
                                {
                                    "match": "regex",
                                    "regex": "^(([0-3]?\\d+)\\/((0?[1-9])|(1[0-2]))\\/20\\d{2})$"
                                }
                            ],
                            "combine": "AND"
                        }
                    }
                }

However, when I try with the "new" style, which is lambda Dsl for Pact (Java8), it doesn't generate the same matching rules; hence doesn't work the same way and my provider test always fails with the below error message

$ -> Expected a List with 1 elements but received 3 elements

Below is the code and the matching rules that my code generate. Notice the $[0] and $[1]: Matching rules

...
"matchingRules": {
                    "body": {
                        "$[0]": {
                            "matchers": [
                                {
                                    "match": "regex",
                                    "regex": "^(([0-3]?\\d+)\\/((0?[1-9])|(1[0-2]))\\/20\\d{2})$"
                                }
                            ],
                            "combine": "AND"
                        },
                        "$[1]": {
                            "matchers": [
                                {
                                    "match": "type",
                                    "min": 1
                                }
                            ],
                            "combine": "AND"
                        }
                    }
                }

And the code

DslPart body = newJsonArray((array) -> {
    array
            .stringMatcher(Constants.DATETIMEPATTERN, "01/01/2019");
}).minArrayLike(1).build();

Tried another way

DslPart body = newJsonArray((array) -> {
    array
            .stringMatcher(Constants.DATETIMEPATTERN, "01/01/2019")
            .minArrayLike(1);
}).build();

I've been trying to place the .minArrayLike(1) in different places but none of them works. Is there anyway to produce the same matching rules using lambda Dsl for Pact?

Sam Vo
  • 113
  • 10

1 Answers1

0

I ran into the same issue. Please see the problem and the solution here

eg:-

final DslPart actualPactDsl = LambdaDsl.newJsonBody((bodyDsl) -> {
            bodyDsl
                    .stringType("id", "1")
                    .stringType("name","Dep 1")
                    .minArrayLike("students",1,(stud) ->{
                        stud
                            .stringType("id","1")
                               .stringType("firstName","John")
                               .stringType("lastName","Smith")
                               .numberType("age",21);
                    });

        }).build();
ShefZee
  • 95
  • 7