Search code examples
javarest-assuredweb-api-testingrest-assured-jsonpath

How to calculate the sum of a Respond (Json) using response.path?


Seem to be struggling to calculate the number of squad members who have the role type of 'PLAYER' by performing my restassured logic against my Json response.

Currently I have a simple setup which hits an endpoint and then executes my query, I can see that the endpoint and response is valid however it seems there is a problem when attempting to filter via my query in order to calculate the total sum of squad members who have a role of 'PLAYER'.

My Rest-assured code:

@Test
public void locatePlayerCalculateSum() {
    Response response = given()
            .spec(footballCompetitions_requestSpecification)
            .when().get(EndPoint.TEAMS + EndPoint.SQUAD);
    int sum = response.path("squad.collect { it.role == \"PLAYER\" }.sum()");
    System.out.println(sum);
}

Exception Message: java.lang.IllegalArgumentException: No signature of method: java.lang.Boolean.plus() is applicable for argument types: (java.lang.Boolean) values: [true] Possible solutions: is(java.lang.Object), or(java.lang.Boolean), implies(java.lang.Boolean), and(java.lang.Boolean), use([Ljava.lang.Object;), split(groovy.lang.Closure)

Example JSON response:

  "id": 66,
  "area": {
      "id": 2072,
      "name": "England"
  },
  "activeCompetitions": [
      {
          "id": 2021,
          "area": {
              "id": 2072,
              "name": "England"
          },
          "name": "Premier League",
          "code": "PL",
          "plan": "TIER_ONE",
          "lastUpdated": "2019-01-03T23:39:45Z"
      },
      {
          "id": 2001,
          "area": {
              "id": 2077,
              "name": "Europe"
          },
          "name": "UEFA Champions League",
          "code": "CL",
          "plan": "TIER_ONE",
          "lastUpdated": "2018-12-13T18:55:02Z"
      }
  ],
  "name": "Manchester United FC",
  "shortName": "Man United",
  "tla": "MNU",
  "crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
  "address": "Sir Matt Busby Way Manchester M16 0RA",
  "phone": "+44 (0161) 8688000",
  "website": "http://www.manutd.com",
  "email": "enquiries@manutd.co.uk",
  "founded": 1878,
  "clubColors": "Red / White",
  "venue": "Old Trafford",
  "squad": [
      {
          "id": 3188,
          "name": "David De Gea",
          "position": "Goalkeeper",
          "dateOfBirth": "1990-11-07T00:00:00Z",
          "countryOfBirth": "Spain",
          "nationality": "Spain",
          "shirtNumber": 1,
          "role": "PLAYER"
      },
      {
          "id": 3202,
          "name": "Sergio Romero",
          "position": "Goalkeeper",
          "dateOfBirth": "1987-02-22T00:00:00Z",
          "countryOfBirth": "Argentina",
          "nationality": "Argentina",
          "shirtNumber": null,
          "role": "PLAYER"
      },
      {
          "id": 7942,
          "name": "Lee Grant",
          "position": "Goalkeeper",
          "dateOfBirth": "1983-01-27T00:00:00Z",
          "countryOfBirth": "England",
          "nationality": "England",
          "shirtNumber": 13,
          "role": "PLAYER"
      },
      {
          "id": 3206,
          "name": "Marcos Rojo",
          "position": "Defender",
          "dateOfBirth": "1990-03-20T00:00:00Z",
          "countryOfBirth": "Argentina",
          "nationality": "Argentina",
          "shirtNumber": 16,
          "role": "PLAYER"
      },```



Solution

  • This should work:

    @Test
    public void locatePlayerCalculateSum() {
        Response response = given()
                .spec(footballCompetitions_requestSpecification)
                .when().get(EndPoint.TEAMS + EndPoint.SQUAD);
        int sum = response.path("squad.count { it.role == 'PLAYER' }");
        System.out.println(sum);
    }