/** 
 * Test create, update and delete events of process instances.
 */
@Deployment(resources={"org/activiti/engine/test/api/runtime/nestedSubProcess.bpmn20.xml","org/activiti/engine/test/api/runtime/subProcess.bpmn20.xml"}) public void testSubProcessInstanceEvents() throws Exception {
  ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("nestedSimpleSubProcess");
  assertNotNull(processInstance);
  String processDefinitionId=processInstance.getProcessDefinitionId();
  assertEquals(9,listener.getEventsReceived().size());
  assertTrue(listener.getEventsReceived().get(0) instanceof ActivitiEntityEvent);
  ActivitiEntityEvent event=(ActivitiEntityEvent)listener.getEventsReceived().get(0);
  assertEquals(ActivitiEventType.ENTITY_CREATED,event.getType());
  assertEquals(processInstance.getId(),((ProcessInstance)event.getEntity()).getId());
  assertEquals(processInstance.getId(),event.getProcessInstanceId());
  assertEquals(processInstance.getId(),event.getExecutionId());
  assertEquals(processDefinitionId,event.getProcessDefinitionId());
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(1);
  String processExecutionId=event.getExecutionId();
  assertEquals(ActivitiEventType.ENTITY_INITIALIZED,event.getType());
  assertEquals(processInstance.getId(),event.getProcessInstanceId());
  assertEquals(processInstance.getId(),processExecutionId);
  assertEquals(processDefinitionId,event.getProcessDefinitionId());
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(2);
  processExecutionId=event.getExecutionId();
  assertEquals(ActivitiEventType.ENTITY_CREATED,event.getType());
  assertEquals(processInstance.getId(),event.getProcessInstanceId());
  assertNotEquals(processInstance.getId(),processExecutionId);
  assertEquals(processDefinitionId,event.getProcessDefinitionId());
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(3);
  assertEquals(ActivitiEventType.ENTITY_INITIALIZED,event.getType());
  assertEquals(processInstance.getId(),event.getProcessInstanceId());
  assertNotEquals(processInstance.getId(),((ExecutionEntity)event.getEntity()).getId());
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(4);
  assertEquals(ActivitiEventType.PROCESS_STARTED,event.getType());
  assertEquals(processInstance.getId(),event.getProcessInstanceId());
  assertEquals(processInstance.getProcessDefinitionId(),event.getProcessDefinitionId());
  assertTrue(event instanceof ActivitiProcessStartedEvent);
  assertNull(((ActivitiProcessStartedEvent)event).getNestedProcessDefinitionId());
  assertNull(((ActivitiProcessStartedEvent)event).getNestedProcessInstanceId());
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(5);
  assertEquals(ActivitiEventType.ENTITY_CREATED,event.getType());
  ExecutionEntity subProcessEntity=(ExecutionEntity)event.getEntity();
  assertEquals(processExecutionId,subProcessEntity.getSuperExecutionId());
  String subProcessInstanceId=subProcessEntity.getProcessInstanceId();
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(6);
  assertEquals(ActivitiEventType.ENTITY_CREATED,event.getType());
  assertEquals(subProcessInstanceId,event.getProcessInstanceId());
  assertNotEquals(subProcessInstanceId,event.getExecutionId());
  String subProcessDefinitionId=((ExecutionEntity)event.getEntity()).getProcessDefinitionId();
  assertNotNull(subProcessDefinitionId);
  ProcessDefinition subProcessDefinition=repositoryService.getProcessDefinition(subProcessDefinitionId);
  assertEquals("simpleSubProcess",subProcessDefinition.getKey());
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(7);
  assertEquals(ActivitiEventType.ENTITY_INITIALIZED,event.getType());
  assertEquals(subProcessInstanceId,event.getProcessInstanceId());
  assertNotEquals(subProcessInstanceId,event.getExecutionId());
  subProcessDefinitionId=((ExecutionEntity)event.getEntity()).getProcessDefinitionId();
  assertNotNull(subProcessDefinitionId);
  event=(ActivitiEntityEvent)listener.getEventsReceived().get(8);
  assertEquals(ActivitiEventType.PROCESS_STARTED,event.getType());
  assertEquals(subProcessInstanceId,event.getProcessInstanceId());
  assertEquals(subProcessDefinitionId,event.getProcessDefinitionId());
  assertTrue(event instanceof ActivitiProcessStartedEvent);
  assertEquals(processDefinitionId,((ActivitiProcessStartedEvent)event).getNestedProcessDefinitionId());
  assertEquals(processInstance.getId(),((ActivitiProcessStartedEvent)event).getNestedProcessInstanceId());
  listener.clearEventsReceived();
}
