Search code examples
jsonrest-assuredmatcher

How can I validate that JSON Array contains a particular value in Rest Assured?


I am running into a problem where I wish to validate contents of an JSON array.Below is the response :

[
  {
    "airportId": "5d5448a4-f7d7-461d-8ec0-7881719cc013",
    "code": "HNL",
    "name": "Honolulu, Oahu",
    "location": {
      "longitude": -157.925,
      "latitude": 21.321667
    },
    "cityId": "7b244a1b-b2fb-4e9b-a611-3f3107f9adfc",
    "city": "Honolulu",
    "countryCode": "US",
    "themes": [
      "BEACH",
      "HISTORIC",
      "OUTDOORS",
      "ROMANTIC",
      "SHOPPING"
    ],
    "pointsOfSale": [
      "US"
    ]
  },
  {
    "airportId": "896d2041-5083-4d5d-b11b-575d388e8e6f",
    "code": "NRI",
    "name": "Shangri-la Airport",
    "location": {
      "longitude": -157.794932,
      "latitude": 21.256836
    },
    "cityId": "7b244a1b-b2fb-4e9b-a611-3f3107f9adfc",
    "city": "Honolulu",
    "countryCode": "US",
    "themes": [],
    "pointsOfSale": [
      "US"
    ]
  },
  {
    "airportId": "0bf1e80e-bf08-4a62-9f18-41328cc62fc4",
    "code": "HIK",
    "name": "Hickam AFB Airport",
    "location": {
      "longitude": -157.8583333,
      "latitude": 21.3069444
    },
    "cityId": "7b244a1b-b2fb-4e9b-a611-3f3107f9adfc",
    "city": "Honolulu",
    "countryCode": "US",
    "themes": [],
    "pointsOfSale": [
      "US"
    ]
  },
  {
    "airportId": "aed7c2eb-54ce-47c2-966f-9e2079506b9c",
    "code": "JRF",
    "name": "Kalaeloa (John Rodgers Field) Airport",
    "location": {
      "longitude": -158.0568965,
      "latitude": 21.3353909
    },
    "cityId": "438dd674-e443-466c-a62e-ce6a9c80cf86",
    "city": "Kapolei",
    "countryCode": "US",
    "themes": [],
    "pointsOfSale": [
      "US"
    ]
  }
]

I want to validate that Honolulu, Oahu is present in the name node as a part of the response. I was able to do it with JsonPath class and storing all the name results in a List. However, I wish to do it via body and matchers.

Below is the the code for hitting the endpoint :

public void getAirport() {
        RestAssured.baseURI="https://hotels4.p.rapidapi.com";

        Response res = given()
        .headers("X-RapidAPI-Host", "cometari-airportsfinder-v1.p.rapidapi.com", 
                "X-RapidAPI-Key", "KEY_GOES_HERE").
        queryParams("radius", "20","lng","-157.895277","lat","21.265600").
        when()
        .get("/api/airports/by-radius").
        then()
        .assertThat().statusCode(200)
        .and().header("Server", "RapidAPI-1.0.39")
        .and().body("name",Matchers.hasItems("Honolulu"))
        .and().time(Matchers.lessThan(10000l))
        .extract().response();

    }

Approach 1: Matchers.hasItems

.body("name",Matchers.hasItems("Honolulu"))

Error message:

JSON path name doesn't match.
Expected: (a collection containing "Honolulu")
  Actual: [Honolulu, Oahu, Shangri-la Airport, Hickam AFB Airport, Kalaeloa (John Rodgers Field) Airport]

Approach 2 : Matchers.contains

.body("name",Matchers.contains("Honolulu"))

Error Message:

JSON path name doesn't match.
Expected: iterable containing ["Honolulu"]
  Actual: [Honolulu, Oahu, Shangri-la Airport, Hickam AFB Airport, Kalaeloa (John Rodgers Field) Airport]

Approach 3 : Matchers.hasKey

.body("name",Matchers.hasKey("Honolulu"))

Error Message :

JSON path name doesn't match.
Expected: map containing ["Honolulu"->ANYTHING]
  Actual: [Honolulu, Oahu, Shangri-la Airport, Hickam AFB Airport, Kalaeloa (John Rodgers Field) Airport]

The above approaches works fine when I replace name with name[0]but I don't want to perform validation using Indexes.


Solution

  • Name Collection contains "Honolulu, Oahu" as a single value.

    Matchers.hasItems("Honolulu, Oahu")) should be working