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());
}