@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());
  }
  @Deployment
  public void testHistoricTaskInstanceQuery() throws Exception {
    // First instance is finished
    ProcessInstance finishedInstance =
        runtimeService.startProcessInstanceByKey("HistoricTaskQueryTest");

    // Set priority to non-default value
    Task task =
        taskService.createTaskQuery().processInstanceId(finishedInstance.getId()).singleResult();
    task.setPriority(1234);
    Date dueDate = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse("01/02/2003 04:05:06");
    task.setDueDate(dueDate);

    taskService.saveTask(task);

    // Complete the task
    String taskId = task.getId();
    taskService.complete(taskId);

    // Task id
    assertEquals(1, historyService.createHistoricTaskInstanceQuery().taskId(taskId).count());
    assertEquals(
        0, historyService.createHistoricTaskInstanceQuery().taskId("unexistingtaskid").count());

    // Name
    assertEquals(1, historyService.createHistoricTaskInstanceQuery().taskName("Clean up").count());
    assertEquals(
        0, historyService.createHistoricTaskInstanceQuery().taskName("unexistingname").count());
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskNameLike("Clean u%").count());
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskNameLike("%lean up").count());
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskNameLike("%lean u%").count());
    assertEquals(
        0,
        historyService.createHistoricTaskInstanceQuery().taskNameLike("%unexistingname%").count());

    // Description
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDescription("Historic task description")
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDescription("unexistingdescription")
            .count());
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDescriptionLike("%task description")
            .count());
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDescriptionLike("Historic task %")
            .count());
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskDescriptionLike("%task%").count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDescriptionLike("%unexistingdescripton%")
            .count());

    // Execution id
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .executionId(finishedInstance.getId())
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .executionId("unexistingexecution")
            .count());

    // Process instance id
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .processInstanceId(finishedInstance.getId())
            .count());
    assertEquals(
        0,
        historyService.createHistoricTaskInstanceQuery().processInstanceId("unexistingid").count());

    // Process definition id
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .processDefinitionId(finishedInstance.getProcessDefinitionId())
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .processDefinitionId("unexistingdefinitionid")
            .count());

    // Process definition name
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .processDefinitionName("Historic task query test process")
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .processDefinitionName("unexistingdefinitionname")
            .count());

    // Process definition key
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .processDefinitionKey("HistoricTaskQueryTest")
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .processDefinitionKey("unexistingdefinitionkey")
            .count());

    // Assignee
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskAssignee("kermit").count());
    assertEquals(
        0, historyService.createHistoricTaskInstanceQuery().taskAssignee("johndoe").count());
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("%ermit").count());
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("kermi%").count());
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("%ermi%").count());
    assertEquals(
        0, historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("%johndoe%").count());

    // Delete reason
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDeleteReason(TaskEntity.DELETE_REASON_COMPLETED)
            .count());
    assertEquals(
        0, historyService.createHistoricTaskInstanceQuery().taskDeleteReason("deleted").count());

    // Task definition ID
    assertEquals(
        1, historyService.createHistoricTaskInstanceQuery().taskDefinitionKey("task").count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDefinitionKey("unexistingkey")
            .count());

    // Task priority
    assertEquals(1, historyService.createHistoricTaskInstanceQuery().taskPriority(1234).count());
    assertEquals(0, historyService.createHistoricTaskInstanceQuery().taskPriority(5678).count());

    // Due date
    Calendar anHourAgo = Calendar.getInstance();
    anHourAgo.setTime(dueDate);
    anHourAgo.add(Calendar.HOUR, -1);

    Calendar anHourLater = Calendar.getInstance();
    anHourLater.setTime(dueDate);
    anHourLater.add(Calendar.HOUR, 1);

    assertEquals(1, historyService.createHistoricTaskInstanceQuery().taskDueDate(dueDate).count());
    assertEquals(
        0,
        historyService.createHistoricTaskInstanceQuery().taskDueDate(anHourAgo.getTime()).count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueDate(anHourLater.getTime())
            .count());

    // Due date before
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueBefore(anHourLater.getTime())
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueBefore(anHourAgo.getTime())
            .count());

    // Due date after
    assertEquals(
        1,
        historyService.createHistoricTaskInstanceQuery().taskDueAfter(anHourAgo.getTime()).count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueAfter(anHourLater.getTime())
            .count());

    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueDate(dueDate)
            .taskDueBefore(anHourLater.getTime())
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueDate(dueDate)
            .taskDueBefore(anHourAgo.getTime())
            .count());
    assertEquals(
        1,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueDate(dueDate)
            .taskDueAfter(anHourAgo.getTime())
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueDate(dueDate)
            .taskDueAfter(anHourLater.getTime())
            .count());
    assertEquals(
        0,
        historyService
            .createHistoricTaskInstanceQuery()
            .taskDueBefore(anHourAgo.getTime())
            .taskDueAfter(anHourLater.getTime())
            .count());

    // Finished and Unfinished - Add anther other instance that has a running task (unfinished)
    runtimeService.startProcessInstanceByKey("HistoricTaskQueryTest");

    assertEquals(1, historyService.createHistoricTaskInstanceQuery().finished().count());
    assertEquals(1, historyService.createHistoricTaskInstanceQuery().unfinished().count());
    assertEquals(
        0, historyService.createHistoricTaskInstanceQuery().unfinished().finished().count());
  }