lundi 9 septembre 2019

Spring Boot MockMvc validate JSON array objects

I am looking for a good way to test array items against each other. I did write my own Matchers but I wonder if there is a predefined way of doing that.

Here's an example:

{
  "response-id": "12de556e2a76bb",
  "overallsize": 32,
  "data": [
      {
        "id": 32345,
        "timestamp": "2019-12-09'T'12:54:21.123"
      },
      {
        "id": 73145,
        "timestamp": "2018-11-23'T'19:12:11.321"
      }
  ]
}

Actually for example I want to check that each id of all data elements do increase; next one is higher and not equal than the one before. Also I want to check each timestamp is less than the one before; which can be done by String comparison in this case.

mockMvc.perform( post("/datas" ) )
    .andExpect( status().isOk() )
    .andExpect( jsonValue("$.data..id").value( Matchers??? ) )
    .andExpect( jsonValue("$.data..timestamp").value( Matchers??? ) );

How to do that the most elegant way? I did write my own Matchers actually running a BaseMatcher, but I am not definitely not first one trying to solve that.

Aucun commentaire:

Enregistrer un commentaire