Search code examples
lambdapactpact-jvm

How to model named arrays and objects in Lambda DSL for Pact (pact-jvm-consumer)


The Json below contains three things that I don't know how to model in the lambda based DSL for Pact (and that I can't figure out by reading the examples provided on https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-java8 ).

The Json-object consists of 3 properties; "Inventory" which holds an array (which is only one element long), and two simple key-value pairs.

1) How do I declare an named array using the lambda based DSL?

The first (and only) object in the Inventory-array consists of two named objects "Car" and "Camera".

2) How do I declare a named object using the lambda based DSL?

In the "Camera"-object there is an array called "Conditions" which holds two string values.

3) How do I declare two example string values in a named array using the lambda based DSL?

 {
   "Inventory":[
      {
         "Car":{
            "gearbox":"automatic",
            "ProductId":30212
         },
         "Camera":{
            "EndPrice":1235,
            "Conditions":[
               "FaultyButtons",
               "FaultyCasing"
            ],
            "ModelId":"650"
         }
      }
   ],
   "IsSuccess":true,
   "Info":"Ok"
}

Solution

  • I haven't had the opportunity to use the new Lambda DSL as I use Node more often, but I'll give it a crack:

    newJsonBody((o) -> {
        // Inventory Array
        o
            .array("Inventory", (a) -> {
                // The first and only object in the array
                a.object((ao) -> {
                    ao
                        // Car Object
                        .object("Car", (car) -> {
                            car
                                .stringValue("gearbox", "automatic")
                                .numberValue("ProductId", 30212);
                        })
                        // Camera Object
                        .object("Camera", (camera) -> {
                            camera
                                .numberValue("EndPrice", 1235)
                                .stringValue("ModelId", "650")
                                .array("Condition", (c) -> { 
                                    c
                                        .stringValue("FaultyButtons")
                                        .stringValue("FaultyCasing");
                                });
                        });
                });
            })
            .booleanValue("IsSuccess", true)
            .stringValue("Info", "Ok");
    }).build()
    

    I'm not sure if this is working 100% as I haven't actually used an IDE to validate it, but I think this is going to give you a good idea of what I'm trying to accomplish.

    As you can see, you can chain multiple functions with each other since it keeps referring the start object, making it simple to add many values one after another, or if you prefer a more readable structure, you can create a newline for each function that references the scoped variable; it's really up to you. The Lambda DSL tries to take advantage of the scoping to create there more complex objects in almost the same way that the JSON would appear in the end.

    I know that the current documentation is slightly lacking on how to use this powerful new way of doing the DSL, but you might want to take a gander at the code that powers it for further reference as it is easy to read and help you decipher what you're after.