lundi 8 avril 2019

Is there any proper matcher to parse and compare LocalDateTime field in Json response from MockMvc

I'm testing get method of my SpringBoot controller, which is providing objects, written in base in particular time range.

I realize that I can get json after mockMvc performing and parse it with object mapper, use some streams and one assert, but I wonder if there built-in way to make it with andExpect() sequence.

I've tried Hamcrest Date Matchers, but it can't parse LocalDateTime format java.lang.AssertionError: JSON path "data.SENT[0].sentAt" Expected: the date is within 10 days of "08 апр 2019 19:03:48 614ms +0300" but: was "2019-04-02T11:36:16.51"

this.mockMvc.perform(get(BASE_URL)
                .accept(MediaType.APPLICATION_JSON_VALUE)
                .contentType(MediaType.APPLICATION_JSON)
                .content(jsonMockObjectMapper.writeValueAsString(smsStatisticFullRequest)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("data.SENT[*].sentAt", Matchers.hasItems("2019-04-02T11:36:16.51")))
//              .andExpect(jsonPath("data.SENT[0].sentAt", DateMatchers.within(10, TimeUnit.DAYS, Timestamp.valueOf(LocalDateTime.now()))))
//              .andExpect(jsonPath("data.SENT[0].sentAt", DateMatchers.before(Timestamp.valueOf(LocalDateTime.now()))))
                .andDo(CustomResultHandler.handleResult(name.getMethodName(), MockMvcRestDocumentation::document));

I'm expecting to be able to check all objects in return data to be in assert time range.

Content-Type: application/json;charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate

{
  "apiVersion" : "1.0.1",
  "error" : false,
  "data" : {
    "SENT" : [ {
      "id" : 3,
      "phone" : "9111233456",
      "userId" : 683581,
      "sentAt" : "2019-04-02T11:36:16.51",
      "operation" : "RECOVERY_PASSWORD",
      "smsCode" : "2112"
    } ],


I can check if there is some concrete object. But I can't be sure there is no records aboard time period in my return data.

Aucun commentaire:

Enregistrer un commentaire