use of org.camunda.bpm.engine.history.HistoricProcessInstanceQuery in project camunda-bpm-platform by camunda.
the class HistoryServiceTest method testQueryStringVariable.
/**
* basically copied from {@link ProcessInstanceQueryTest}
*/
@Deployment(resources = { "org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml" })
public void testQueryStringVariable() {
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("stringVar", "abcdef");
ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS, vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance1.getId()).singleResult().getId());
vars = new HashMap<String, Object>();
vars.put("stringVar", "abcdef");
vars.put("stringVar2", "ghijkl");
ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS, vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance2.getId()).singleResult().getId());
vars = new HashMap<String, Object>();
vars.put("stringVar", "azerty");
ProcessInstance processInstance3 = runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS, vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance3.getId()).singleResult().getId());
// Test EQUAL on single string variable, should result in 2 matches
HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().variableValueEquals("stringVar", "abcdef");
List<HistoricProcessInstance> processInstances = query.list();
Assert.assertNotNull(processInstances);
Assert.assertEquals(2, processInstances.size());
// Test EQUAL on two string variables, should result in single match
query = historyService.createHistoricProcessInstanceQuery().variableValueEquals("stringVar", "abcdef").variableValueEquals("stringVar2", "ghijkl");
HistoricProcessInstance resultInstance = query.singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance2.getId(), resultInstance.getId());
// Test NOT_EQUAL, should return only 1 resultInstance
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueNotEquals("stringVar", "abcdef").singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
// Test GREATER_THAN, should return only matching 'azerty'
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("stringVar", "abcdef").singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("stringVar", "z").singleResult();
Assert.assertNull(resultInstance);
// Test GREATER_THAN_OR_EQUAL, should return 3 results
assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("stringVar", "abcdef").count());
assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("stringVar", "z").count());
// Test LESS_THAN, should return 2 results
processInstances = historyService.createHistoricProcessInstanceQuery().variableValueLessThan("stringVar", "abcdeg").list();
Assert.assertEquals(2, processInstances.size());
List<String> expecedIds = Arrays.asList(processInstance1.getId(), processInstance2.getId());
List<String> ids = new ArrayList<String>(Arrays.asList(processInstances.get(0).getId(), processInstances.get(1).getId()));
ids.removeAll(expecedIds);
assertTrue(ids.isEmpty());
assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueLessThan("stringVar", "abcdef").count());
assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("stringVar", "z").count());
// Test LESS_THAN_OR_EQUAL
processInstances = historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("stringVar", "abcdef").list();
Assert.assertEquals(2, processInstances.size());
expecedIds = Arrays.asList(processInstance1.getId(), processInstance2.getId());
ids = new ArrayList<String>(Arrays.asList(processInstances.get(0).getId(), processInstances.get(1).getId()));
ids.removeAll(expecedIds);
assertTrue(ids.isEmpty());
assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("stringVar", "z").count());
assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("stringVar", "aa").count());
// Test LIKE
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueLike("stringVar", "azert%").singleResult();
assertNotNull(resultInstance);
assertEquals(processInstance3.getId(), resultInstance.getId());
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueLike("stringVar", "%y").singleResult();
assertNotNull(resultInstance);
assertEquals(processInstance3.getId(), resultInstance.getId());
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueLike("stringVar", "%zer%").singleResult();
assertNotNull(resultInstance);
assertEquals(processInstance3.getId(), resultInstance.getId());
assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueLike("stringVar", "a%").count());
assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueLike("stringVar", "%x%").count());
historyService.deleteHistoricProcessInstance(processInstance1.getId());
historyService.deleteHistoricProcessInstance(processInstance2.getId());
historyService.deleteHistoricProcessInstance(processInstance3.getId());
}
use of org.camunda.bpm.engine.history.HistoricProcessInstanceQuery in project camunda-bpm-platform by camunda.
the class HistoryServiceTest method testHistoricProcessInstanceQueryByProcessInstanceIds.
@Deployment(resources = { "org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml", "org/camunda/bpm/engine/test/api/runtime/otherOneTaskProcess.bpmn20.xml" })
public void testHistoricProcessInstanceQueryByProcessInstanceIds() {
HashSet<String> processInstanceIds = new HashSet<String>();
for (int i = 0; i < 4; i++) {
processInstanceIds.add(runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS, i + "").getId());
}
processInstanceIds.add(runtimeService.startProcessInstanceByKey("otherOneTaskProcess", "1").getId());
// start an instance that will not be part of the query
runtimeService.startProcessInstanceByKey("otherOneTaskProcess", "2");
HistoricProcessInstanceQuery processInstanceQuery = historyService.createHistoricProcessInstanceQuery().processInstanceIds(processInstanceIds);
assertEquals(5, processInstanceQuery.count());
List<HistoricProcessInstance> processInstances = processInstanceQuery.list();
assertNotNull(processInstances);
assertEquals(5, processInstances.size());
for (HistoricProcessInstance historicProcessInstance : processInstances) {
assertTrue(processInstanceIds.contains(historicProcessInstance.getId()));
}
// making a query that has contradicting conditions should succeed
assertEquals(0, processInstanceQuery.processInstanceId("dummy").count());
}
use of org.camunda.bpm.engine.history.HistoricProcessInstanceQuery in project camunda-bpm-platform by camunda.
the class HistoryServiceTest method testQueryDateVariable.
/**
* Only do one second type, as the logic is same as in {@link ProcessInstanceQueryTest} and I do not want to duplicate
* all test case logic here.
* Basically copied from {@link ProcessInstanceQueryTest}
*/
@Deployment(resources = { "org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml" })
public void testQueryDateVariable() throws Exception {
Map<String, Object> vars = new HashMap<String, Object>();
Date date1 = Calendar.getInstance().getTime();
vars.put("dateVar", date1);
ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS, vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance1.getId()).singleResult().getId());
Date date2 = Calendar.getInstance().getTime();
vars = new HashMap<String, Object>();
vars.put("dateVar", date1);
vars.put("dateVar2", date2);
ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS, vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance2.getId()).singleResult().getId());
Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
vars = new HashMap<String, Object>();
vars.put("dateVar", nextYear.getTime());
ProcessInstance processInstance3 = runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS, vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance3.getId()).singleResult().getId());
Calendar nextMonth = Calendar.getInstance();
nextMonth.add(Calendar.MONTH, 1);
Calendar twoYearsLater = Calendar.getInstance();
twoYearsLater.add(Calendar.YEAR, 2);
Calendar oneYearAgo = Calendar.getInstance();
oneYearAgo.add(Calendar.YEAR, -1);
// Query on single short variable, should result in 2 matches
HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().variableValueEquals("dateVar", date1);
List<HistoricProcessInstance> processInstances = query.list();
Assert.assertNotNull(processInstances);
Assert.assertEquals(2, processInstances.size());
// Query on two short variables, should result in single value
query = historyService.createHistoricProcessInstanceQuery().variableValueEquals("dateVar", date1).variableValueEquals("dateVar2", date2);
HistoricProcessInstance resultInstance = query.singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance2.getId(), resultInstance.getId());
// Query with unexisting variable value
Date unexistingDate = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse("01/01/1989 12:00:00");
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueEquals("dateVar", unexistingDate).singleResult();
Assert.assertNull(resultInstance);
// Test NOT_EQUALS
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueNotEquals("dateVar", date1).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
// Test GREATER_THAN
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("dateVar", nextMonth.getTime()).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
Assert.assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("dateVar", nextYear.getTime()).count());
Assert.assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("dateVar", oneYearAgo.getTime()).count());
// Test GREATER_THAN_OR_EQUAL
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("dateVar", nextMonth.getTime()).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("dateVar", nextYear.getTime()).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
Assert.assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("dateVar", oneYearAgo.getTime()).count());
// Test LESS_THAN
processInstances = historyService.createHistoricProcessInstanceQuery().variableValueLessThan("dateVar", nextYear.getTime()).list();
Assert.assertEquals(2, processInstances.size());
List<String> expecedIds = Arrays.asList(processInstance1.getId(), processInstance2.getId());
List<String> ids = new ArrayList<String>(Arrays.asList(processInstances.get(0).getId(), processInstances.get(1).getId()));
ids.removeAll(expecedIds);
assertTrue(ids.isEmpty());
Assert.assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueLessThan("dateVar", date1).count());
Assert.assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueLessThan("dateVar", twoYearsLater.getTime()).count());
// Test LESS_THAN_OR_EQUAL
processInstances = historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("dateVar", nextYear.getTime()).list();
Assert.assertEquals(3, processInstances.size());
Assert.assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("dateVar", oneYearAgo.getTime()).count());
historyService.deleteHistoricProcessInstance(processInstance1.getId());
historyService.deleteHistoricProcessInstance(processInstance2.getId());
historyService.deleteHistoricProcessInstance(processInstance3.getId());
}
use of org.camunda.bpm.engine.history.HistoricProcessInstanceQuery in project camunda-bpm-platform by camunda.
the class HistoryServiceAsyncOperationsTest method testDeleteHistoryProcessInstancesAsyncWithQuery.
@Test
public void testDeleteHistoryProcessInstancesAsyncWithQuery() throws Exception {
// given
HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processInstanceIds(new HashSet<String>(historicProcessInstances));
Batch batch = historyService.deleteHistoricProcessInstancesAsync(query, TEST_REASON);
executeSeedJob(batch);
// when
List<Exception> exceptions = executeBatchJobs(batch);
// then
assertThat(exceptions.size(), is(0));
assertNoHistoryForTasks();
assertHistoricBatchExists(testRule);
assertAllHistoricProcessInstancesAreDeleted();
}
use of org.camunda.bpm.engine.history.HistoricProcessInstanceQuery in project camunda-bpm-platform by camunda.
the class MigrationHistoricProcessInstanceTest method testMigrateHistoryProcessInstance.
@Test
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_ACTIVITY)
public void testMigrateHistoryProcessInstance() {
// given
HistoricProcessInstanceQuery sourceHistoryProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery().processDefinitionId(sourceProcessDefinition.getId());
HistoricProcessInstanceQuery targetHistoryProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery().processDefinitionId(targetProcessDefinition.getId());
// when
assertEquals(1, sourceHistoryProcessInstanceQuery.count());
assertEquals(0, targetHistoryProcessInstanceQuery.count());
ProcessInstanceQuery sourceProcessInstanceQuery = runtimeService.createProcessInstanceQuery().processDefinitionId(sourceProcessDefinition.getId());
runtimeService.newMigration(migrationPlan).processInstanceQuery(sourceProcessInstanceQuery).execute();
// then
assertEquals(0, sourceHistoryProcessInstanceQuery.count());
assertEquals(1, targetHistoryProcessInstanceQuery.count());
HistoricProcessInstance instance = targetHistoryProcessInstanceQuery.singleResult();
assertEquals(instance.getProcessDefinitionKey(), targetProcessDefinition.getKey());
}
Aggregations