Search in sources :

Example 26 with ActivitiEvent

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

the class ProcessDefinitionEventsTest method assertSequence.

private void assertSequence(ActivitiEntityEvent before, ActivitiEntityEvent after) {
    int beforeIndex = 0;
    int afterIndex = 0;
    for (int index = 0; index < listener.getEventsReceived().size(); index++) {
        ActivitiEvent activitiEvent = listener.getEventsReceived().get(index);
        if (isEqual(before, activitiEvent))
            beforeIndex = index;
        if (isEqual(after, activitiEvent))
            afterIndex = index;
    }
    assertTrue(beforeIndex < afterIndex);
}
Also used : ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent)

Example 27 with ActivitiEvent

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

the class ProcessDefinitionScopedEventListenerDefinitionTest method testProcessDefinitionListenerDefinition.

/**
	 * Test to verify listeners defined in the BPMN xml are added to the process
	 * definition and are active.
	 */
@Deployment
public void testProcessDefinitionListenerDefinition() throws Exception {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testEventListeners");
    assertNotNull(testListenerBean);
    Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
    taskService.complete(task.getId());
    // Check if the listener (defined as bean) received events (only creation, not other events)
    assertFalse(testListenerBean.getEventsReceived().isEmpty());
    for (ActivitiEvent event : testListenerBean.getEventsReceived()) {
        assertEquals(ActivitiEventType.ENTITY_CREATED, event.getType());
    }
    // First event received should be creation of Process-definition
    assertTrue(testListenerBean.getEventsReceived().get(0) instanceof ActivitiEntityEvent);
    ActivitiEntityEvent event = (ActivitiEntityEvent) testListenerBean.getEventsReceived().get(0);
    assertTrue(event.getEntity() instanceof ProcessDefinition);
    assertEquals(processInstance.getProcessDefinitionId(), ((ProcessDefinition) event.getEntity()).getId());
    // First event received should be creation of Process-instance
    assertTrue(testListenerBean.getEventsReceived().get(1) instanceof ActivitiEntityEvent);
    event = (ActivitiEntityEvent) testListenerBean.getEventsReceived().get(1);
    assertTrue(event.getEntity() instanceof ProcessInstance);
    assertEquals(processInstance.getId(), ((ProcessInstance) event.getEntity()).getId());
    // Check if listener, defined by classname, received all events
    List<ActivitiEvent> events = StaticTestActivitiEventListener.getEventsReceived();
    assertFalse(events.isEmpty());
    boolean insertFound = false;
    boolean deleteFound = false;
    for (ActivitiEvent e : events) {
        if (ActivitiEventType.ENTITY_CREATED == e.getType()) {
            insertFound = true;
        } else if (ActivitiEventType.ENTITY_DELETED == e.getType()) {
            deleteFound = true;
        }
    }
    assertTrue(insertFound);
    assertTrue(deleteFound);
}
Also used : Task(org.activiti.engine.task.Task) ActivitiEntityEvent(org.activiti.engine.delegate.event.ActivitiEntityEvent) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) Deployment(org.activiti.engine.test.Deployment)

Example 28 with ActivitiEvent

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

the class TypedEventListenersConfigurationTest method testEventListenerConfiguration.

public void testEventListenerConfiguration() {
    // Fetch the listener to check received events
    TestActivitiEventListener listener = (TestActivitiEventListener) processEngineConfiguration.getBeans().get("eventListener");
    assertNotNull(listener);
    // Clear any events received (eg. engine initialisation)
    listener.clearEventsReceived();
    // Dispath a custom event
    ActivitiEvent event = new ActivitiEventImpl(ActivitiEventType.CUSTOM);
    processEngineConfiguration.getEventDispatcher().dispatchEvent(event);
    assertEquals(1, listener.getEventsReceived().size());
    assertEquals(event, listener.getEventsReceived().get(0));
    listener.clearEventsReceived();
    // Dispatch another event the listener is registered for
    event = new ActivitiEventImpl(ActivitiEventType.ENTITY_DELETED);
    processEngineConfiguration.getEventDispatcher().dispatchEvent(event);
    event = new ActivitiEventImpl(ActivitiEventType.ENTITY_UPDATED);
    processEngineConfiguration.getEventDispatcher().dispatchEvent(event);
    assertEquals(2, listener.getEventsReceived().size());
    assertEquals(ActivitiEventType.ENTITY_DELETED, listener.getEventsReceived().get(0).getType());
    assertEquals(ActivitiEventType.ENTITY_UPDATED, listener.getEventsReceived().get(1).getType());
    listener.clearEventsReceived();
    // Dispatch an event that is NOT part of the types configured
    event = new ActivitiEventImpl(ActivitiEventType.ENTITY_CREATED);
    processEngineConfiguration.getEventDispatcher().dispatchEvent(event);
    assertTrue(listener.getEventsReceived().isEmpty());
}
Also used : ActivitiEventImpl(org.activiti.engine.delegate.event.impl.ActivitiEventImpl) TestActivitiEventListener(org.activiti.engine.test.api.event.TestActivitiEventListener) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent)

Example 29 with ActivitiEvent

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

the class StartTimerEventRepeatWithEndExpressionTest 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/StartTimerEventRepeatWithEndExpressionTest.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 to 11 dec -> the system will execute the pending job and will create a new one
    moveByMinutes(60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
        fail("there must be a pending job because the endDate is not reached yet");
    } catch (Exception e) {
    //expected failure
    }
    // After the first startEvent Execution should be one process instance started
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(1, processInstances.size());
    // one task to be executed (the userTask "Task A")
    tasks = taskService.createTaskQuery().list();
    assertEquals(1, 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());
    dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 12, 0, 0, 0);
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    // ADVANCE THE CLOCK SO THE END DATE WILL BE REACHED
    // 12 dec (last execution)
    moveByMinutes(60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
    } catch (Exception e) {
        fail("Because the endDate is reached it will not be executed other jobs");
    }
    // After the second startEvent Execution should have 2 process instances started
    // (since the first one was not completed)
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(2, processInstances.size());
    // Because the endDate 12.dec.2025 is reached
    // the current job will be deleted after execution and a new one will not be created.
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    // 2 tasks to be executed (the userTask "Task A")
    // one task for each process instance
    tasks = taskService.createTaskQuery().list();
    assertEquals(2, tasks.size());
    // 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++;
        }
    }
    //2 timers fired
    assertEquals(2, timerFiredCount);
    //2 jobs created
    assertEquals(2, eventCreatedCount);
    //2 jobs deleted
    assertEquals(2, 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)

Example 30 with ActivitiEvent

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

the class StartTimerEventRepeatWithEndTest 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/StartTimerEventRepeatWithEndTest.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 to 11 dec -> the system will execute the pending job and will create a new one
    moveByMinutes(60 * 25);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
        fail("there must be a pending job because the endDate is not reached yet");
    } catch (Exception e) {
    //expected failure
    }
    jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    // After the first startEvent Execution should be one process instance started
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(1, processInstances.size());
    // one task to be executed (the userTask "Task A")
    tasks = taskService.createTaskQuery().list();
    assertEquals(1, 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());
    dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 12, 0, 0, 0);
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    // ADVANCE THE CLOCK SO THE END DATE WILL BE REACHED
    // 12 dec (last execution)
    moveByMinutes(60 * 25);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
    } catch (Exception e) {
        fail("Because the endDate is reached it will not be executed other jobs");
    }
    // After the second startEvent Execution should have 2 process instances started
    // (since the first one was not completed)
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(2, processInstances.size());
    // Because the endDate 12.dec.2025 is reached
    // the current job will be deleted after execution and a new one will not be created.
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    // 2 tasks to be executed (the userTask "Task A")
    // one task for each process instance
    tasks = taskService.createTaskQuery().list();
    assertEquals(2, tasks.size());
    // 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++;
        }
    }
    //2 timers fired
    assertEquals(2, timerFiredCount);
    //2 jobs created
    assertEquals(2, eventCreatedCount);
    //2 jobs deleted
    assertEquals(2, 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