Search in sources :

Example 11 with UserData

use of org.alfresco.bm.user.UserData in project records-management by Alfresco.

the class PrepareRMSiteUnitTest method testCreateRMSiteWhenRMSiteDoesNotExistAndNotLoadedInDb.

@Test
public void testCreateRMSiteWhenRMSiteDoesNotExistAndNotLoadedInDb() throws Exception {
    String username = "bob";
    String password = "secret";
    prepareRMSite.setUsername(username);
    prepareRMSite.setPassword(password);
    when(mockedRestAPIFactory.getRMSiteAPI(any(UserModel.class))).thenReturn(mockedRMSiteAPI);
    when(mockedRMSiteAPI.existsRMSite()).thenReturn(false);
    People mockedPeople = mock(People.class);
    RestCoreAPI mockedCoreAPI = mock(RestCoreAPI.class);
    when(mockedCoreAPI.usingAuthUser()).thenReturn(mockedPeople);
    when(mockedRestCoreAPI.withCoreAPI()).thenReturn(mockedCoreAPI);
    when(mockedRestCoreAPI.getStatusCode()).thenReturn(HttpStatus.OK.toString());
    EventResult result = prepareRMSite.processEvent(null);
    ArgumentCaptor<UserData> userData = forClass(UserData.class);
    verify(mockedUserDataService).createNewUser(userData.capture());
    // Check events
    assertEquals(true, result.isSuccess());
    List<Event> events = result.getNextEvents();
    assertEquals(1, events.size());
    Event event = events.get(0);
    assertEquals(DEFAULT_EVENT_NAME_RM_SITE_PREPARED, event.getName());
    DBObject data = (DBObject) event.getData();
    notNull(data);
    assertEquals(RM_SITE_ID, (String) data.get(FIELD_SITE_ID));
    assertEquals(username, (String) data.get(FIELD_SITE_MANAGER_NAME));
    assertEquals(password, (String) data.get(FIELD_SITE_MANAGER_PASSWORD));
    assertEquals(null, (String) data.get(FIELD_ONLY_DB_LOAD));
}
Also used : UserModel(org.alfresco.utility.model.UserModel) EventResult(org.alfresco.bm.event.EventResult) UserData(org.alfresco.bm.user.UserData) RestCoreAPI(org.alfresco.rest.requests.coreAPI.RestCoreAPI) People(org.alfresco.rest.requests.People) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 12 with UserData

use of org.alfresco.bm.user.UserData in project records-management by Alfresco.

the class PrepareRMSite method processEvent.

/**
 * @see org.alfresco.bm.event.AbstractEventProcessor#processEvent(org.alfresco.bm.event.Event)
 */
@Override
protected EventResult processEvent(Event event) throws Exception {
    StringBuilder msg = new StringBuilder("Preparing Records Management: \n");
    List<Event> events = new ArrayList<Event>(10);
    UserModel userModel = new UserModel(getUsername(), getPassword());
    // authenticate with provided credentials and verify that they are valid
    restCoreAPI.authenticateUser(userModel);
    restCoreAPI.withCoreAPI().usingAuthUser().getPerson();
    String statusCode = restCoreAPI.getStatusCode();
    if (HttpStatus.valueOf(Integer.parseInt(statusCode)) != HttpStatus.OK) {
        return new EventResult("Provided RM Site Creator does not exist, or provided credentials are not valid.", false);
    }
    UserData rmAdmin = userDataService.findUserByUsername(getUsername());
    if (rmAdmin == null) {
        rmAdmin = new UserData();
        rmAdmin.setCreationState(Created);
        rmAdmin.setDomain(RM_SITE_DOMAIN);
        rmAdmin.setUsername(getUsername());
        rmAdmin.setPassword(getPassword());
        userDataService.createNewUser(rmAdmin);
    } else {
        // Check for creation
        if (rmAdmin.getCreationState() != Created) {
            userDataService.setUserCreationState(getUsername(), Created);
            msg.append("   Updating user " + getUsername() + " state to created.\n");
        }
    }
    SiteData rmSite = siteDataService.getSite(RM_SITE_ID);
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    builder.add(FIELD_SITE_ID, RM_SITE_ID).add(FIELD_SITE_MANAGER_NAME, getUsername()).add(FIELD_SITE_MANAGER_PASSWORD, getPassword());
    boolean existsRMSite = restAPIFactory.getRMSiteAPI(userModel).existsRMSite();
    // RM site exists and it is loaded in MongoDB
    if (existsRMSite && rmSite != null && rmSite.getCreationState() == Created) {
        return new EventResult("RM Site already created, continue loading data.", new Event(getEventNameContinueLoadingData(), null));
    }
    // RM site exists and it is not loaded in MongoDB
    if (existsRMSite && rmSite == null) {
        builder.add(FIELD_ONLY_DB_LOAD, true);
        DBObject data = builder.get();
        events.add(new Event(getEventNameLoadRMSiteIntoDB(), data));
    }
    // RM site does not exist and will be created
    if (!existsRMSite) {
        DBObject data = builder.get();
        events.add(new Event(getEventNameRMSitePrepared(), data));
    }
    // Done
    return new EventResult(msg.toString(), events);
}
Also used : UserModel(org.alfresco.utility.model.UserModel) SiteData(org.alfresco.bm.site.SiteData) EventResult(org.alfresco.bm.event.EventResult) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) UserData(org.alfresco.bm.user.UserData) ArrayList(java.util.ArrayList) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject)

Example 13 with UserData

use of org.alfresco.bm.user.UserData 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)

Example 14 with UserData

use of org.alfresco.bm.user.UserData in project records-management by Alfresco.

the class RMBaseEventProcessorUnitTest method testGetUserSuccessfull.

@Test
public void testGetUserSuccessfull() throws Exception {
    String userName = "user1";
    Logger mockedLog = mock(Logger.class);
    SiteData mockedSiteData = mock(SiteData.class);
    when(mockedSiteDataService.getSite(PATH_SNIPPET_RM_SITE_ID)).thenReturn(mockedSiteData);
    SiteMemberData mockedSiteMemberData = mock(SiteMemberData.class);
    when(mockedSiteMemberData.getUsername()).thenReturn(userName);
    when(mockedSiteDataService.randomSiteMember(PATH_SNIPPET_RM_SITE_ID, DataCreationState.Created, null, RMRole.Administrator.toString())).thenReturn(mockedSiteMemberData);
    UserData mockedUserData = mock(UserData.class);
    when(mockedUserDataService.findUserByUsername(userName)).thenReturn(mockedUserData);
    UserData user = testRMBaseEventProcessor.getRandomUser(mockedLog);
    assertEquals(mockedUserData, user);
}
Also used : SiteData(org.alfresco.bm.site.SiteData) SiteMemberData(org.alfresco.bm.site.SiteMemberData) UserData(org.alfresco.bm.user.UserData) Logger(org.slf4j.Logger) Test(org.junit.Test)

Aggregations

UserData (org.alfresco.bm.user.UserData)14 Event (org.alfresco.bm.event.Event)11 EventResult (org.alfresco.bm.event.EventResult)11 Test (org.junit.Test)9 SiteMemberData (org.alfresco.bm.site.SiteMemberData)7 UserModel (org.alfresco.utility.model.UserModel)7 DBObject (com.mongodb.DBObject)6 SiteData (org.alfresco.bm.site.SiteData)6 People (org.alfresco.rest.requests.People)4 RestCoreAPI (org.alfresco.rest.requests.coreAPI.RestCoreAPI)4 ArrayList (java.util.ArrayList)2 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 RMRole (org.alfresco.bm.dataload.rm.role.RMRole)1 Logger (org.slf4j.Logger)1