2

I have problems with closeArray in pact-jvm-consumer.

Given Json like this, how would the "DslPart imeiResults = new PactDslJsonBody()"-statment be constructed.

{ 
   "Car": {
     "Price": 123,     
     "Features": [
         "rain sensor",
         "cruise control"
     ],
     "Id": "6500"
   }
}

I tried like this:

    DslPart etaResults = new PactDslJsonBody()
           .object("Car")
                .integerType("Price",123)
                .array("Features")
                    .stringValue("rain sensor")
                    .stringValue("cruise control")
                .closeArray()
                .stringValue("Id","6500")
            .closeObject()
            .asBody();

But that does not work, for example .closeArray() does not return PactDslJsonBody but DslPart, so you can never have anything after .closeArray()? I don't get it, can someone show the code on how to do this the correct way?

1 Answers1

1

I'm taking a guess that your stringValue after the closeArray isn't working?

Sadly, when creating an array using the array function, it actually creates a new PactDslJsonArray and when closing it, there's no way for that class to know what the parent is, hence it just returns the common superclass of DslPart, which can cause some confusion. What needs to be done is cast that DslPart back to a PactDslJsonBody using the asBody function. So, your example should be something like:

DslPart etaResults = new PactDslJsonBody()
   .object("Car")
        .integerType("Price",123)
        .array("Features")
            .stringValue("rain sensor")
            .stringValue("cruise control")
        .closeArray()
        .asBody()
        .stringValue("Id","6500")
    .closeObject();

Now, we know that this is confusing, hence why we started working on a new DSL using Java 8's Lambda functions to try to make the experience better. Hope that helps.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • OK, I could get that example to work. But now I am stuck again. Ca you assist in creating a DslPart from this Json: { "Inventory": [ { "Car": { "gearbox": "automatic", "ProductId": 30212 }, "Camera": { "EndPrice": 1235, "Conditions": [ "FaultyButtons", "FaultyCasing" ], "ModelId": "650" } } ], "IsSuccess": true, "Info": "Ok" } – Mattias Malmgren Dec 22 '17 at 13:07
  • What's the issue you're having? I'm not keen on doing the work for you. Please try and come back with an actual question. Might be better to start a new question altogether. – J_A_X Dec 28 '17 at 23:46