6

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.

Ludov Dmitrii
  • 425
  • 5
  • 9

1 Answers1

0

I've made my own matcher:


import net.minidev.json.JSONArray;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import java.time.LocalDateTime;

class CustomMatcher extends BaseMatcher<LocalDateTime> {

    private LocalDateTime from;
    private LocalDateTime to;
    private int misMatchAtIndex;

    CustomMatcher(LocalDateTime from, LocalDateTime to) {
        this.from = from;
        this.to = to;
    }

    @Override
    public boolean matches(Object item) {
        JSONArray rawData = (JSONArray) item;
        for (Object raw : rawData) {
            LocalDateTime parsed = LocalDateTime.parse(raw.toString());
            if (!parsed.isBefore(to) || !parsed.isAfter(from)) {
                misMatchAtIndex = rawData.indexOf(raw);
                return false;
            }
        }
        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(String.format("All DateTime fields from %s to %s, mismatch at index %d",
                from, to, misMatchAtIndex));
    }
}

use:

 .andExpect(jsonPath("data." + SENT + "[*].sentAt", new CustomMatcher(fromDate, toDate)))
Ludov Dmitrii
  • 425
  • 5
  • 9