// Add membership
membershipListener.clearEventsReceived();
identityService.createMembership("kermit", "sales");
assertEquals(1, membershipListener.getEventsReceived().size());
assertTrue(membershipListener.getEventsReceived().get(0) instanceof ActivitiMembershipEvent);
ActivitiMembershipEvent event = (ActivitiMembershipEvent) membershipListener.getEventsReceived().get(0);
assertEquals(ActivitiEventType.MEMBERSHIP_CREATED, event.getType());
assertEquals("sales", event.getGroupId());
assertEquals("kermit", event.getUserId());
assertNull(event.getExecutionId());
assertNull(event.getProcessDefinitionId());
assertNull(event.getProcessInstanceId());
membershipListener.clearEventsReceived();
// Delete membership
identityService.deleteMembership("kermit", "sales");
assertEquals(1, membershipListener.getEventsReceived().size());
assertTrue(membershipListener.getEventsReceived().get(0) instanceof ActivitiMembershipEvent);
event = (ActivitiMembershipEvent) membershipListener.getEventsReceived().get(0);
assertEquals(ActivitiEventType.MEMBERSHIP_DELETED, event.getType());
assertEquals("sales", event.getGroupId());
assertEquals("kermit", event.getUserId());
assertNull(event.getExecutionId());
assertNull(event.getProcessDefinitionId());
assertNull(event.getProcessInstanceId());
membershipListener.clearEventsReceived();
// Deleting group will dispatch an event, informing ALL memberships are deleted
identityService.createMembership("kermit", "sales");
membershipListener.clearEventsReceived();
identityService.deleteGroup(group.getId());
assertEquals(2, membershipListener.getEventsReceived().size());
assertTrue(membershipListener.getEventsReceived().get(0) instanceof ActivitiMembershipEvent);
event = (ActivitiMembershipEvent) membershipListener.getEventsReceived().get(0);
assertEquals(ActivitiEventType.MEMBERSHIPS_DELETED, event.getType());
assertEquals("sales", event.getGroupId());
assertNull(event.getUserId());
assertNull(event.getExecutionId());
assertNull(event.getProcessDefinitionId());
assertNull(event.getProcessInstanceId());
membershipListener.clearEventsReceived();
} finally {
processEngineConfiguration.getEventDispatcher().removeEventListener(membershipListener);
if(user != null) {
identityService.deleteUser(user.getId());