@Deployment(resources={"org/activiti/engine/test/api/event/CancelCallActivityByMessageTest.testActivityMessageBoundaryEventsOnCallActivity.bpmn20.xml","org/activiti/engine/test/api/event/CancelCallActivityByMessageTest.testActivityMessageBoundaryEventsExternalSubProcess.bpmn20.xml"}) public void testCancelCallActivityByMessage() throws Exception {
  CallActivityByMessageEventListener myEventListener=new CallActivityByMessageEventListener();
  processEngineConfiguration.getEventDispatcher().addEventListener(myEventListener);
  ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("messageEventOnCallActivity");
  assertNotNull(processInstance);
  Execution executionWithMessageEvent=runtimeService.createExecutionQuery().activityId("cancelBoundaryMessageEvent").singleResult();
  assertNotNull(executionWithMessageEvent);
  runtimeService.messageEventReceived("cancel",executionWithMessageEvent.getId());
  ActivitiEntityEvent entityEvent=(ActivitiEntityEvent)myEventListener.getEventsReceived().get(0);
  assertEquals(ActivitiEventType.ENTITY_CREATED,entityEvent.getType());
  ExecutionEntity executionEntity=(ExecutionEntity)entityEvent.getEntity();
  assertNull(executionEntity.getParentId());
  String processExecutionId=executionEntity.getId();
  entityEvent=(ActivitiEntityEvent)myEventListener.getEventsReceived().get(1);
  assertEquals(ActivitiEventType.ENTITY_CREATED,entityEvent.getType());
  executionEntity=(ExecutionEntity)entityEvent.getEntity();
  assertNotNull(executionEntity.getParentId());
  assertEquals(processExecutionId,executionEntity.getParentId());
  ActivitiEvent activitiEvent=(ActivitiEvent)myEventListener.getEventsReceived().get(2);
  assertEquals(ActivitiEventType.PROCESS_STARTED,activitiEvent.getType());
  ActivitiActivityEvent activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(3);
  assertEquals(ActivitiEventType.ACTIVITY_STARTED,activityEvent.getType());
  assertEquals("startEvent",activityEvent.getActivityType());
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(4);
  assertEquals(ActivitiEventType.ACTIVITY_COMPLETED,activityEvent.getType());
  assertEquals("startEvent",activityEvent.getActivityType());
  entityEvent=(ActivitiEntityEvent)myEventListener.getEventsReceived().get(5);
  assertEquals(ActivitiEventType.ENTITY_CREATED,entityEvent.getType());
  executionEntity=(ExecutionEntity)entityEvent.getEntity();
  assertEquals("cancelBoundaryMessageEvent",executionEntity.getActivityId());
  String boundaryExecutionId=executionEntity.getId();
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(6);
  assertEquals(ActivitiEventType.ACTIVITY_STARTED,activityEvent.getType());
  assertEquals("callActivity1",activityEvent.getActivityId());
  entityEvent=(ActivitiEntityEvent)myEventListener.getEventsReceived().get(7);
  assertEquals(ActivitiEventType.ENTITY_CREATED,entityEvent.getType());
  executionEntity=(ExecutionEntity)entityEvent.getEntity();
  assertNull(executionEntity.getParentId());
  assertEquals(executionEntity.getId(),executionEntity.getProcessInstanceId());
  entityEvent=(ActivitiEntityEvent)myEventListener.getEventsReceived().get(8);
  assertEquals(ActivitiEventType.ENTITY_CREATED,entityEvent.getType());
  executionEntity=(ExecutionEntity)entityEvent.getEntity();
  assertEquals("calledSubprocessTask",executionEntity.getActivityId());
  activitiEvent=(ActivitiEvent)myEventListener.getEventsReceived().get(9);
  assertEquals(ActivitiEventType.PROCESS_STARTED,activitiEvent.getType());
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(10);
  assertEquals(ActivitiEventType.ACTIVITY_STARTED,activityEvent.getType());
  assertEquals("startEvent",activityEvent.getActivityType());
  assertEquals("startevent2",activityEvent.getActivityId());
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(11);
  assertEquals(ActivitiEventType.ACTIVITY_COMPLETED,activityEvent.getType());
  assertEquals("startEvent",activityEvent.getActivityType());
  assertEquals("startevent2",activityEvent.getActivityId());
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(12);
  assertEquals(ActivitiEventType.ACTIVITY_STARTED,activityEvent.getType());
  assertEquals("calledSubprocessTask",activityEvent.getActivityId());
  assertEquals("userTask",activityEvent.getActivityType());
  entityEvent=(ActivitiEntityEvent)myEventListener.getEventsReceived().get(13);
  assertEquals(ActivitiEventType.TASK_CREATED,entityEvent.getType());
  TaskEntity taskEntity=(TaskEntity)entityEvent.getEntity();
  assertEquals("Sample User Task2 in External",taskEntity.getName());
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(14);
  assertEquals(ActivitiEventType.ACTIVITY_CANCELLED,activityEvent.getType());
  assertEquals("callActivity",activityEvent.getActivityType());
  assertEquals(boundaryExecutionId,activityEvent.getExecutionId());
  ActivitiActivityCancelledEvent taskCancelledEvent=(ActivitiActivityCancelledEvent)myEventListener.getEventsReceived().get(15);
  assertEquals(ActivitiEventType.ACTIVITY_CANCELLED,taskCancelledEvent.getType());
  assertEquals(taskEntity.getName(),taskCancelledEvent.getActivityName());
  ActivitiCancelledEvent processCancelledEvent=(ActivitiCancelledEvent)myEventListener.getEventsReceived().get(16);
  assertEquals(ActivitiEventType.PROCESS_CANCELLED,processCancelledEvent.getType());
  assertEquals(processCancelledEvent.getProcessInstanceId(),processCancelledEvent.getExecutionId());
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(17);
  assertEquals(ActivitiEventType.ACTIVITY_COMPLETED,activityEvent.getType());
  assertEquals("boundaryEvent",activityEvent.getActivityType());
  assertEquals("cancelBoundaryMessageEvent",activityEvent.getActivityId());
  activityEvent=(ActivitiActivityEvent)myEventListener.getEventsReceived().get(18);
  assertEquals(ActivitiEventType.ACTIVITY_STARTED,activityEvent.getType());
  assertEquals("usertask1",activityEvent.getActivityId());
  assertEquals("userTask",activityEvent.getActivityType());
  entityEvent=(ActivitiEntityEvent)myEventListener.getEventsReceived().get(19);
  assertEquals(ActivitiEventType.TASK_CREATED,entityEvent.getType());
  taskEntity=(TaskEntity)entityEvent.getEntity();
  assertEquals("Sample User Task1",taskEntity.getName());
  assertEquals(20,myEventListener.getEventsReceived().size());
}
