@Test public void shouldExportTask() throws Exception { long taskId = 12345; StringWriter writer = new StringWriter(); ObjectMapper mapper = new ObjectMapper(); IOUtils.copy(this.getClass().getResourceAsStream("/new-task-version.json"), writer); ObjectNode node = (ObjectNode) mapper.readTree(writer.toString()); node.remove(asList("validationErrors", "type", "_id", "_rev")); when(response.getWriter()).thenReturn(printWriter); when(taskService.exportTask(taskId)).thenReturn(node.toString()); controller.exportTask(taskId, response); ArgumentCaptor<String> headerCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> jsonCaptor = ArgumentCaptor.forClass(String.class); verify(response).setContentType(APPLICATION_JSON_VALUE); verify(response).setCharacterEncoding(UTF_8); verify(response).setHeader(eq("Content-Disposition"), headerCaptor.capture()); verify(printWriter).write(jsonCaptor.capture()); assertEquals( format("attachment; filename=%s.json", encode("Pregnancy SMS", UTF_8)), headerCaptor.getValue() ); assertEquals(node, mapper.readTree(jsonCaptor.getValue())); }
@RequestMapping(value = "/task/{taskId}/export", method = RequestMethod.GET) public void exportTask(@PathVariable Long taskId, HttpServletResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper().enable(INDENT_OUTPUT); String json = taskService.exportTask(taskId); JsonNode node = mapper.readTree(json); String fileName = node.has(JSON_NAME_FIELD) ? node.get(JSON_NAME_FIELD).getTextValue() : "task"; response.setContentType(APPLICATION_JSON_VALUE); response.setCharacterEncoding(UTF_8); response.setHeader( "Content-Disposition", format("attachment; filename=%s.json", encode(fileName, UTF_8)) ); response.getWriter().write(mapper.writeValueAsString(node)); }
TaskController { @RequestMapping(value = "/task/{taskId}/export", method = RequestMethod.GET) public void exportTask(@PathVariable Long taskId, HttpServletResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper().enable(INDENT_OUTPUT); String json = taskService.exportTask(taskId); JsonNode node = mapper.readTree(json); String fileName = node.has(JSON_NAME_FIELD) ? node.get(JSON_NAME_FIELD).getTextValue() : "task"; response.setContentType(APPLICATION_JSON_VALUE); response.setCharacterEncoding(UTF_8); response.setHeader( "Content-Disposition", format("attachment; filename=%s.json", encode(fileName, UTF_8)) ); response.getWriter().write(mapper.writeValueAsString(node)); } }
TaskController { @RequestMapping(value = "/task/{taskId}/export", method = RequestMethod.GET) public void exportTask(@PathVariable Long taskId, HttpServletResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper().enable(INDENT_OUTPUT); String json = taskService.exportTask(taskId); JsonNode node = mapper.readTree(json); String fileName = node.has(JSON_NAME_FIELD) ? node.get(JSON_NAME_FIELD).getTextValue() : "task"; response.setContentType(APPLICATION_JSON_VALUE); response.setCharacterEncoding(UTF_8); response.setHeader( "Content-Disposition", format("attachment; filename=%s.json", encode(fileName, UTF_8)) ); response.getWriter().write(mapper.writeValueAsString(node)); }  }
TaskController { @RequestMapping(value = "/task/{taskId}/export", method = RequestMethod.GET) public void exportTask(@PathVariable Long taskId, HttpServletResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper().enable(INDENT_OUTPUT); String json = taskService.exportTask(taskId); JsonNode node = mapper.readTree(json); String fileName = node.has(JSON_NAME_FIELD) ? node.get(JSON_NAME_FIELD).getTextValue() : "task"; response.setContentType(APPLICATION_JSON_VALUE); response.setCharacterEncoding(UTF_8); response.setHeader( "Content-Disposition", format("attachment; filename=%s.json", encode(fileName, UTF_8)) ); response.getWriter().write(mapper.writeValueAsString(node)); }  @RequestMapping(value = "/task", method = RequestMethod.GET) @ResponseBody List<TaskDto> getAllTasks(); @RequestMapping(value = "/task/import", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) void importTask(@RequestParam(value = "file") MultipartFile file); @RequestMapping(value = "/task/{taskId}", method = RequestMethod.GET) @ResponseBody TaskDto getTask(@PathVariable Long taskId); @RequestMapping(value = "/task/{taskId}", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @ResponseBody Set<TaskErrorDto> saveTask(@RequestBody Task task); @RequestMapping(value = "/task/enable-or-disable/{taskId}", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @ResponseBody void setTaskEnabledOrDisabled(@RequestBody Task task); @RequestMapping(value = "/task/{taskId}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.OK) void deleteTask(@PathVariable Long taskId); @RequestMapping(value = "/task/{taskId}/export", method = RequestMethod.GET) void exportTask(@PathVariable Long taskId, HttpServletResponse response); @RequestMapping(value = "/task/save", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @ResponseBody Set<TaskErrorDto> save(@RequestBody Task task); @ExceptionHandler(ValidationException.class) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody Set<TaskErrorDto> handleException(ValidationException e); @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody String handleException(Exception e); }
TaskController { @RequestMapping(value = "/task/{taskId}/export", method = RequestMethod.GET) public void exportTask(@PathVariable Long taskId, HttpServletResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper().enable(INDENT_OUTPUT); String json = taskService.exportTask(taskId); JsonNode node = mapper.readTree(json); String fileName = node.has(JSON_NAME_FIELD) ? node.get(JSON_NAME_FIELD).getTextValue() : "task"; response.setContentType(APPLICATION_JSON_VALUE); response.setCharacterEncoding(UTF_8); response.setHeader( "Content-Disposition", format("attachment; filename=%s.json", encode(fileName, UTF_8)) ); response.getWriter().write(mapper.writeValueAsString(node)); }  @RequestMapping(value = "/task", method = RequestMethod.GET) @ResponseBody List<TaskDto> getAllTasks(); @RequestMapping(value = "/task/import", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) void importTask(@RequestParam(value = "file") MultipartFile file); @RequestMapping(value = "/task/{taskId}", method = RequestMethod.GET) @ResponseBody TaskDto getTask(@PathVariable Long taskId); @RequestMapping(value = "/task/{taskId}", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @ResponseBody Set<TaskErrorDto> saveTask(@RequestBody Task task); @RequestMapping(value = "/task/enable-or-disable/{taskId}", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @ResponseBody void setTaskEnabledOrDisabled(@RequestBody Task task); @RequestMapping(value = "/task/{taskId}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.OK) void deleteTask(@PathVariable Long taskId); @RequestMapping(value = "/task/{taskId}/export", method = RequestMethod.GET) void exportTask(@PathVariable Long taskId, HttpServletResponse response); @RequestMapping(value = "/task/save", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @ResponseBody Set<TaskErrorDto> save(@RequestBody Task task); @ExceptionHandler(ValidationException.class) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody Set<TaskErrorDto> handleException(ValidationException e); @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody String handleException(Exception e);  }