0

Json body:

{
  "result": [
    {
      "object": {
        "type": "mattress"
      }
    },
    {
      "object": {
        "type": "pillow"
      }
    }
  ]
}

How do I assert that type is only either pillow or a mattress (there can be more so I am looking for a generic solution) using rest-assured body and hamcrest assertions?

Example assertion:

response.then().assertThat().body("result", hasSize(greaterThan(0)));
azro
  • 53,056
  • 7
  • 34
  • 70
Marij Khan
  • 151
  • 1
  • 12
  • See JSON SCHEMA that si a JSON validation format https://stackoverflow.com/questions/32038543/how-to-validate-a-json-object-in-java – azro Dec 27 '21 at 11:49

1 Answers1

1

This code would solve your problem:

.body("result.object.type",  everyItem(isOneOf("mattress", "pillow")));
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • is there some way to do it with list? as I said there can be more types so I have created a list and the type can be any one item of the list. This does not work for some reason: response.then().assertThat().body("result.object.type", everyItem(is(oneOf(types)))) // where types is a list of strings – Marij Khan Dec 27 '21 at 13:28
  • I converted the list to array and it worked. Thanks. One sidenote isOneOf is marked as deprecated and is(oneOf()) should be used. – Marij Khan Dec 27 '21 at 13:32
  • @MarijKhan Thanks for your note. I've tested with version 1.3 – lucas-nguyen-17 Dec 27 '21 at 13:33