@Test public void claimTask_exception() throws Exception { Long taskId = 500L; String userId = "user"; String ipAddress = "ipAddress"; AcmTask found = new AcmTask(); found.setTaskId(taskId); Capture<AcmApplicationTaskEvent> capturedEvent = new Capture<>(); mockHttpSession.setAttribute("acm_ip_address", ipAddress); mockTaskDao.claimTask(eq(taskId), eq(userId)); expectLastCall().andThrow(new AcmTaskException("testException")); mockTaskEventPublisher.publishTaskEvent(capture(capturedEvent)); expect(mockAuthentication.getName()).andReturn(userId).atLeastOnce(); replayAll(); mockMvc.perform( post("/api/v1/plugin/task/claim/{taskId}", taskId) .accept(MediaType.parseMediaType("application/json;charset=UTF-8")) .session(mockHttpSession) .principal(mockAuthentication)) .andExpect(status().isBadRequest()) .andExpect(content().contentType(MediaType.TEXT_PLAIN)); verifyAll(); AcmApplicationTaskEvent event = capturedEvent.getValue(); assertEquals(taskId, event.getObjectId()); assertEquals("TASK", event.getObjectType()); assertFalse(event.isSucceeded()); }
@RequestMapping(value = "/claim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask claimTask( @PathVariable("taskId") Long taskId, Authentication authentication, HttpSession httpSession) throws AcmUserActionFailedException { log.info("User [{}] is claiming workflow task with ID [{}]", authentication.getName(), taskId); try { getTaskDao().claimTask(taskId, authentication.getName()); AcmTask claimedTask = getTaskDao().findById(taskId); publishTaskClaimEvent(authentication, httpSession, claimedTask, "claim", true); return getTaskDao().save(claimedTask); } catch (AcmTaskException e) { AcmTask fakeTask = new AcmTask(); fakeTask.setTaskId(taskId); publishTaskClaimEvent(authentication, httpSession, fakeTask, "claim", false); throw new AcmUserActionFailedException("claim", "task", taskId, e.getMessage(), e); } }
ClaimTaskAPIController { @RequestMapping(value = "/claim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask claimTask( @PathVariable("taskId") Long taskId, Authentication authentication, HttpSession httpSession) throws AcmUserActionFailedException { log.info("User [{}] is claiming workflow task with ID [{}]", authentication.getName(), taskId); try { getTaskDao().claimTask(taskId, authentication.getName()); AcmTask claimedTask = getTaskDao().findById(taskId); publishTaskClaimEvent(authentication, httpSession, claimedTask, "claim", true); return getTaskDao().save(claimedTask); } catch (AcmTaskException e) { AcmTask fakeTask = new AcmTask(); fakeTask.setTaskId(taskId); publishTaskClaimEvent(authentication, httpSession, fakeTask, "claim", false); throw new AcmUserActionFailedException("claim", "task", taskId, e.getMessage(), e); } } }
ClaimTaskAPIController { @RequestMapping(value = "/claim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask claimTask( @PathVariable("taskId") Long taskId, Authentication authentication, HttpSession httpSession) throws AcmUserActionFailedException { log.info("User [{}] is claiming workflow task with ID [{}]", authentication.getName(), taskId); try { getTaskDao().claimTask(taskId, authentication.getName()); AcmTask claimedTask = getTaskDao().findById(taskId); publishTaskClaimEvent(authentication, httpSession, claimedTask, "claim", true); return getTaskDao().save(claimedTask); } catch (AcmTaskException e) { AcmTask fakeTask = new AcmTask(); fakeTask.setTaskId(taskId); publishTaskClaimEvent(authentication, httpSession, fakeTask, "claim", false); throw new AcmUserActionFailedException("claim", "task", taskId, e.getMessage(), e); } }  }
ClaimTaskAPIController { @RequestMapping(value = "/claim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask claimTask( @PathVariable("taskId") Long taskId, Authentication authentication, HttpSession httpSession) throws AcmUserActionFailedException { log.info("User [{}] is claiming workflow task with ID [{}]", authentication.getName(), taskId); try { getTaskDao().claimTask(taskId, authentication.getName()); AcmTask claimedTask = getTaskDao().findById(taskId); publishTaskClaimEvent(authentication, httpSession, claimedTask, "claim", true); return getTaskDao().save(claimedTask); } catch (AcmTaskException e) { AcmTask fakeTask = new AcmTask(); fakeTask.setTaskId(taskId); publishTaskClaimEvent(authentication, httpSession, fakeTask, "claim", false); throw new AcmUserActionFailedException("claim", "task", taskId, e.getMessage(), e); } }  @RequestMapping(value = "/claim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody AcmTask claimTask(
            @PathVariable("taskId") Long taskId,
            Authentication authentication,
            HttpSession httpSession); @RequestMapping(value = "/unclaim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody AcmTask unclaimTask(
            @PathVariable("taskId") Long taskId,
            Authentication authentication,
            HttpSession httpSession); TaskDao getTaskDao(); void setTaskDao(TaskDao taskDao); TaskEventPublisher getTaskEventPublisher(); void setTaskEventPublisher(TaskEventPublisher taskEventPublisher); }
ClaimTaskAPIController { @RequestMapping(value = "/claim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask claimTask( @PathVariable("taskId") Long taskId, Authentication authentication, HttpSession httpSession) throws AcmUserActionFailedException { log.info("User [{}] is claiming workflow task with ID [{}]", authentication.getName(), taskId); try { getTaskDao().claimTask(taskId, authentication.getName()); AcmTask claimedTask = getTaskDao().findById(taskId); publishTaskClaimEvent(authentication, httpSession, claimedTask, "claim", true); return getTaskDao().save(claimedTask); } catch (AcmTaskException e) { AcmTask fakeTask = new AcmTask(); fakeTask.setTaskId(taskId); publishTaskClaimEvent(authentication, httpSession, fakeTask, "claim", false); throw new AcmUserActionFailedException("claim", "task", taskId, e.getMessage(), e); } }  @RequestMapping(value = "/claim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody AcmTask claimTask(
            @PathVariable("taskId") Long taskId,
            Authentication authentication,
            HttpSession httpSession); @RequestMapping(value = "/unclaim/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody AcmTask unclaimTask(
            @PathVariable("taskId") Long taskId,
            Authentication authentication,
            HttpSession httpSession); TaskDao getTaskDao(); void setTaskDao(TaskDao taskDao); TaskEventPublisher getTaskEventPublisher(); void setTaskEventPublisher(TaskEventPublisher taskEventPublisher);  }