Search in sources :

Example 1 with ActivitiMembershipEvent

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

the class GroupEventsTest method testGroupMembershipEvents.

/**
	 * Test create, update and delete events of Groups.
	 */
public void testGroupMembershipEvents() throws Exception {
    TestActivitiEventListener membershipListener = new TestActivitiEventListener();
    processEngineConfiguration.getEventDispatcher().addEventListener(membershipListener);
    User user = null;
    Group group = null;
    try {
        user = identityService.newUser("kermit");
        identityService.saveUser(user);
        group = identityService.newGroup("sales");
        identityService.saveGroup(group);
        // 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());
        }
        if (group != null) {
            identityService.deleteGroup(group.getId());
        }
    }
}
Also used : Group(org.activiti.engine.identity.Group) User(org.activiti.engine.identity.User) ActivitiMembershipEvent(org.activiti.engine.delegate.event.ActivitiMembershipEvent)

Aggregations

ActivitiMembershipEvent (org.activiti.engine.delegate.event.ActivitiMembershipEvent)1 Group (org.activiti.engine.identity.Group)1 User (org.activiti.engine.identity.User)1