2

Could someone please me understand how to validate a list of items from the response. Say the response looks something like below,

{  
   "store":{  
      "book":[  
         {  
            "author":"Nigel Rees",
            "category":"reference",
            "price":8.95,
            "title":"Sayings of the Century"
         },
         {  
            "author":"Evelyn Waugh",
            "category":"fiction",
            "price":12.99,
            "title":"Sword of Honour"
         },
         {  
            "author":"Herman Melville",
            "category":"fiction",
            "isbn":"0-553-21311-3",
            "price":8.99,
            "title":"Moby Dick"
         },
         {  
            "author":"J. R. R. Tolkien",
            "category":"fiction",
            "isbn":"0-395-19395-8",
            "price":22.99,
            "title":"The Lord of the Rings"
         }
      ]
   }
}

The element Book has four lists under it with different data, now if I want to validate the author name and the price sequentially (in a loop for instance) how can I achieve that..?

I usually convert the response into a Json document and then validate, but in this case, if I use the Json path "Store.book.author", out of the four lists from the response, which list would it refer to..? That's where my confusion is.

Qaddaffi
  • 77
  • 1
  • 2
  • 9
  • These are not four lists, these are four objects in an array. `store.book[0].author` refers to the author of the first item, `store.book[1].author` refers to the author of the second item and so on. – Ben Kauer Apr 19 '18 at 11:11
  • Thank you for the answer. But in case if the response contains only the array of objects (not under store or book), how should I access it..? ex, `{ "author":"Nigel Rees", "category":"reference", "price":8.95, "title":"Sayings of the Century" }, { "author":"Evelyn Waugh", "category":"fiction", "price":12.99, "title":"Sword of Honour" }` – Qaddaffi Apr 19 '18 at 11:49
  • Got it. Used `[0].author` and so on to access it. – Qaddaffi Apr 19 '18 at 11:58

3 Answers3

1

There is in-build method in rest assured which you can use to get all item of Array as List of map.

String key="book";//array key (as it mentioned in your Json)
Response response=//your API call which will return Json Object
List<Hash<String,Object>>booksList=response.jsonPath().getList(key);
//Now parse value from List
Hash<String,Object> firstBookDetails=booksList.get(0);// for first index
String author=(String)firstBookDetails.get("author");
1

When using Rest Assured with BDD, you can try this out.

given()
.when()
    .get(API URL)
.then()
     .assertThat().body("store.book.author[0]", equalTo("Nigel Rees"));
0

My answer below is based on Cucumber (feature file) and Rest Assured implementation.

Your feature file should have below step

And the json response field "books" contains 4 records with below properties:
 | author   | category | price | title|
 |Nigel Rees| reference|8.95|Sayings of the Century|
 |Evelyn Waugh| fiction|12.99|Sword of Honour|
 |Herman Melville| fiction|8.99|Moby Dick|
 |J. R. R. Tolkien| fiction|22.99|The Lord of the Rings|

And its implementation in rest assured would be like below

@Then("the json response field {string} contains {int} records with below properties:")
  public void responseBodyMatchesListResponse(final String path,
      final int count, final List<Map<String, String>> expectedItems) {

    var actualItems = response.jsonPath().getList(path, Map.class);

    assertThat(actualItems).as("Response body element at %s dont match the expected size %d",path,count)
        .hasSize(count)
        .as("Response body element at %s dont match the expected elements %s",path,expectedItems)
        .containsExactlyInAnyOrderElementsOf(expectedItems);
  }

And this works perfectly. Feature file with data table makes the test readable.

Sanjay Bharwani
  • 3,317
  • 34
  • 31