Search code examples
javaarrayscucumberrest-assuredjsonpath

How to implement JSON Path with conditional value in RestAssured(instead of array index using a value)


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());
}

JSON data

{
  "group":{
  "id":608,
  "platformType":"all",
  "showGroupType":"default",
  "title":"All Shows",
  "showGroupItems":[
     {
        "showId":19923,
        "filepathShowGroupItemLogo":null,
        "title":"Clearwise",
        "showGroupId":705
     },
     {
        "showId":18454,
        "filepathShowGroupItemLogo":null,
        "title":"Roll It",
        "showGroupId":705
     },
     {
        "showId":183040,
        "filepathShowGroupItemLogo":null,
        "title":"The Dog",
        "showGroupId":705
      }
    ]
  },
  "profileLevels":[
   {
     "title":"Adult",
     "profileType":"ADULT"
   },
   {
     "title":"Older Kids",
     "profileType":"KIDS"
   },
   {
     "title":"Younger Kids",
     "profileType":"YOUNGER_KIDS"
   }
  ]
}

Solution

  • I think you can achieve that with some changes in your code:

    • Cucumber file: No asterisk *
    | group.id                    | 608       |
    | group.platformType2         | all       |
    | group.showGroupItems.showId | 180340    |
    | group.showGroupItems.title  | Clearwise |
    | profileLevels.profileType   | ADULT     |
    
    • Utils file:
    public void verifyResponseBody(Map<String, Object> data) {
        SoftAssert softAssert = new SoftAssert();
        for (String field : data.keySet()) {
            try {
                List<?> list = response.getBody().jsonPath().getList(field);
                Object value = data.get(field);
                softAssert.assertTrue(list.contains(value),
                        String.format("%s doesn't contain '%s'", list, value));
            } catch (ClassCastException e) {
                softAssert.assertEquals(response.getBody().jsonPath().get(field), data.get(field));
            } catch (NullPointerException e) {
                System.out.printf("Key %s is wrong \n", field);
            }
    
        }
        softAssert.assertAll();
    }