1

in RestAssured based integration tests I get the following response:

{
  "status": "UP",
  "checks": [
    {
        "name": "boot-errors",
        "status": "UP"
    },
    {
        "name": "server-state",
        "status": "UP",
        "data": {
            "value": "running"
        }
    },
    ...

  ]
}

I need to check weather this response contains "server-state" for example. I did this:

.body("name", everyItem(hasItem("server-state")))

or

.body("checks", everyItem(hasItem("server-state")))

but none has worked, they made both fail the integration test. How can I do that ? Sorry but I don't understand the Hamcrest matcher syntax and I don't find a clear documentation.

Many thanks in advance.

Kind regards,

Seymour

Seymour Glass
  • 81
  • 2
  • 15

2 Answers2

0

I finished up with that crazy statement:

   Response resp = RestAssured.given()
    .accept(MediaType.APPLICATION_JSON)
    .when()
    .get(UriBuilder.fromPath("...")
    .scheme("http")
    .host(wildfly.getHost())
    .port(wildfly.getMappedPort(...))
    .build());
   Map<Object, Object> jsonResponse = resp.jsonPath().getMap("$");
   ArrayList<Object> checks = (ArrayList) jsonResponse.get("checks");
   assertThat(checks, hasItem(Map.of("name", "server-state", "status", "UP")));

It works but it's ugly. Does anyone know how to simplify it ?

Seymour Glass
  • 81
  • 2
  • 15
0

I found a better way:

  RestAssured.given()
  .accept(MediaType.APPLICATION_JSON)
  .when()
  .get(UriBuilder.fromPath("...")
    .scheme("http")
    .host(wildfly.getHost())
    .port(wildfly.getMappedPort(...))
    .build())
  .then()
  .assertThat().body("checks.name", anyOf(hasItem("server-state")));
Seymour Glass
  • 81
  • 2
  • 15