How to implement JSON Path with conditional value in RestAssured(instead of array index using a value)
Here in the below example Cucumber file, I am trying to match the response fields with array index using a value.
For ex, in the first run group.showGroupItems[3].showId will be 183040. In the next run, group.showGroupItems[2].showId value will be 183040. Here the value ‘183040’ will have different array index in every run.
Similarly, in the first run group.showGroupItems[0].title will be Clearwise. In the next run, group.showGroupItems[2].title value will be Clearwise. Here the value will ‘Clearwise’ will have different array index in every run.
Similarly, in the first run profileLevels[2].profileType will be ‘ADULT’. In the next run, profileLevels[4].profileType value will be ‘ADULT’. Here the value will ‘ADULT’ will have different array index in every run.
I want my code to have JSON Path conditional value and want to be dynamic. I want in the below way with a condition , how can I change the logic for the method verifyResponseBody() in UtilFile using Java? How can I achieve it JSON Path with the below conditional values?
group.showGroupItems[i].showId==183040
group.showGroupItems[i].title==Clearwise
profileLevels[i].profileType==ADULT
UtilFile:
public void verifyResponseBody (Map < String, String > data) throws JsonPathException
{
SoftAssert softAssert = new SoftAssert();
for (String field : data.keySet()) {
softAssert.assertEquals(response.getBody().jsonPath().getString(field),
data.get(field));
}
softAssert.assertAll();
}
Cucumber file:
And I see response matches for fields
| group.showGroupItems[3].showId | 180340 |
| group.showGroupItems[0].title | Clearwise |
| profileLevels[2].profileType | ADULT |
Common Stepdefintion File:
@Then("I see response matches for fields")
public void i_see_response_matches_for_fields(DataTable data) {
apiUtil.verifyResponseBody(data.asMap());
}