private void assertThatUpdateHasAllImportantInformation(HistoricVariableUpdate variableUpdate) { assertThat(variableUpdate, notNullValue()); assertThat(variableUpdate.getId(), notNullValue()); assertThat(variableUpdate.getProcessDefinitionKey(), is("process")); assertThat(variableUpdate.getProcessDefinitionId(), notNullValue()); assertThat(variableUpdate.getVariableName(), is("stringVar")); assertThat(variableUpdate.getValue().toString(), is("foo")); assertThat(variableUpdate.getTypeName(), is("string")); assertThat(variableUpdate.getTime(), notNullValue()); }
@Test public void occurredAfterParameterWorks() { // given BpmnModelInstance simpleDefinition = Bpmn.createExecutableProcess("process") .startEvent() .endEvent() .done(); testHelper.deploy(simpleDefinition); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("stringVar", "value1"); Date now = new Date(); ClockUtil.setCurrentTime(now); runtimeService.startProcessInstanceByKey("process", variables); Date nowPlus2Seconds = new Date(new Date().getTime() + 2000L); ClockUtil.setCurrentTime(nowPlus2Seconds); variables.put("stringVar", "value2"); runtimeService.startProcessInstanceByKey("process", variables); // when List<HistoricVariableUpdate> variableUpdates = optimizeService.getHistoricVariableUpdates(now, null, 10); // then assertThat(variableUpdates.size(), is(1)); assertThat(variableUpdates.get(0).getValue().toString(), is("value2")); }
@Test public void occurredAtParameterWorks() { // given BpmnModelInstance simpleDefinition = Bpmn.createExecutableProcess("process") .startEvent() .endEvent() .done(); testHelper.deploy(simpleDefinition); Date now = new Date(); ClockUtil.setCurrentTime(now); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("stringVar", "value1"); runtimeService.startProcessInstanceByKey("process", variables); Date nowPlus2Seconds = new Date(now.getTime() + 2000L); ClockUtil.setCurrentTime(nowPlus2Seconds); variables.put("stringVar", "value2"); runtimeService.startProcessInstanceByKey("process", variables); // when List<HistoricVariableUpdate> variableUpdates = optimizeService.getHistoricVariableUpdates(null, now, 10); // then assertThat(variableUpdates.size(), is(1)); assertThat(variableUpdates.get(0).getValue().toString(), is("value1")); }
@Deployment(resources = "org/camunda/bpm/engine/test/history/HistoricVariableInstanceTest.testImplicitVariableUpdate.bpmn20.xml") public void testImplicitVariableRemoveAndUpdateInOneTransaction() { // given runtimeService.startProcessInstanceByKey("serviceTaskProcess", Variables.createVariables() .putValue("listVar", new ArrayList<String>()) .putValue("delegate", new RemoveAndUpdateValueDelegate())); if (isFullHistoryEnabled()) { List<HistoricDetail> historicDetails = historyService .createHistoricDetailQuery() .variableUpdates() .orderPartiallyByOccurrence().asc() .list(); Iterator<HistoricDetail> detailsIt = historicDetails.iterator(); while(detailsIt.hasNext()) { if (!"listVar".equals(((HistoricVariableUpdate) detailsIt.next()).getVariableName())) { detailsIt.remove(); } } // one for creation, one for deletion, none for update assertEquals(2, historicDetails.size()); HistoricVariableUpdate update1 = (HistoricVariableUpdate) historicDetails.get(0); List<String> value1 = (List<String>) update1.getValue(); assertNotNull(value1); assertTrue(value1.isEmpty()); HistoricVariableUpdate update2 = (HistoricVariableUpdate) historicDetails.get(1); assertNull(update2.getValue()); } }
HistoricVariableUpdate update3 = (HistoricVariableUpdate) historicDetails.get(2); List<String> value1 = (List<String>) update1.getValue(); List<String> value2 = (List<String>) update2.getValue(); assertEquals(UpdateValueDelegate.NEW_ELEMENT, value2.get(0)); List<String> value3 = (List<String>) update3.getValue();
/** * TODO: add when history for case execution variables is implemented */ @Deployment public void FAILING_testListenerDoesNotInterfereWithHistory() { CaseInstance caseInstance = caseService .withCaseDefinitionByKey("case") .create(); // when i set a variable that causes the listener to be notified // and that listener sets the same variable to another value (here "value2") caseService.withCaseExecution(caseInstance.getId()).setVariableLocal("variable", "value1").execute(); // then there should be two historic variable updates for both values if (processEngineConfiguration.getHistoryLevel().getId() >= HistoryLevel.HISTORY_LEVEL_FULL.getId()) { List<HistoricDetail> variableUpdates = historyService.createHistoricDetailQuery().variableUpdates().list(); assertEquals(2, variableUpdates.size()); for (HistoricDetail detail : variableUpdates) { HistoricVariableUpdate update = (HistoricVariableUpdate) detail; boolean update1Processed = false; boolean update2Processed = false; if (!update1Processed && update.getValue().equals("value1")) { update1Processed = true; } else if (!update2Processed && update.getValue().equals("value2")) { update2Processed = true; } else { fail("unexpected variable update"); } } } }
HistoricVariableUpdate varUpdate1 = (HistoricVariableUpdate) details.get(2); assertEquals("variable1", varUpdate1.getVariableName()); assertEquals("activiti rocks!", varUpdate1.getValue()); HistoricVariableUpdate varUpdate2 = (HistoricVariableUpdate) details.get(3); assertEquals("variable2", varUpdate2.getVariableName()); assertEquals(12345L, varUpdate2.getValue());
@Test public void testBinaryFetchingEnabled() { // by default, binary fetching is enabled Task newTask = taskService.newTask(); taskService.saveTask(newTask); String variableName = "binaryVariableName"; taskService.setVariable(newTask.getId(), variableName, "some bytes".getBytes()); HistoricDetail result = historyService.createHistoricDetailQuery() .variableUpdates() .singleResult(); assertNotNull(((HistoricVariableUpdate)result).getValue()); taskService.deleteTask(newTask.getId(), true); }
@Deployment(resources = {"org/camunda/bpm/engine/test/history/HistoricVariableInstanceTest.testHistoricVariableInstanceRevision.bpmn20.xml"}) public void testVariableUpdateOrder() { // given: // a finished process instance ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("process"); assertProcessEnded(processInstance.getId()); // when // then HistoricVariableInstance variable = historyService .createHistoricVariableInstanceQuery() .singleResult(); assertNotNull(variable); if (isFullHistoryEnabled()) { List<HistoricDetail> details = historyService .createHistoricDetailQuery() .variableInstanceId(variable.getId()) .orderPartiallyByOccurrence() .asc() .list(); assertEquals(3, details.size()); HistoricVariableUpdate firstUpdate = (HistoricVariableUpdate) details.get(0); assertEquals(1, firstUpdate.getValue()); HistoricVariableUpdate secondUpdate = (HistoricVariableUpdate) details.get(1); assertEquals(2, secondUpdate.getValue()); assertTrue(((HistoryEvent)secondUpdate).getSequenceCounter() > ((HistoryEvent)firstUpdate).getSequenceCounter()); HistoricVariableUpdate thirdUpdate = (HistoricVariableUpdate) details.get(2); assertEquals(3, thirdUpdate.getValue()); assertTrue(((HistoryEvent)thirdUpdate).getSequenceCounter() > ((HistoryEvent)secondUpdate).getSequenceCounter()); } }
public void execute(DelegateExecution execution) throws Exception { HistoryService historyService = execution.getProcessEngineServices().getHistoryService(); HistoricVariableInstance variableInstance = historyService .createHistoricVariableInstanceQuery() .variableName("listVar") .singleResult(); HistoricVariableUpdate initialUpdate = (HistoricVariableUpdate) historyService .createHistoricDetailQuery() .variableUpdates() .variableInstanceId(variableInstance.getId()) .orderPartiallyByOccurrence().asc() .list() .get(0); List<String> list = (List<String>) initialUpdate.getValue(); // implicit update of the list, should not trigger an update // of the value since we deal with historic variables list.add(NEW_ELEMENT); }
assertEquals(1, firstUpdate.getValue()); assertEquals(2, secondUpdate.getValue()); assertTrue(((HistoryEvent)secondUpdate).getSequenceCounter() > ((HistoryEvent)firstUpdate).getSequenceCounter()); assertEquals(3, thirdUpdate.getValue()); assertTrue(((HistoryEvent)thirdUpdate).getSequenceCounter() > ((HistoryEvent)secondUpdate).getSequenceCounter());
@Test public void testBinaryFetchingDisabled() { Task newTask = taskService.newTask(); taskService.saveTask(newTask); String variableName = "binaryVariableName"; taskService.setVariable(newTask.getId(), variableName, "some bytes".getBytes()); HistoricDetail result = historyService.createHistoricDetailQuery() .disableBinaryFetching() .variableUpdates() .singleResult(); assertNull(((HistoricVariableUpdate)result).getValue()); taskService.deleteTask(newTask.getId(), true); }
assertEquals(1, firstUpdate.getValue()); assertEquals(1, secondUpdate.getValue()); assertTrue(((HistoryEvent)secondUpdate).getSequenceCounter() > ((HistoryEvent)firstUpdate).getSequenceCounter()); assertEquals(2, thirdUpdate.getValue()); assertTrue(((HistoryEvent)thirdUpdate).getSequenceCounter() > ((HistoryEvent)secondUpdate).getSequenceCounter());
List<String> value1 = (List<String>) update1.getValue();
List<String> value1 = (List<String>) update1.getValue();
@Test public void testErrorMessage() { Task newTask = taskService.newTask(); taskService.saveTask(newTask); String variableName = "failingSerializable"; taskService.setVariable(newTask.getId(), variableName, new FailingSerializable()); HistoricDetail result = historyService.createHistoricDetailQuery() .disableBinaryFetching() .variableUpdates() .singleResult(); assertNull(((HistoricVariableUpdate)result).getValue()); assertNotNull(((HistoricVariableUpdate)result).getErrorMessage()); taskService.deleteTask(newTask.getId(), true); }
@Test public void testHistoricDetailQueryById() { Task newTask = taskService.newTask(); taskService.saveTask(newTask); String variableName = "someName"; String variableValue = "someValue"; taskService.setVariable(newTask.getId(), variableName, variableValue); HistoricDetail result = historyService.createHistoricDetailQuery() .singleResult(); HistoricDetail resultById = historyService.createHistoricDetailQuery().detailId(result.getId()).singleResult(); assertNotNull(resultById); assertEquals(result.getId(), resultById.getId()); assertEquals(variableName, ((HistoricVariableUpdate)resultById).getVariableName()); assertEquals(variableValue, ((HistoricVariableUpdate)resultById).getValue()); assertEquals(ValueType.STRING.getName(), ((HistoricVariableUpdate)resultById).getVariableTypeName()); assertEquals(ValueType.STRING.getName(), ((HistoricVariableUpdate)resultById).getTypeName()); taskService.deleteTask(newTask.getId(), true); }
@Deployment(resources = EXCLUSIVE_GATEWAY_PROCESS) public void testHistoricVariableInstanceForSingleActivityInstantiation() { // when ProcessInstance instance = runtimeService .createProcessInstanceByKey("exclusiveGateway") .startBeforeActivity("task1") .setVariable("aVar", "aValue") .execute(); ActivityInstance activityInstance = runtimeService.getActivityInstance(instance.getId()); // then HistoricVariableInstance historicVariable = historyService.createHistoricVariableInstanceQuery() .variableName("aVar") .singleResult(); assertNotNull(historicVariable); assertEquals(instance.getId(), historicVariable.getProcessInstanceId()); assertEquals(activityInstance.getId(), historicVariable.getActivityInstanceId()); assertEquals("aVar", historicVariable.getName()); assertEquals("aValue", historicVariable.getValue()); HistoricDetail historicDetail = historyService.createHistoricDetailQuery() .variableInstanceId(historicVariable.getId()).singleResult(); assertEquals(instance.getId(), historicDetail.getProcessInstanceId()); assertNotNull(historicDetail); // TODO: fix if this is not ok due to CAM-3886 assertNull(historicDetail.getActivityInstanceId()); assertTrue(historicDetail instanceof HistoricVariableUpdate); assertEquals("aVar", ((HistoricVariableUpdate) historicDetail).getVariableName()); assertEquals("aValue", ((HistoricVariableUpdate) historicDetail).getValue()); }
@Deployment(resources = EXCLUSIVE_GATEWAY_PROCESS) public void testHistoricVariableInstanceSetOnProcessInstance() { // when ProcessInstance instance = runtimeService .createProcessInstanceByKey("exclusiveGateway") // set the variables directly one the instance .setVariable("aVar", "aValue") .startBeforeActivity("task1") .execute(); ActivityInstance activityInstance = runtimeService.getActivityInstance(instance.getId()); // then HistoricVariableInstance historicVariable = historyService.createHistoricVariableInstanceQuery() .variableName("aVar") .singleResult(); assertNotNull(historicVariable); assertEquals(instance.getId(), historicVariable.getProcessInstanceId()); assertEquals(activityInstance.getId(), historicVariable.getActivityInstanceId()); assertEquals("aVar", historicVariable.getName()); assertEquals("aValue", historicVariable.getValue()); HistoricDetail historicDetail = historyService.createHistoricDetailQuery() .variableInstanceId(historicVariable.getId()).singleResult(); assertEquals(instance.getId(), historicDetail.getProcessInstanceId()); assertNotNull(historicDetail); // TODO: fix if this is not ok due to CAM-3886 assertEquals(instance.getId(), historicDetail.getActivityInstanceId()); assertTrue(historicDetail instanceof HistoricVariableUpdate); assertEquals("aVar", ((HistoricVariableUpdate) historicDetail).getVariableName()); assertEquals("aValue", ((HistoricVariableUpdate) historicDetail).getValue()); }