Search in sources :

Example 6 with ActivitiEvent

use of org.activiti.engine.delegate.event.ActivitiEvent in project Activiti by Activiti.

the class ProcessInstanceEventsTest method testProcessInstanceCancelledEvents_cancell.

@Deployment(resources = { "org/activiti/engine/test/api/runtime/oneTaskProcess.bpmn20.xml" })
public void testProcessInstanceCancelledEvents_cancell() throws Exception {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
    assertNotNull(processInstance);
    listener.clearEventsReceived();
    runtimeService.deleteProcessInstance(processInstance.getId(), "delete_test");
    List<ActivitiEvent> processCancelledEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
    assertEquals("ActivitiEventType.PROCESS_CANCELLED was expected 1 time.", 1, processCancelledEvents.size());
    ActivitiCancelledEvent processCancelledEvent = (ActivitiCancelledEvent) processCancelledEvents.get(0);
    assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiCancelledEvent.class.isAssignableFrom(processCancelledEvent.getClass()));
    assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getProcessInstanceId());
    assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getExecutionId());
    assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", processCancelledEvent.getCause());
    List<ActivitiEvent> taskCancelledEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
    assertEquals("ActivitiEventType.ACTIVITY_CANCELLED was expected 1 time.", 1, taskCancelledEvents.size());
    ActivitiActivityCancelledEvent activityCancelledEvent = (ActivitiActivityCancelledEvent) taskCancelledEvents.get(0);
    assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiActivityCancelledEvent.class.isAssignableFrom(activityCancelledEvent.getClass()));
    assertEquals("The activity id has to be the same as processInstance activity", processInstance.getActivityId(), activityCancelledEvent.getActivityId());
    assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), activityCancelledEvent.getProcessInstanceId());
    assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), activityCancelledEvent.getExecutionId());
    assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", activityCancelledEvent.getCause());
    listener.clearEventsReceived();
}
Also used : ActivitiActivityCancelledEvent(org.activiti.engine.delegate.event.ActivitiActivityCancelledEvent) ActivitiCancelledEvent(org.activiti.engine.delegate.event.ActivitiCancelledEvent) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Deployment(org.activiti.engine.test.Deployment)

Example 7 with ActivitiEvent

use of org.activiti.engine.delegate.event.ActivitiEvent in project Activiti by Activiti.

the class ProcessInstanceEventsTest method testProcessInstanceTerminatedEvents_terminateInParentProcess.

@Deployment(resources = { "org/activiti/engine/test/bpmn/event/end/TerminateEndEventTest.testTerminateInParentProcess.bpmn", "org/activiti/engine/test/api/runtime/oneTaskProcess.bpmn20.xml" })
public void testProcessInstanceTerminatedEvents_terminateInParentProcess() throws Exception {
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("terminateParentProcess");
    // should terminate the called process and continue the parent
    Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).taskDefinitionKey("preTerminateEnd").singleResult();
    taskService.complete(task.getId());
    assertProcessEnded(pi.getId());
    List<ActivitiEvent> processTerminatedEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
    assertEquals("There should be exactly one ActivitiEventType.PROCESS_TERMINATED event after the task complete.", 1, processTerminatedEvents.size());
    ActivitiProcessCancelledEventImpl processCancelledEvent = (ActivitiProcessCancelledEventImpl) processTerminatedEvents.get(0);
    assertThat(processCancelledEvent.getProcessInstanceId(), is(pi.getProcessInstanceId()));
    assertThat(processCancelledEvent.getProcessDefinitionId(), containsString("terminateParentProcess"));
    List<ActivitiEvent> activityTerminatedEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
    assertThat("3 activities must be cancelled.", activityTerminatedEvents.size(), is(3));
    ActivitiActivityCancelledEventImpl activityEvent = (ActivitiActivityCancelledEventImpl) activityTerminatedEvents.get(0);
    assertThat("The user task must be terminated in the called sub process.", activityEvent.getActivityId(), is("theTask"));
    assertThat("The cause must be terminate end event", ((ActivityImpl) activityEvent.getCause()).getId(), is("EndEvent_3"));
    activityEvent = (ActivitiActivityCancelledEventImpl) activityTerminatedEvents.get(1);
    assertThat("The call activity must be terminated", activityEvent.getActivityId(), is("CallActivity_1"));
    assertThat("The cause must be terminate end event", ((ActivityImpl) activityEvent.getCause()).getId(), is("EndEvent_3"));
    activityEvent = (ActivitiActivityCancelledEventImpl) activityTerminatedEvents.get(2);
    assertThat("The gateway must be terminated", activityEvent.getActivityId(), is("ParallelGateway_1"));
    assertThat("The cause must be terminate end event", ((ActivityImpl) activityEvent.getCause()).getId(), is("EndEvent_3"));
}
Also used : Task(org.activiti.engine.task.Task) ActivitiActivityCancelledEventImpl(org.activiti.engine.delegate.event.impl.ActivitiActivityCancelledEventImpl) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) ActivitiProcessCancelledEventImpl(org.activiti.engine.delegate.event.impl.ActivitiProcessCancelledEventImpl) Deployment(org.activiti.engine.test.Deployment)

Example 8 with ActivitiEvent

use of org.activiti.engine.delegate.event.ActivitiEvent in project Activiti by Activiti.

the class JobEventsTest method testJobEntityEvents.

/**
	 * Test create, update and delete events of jobs entities.
	 */
@Deployment
public void testJobEntityEvents() throws Exception {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testJobEvents");
    Job theJob = managementService.createJobQuery().processInstanceId(processInstance.getId()).singleResult();
    assertNotNull(theJob);
    // Check if create-event has been dispatched
    assertEquals(2, listener.getEventsReceived().size());
    ActivitiEvent event = listener.getEventsReceived().get(0);
    assertEquals(ActivitiEventType.ENTITY_CREATED, event.getType());
    checkEventContext(event, theJob, false);
    event = listener.getEventsReceived().get(1);
    assertEquals(ActivitiEventType.ENTITY_INITIALIZED, event.getType());
    checkEventContext(event, theJob, false);
    listener.clearEventsReceived();
    // Update the job-entity. Check if update event is dispatched with update job entity
    managementService.setJobRetries(theJob.getId(), 5);
    assertEquals(1, listener.getEventsReceived().size());
    event = listener.getEventsReceived().get(0);
    assertEquals(ActivitiEventType.ENTITY_UPDATED, event.getType());
    Job updatedJob = (Job) ((ActivitiEntityEvent) event).getEntity();
    assertEquals(5, updatedJob.getRetries());
    checkEventContext(event, theJob, true);
    listener.clearEventsReceived();
    // Force timer to fire
    Calendar tomorrow = Calendar.getInstance();
    tomorrow.add(Calendar.DAY_OF_YEAR, 1);
    processEngineConfiguration.getClock().setCurrentTime(tomorrow.getTime());
    waitForJobExecutorToProcessAllJobs(2000, 100);
    // Check delete-event has been dispatched
    assertEquals(3, listener.getEventsReceived().size());
    // First, a timer fired event has been dispatched
    event = listener.getEventsReceived().get(0);
    assertEquals(ActivitiEventType.TIMER_FIRED, event.getType());
    checkEventContext(event, theJob, true);
    // Next, a delete event has been dispatched
    event = listener.getEventsReceived().get(1);
    assertEquals(ActivitiEventType.ENTITY_DELETED, event.getType());
    checkEventContext(event, theJob, true);
    // Finally, a complete event has been dispatched
    event = listener.getEventsReceived().get(2);
    assertEquals(ActivitiEventType.JOB_EXECUTION_SUCCESS, event.getType());
    checkEventContext(event, theJob, true);
}
Also used : Calendar(java.util.Calendar) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Job(org.activiti.engine.runtime.Job) Deployment(org.activiti.engine.test.Deployment)

Example 9 with ActivitiEvent

use of org.activiti.engine.delegate.event.ActivitiEvent in project Activiti by Activiti.

the class ActivityEventsTest method testActivityErrorEventsFromBPMNError.

/**
	 * Test events related to error-events, thrown from within process-execution (eg. service-task).
	 */
@Deployment
public void testActivityErrorEventsFromBPMNError() throws Exception {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("errorProcess");
    assertNotNull(processInstance);
    // Error-handling should have ended the process
    ProcessInstance afterErrorInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
    assertNull(afterErrorInstance);
    ActivitiErrorEvent errorEvent = null;
    for (ActivitiEvent event : listener.getEventsReceived()) {
        if (event instanceof ActivitiErrorEvent) {
            if (errorEvent == null) {
                errorEvent = (ActivitiErrorEvent) event;
            } else {
                fail("Only one ActivityErrorEvent expected");
            }
        }
    }
    assertNotNull(errorEvent);
    assertEquals(ActivitiEventType.ACTIVITY_ERROR_RECEIVED, errorEvent.getType());
    assertEquals("catchError", errorEvent.getActivityId());
    assertEquals("23", errorEvent.getErrorCode());
    assertEquals(processInstance.getId(), errorEvent.getProcessInstanceId());
    assertEquals(processInstance.getProcessDefinitionId(), errorEvent.getProcessDefinitionId());
    assertFalse(processInstance.getId().equals(errorEvent.getExecutionId()));
}
Also used : ActivitiErrorEvent(org.activiti.engine.delegate.event.ActivitiErrorEvent) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Deployment(org.activiti.engine.test.Deployment)

Example 10 with ActivitiEvent

use of org.activiti.engine.delegate.event.ActivitiEvent in project Activiti by Activiti.

the class StartTimerEventRepeatWithoutEndDateTest method testCycleDateStartTimerEvent.

/**
   * Timer repetition
   */
public void testCycleDateStartTimerEvent() throws Exception {
    Clock previousClock = processEngineConfiguration.getClock();
    Clock testClock = new DefaultClockImpl();
    processEngineConfiguration.setClock(testClock);
    Calendar calendar = Calendar.getInstance();
    calendar.set(2025, Calendar.DECEMBER, 10, 0, 0, 0);
    testClock.setCurrentTime(calendar.getTime());
    //deploy the process
    repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/bpmn/event/timer/StartTimerEventRepeatWithoutEndDateTest.testCycleDateStartTimerEvent.bpmn20.xml").deploy();
    assertEquals(1, repositoryService.createProcessDefinitionQuery().count());
    //AFTER DEPLOYMENT
    //when the process is deployed there will be created a timerStartEvent job which will wait to be executed.
    List<Job> jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    //dueDate should be after 24 hours from the process deployment
    Calendar dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 11, 0, 0, 0);
    //check the due date is inside the 2 seconds range
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    //No process instances
    List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //No tasks
    List<Task> tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    // ADVANCE THE CLOCK
    // advance the clock after 9 days from starting the process ->
    // the system will execute the pending job and will create a new one (day by day)
    moveByMinutes(9 * 60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(10000, 200);
        fail("there must be a pending job because the endDate is not reached yet");
    } catch (Exception e) {
    //expected failure
    }
    // After time advanced 9 days  there should be 9 process instance started
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(9, processInstances.size());
    // 9 task to be executed (the userTask "Task A")
    tasks = taskService.createTaskQuery().list();
    assertEquals(9, tasks.size());
    // one new job will be created (and the old one will be deleted after execution)
    jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    //check if the last job to be executed has the dueDate set correctly
    // (10'th repeat after 10 dec. => dueDate must have DueDate = 20 dec.)
    dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 20, 0, 0, 0);
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    // ADVANCE THE CLOCK SO that all 10 repeats to be executed
    // (last execution)
    moveByMinutes(60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
    } catch (Exception e) {
        fail("Because the maximum number of repeats is reached it will not be executed other jobs");
    }
    // After the 10nth startEvent Execution should have 10 process instances started
    // (since the first one was not completed)
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(10, processInstances.size());
    // the current job will be deleted after execution and a new one will not be created. (all 10 has already executed)
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    // 10 tasks to be executed (the userTask "Task A")
    // one task for each process instance
    tasks = taskService.createTaskQuery().list();
    assertEquals(10, tasks.size());
    //FINAL CHECK
    // count "timer fired" events
    int timerFiredCount = 0;
    List<ActivitiEvent> eventsReceived = listener.getEventsReceived();
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.TIMER_FIRED.equals(eventReceived.getType())) {
            timerFiredCount++;
        }
    }
    //count "entity created" events
    int eventCreatedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_CREATED.equals(eventReceived.getType())) {
            eventCreatedCount++;
        }
    }
    // count "entity deleted" events
    int eventDeletedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_DELETED.equals(eventReceived.getType())) {
            eventDeletedCount++;
        }
    }
    //10 timers fired
    assertEquals(10, timerFiredCount);
    //10 jobs created
    assertEquals(10, eventCreatedCount);
    //10 jobs deleted
    assertEquals(10, eventDeletedCount);
    // let's complete the userTasks where the process is hanging in order to complete the processes.
    for (ProcessInstance processInstance : processInstances) {
        tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();
        Task task = tasks.get(0);
        assertEquals("Task A", task.getName());
        assertEquals(1, tasks.size());
        taskService.complete(task.getId());
    }
    //now All the process instances should be completed
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //no jobs
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    //no tasks
    tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    listener.clearEventsReceived();
    processEngineConfiguration.setClock(previousClock);
    repositoryService.deleteDeployment(repositoryService.createDeploymentQuery().singleResult().getId(), true);
}
Also used : Task(org.activiti.engine.task.Task) Calendar(java.util.Calendar) Clock(org.activiti.engine.runtime.Clock) DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Job(org.activiti.engine.runtime.Job)

Aggregations

ActivitiEvent (org.activiti.engine.delegate.event.ActivitiEvent)31 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)23 Deployment (org.activiti.engine.test.Deployment)19 Task (org.activiti.engine.task.Task)11 Job (org.activiti.engine.runtime.Job)10 Calendar (java.util.Calendar)9 ActivitiActivityCancelledEvent (org.activiti.engine.delegate.event.ActivitiActivityCancelledEvent)5 DefaultClockImpl (org.activiti.engine.impl.util.DefaultClockImpl)5 Clock (org.activiti.engine.runtime.Clock)5 ArrayList (java.util.ArrayList)4 Function (org.activiti.crystalball.simulator.delegate.event.Function)4 ProcessInstanceCreateTransformer (org.activiti.crystalball.simulator.delegate.event.impl.ProcessInstanceCreateTransformer)4 UserTaskCompleteTransformer (org.activiti.crystalball.simulator.delegate.event.impl.UserTaskCompleteTransformer)4 ActivitiEntityEvent (org.activiti.engine.delegate.event.ActivitiEntityEvent)4 ActivitiCancelledEvent (org.activiti.engine.delegate.event.ActivitiCancelledEvent)3 ActivitiProcessCancelledEventImpl (org.activiti.engine.delegate.event.impl.ActivitiProcessCancelledEventImpl)3 TimerEntity (org.activiti.engine.impl.persistence.entity.TimerEntity)3 DeploymentCreateTransformer (org.activiti.crystalball.simulator.delegate.event.impl.DeploymentCreateTransformer)2 ActivitiErrorEvent (org.activiti.engine.delegate.event.ActivitiErrorEvent)2 ActivitiActivityCancelledEventImpl (org.activiti.engine.delegate.event.impl.ActivitiActivityCancelledEventImpl)2