1

I am trying to create a contract between with a consumer when making a request for an item information. The item has a very complex data structure with nested properties and contains fields that ranges from strings, lists, arrays of strings, arrays of enums etc. There is also a complexity added where some of the fields may be returned as a null or populated with a value respective to its type.

What I would like to do is to create a pact response matcher that can be re-used for any item. At the moment, there are 120 items (each item is a separate API call with the item name in the query param of the request) that I could request information for and writing a pact matcher for each item is not feasible / maintainable.

E.g. Sample response cut down ( the actual response is over 60 lines):

   return new PactDslJsonBody()
                .object("metadata")
                   .stringValue("name", "item-1")
                   .integerType("index", 1)
                   .eachLike("contents")
                       .stringType("param", "content-param")
                       .stringType("value", "content-value")
                   .closeArray
                .closeObject

The problem I have is that the field could also come back as from the provider. What I am trying to achieve is whether I can within the same PactDslJsonBody have an OR condition or something similar which can check for the eachLike but also can accept that the field is null.

Contents field populated:

{
   "metadata":{
      "name":"item-1",
      "index":1,
      "contents":[
         {
            "param":"content-param",
            "value":"content-value"
         }
      ]
   }
}

Contents field null:

{
   "metadata":{
      "name":"item-1",
      "index":1,
      "contents":null
   }
}

I see there is PactDslJsonBody.or() available but the usage of this is not documented clearly, hence I am unsure if this is the intended usage for the above use case.

Writing separate tests is not feasible as this is a specific use case to repeat the same PactDslJsonBody for n number of consumer tests, one for each item. We need to test each item as we have a direct mapping of some field values as enums between the consumer and provider hence we want to make sure that each item has the expected values for such fields. We also dont know in advance which items will this field as null and which ones have it populated, hence why we wanted to have the option to use something like orNull etc.

I have looked quite a lot in the web and couldn't find a definitive answer to the above. Any help on this would be appreciated on how to move forward with this. We really want to use Pact since it is the go to tool for us and the above is a crucial use case for us to test.

I am using Pact JVM version 4.3.15 and using V3 specification.

Thanks.

1 Answers1

1

See https://docs.pact.io/faq#why-is-there-no-support-for-specifying-optional-attributes. You need to write two separate test cases that cover both scenarios.

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18