@Deployment
  public void FAILING_testSubprocessCompensationHandler() {

    // given a process instance
    ProcessInstance processInstance =
        runtimeService.startProcessInstanceByKey("subProcessCompensationHandler");

    // when throwing compensation
    Task beforeCompensationTask = taskService.createTaskQuery().singleResult();
    taskService.complete(beforeCompensationTask.getId());

    // then the compensation handler has been activated
    // and the user task in the sub process can be successfully completed
    Task subProcessTask = taskService.createTaskQuery().singleResult();
    assertNotNull(subProcessTask);
    assertEquals("subProcessTask", subProcessTask.getTaskDefinitionKey());

    taskService.complete(subProcessTask.getId());

    // and the task following compensation should can be successfully completed
    Task afterCompensationTask = taskService.createTaskQuery().singleResult();
    assertNotNull(afterCompensationTask);
    assertEquals("beforeEnd", afterCompensationTask.getTaskDefinitionKey());

    taskService.complete(afterCompensationTask.getId());

    // and the process has successfully ended
    assertProcessEnded(processInstance.getId());
  }
  @Override
  public void execute(DelegateExecution execution) throws Exception {

    Map<String, Object> vars = new HashMap<String, Object>();

    String busKey = (String) execution.getVariable("busKey");

    RandomGeneratorUtil randomUtil = new RandomGeneratorUtil();

    vars.put("paymentRejected", randomUtil.getRandomPaymentRejected());
    vars.put("numberOfPayouts", randomUtil.getRandomNumberOfPayments());
    vars.put("historyOfFraud", randomUtil.getRandomHistoryOfFraud());
    vars.put("userDeterminedFraud", randomUtil.getRandomUserDeterminedFraud());

    Task currentTask =
        execution
            .getProcessEngineServices()
            .getTaskService()
            .createTaskQuery()
            .processInstanceBusinessKey(busKey)
            .singleResult();

    if (currentTask.getTaskDefinitionKey().equals("Task_Select_Manual_User")) {
      execution.getProcessEngineServices().getTaskService().complete(currentTask.getId(), vars);
      throw new BpmnError("TASK_NOT_FOUND");
    }

    execution.getProcessEngineServices().getTaskService().complete(currentTask.getId(), vars);
  }
  /** CAM-3628 */
  @Deployment
  public void testCompensateSubprocessWithBoundaryEvent() {
    ProcessInstance instance = runtimeService.startProcessInstanceByKey("compensateProcess");

    Task compensationTask = taskService.createTaskQuery().singleResult();
    assertNotNull(compensationTask);
    assertEquals("undoSubprocess", compensationTask.getTaskDefinitionKey());

    taskService.complete(compensationTask.getId());
    runtimeService.signal(instance.getId());
    assertProcessEnded(instance.getId());
  }
  @Deployment
  public void testCompensateActivityInSubprocess() {
    // given
    ProcessInstance instance = runtimeService.startProcessInstanceByKey("compensateProcess");

    Task scopeTask = taskService.createTaskQuery().singleResult();
    taskService.complete(scopeTask.getId());

    // process has not yet thrown compensation
    // when throw compensation
    runtimeService.signal(instance.getId());
    // then
    Task compensationTask = taskService.createTaskQuery().singleResult();
    assertNotNull(compensationTask);
    assertEquals("undoScopeTask", compensationTask.getTaskDefinitionKey());

    taskService.complete(compensationTask.getId());
    runtimeService.signal(instance.getId());
    assertProcessEnded(instance.getId());
  }
  @Test
  @ScenarioUnderTest("init.4")
  public void testInitThrowError() {
    // given
    ProcessInstance instance = rule.processInstance();
    Task innerMiSubProcessTask =
        rule.taskQuery().taskDefinitionKey("innerSubProcessTask").singleResult();

    // when
    rule.getRuntimeService()
        .setVariable(instance.getId(), ThrowBpmnErrorDelegate.ERROR_INDICATOR_VARIABLE, true);
    rule.getTaskService().complete(innerMiSubProcessTask.getId());

    // then
    Task escalatedTask = rule.taskQuery().singleResult();
    Assert.assertEquals("escalatedTask", escalatedTask.getTaskDefinitionKey());
    Assert.assertNotNull(escalatedTask);

    rule.getTaskService().complete(escalatedTask.getId());
    rule.assertScenarioEnded();
  }
  @Deployment
  public void testHistoricTaskInstance() throws Exception {
    String processInstanceId =
        runtimeService.startProcessInstanceByKey("HistoricTaskInstanceTest").getId();

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

    // Set priority to non-default value
    Task runtimeTask =
        taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
    runtimeTask.setPriority(1234);

    // Set due-date
    Date dueDate = sdf.parse("01/02/2003 04:05:06");
    runtimeTask.setDueDate(dueDate);
    taskService.saveTask(runtimeTask);

    String taskId = runtimeTask.getId();
    String taskDefinitionKey = runtimeTask.getTaskDefinitionKey();

    HistoricTaskInstance historicTaskInstance =
        historyService.createHistoricTaskInstanceQuery().singleResult();
    assertEquals(taskId, historicTaskInstance.getId());
    assertEquals(1234, historicTaskInstance.getPriority());
    assertEquals("Clean up", historicTaskInstance.getName());
    assertEquals(
        "Schedule an engineering meeting for next week with the new hire.",
        historicTaskInstance.getDescription());
    assertEquals(dueDate, historicTaskInstance.getDueDate());
    assertEquals("kermit", historicTaskInstance.getAssignee());
    assertEquals(taskDefinitionKey, historicTaskInstance.getTaskDefinitionKey());
    assertNull(historicTaskInstance.getEndTime());
    assertNull(historicTaskInstance.getDurationInMillis());

    assertNull(historicTaskInstance.getCaseDefinitionId());
    assertNull(historicTaskInstance.getCaseInstanceId());
    assertNull(historicTaskInstance.getCaseExecutionId());

    // the activity instance id is set
    assertEquals(
        ((TaskEntity) runtimeTask).getExecution().getActivityInstanceId(),
        historicTaskInstance.getActivityInstanceId());

    runtimeService.setVariable(processInstanceId, "deadline", "yesterday");

    // move clock by 1 second
    Date now = ClockUtil.getCurrentTime();
    ClockUtil.setCurrentTime(new Date(now.getTime() + 1000));

    taskService.complete(taskId);

    assertEquals(1, historyService.createHistoricTaskInstanceQuery().count());

    historicTaskInstance = historyService.createHistoricTaskInstanceQuery().singleResult();
    assertEquals(taskId, historicTaskInstance.getId());
    assertEquals(1234, historicTaskInstance.getPriority());
    assertEquals("Clean up", historicTaskInstance.getName());
    assertEquals(
        "Schedule an engineering meeting for next week with the new hire.",
        historicTaskInstance.getDescription());
    assertEquals(dueDate, historicTaskInstance.getDueDate());
    assertEquals("kermit", historicTaskInstance.getAssignee());
    assertEquals(TaskEntity.DELETE_REASON_COMPLETED, historicTaskInstance.getDeleteReason());
    assertEquals(taskDefinitionKey, historicTaskInstance.getTaskDefinitionKey());
    assertNotNull(historicTaskInstance.getEndTime());
    assertNotNull(historicTaskInstance.getDurationInMillis());
    assertTrue(historicTaskInstance.getDurationInMillis() >= 1000);
    assertTrue(((HistoricTaskInstanceEntity) historicTaskInstance).getDurationRaw() >= 1000);

    assertNull(historicTaskInstance.getCaseDefinitionId());
    assertNull(historicTaskInstance.getCaseInstanceId());
    assertNull(historicTaskInstance.getCaseExecutionId());

    historyService.deleteHistoricTaskInstance(taskId);

    assertEquals(0, historyService.createHistoricTaskInstanceQuery().count());
  }