// set time ahead to get different durations
ClockUtil.setCurrentTime(DateTimeUtil.now().plusHours(1).toDate());
terminate(twoCaseInstanceId);
close(twoCaseInstanceId);
HistoricCaseInstance oneCaseInstance = queryHistoricCaseInstance(oneCaseInstanceId);
HistoricCaseInstance twoCaseInstance = queryHistoricCaseInstance(twoCaseInstanceId);
// sort by case instance ids
String property = "id";
List<? extends Comparable> sortedList = Arrays.asList(oneCaseInstance.getId(), twoCaseInstance.getId());
Collections.sort(sortedList);
List<HistoricCaseInstance> instances = historicQuery().orderByCaseInstanceId().asc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(0))),
hasProperty(property, equalTo(sortedList.get(1)))
));
instances = historicQuery().orderByCaseInstanceId().desc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(1))),
hasProperty(property, equalTo(sortedList.get(0)))
));
// sort by case definition ids
property = "caseDefinitionId";
sortedList = Arrays.asList(oneCaseInstance.getCaseDefinitionId(), twoCaseInstance.getCaseDefinitionId());
Collections.sort(sortedList);
instances = historicQuery().orderByCaseDefinitionId().asc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(0))),
hasProperty(property, equalTo(sortedList.get(1)))
));
instances = historicQuery().orderByCaseDefinitionId().desc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(1))),
hasProperty(property, equalTo(sortedList.get(0)))
));
// sort by business keys
property = "businessKey";
sortedList = Arrays.asList(oneCaseInstance.getBusinessKey(), twoCaseInstance.getBusinessKey());
Collections.sort(sortedList);
instances = historicQuery().orderByCaseInstanceBusinessKey().asc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(0))),
hasProperty(property, equalTo(sortedList.get(1)))
));
instances = historicQuery().orderByCaseInstanceBusinessKey().desc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(1))),
hasProperty(property, equalTo(sortedList.get(0)))
));
// sort by create time
property = "createTime";
sortedList = Arrays.asList(oneCaseInstance.getCreateTime(), twoCaseInstance.getCreateTime());
Collections.sort(sortedList);
instances = historicQuery().orderByCaseInstanceCreateTime().asc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(0))),
hasProperty(property, equalTo(sortedList.get(1)))
));
instances = historicQuery().orderByCaseInstanceCreateTime().desc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(1))),
hasProperty(property, equalTo(sortedList.get(0)))
));
// sort by close time
property = "closeTime";
sortedList = Arrays.asList(oneCaseInstance.getCloseTime(), twoCaseInstance.getCloseTime());
Collections.sort(sortedList);
instances = historicQuery().orderByCaseInstanceCloseTime().asc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(0))),
hasProperty(property, equalTo(sortedList.get(1)))
));
instances = historicQuery().orderByCaseInstanceCloseTime().desc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(
hasProperty(property, equalTo(sortedList.get(1))),
hasProperty(property, equalTo(sortedList.get(0)))
));
// sort by duration
property = "durationInMillis";
sortedList = Arrays.asList(oneCaseInstance.getDurationInMillis(), twoCaseInstance.getDurationInMillis());
Collections.sort(sortedList);
instances = historicQuery().orderByCaseInstanceDuration().asc().list();
assertEquals(2, instances.size());
assertThat(instances, contains(