1

I want to get the value of status from my response. So that i can assert it. I'm using rest assured with java & serenity BDD.

Response

{
    "locationType": "STORE",
    "locationId": "0003",
    "events": {
        "66e326db-fbfb-4f6e-9d2b-9425e7test5": {
            "status": "BOOKING_OPEN"
        }
    }
}

So, here the event id (66e326db-fbfb-4f6e-9d2b-9425e7test5) is dynamic, which means for each run this UUID will get change.

Code

Response response = SerenityRest.lastResponse();
        final ValidatableResponse validatableResponse = response.then();
        validatableResponse.assertThat().body("events.*.status", containsString(expectedResponse));

When i run this, i'm getting Unrecognized Exception from serenity BDD. I think, that there is some issue in traversing in JSON.
Can someone please help me on getting the value of status here? So in this case, i'm looking for BOOKING_OPEN

Aishu
  • 1,310
  • 6
  • 28
  • 52
  • Doesn't your `body("events")` return you a list of events? Try to iterate them first and reach `status` for each event after. – Lia Apr 03 '21 at 15:43

2 Answers2

1

I think you should store UUID as a variable, and change your locator from your response.

response.getBody().jsonPath().get("events."+yourUUID+".status");
Joe Phan
  • 51
  • 4
0

Groovy JsonSlurper does not support * breadthFirst() or ** depthFirst() tokens.

You could use below one to get the String result:

response.getBody().jsonPath().get("events.collect{it.value.status}.find()");
// would return "BOOKING_OPEN"

or below one to get a List result:

response.getBody().jsonPath().get("events.collect{it.value.status}");
//would return ["BOOKING_OPEN"]
Xiao
  • 12,235
  • 2
  • 29
  • 36