@Test public void shouldGenerateReportInRequestedFormat() throws Exception { Template template = new Template(); String format = "pdf"; Map<String, Object> parameterMap = new HashMap<>(); JasperReportsMultiFormatView mockView = mock(JasperReportsMultiFormatView.class); when(templateService.getById(1L)).thenReturn(template); when(viewFactory.getJasperReportsView(template)).thenReturn(mockView); when(templateService.getParametersMap(template, 1, request, format)).thenReturn(parameterMap); ModelAndView modelAndView = controller.generateReport(request, 1L, format); assertThat((JasperReportsMultiFormatView) modelAndView.getView(), is(mockView)); assertThat(modelAndView.getModel(), is(parameterMap)); verify(viewFactory).getJasperReportsView(template); verify(templateService).getById(1L); verify(templateService).getParametersMap(template, 1, request, format); }
@RequestMapping(method = GET, value = "/reports/{id}/{format}") public ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") Long id, @PathVariable("format") String format) { JasperReportsMultiFormatView jasperView; Map<String, Object> map; try { int userId = loggedInUserId(request).intValue(); Template template = templateService.getById(id); jasperView = jasperReportsViewFactory.getJasperReportsView(template); map = templateService.getParametersMap(template, userId, request, format); } catch (Exception e) { return new ModelAndView("error-page"); } return new ModelAndView(jasperView, map); }
ReportController { @RequestMapping(method = GET, value = "/reports/{id}/{format}") public ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") Long id, @PathVariable("format") String format) { JasperReportsMultiFormatView jasperView; Map<String, Object> map; try { int userId = loggedInUserId(request).intValue(); Template template = templateService.getById(id); jasperView = jasperReportsViewFactory.getJasperReportsView(template); map = templateService.getParametersMap(template, userId, request, format); } catch (Exception e) { return new ModelAndView("error-page"); } return new ModelAndView(jasperView, map); } }
ReportController { @RequestMapping(method = GET, value = "/reports/{id}/{format}") public ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") Long id, @PathVariable("format") String format) { JasperReportsMultiFormatView jasperView; Map<String, Object> map; try { int userId = loggedInUserId(request).intValue(); Template template = templateService.getById(id); jasperView = jasperReportsViewFactory.getJasperReportsView(template); map = templateService.getParametersMap(template, userId, request, format); } catch (Exception e) { return new ModelAndView("error-page"); } return new ModelAndView(jasperView, map); }  }
ReportController { @RequestMapping(method = GET, value = "/reports/{id}/{format}") public ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") Long id, @PathVariable("format") String format) { JasperReportsMultiFormatView jasperView; Map<String, Object> map; try { int userId = loggedInUserId(request).intValue(); Template template = templateService.getById(id); jasperView = jasperReportsViewFactory.getJasperReportsView(template); map = templateService.getParametersMap(template, userId, request, format); } catch (Exception e) { return new ModelAndView("error-page"); } return new ModelAndView(jasperView, map); }  @RequestMapping(method = GET, value = "/reports/{id}") Template getReportWithParameters(@PathVariable("id") Long id); @RequestMapping(method = GET, value = "/reports/{id}/{format}") ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") Long id,
                                     @PathVariable("format") String format); }
ReportController { @RequestMapping(method = GET, value = "/reports/{id}/{format}") public ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") Long id, @PathVariable("format") String format) { JasperReportsMultiFormatView jasperView; Map<String, Object> map; try { int userId = loggedInUserId(request).intValue(); Template template = templateService.getById(id); jasperView = jasperReportsViewFactory.getJasperReportsView(template); map = templateService.getParametersMap(template, userId, request, format); } catch (Exception e) { return new ModelAndView("error-page"); } return new ModelAndView(jasperView, map); }  @RequestMapping(method = GET, value = "/reports/{id}") Template getReportWithParameters(@PathVariable("id") Long id); @RequestMapping(method = GET, value = "/reports/{id}/{format}") ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") Long id,
                                     @PathVariable("format") String format); static final String USER_ID; }