2

I can successfully use PactDslJsonArray.arrayMaxLike(3,3) to create a pact that validates a maximum of 3 items returned.

"body": [
{
    "firstName": "first",
    "lastName": "last",
    "city": "test",
},
{
    "firstName": "first",
    "lastName": "last",
    "city": "test",
},
{
    "firstName": "first",
    "lastName": "last",
    "city": "test",
}
]


"body": {
"$": {
    "matchers": [
        {
            "match": "type",
            "max": 3
        }
    ]
...

However, I would like to reuse the body from another request without the need to specify the attributes again.

DslPart body = new PactDslJsonBody()
    .stringType("firstName","first")
    .stringType("lastName","last")
    .stringType("city", "test")

What I'm looking for is something like :

PactDslJsonArray.arrayMaxLike(3,3).template(body)

instead of

PactDslJsonArray.arrayMaxLike(3,3)
  .stringType("firstName","first")  
  .stringType("lastName","last")  
  .stringType("city", "test")

Thanks

Dan

  • So long as the [body is-a or subclasses `DslPart`](https://github.com/DiUS/pact-jvm/blob/3_5_14_2.11/pact-jvm-consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonArray.java#L651), what you're looking (_i.e._ `PactDslJsonArray.arrayMaxLike(3,3).template(body)`) for should run without any errors. Did you run into errors trying it? – Oluwafemi Sule Apr 06 '18 at 20:27
  • Unfortunately, `PactDslJsonArray.arrayMaxLike()` returns a `PactDslJsonBody` which does not have a `template` method.This method is only available on a `PactDslJsonArray` object. – user9609076 Apr 07 '18 at 16:22
  • I see, you can make use of [one of the method signatures returning a PactDslJsonArray object](https://github.com/DiUS/pact-jvm/blob/3_5_14_2.11/pact-jvm-consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonArray.java#L804) – Oluwafemi Sule Apr 07 '18 at 16:31
  • Thanks for your help. `PactDslJsonArray.arrayMaxLike()` returns an array but requires a `PactDslJsonRootValue` parameter which from what I understand, is meant for values at the root of the json body. I am unsure as to how to use it or if it is the right approach. – user9609076 Apr 07 '18 at 17:37
  • If we could access the parent from the DslPart, it would be possible to verify its type and add the body to it when the parent is a `PactDslJsonArray` : `DslPart parent = pactDslJsonBody.getParent(); if (parent instanceof PactDslJsonArray) { ((PactDslJsonArray)parent).template(template); }` Would that be _acceptable_? – user9609076 Apr 09 '18 at 17:30
  • How will you test for when parent is not a PactDslJsonArray instance? – Oluwafemi Sule Apr 09 '18 at 20:18
  • In this use case, it would only make sense if it is an array as we want to have a Pact matching rule that verifies a max array size. I've found a request for an enhancement similar to this on the Pact github issue list : https://github.com/DiUS/pact-jvm/issues/661 – user9609076 Apr 10 '18 at 19:47

1 Answers1

2

The point of the DSL is to do validations of the Pact interactions in code. Using a template kinda goes against that concept. What I would recommend is that if you have the same interactions in multiple places, then adding a shared function to add said interaction would be the best way to do so. For example:

private void personalDetailInteraction(DslPart part) {
   return part.stringType("firstName","first")
    .stringType("lastName","last")
    .stringType("city", "test");
}

private void yourTest() {
    personalDetailInteraction(
        PactDslJsonArray.arrayMaxLike(3,3)
    )
    .stringType("blarg", "weee")
    ...
}

If it needs to be shared across different classes, create a InteractionUtils class that can be shared across. This is the best way to do it in my opinion because the compiler makes sure no mistakes are made while creating the interactions, which is kind of the point of the whole framework; to reduce human error.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • Thanks for your suggestion. This is way more elegant than appending to parent as I was proposing. I understand using a template might seems to defeat the purpose of the DSL but it was more a DRY concern that I was aiming for. Merci! – user9609076 Apr 20 '18 at 20:28