@Test public void completeTask_noAvailableOutcome_taskShouldBeCompleted() throws Exception { Long taskId = 500L; String ipAddress = "ipAddress"; AcmTask request = new AcmTask(); request.setTaskId(taskId); Capture<AcmTask> toSave = new Capture<>(); AcmTask found = new AcmTask(); found.setTaskId(taskId); AcmTask saved = new AcmTask(); saved.setTaskId(taskId); AcmTask completed = new AcmTask(); completed.setTaskId(taskId); Capture<AcmApplicationTaskEvent> capturedEvent = new Capture<>(); mockHttpSession.setAttribute("acm_ip_address", ipAddress); expect(mockTaskDao.findById(taskId)).andReturn(found); expect(mockTaskDao.save(capture(toSave))).andReturn(saved); expect(mockTaskDao.completeTask(mockAuthentication, taskId, null, null)).andReturn(completed); mockTaskEventPublisher.publishTaskEvent(capture(capturedEvent)); expect(mockAuthentication.getName()).andReturn("user").atLeastOnce(); ObjectMapper om = new ObjectMapper(); String requestJson = om.writeValueAsString(request); replayAll(); MvcResult result = mockMvc.perform( post("/api/v1/plugin/task/completeTask") .accept(MediaType.parseMediaType("application/json;charset=UTF-8")) .content(requestJson) .contentType(MediaType.APPLICATION_JSON) .session(mockHttpSession) .principal(mockAuthentication)) .andReturn(); verifyAll(); assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); assertTrue(result.getResponse().getContentType().startsWith(MediaType.APPLICATION_JSON_VALUE)); String returned = result.getResponse().getContentAsString(); log.info("results: " + returned); ObjectMapper objectMapper = new ObjectMapper(); AcmTask completedTask = objectMapper.readValue(returned, AcmTask.class); assertNotNull(completedTask); assertEquals(taskId, completedTask.getTaskId()); AcmApplicationTaskEvent event = capturedEvent.getValue(); assertEquals(taskId, event.getObjectId()); assertEquals("TASK", event.getObjectType()); assertTrue(event.isSucceeded()); AcmTask capturedTaskToSave = toSave.getValue(); assertEquals(taskId, capturedTaskToSave.getTaskId()); }
@RequestMapping(value = "/completeTask", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask completeTask(Authentication authentication, @RequestBody AcmTask in, HttpSession httpSession) throws AcmUserActionFailedException { log.info("Completing task '{}'", in.getTaskId()); try { AcmTask current = getTaskDao().findById(in.getTaskId()); validateCompleteTaskRequirements(current, in); getTaskDao().save(in); AcmTask completed = getTaskDao().completeTask(authentication, in.getTaskId(), in.getOutcomeName(), in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); completed.setTaskOutcome(in.getTaskOutcome()); publishTaskCompletedEvent(authentication, httpSession, completed, true, in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); return completed; } catch (AcmTaskException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw new AcmUserActionFailedException("complete", "task", in.getTaskId(), e.getMessage(), e); } catch (AcmUserActionFailedException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw e; } }
CompleteTaskWithOutcomeAPIController { @RequestMapping(value = "/completeTask", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask completeTask(Authentication authentication, @RequestBody AcmTask in, HttpSession httpSession) throws AcmUserActionFailedException { log.info("Completing task '{}'", in.getTaskId()); try { AcmTask current = getTaskDao().findById(in.getTaskId()); validateCompleteTaskRequirements(current, in); getTaskDao().save(in); AcmTask completed = getTaskDao().completeTask(authentication, in.getTaskId(), in.getOutcomeName(), in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); completed.setTaskOutcome(in.getTaskOutcome()); publishTaskCompletedEvent(authentication, httpSession, completed, true, in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); return completed; } catch (AcmTaskException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw new AcmUserActionFailedException("complete", "task", in.getTaskId(), e.getMessage(), e); } catch (AcmUserActionFailedException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw e; } } }
CompleteTaskWithOutcomeAPIController { @RequestMapping(value = "/completeTask", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask completeTask(Authentication authentication, @RequestBody AcmTask in, HttpSession httpSession) throws AcmUserActionFailedException { log.info("Completing task '{}'", in.getTaskId()); try { AcmTask current = getTaskDao().findById(in.getTaskId()); validateCompleteTaskRequirements(current, in); getTaskDao().save(in); AcmTask completed = getTaskDao().completeTask(authentication, in.getTaskId(), in.getOutcomeName(), in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); completed.setTaskOutcome(in.getTaskOutcome()); publishTaskCompletedEvent(authentication, httpSession, completed, true, in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); return completed; } catch (AcmTaskException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw new AcmUserActionFailedException("complete", "task", in.getTaskId(), e.getMessage(), e); } catch (AcmUserActionFailedException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw e; } }  }
CompleteTaskWithOutcomeAPIController { @RequestMapping(value = "/completeTask", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask completeTask(Authentication authentication, @RequestBody AcmTask in, HttpSession httpSession) throws AcmUserActionFailedException { log.info("Completing task '{}'", in.getTaskId()); try { AcmTask current = getTaskDao().findById(in.getTaskId()); validateCompleteTaskRequirements(current, in); getTaskDao().save(in); AcmTask completed = getTaskDao().completeTask(authentication, in.getTaskId(), in.getOutcomeName(), in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); completed.setTaskOutcome(in.getTaskOutcome()); publishTaskCompletedEvent(authentication, httpSession, completed, true, in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); return completed; } catch (AcmTaskException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw new AcmUserActionFailedException("complete", "task", in.getTaskId(), e.getMessage(), e); } catch (AcmUserActionFailedException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw e; } }  @RequestMapping(value = "/completeTask", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody AcmTask completeTask(Authentication authentication, @RequestBody AcmTask in, HttpSession httpSession); TaskDao getTaskDao(); void setTaskDao(TaskDao taskDao); TaskEventPublisher getTaskEventPublisher(); void setTaskEventPublisher(TaskEventPublisher taskEventPublisher); }
CompleteTaskWithOutcomeAPIController { @RequestMapping(value = "/completeTask", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask completeTask(Authentication authentication, @RequestBody AcmTask in, HttpSession httpSession) throws AcmUserActionFailedException { log.info("Completing task '{}'", in.getTaskId()); try { AcmTask current = getTaskDao().findById(in.getTaskId()); validateCompleteTaskRequirements(current, in); getTaskDao().save(in); AcmTask completed = getTaskDao().completeTask(authentication, in.getTaskId(), in.getOutcomeName(), in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); completed.setTaskOutcome(in.getTaskOutcome()); publishTaskCompletedEvent(authentication, httpSession, completed, true, in.getTaskOutcome() == null ? null : in.getTaskOutcome().getName()); return completed; } catch (AcmTaskException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw new AcmUserActionFailedException("complete", "task", in.getTaskId(), e.getMessage(), e); } catch (AcmUserActionFailedException e) { publishTaskCompletedEvent(authentication, httpSession, in, false); throw e; } }  @RequestMapping(value = "/completeTask", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody AcmTask completeTask(Authentication authentication, @RequestBody AcmTask in, HttpSession httpSession); TaskDao getTaskDao(); void setTaskDao(TaskDao taskDao); TaskEventPublisher getTaskEventPublisher(); void setTaskEventPublisher(TaskEventPublisher taskEventPublisher);  }