Search in sources :

Example 11 with SiteMemberData

use of org.alfresco.bm.site.SiteMemberData in project records-management by Alfresco.

the class CreateRMSiteMembersUnitTest method testScheduleSiteMembers.

@Test
public void testScheduleSiteMembers() throws Exception {
    int batchSize = 100;
    createRMSiteMembers.setBatchSize(batchSize);
    SiteMemberData mockedSiteMemberData1 = mock(SiteMemberData.class);
    SiteMemberData mockedSiteMemberData2 = mock(SiteMemberData.class);
    when(mockedSiteMemberData1.getSiteId()).thenReturn(PATH_SNIPPET_RM_SITE_ID);
    when(mockedSiteMemberData2.getSiteId()).thenReturn(PATH_SNIPPET_RM_SITE_ID);
    String username1 = "user1";
    String username2 = "user2";
    when(mockedSiteMemberData1.getUsername()).thenReturn(username1);
    when(mockedSiteMemberData2.getUsername()).thenReturn(username2);
    when(mockedSiteDataService.getSiteMembers(PATH_SNIPPET_RM_SITE_ID, DataCreationState.NotScheduled, null, 0, batchSize)).thenReturn(Arrays.asList(mockedSiteMemberData1, mockedSiteMemberData2));
    SiteData mockedSiteData = mock(SiteData.class);
    when(mockedSiteDataService.getSite(PATH_SNIPPET_RM_SITE_ID)).thenReturn(mockedSiteData);
    EventResult result = createRMSiteMembers.processEvent(null);
    verify(mockedSiteDataService, times(2)).getSite(PATH_SNIPPET_RM_SITE_ID);
    verify(mockedSiteDataService, never()).setSiteMemberCreationState(eq(PATH_SNIPPET_RM_SITE_ID), any(String.class), eq(DataCreationState.Failed));
    verify(mockedSiteDataService, times(2)).setSiteMemberCreationState(eq(PATH_SNIPPET_RM_SITE_ID), any(String.class), eq(DataCreationState.Scheduled));
    assertEquals(true, result.isSuccess());
    assertEquals(MessageFormat.format(CreateRMSiteMembers.SCHEDULED_MEMBERS_MSG_TEMPLATE, 2), result.getData());
    List<Event> nextEvents = result.getNextEvents();
    assertEquals(3, nextEvents.size());
    // event1
    Event event = nextEvents.get(0);
    assertEquals(createRMSiteMembers.getEventNameCreateRMSiteMember(), event.getName());
    DBObject dataObj = (DBObject) event.getData();
    assertNotNull(dataObj);
    assertEquals(username1, (String) dataObj.get(CreateRMSiteMember.FIELD_USERNAME));
    // event2
    event = nextEvents.get(1);
    assertEquals(createRMSiteMembers.getEventNameCreateRMSiteMember(), event.getName());
    dataObj = (DBObject) event.getData();
    assertNotNull(dataObj);
    assertEquals(username2, (String) dataObj.get(CreateRMSiteMember.FIELD_USERNAME));
    // self event
    assertEquals(createRMSiteMembers.getEventNameCreateRMSiteMembers(), nextEvents.get(2).getName());
}
Also used : SiteData(org.alfresco.bm.site.SiteData) SiteMemberData(org.alfresco.bm.site.SiteMemberData) EventResult(org.alfresco.bm.event.EventResult) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 12 with SiteMemberData

use of org.alfresco.bm.site.SiteMemberData in project records-management by Alfresco.

the class PrepareRMSiteMembersUnitTest method testNoNewUsersAvailable.

@Test
public void testNoNewUsersAvailable() throws Exception {
    int userCount = 2;
    String user1 = "user1";
    String user2 = "user2";
    prepareRMSiteMembers.setRole("Administrator");
    prepareRMSiteMembers.setAssignRMRoleToUsers(true);
    prepareRMSiteMembers.setUserCount(userCount);
    UserData mockedUserData1 = mock(UserData.class);
    when(mockedUserData1.getUsername()).thenReturn(user1);
    UserData mockedUserData2 = mock(UserData.class);
    when(mockedUserData2.getUsername()).thenReturn(user2);
    List<UserData> users = Arrays.asList(mockedUserData1, mockedUserData2);
    when(mockedUserDataService.getUsersByCreationState(DataCreationState.Created, 0, 100)).thenReturn(users);
    when(mockedSiteDataService.getSiteMembers(PATH_SNIPPET_RM_SITE_ID, DataCreationState.Created, null, 0, userCount)).thenReturn(new ArrayList<>());
    SiteMemberData mockedSiteMemberData1 = mock(SiteMemberData.class);
    SiteMemberData mockedSiteMemberData2 = mock(SiteMemberData.class);
    when(mockedSiteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, user1)).thenReturn(mockedSiteMemberData1);
    when(mockedSiteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, user2)).thenReturn(mockedSiteMemberData2);
    EventResult result = prepareRMSiteMembers.processEvent(null);
    verify(mockedSiteDataService, never()).addSiteMember(any(SiteMemberData.class));
    assertEquals(true, result.isSuccess());
    assertEquals(PrepareRMSiteMembers.NO_NEW_USERS_FOUND_MSG, result.getData());
    List<Event> nextEvents = result.getNextEvents();
    assertEquals(1, nextEvents.size());
    assertEquals(prepareRMSiteMembers.getEventNameContinueLoadingData(), nextEvents.get(0).getName());
}
Also used : SiteMemberData(org.alfresco.bm.site.SiteMemberData) EventResult(org.alfresco.bm.event.EventResult) UserData(org.alfresco.bm.user.UserData) Event(org.alfresco.bm.event.Event) Test(org.junit.Test)

Example 13 with SiteMemberData

use of org.alfresco.bm.site.SiteMemberData in project records-management by Alfresco.

the class PrepareRMSiteMembersUnitTest method testLessThanRequestedNumeberOfUsersFound.

@Test
public void testLessThanRequestedNumeberOfUsersFound() throws Exception {
    int userCount = 2;
    String user1 = "user1";
    String user2 = "user2";
    prepareRMSiteMembers.setRole("Administrator");
    prepareRMSiteMembers.setAssignRMRoleToUsers(true);
    prepareRMSiteMembers.setUserCount(userCount);
    UserData mockedUserData1 = mock(UserData.class);
    when(mockedUserData1.getUsername()).thenReturn(user1);
    UserData mockedUserData2 = mock(UserData.class);
    when(mockedUserData2.getUsername()).thenReturn(user2);
    List<UserData> users = Arrays.asList(mockedUserData1, mockedUserData2);
    when(mockedUserDataService.getUsersByCreationState(DataCreationState.Created, 0, 100)).thenReturn(users);
    when(mockedSiteDataService.getSiteMembers(PATH_SNIPPET_RM_SITE_ID, DataCreationState.Created, null, 0, userCount)).thenReturn(new ArrayList<>());
    SiteMemberData mockedSiteMemberData1 = mock(SiteMemberData.class);
    when(mockedSiteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, user1)).thenReturn(mockedSiteMemberData1);
    when(mockedSiteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, user2)).thenReturn(null);
    EventResult result = prepareRMSiteMembers.processEvent(null);
    verify(mockedSiteDataService, times(1)).addSiteMember(any(SiteMemberData.class));
    assertEquals(true, result.isSuccess());
    assertEquals(MessageFormat.format(PrepareRMSiteMembers.PREPARED_INCOMPLETE_MSG_TEMPLATE, 1, userCount), result.getData());
    List<Event> nextEvents = result.getNextEvents();
    assertEquals(1, nextEvents.size());
    assertEquals(prepareRMSiteMembers.getEventNameSiteMembersPrepared(), nextEvents.get(0).getName());
}
Also used : SiteMemberData(org.alfresco.bm.site.SiteMemberData) EventResult(org.alfresco.bm.event.EventResult) UserData(org.alfresco.bm.user.UserData) Event(org.alfresco.bm.event.Event) Test(org.junit.Test)

Example 14 with SiteMemberData

use of org.alfresco.bm.site.SiteMemberData in project records-management by Alfresco.

the class CreateRMSiteMembers method processEvent.

@Override
protected EventResult processEvent(Event event) throws Exception {
    List<Event> nextEvents = new ArrayList<Event>();
    // Schedule events for each site member to be created
    int numSitesMembers = 0;
    List<SiteMemberData> pendingSiteMembers = siteDataService.getSiteMembers(PATH_SNIPPET_RM_SITE_ID, DataCreationState.NotScheduled, null, 0, batchSize);
    if (pendingSiteMembers.isEmpty()) {
        // There is nothing more to do
        Event doneEvent = new Event(eventNameRMSiteMembersCreated, System.currentTimeMillis(), null);
        nextEvents.add(doneEvent);
    } else {
        long nextEventTime = System.currentTimeMillis();
        for (SiteMemberData siteMember : pendingSiteMembers) {
            // Do we need to schedule it?
            String siteId = siteMember.getSiteId();
            String username = siteMember.getUsername();
            SiteData site = siteDataService.getSite(siteId);
            // Ignore RM site not been prepared.
            if (site == null) {
                // This site member cannot be created, so we mark it as an immediate failure
                siteDataService.setSiteMemberCreationState(siteId, username, DataCreationState.Failed);
                continue;
            }
            // RM Site created
            nextEventTime += memberCreationDelay;
            DBObject dataObj = new BasicDBObject().append(CreateRMSiteMember.FIELD_USERNAME, username);
            Event nextEvent = new Event(eventNameCreateRMSiteMember, nextEventTime, dataObj);
            nextEvents.add(nextEvent);
            numSitesMembers++;
            // The member creation is now scheduled
            siteDataService.setSiteMemberCreationState(siteId, username, DataCreationState.Scheduled);
        }
        // Reschedule for the next batch (might be zero next time)
        Event self = new Event(eventNameCreateRMSiteMembers, nextEventTime + memberCreationDelay, null);
        nextEvents.add(self);
    }
    // Return messages + next events
    return new EventResult(MessageFormat.format(SCHEDULED_MEMBERS_MSG_TEMPLATE, numSitesMembers), nextEvents);
}
Also used : SiteData(org.alfresco.bm.site.SiteData) BasicDBObject(com.mongodb.BasicDBObject) SiteMemberData(org.alfresco.bm.site.SiteMemberData) EventResult(org.alfresco.bm.event.EventResult) ArrayList(java.util.ArrayList) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 15 with SiteMemberData

use of org.alfresco.bm.site.SiteMemberData in project records-management by Alfresco.

the class PrepareRMSiteMembers method processEvent.

@Override
protected EventResult processEvent(Event event) throws Exception {
    if (!isAssignRMRoleToUsers()) {
        return new EventResult(ASSIGNATION_NOT_WANTED_MSG, new Event(getEventNameContinueLoadingData(), null));
    }
    if (getUserCount() <= 0) {
        return new EventResult(NO_USERS_WANTED_MSG, new Event(getEventNameContinueLoadingData(), null));
    }
    int membersCount = 0;
    int userSkip = 0;
    final int userPageSize = 100;
    List<UserData> users = userDataService.getUsersByCreationState(DataCreationState.Created, userSkip, userPageSize);
    if (users.isEmpty()) {
        return new EventResult(NO_USERS_AVAILABLE_MSG, new Event(getEventNameContinueLoadingData(), null));
    }
    String siteId = PATH_SNIPPET_RM_SITE_ID;
    // How many users do we have for RM site?
    int currentSiteUsersCount = siteDataService.getSiteMembers(siteId, DataCreationState.Created, null, 0, getUserCount()).size();
    int siteUsersToCreate = getUserCount() - currentSiteUsersCount;
    // Keep going while we attempt to find a user to use
    while (!users.isEmpty() && membersCount < siteUsersToCreate) {
        for (UserData user : users) {
            if (membersCount == siteUsersToCreate) {
                break;
            }
            // Check if the user is already a member
            String username = user.getUsername();
            SiteMemberData siteMember = siteDataService.getSiteMember(siteId, username);
            if (siteMember != null) {
                // The user is already a set to be a site member (we could hit site manager)
                continue;
            }
            // Create the membership
            siteMember = new SiteMemberData();
            siteMember.setCreationState(DataCreationState.NotScheduled);
            RMRole randomRole = getRandomRole();
            siteMember.setRole(randomRole.toString());
            siteMember.setSiteId(siteId);
            siteMember.setUsername(username);
            siteDataService.addSiteMember(siteMember);
            membersCount++;
        }
        userSkip += users.size();
        users = userDataService.getUsersByCreationState(DataCreationState.Created, userSkip, userPageSize);
    }
    if (membersCount == 0) {
        return new EventResult(NO_NEW_USERS_FOUND_MSG, new Event(getEventNameContinueLoadingData(), null));
    }
    String msg = MessageFormat.format(PREPARED_MSG_TEMPLATE, membersCount);
    if (membersCount < siteUsersToCreate) {
        msg = MessageFormat.format(PREPARED_INCOMPLETE_MSG_TEMPLATE, membersCount, siteUsersToCreate);
    }
    // We need an event to mark completion
    Event outputEvent = new Event(eventNameSiteMembersPrepared, null);
    // Create result
    EventResult result = new EventResult(msg, Collections.singletonList(outputEvent));
    // Done
    logger.debug(msg);
    return result;
}
Also used : RMRole(org.alfresco.bm.dataload.rm.role.RMRole) SiteMemberData(org.alfresco.bm.site.SiteMemberData) EventResult(org.alfresco.bm.event.EventResult) UserData(org.alfresco.bm.user.UserData) Event(org.alfresco.bm.event.Event)

Aggregations

SiteMemberData (org.alfresco.bm.site.SiteMemberData)17 Event (org.alfresco.bm.event.Event)13 EventResult (org.alfresco.bm.event.EventResult)12 Test (org.junit.Test)11 DBObject (com.mongodb.DBObject)8 SiteData (org.alfresco.bm.site.SiteData)8 UserData (org.alfresco.bm.user.UserData)6 RMUserAPI (org.alfresco.rest.rm.community.requests.gscore.api.RMUserAPI)3 UserModel (org.alfresco.utility.model.UserModel)3 StopWatch (org.apache.commons.lang3.time.StopWatch)3 RMSite (org.alfresco.rest.rm.community.model.site.RMSite)2 RMSiteAPI (org.alfresco.rest.rm.community.requests.gscore.api.RMSiteAPI)2 Logger (org.slf4j.Logger)2 BasicDBObject (com.mongodb.BasicDBObject)1 ArrayList (java.util.ArrayList)1 FolderData (org.alfresco.bm.cm.FolderData)1 RMRole (org.alfresco.bm.dataload.rm.role.RMRole)1 EventDataObject (org.alfresco.bm.event.selector.EventDataObject)1 EventProcessorResponse (org.alfresco.bm.event.selector.EventProcessorResponse)1 RMRestWrapper (org.alfresco.rest.core.RMRestWrapper)1