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());
}
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());
}
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());
}
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);
}
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;
}
Aggregations