EmailController { @RequestMapping(value = "/emails", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody public EmailRecords<? extends BasicEmailRecordDto> getEmails(GridSettings filter, HttpServletRequest request) { EmailRecordSearchCriteria criteria = prepareCriteria(filter); List<EmailRecord> filtered = auditService.findEmailRecords(criteria); List<? extends BasicEmailRecordDto> rows = hideColumns(filtered, filter); long total = auditService.countEmailRecords(criteria); if (filter.getRows() == null) { int defaultRowsAmount = 10; filter.setRows(defaultRowsAmount); } if (filter.getPage() == null) { int defaultPage = 1; filter.setPage(defaultPage); } int totalPages = (int) Math.ceil((double) total / filter.getRows()); String username = getUsername(request); if (username != null) { lastFilter.put(username, filter); } return new EmailRecords<>((int) total, filter.getPage(), totalPages, rows); }  @RequestMapping(value = "/emails", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody EmailRecords<? extends BasicEmailRecordDto> getEmails(GridSettings filter, HttpServletRequest request); @RequestMapping(value = "/emails/{mailid}", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody EmailRecords<EmailRecordDto> getEmail(@PathVariable int mailid); @RequestMapping(value = "/emails/months/", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody List<String> getAvailableMonths(); @RequestMapping(value = "/emails/export", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) void exportEmailLog(@RequestParam("range") String range,
                               @RequestParam(value = "month", required = false) String month,
                               HttpServletResponse response,
                               HttpServletRequest request); @RequestMapping(value = "/emails/available/", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody List<String> getAvailableMails(@RequestParam("autoComplete") String autoComplete,
                                          @RequestParam("term") String partialAddress);  }
@Test public void shouldReturnEmailRecords() { GridSettings filter = new GridSettings(); filter.setDeliveryStatus("SENT,ERROR"); filter.setPage(1); filter.setRows(10); when(auditService.countEmailRecords(any(EmailRecordSearchCriteria.class))).thenReturn(54L); EmailRecords<? extends BasicEmailRecordDto> records = emailController.getEmails(filter, request); assertEquals(Integer.valueOf(1), records.getPage()); assertEquals(Integer.valueOf(54), records.getRecords()); assertEquals(Integer.valueOf(6), records.getTotal()); ArgumentCaptor<EmailRecordSearchCriteria> captor = ArgumentCaptor.forClass(EmailRecordSearchCriteria.class); verify(auditService).findEmailRecords(captor.capture()); assertEquals(filter.getPage(), captor.getValue().getQueryParams().getPage()); assertEquals(filter.getRows(), captor.getValue().getQueryParams().getPageSize()); }
@Test public void shouldReturnAllEmailRecordsWhenNoFiltersSet() { when(auditService.countEmailRecords(any(EmailRecordSearchCriteria.class))).thenReturn(88L); EmailRecords<? extends BasicEmailRecordDto> records = emailController.getEmails(new GridSettings(), request); assertEquals(Integer.valueOf(1), records.getPage()); assertEquals(Integer.valueOf(88), records.getRecords()); assertEquals(Integer.valueOf(9), records.getTotal()); }