0

I not able to use the PACT DSL .closeObject() to format a PACT interaction response. I'm asking for suggestions to make this work or asking if .closeObject() is not working as intended? I have a Shopping cart with 2 items. When I try to format the expected response, with 2 items, using .closeObject(), it will not compile, see code below. The compile error is on the first .closeObject(), after ".stringMatcher("name","iPhone") line. I need to create a hierarchy of shoppingCartItems, in the PACT files expected response. The advertised usage of PACT DSL .closeObject(), can be found from this link, in the "matching any key in a map section" PACT DSL examples of using .closeObject()

private DslPart respSc6() {
    DslPart body = new PactDslJsonBody()
        .stringMatcher("id", "ShoppingCart_[0-9]*", "ShoppingCart_0")
        .eachLike("shoppingCartItem")               
        .numberValue("quantity", 1)
        .stringMatcher("state","new")
        .object("productOffering")                              
        .stringMatcher("id","IPHONE_7")
        .stringMatcher("name","iPhone")
        .closeObject()
        .numberValue("quantity", 5)
        .stringMatcher("state","new")               
        .object("productOffering")                                              
        .stringMatcher("id","SMSG_GLXY_S8")
        .stringMatcher("name","Samsung_Galaxy_S8")
        .closeObject()              
        .closeObject()
        .closeArray();
    return body;
}

The expected JSON response payload, should look like Expected PACT response payload with hierarchical data

  • What's the compilation error message exactly? It looks like you might be closing the object one too many times? Also, you have an ending closeArray, but you never start an array anywhere. I would recommend that you use the JSON body string matcher instead since it makes things a bit simpler than creating the object using the DSL. – J_A_X Apr 24 '17 at 00:30

1 Answers1

1

Here is the corrected and annotated code to match your example JSON.

  private DslPart respSc6() {
    DslPart body = new PactDslJsonBody()
      .stringMatcher("id", "ShoppingCart_[0-9]*", "ShoppingCart_0")
      .eachLike("shoppingCartItem") // Starts an array [1] and an object [2] (like calling .object(...)) and applies it to all items
        .numberValue("quantity", 1)
        .stringMatcher("state", "new") // You are using a simple string as the regex here, so it will only match 'new'
        .object("productOffering") // Start a new object [3]
          .stringMatcher("id", "IPHONE_7") // Again, this regex will only match 'IPHONE_7'
          .stringMatcher("name", "iPhone") // Again, this regex will only match 'iPhone'
        .closeObject() // Close the object started in [3]
      .closeObject() // Close the object started in [2]
      .closeArray(); // Close the array started in [1]
    return body;
  }

You do not need to provide two example object definitions for the shoppingCartItem array, as the .eachLike matcher is designed to apply one definition to all items in the array. If you want the generated example JSON to contain two items, pass the number two in as the second parameter, e.g. .eachLike("shoppingCartItem", 2).