Search in sources :

Example 6 with SiteMemberData

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

the class CreateRMSiteUnitTest method testRMSiteCreation.

@Test
public void testRMSiteCreation() throws Exception {
    Event mockedEvent = mock(Event.class);
    DBObject mockedData = mock(DBObject.class);
    RMSiteAPI mockedRMSiteAPI = mock(RMSiteAPI.class);
    RMSite mockedRMSite = mock(RMSite.class);
    FilePlanAPI mockedFilePlanAPI = mock(FilePlanAPI.class);
    UnfiledContainerAPI mockedUnfiledContainerAPI = mock(UnfiledContainerAPI.class);
    TransferContainerAPI mockedTransferContainerAPI = mock(TransferContainerAPI.class);
    FilePlan mockedFilePlan = mock(FilePlan.class);
    UnfiledContainer mockedUnfiledRecordsContainer = mock(UnfiledContainer.class);
    TransferContainer mockedTransfers = mock(TransferContainer.class);
    String siteId = randomUUID().toString();
    String siteManager = randomUUID().toString();
    String password = "password";
    when(mockedData.get(FIELD_SITE_ID)).thenReturn(siteId);
    when(mockedData.get(FIELD_SITE_MANAGER_NAME)).thenReturn(siteManager);
    when(mockedData.get(FIELD_SITE_MANAGER_PASSWORD)).thenReturn(password);
    when(mockedEvent.getData()).thenReturn(mockedData);
    when(mockedSiteDataService.getSite(siteId)).thenReturn(null);
    when(mockedRestAPIFactory.getRMSiteAPI(any(UserModel.class))).thenReturn(mockedRMSiteAPI);
    when(mockedRMSiteAPI.createRMSite(any(RMSite.class))).thenReturn(mockedRMSite);
    when(mockedRMSiteAPI.existsRMSite()).thenReturn(true);
    when(mockedRestAPIFactory.getFilePlansAPI(any(UserModel.class))).thenReturn(mockedFilePlanAPI);
    when(mockedFilePlanAPI.getFilePlan(FILE_PLAN_ALIAS)).thenReturn(mockedFilePlan);
    when(mockedFilePlan.getId()).thenReturn(randomUUID().toString());
    when(mockedRestAPIFactory.getUnfiledContainersAPI(any(UserModel.class))).thenReturn(mockedUnfiledContainerAPI);
    when(mockedUnfiledContainerAPI.getUnfiledContainer(UNFILED_RECORDS_CONTAINER_ALIAS)).thenReturn(mockedUnfiledRecordsContainer);
    when(mockedUnfiledRecordsContainer.getId()).thenReturn(randomUUID().toString());
    when(mockedRestAPIFactory.getTransferContainerAPI(any(UserModel.class))).thenReturn(mockedTransferContainerAPI);
    when(mockedTransferContainerAPI.getTransferContainer(TRANSFERS_ALIAS)).thenReturn(mockedTransfers);
    when(mockedTransfers.getId()).thenReturn(randomUUID().toString());
    RMRestWrapper mockedRmRestWrapper = mock(RMRestWrapper.class);
    when(mockedRmRestWrapper.getStatusCode()).thenReturn(HttpStatus.CREATED.toString());
    when(mockedRestAPIFactory.getRmRestWrapper()).thenReturn(mockedRmRestWrapper);
    EventResult result = createRMSite.processEvent(mockedEvent);
    ArgumentCaptor<SiteData> siteData = forClass(SiteData.class);
    verify(mockedSiteDataService).addSite(siteData.capture());
    ArgumentCaptor<SiteMemberData> siteMemberData = forClass(SiteMemberData.class);
    verify(mockedSiteDataService).addSiteMember(siteMemberData.capture());
    // Check RM site
    SiteData siteDataValue = siteData.getValue();
    assertEquals(Scheduled, siteDataValue.getCreationState());
    // Check RM admin member
    SiteMemberData siteMemberDataValue = siteMemberData.getValue();
    assertEquals(Created, siteMemberDataValue.getCreationState());
    assertEquals(Administrator.toString(), siteMemberDataValue.getRole());
    verify(mockedRMSiteAPI, times(1)).createRMSite(any(RMSite.class));
    verify(mockedFileFolderService, times(3)).createNewFolder(any(FolderData.class));
    assertEquals(true, result.isSuccess());
    assertEquals("Created site: " + siteId + " Site creator: " + siteManager, (String) result.getData());
    List<Event> events = result.getNextEvents();
    assertEquals(1, events.size());
    Event event = events.get(0);
    assertEquals(DEFAULT_EVENT_NAME_SITE_CREATED, event.getName());
    DBObject data = (DBObject) event.getData();
    assertEquals(null, data);
}
Also used : SiteData(org.alfresco.bm.site.SiteData) FilePlan(org.alfresco.rest.rm.community.model.fileplan.FilePlan) DBObject(com.mongodb.DBObject) FilePlanAPI(org.alfresco.rest.rm.community.requests.gscore.api.FilePlanAPI) UnfiledContainer(org.alfresco.rest.rm.community.model.unfiledcontainer.UnfiledContainer) UserModel(org.alfresco.utility.model.UserModel) RMSiteAPI(org.alfresco.rest.rm.community.requests.gscore.api.RMSiteAPI) TransferContainer(org.alfresco.rest.rm.community.model.transfercontainer.TransferContainer) SiteMemberData(org.alfresco.bm.site.SiteMemberData) RMSite(org.alfresco.rest.rm.community.model.site.RMSite) EventResult(org.alfresco.bm.event.EventResult) FolderData(org.alfresco.bm.cm.FolderData) TransferContainerAPI(org.alfresco.rest.rm.community.requests.gscore.api.TransferContainerAPI) Event(org.alfresco.bm.event.Event) UnfiledContainerAPI(org.alfresco.rest.rm.community.requests.gscore.api.UnfiledContainerAPI) RMRestWrapper(org.alfresco.rest.core.RMRestWrapper) Test(org.junit.Test)

Example 7 with SiteMemberData

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

the class RMBaseEventProcessor method getRandomUser.

/**
 * Gets a random user from the RM site.
 */
public UserData getRandomUser(Logger logger) {
    // Check
    SiteData siteData = siteDataService.getSite(PATH_SNIPPET_RM_SITE_ID);
    if (siteData == null) {
        throw new IllegalStateException("Unable to find site '" + PATH_SNIPPET_RM_SITE_ID + "'");
    }
    SiteMemberData siteMember = siteDataService.randomSiteMember(PATH_SNIPPET_RM_SITE_ID, DataCreationState.Created, null, RMRole.Administrator.toString());
    if (siteMember == null) {
        throw new IllegalStateException("Unable to find a user with specified roles for site: " + PATH_SNIPPET_RM_SITE_ID);
    }
    String username = siteMember.getUsername();
    // Retrieve the user data
    UserData user = userDataService.findUserByUsername(username);
    if (user == null) {
        throw new IllegalStateException("Unable to find a user '" + username + "' linked to site: " + PATH_SNIPPET_RM_SITE_ID);
    }
    // Done
    logger.debug("Found RM site member '" + username + "'");
    return user;
}
Also used : SiteData(org.alfresco.bm.site.SiteData) SiteMemberData(org.alfresco.bm.site.SiteMemberData) UserData(org.alfresco.bm.user.UserData)

Example 8 with SiteMemberData

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

the class CreateRMSite method processEvent.

@Override
public EventResult processEvent(Event event) throws Exception {
    mandatoryObject("event", event);
    DBObject dataObj = (DBObject) event.getData();
    if (dataObj == null) {
        throw new IllegalStateException("This processor requires data object with fields '" + FIELD_SITE_ID + ", " + FIELD_SITE_MANAGER_NAME + "'.");
    }
    String siteId = (String) dataObj.get(FIELD_SITE_ID);
    String siteManager = (String) dataObj.get(FIELD_SITE_MANAGER_NAME);
    String siteManagersPassword = (String) dataObj.get(FIELD_SITE_MANAGER_PASSWORD);
    Boolean onlyLoadInDb = (Boolean) dataObj.get(FIELD_ONLY_DB_LOAD);
    if (isBlank(siteId) || isBlank(siteManager) || isBlank(siteManagersPassword)) {
        return new EventResult("Requests data not complete for site creation: " + dataObj, false);
    }
    SiteData site = siteDataService.getSite(siteId);
    if (site != null) {
        if (site.getCreationState() == Created) {
            return new EventResult("RM Site already exists in DB: " + site, false);
        }
        if (site.getCreationState() != Created) {
            return new EventResult("Site state has changed: " + site, false);
        }
    }
    String msg = null;
    RMSiteAPI rmSiteAPI = restAPIFactory.getRMSiteAPI(new UserModel(siteManager, siteManagersPassword));
    String guid = null;
    if (onlyLoadInDb == null) {
        RMSite siteModel = RMSite.builder().compliance(STANDARD).title(RM_SITE_TITLE).description(RM_SITE_DESC).build();
        RMSite rmSite = rmSiteAPI.createRMSite(siteModel);
        String statusCode = restAPIFactory.getRmRestWrapper().getStatusCode();
        if (HttpStatus.valueOf(Integer.parseInt(statusCode)) != HttpStatus.CREATED) {
            return new EventResult("RM site could not be created.", false);
        }
        guid = rmSite.getGuid();
        msg = "Created site: " + siteId + " Site creator: " + siteManager;
    } else {
        RMSite alreadyCreatedRMSite = rmSiteAPI.getSite();
        guid = alreadyCreatedRMSite.getGuid();
        msg = "RM site already exists, just loading it in the DB.";
    }
    if (site == null) {
        // Create data
        site = new SiteData();
        site.setSiteId(siteId);
        site.setTitle(RM_SITE_TITLE);
        site.setGuid(RM_SITE_GUID);
        site.setDomain(RM_SITE_DOMAIN);
        site.setDescription(RM_SITE_DESC);
        site.setSitePreset(RM_SITE_PRESET);
        site.setVisibility(RM_SITE_VISIBILITY);
        site.setType(RM_SITE_TYPE);
        site.setCreationState(Scheduled);
        siteDataService.addSite(site);
    }
    // Start by marking them as failures in order to handle all eventualities
    siteDataService.setSiteCreationState(siteId, null, Failed);
    siteDataService.setSiteMemberCreationState(siteId, siteManager, Failed);
    SiteMemberData siteMember = siteDataService.getSiteMember(RM_SITE_ID, siteManager);
    if (siteMember == null) {
        // Record the administrator
        SiteMemberData rmAdminMember = new SiteMemberData();
        rmAdminMember.setCreationState(Created);
        rmAdminMember.setRole(Administrator.toString());
        rmAdminMember.setSiteId(RM_SITE_ID);
        rmAdminMember.setUsername(siteManager);
        siteDataService.addSiteMember(rmAdminMember);
    }
    // Mark the site.
    siteDataService.setSiteCreationState(siteId, guid, Created);
    siteDataService.setSiteMemberCreationState(siteId, siteManager, Created);
    loadSpecialContainersInDB(siteId, siteManager, siteManagersPassword);
    event = new Event(eventNameSiteCreated, null);
    if (logger.isDebugEnabled()) {
        logger.debug(msg);
    }
    return new EventResult(msg, event);
}
Also used : SiteData(org.alfresco.bm.site.SiteData) UserModel(org.alfresco.utility.model.UserModel) RMSiteAPI(org.alfresco.rest.rm.community.requests.gscore.api.RMSiteAPI) SiteMemberData(org.alfresco.bm.site.SiteMemberData) EventResult(org.alfresco.bm.event.EventResult) RMSite(org.alfresco.rest.rm.community.model.site.RMSite) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject)

Example 9 with SiteMemberData

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

the class CreateRMSiteMember method processEvent.

@Override
protected EventResult processEvent(Event event) throws Exception {
    if (event == null) {
        throw new IllegalStateException("This processor requires an event.");
    }
    DBObject dataObj = (DBObject) event.getData();
    if (dataObj == null) {
        throw new IllegalStateException("This processor requires data with field " + FIELD_USERNAME);
    }
    String username = (String) dataObj.get(FIELD_USERNAME);
    EventProcessorResponse response = null;
    Event nextEvent = null;
    String msg = null;
    // Check the input
    if (StringUtils.isBlank(username)) {
        dataObj.put(MSG_KEY, INVALID_SITE_MEMBER_REQUEST_MSG);
        return new EventResult(dataObj, false);
    }
    // Get the membership data
    SiteMemberData siteMember = siteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, username);
    if (siteMember == null) {
        dataObj.put(MSG_KEY, MessageFormat.format(SITE_MEMBER_MISSING_MSG_TEMPLATE, username));
        return new EventResult(dataObj, false);
    }
    if (siteMember.getCreationState() != DataCreationState.Scheduled) {
        dataObj.put(MSG_KEY, MessageFormat.format(SITE_MEMBER_ALREADY_PROCESSED_MSG_TEMPLATE, siteMember));
        return new EventResult(dataObj, false);
    }
    // Start by marking it as a failure in order to handle all failure paths
    siteDataService.setSiteMemberCreationState(PATH_SNIPPET_RM_SITE_ID, username, DataCreationState.Failed);
    String roleStr = siteMember.getRole();
    try {
        // assign RM roles to new members as admin user
        RMUserAPI rmUserAPI = getRestAPIFactory().getRMUserAPI();
        rmUserAPI.assignRoleToUser(username, roleStr);
        siteDataService.setSiteMemberCreationState(PATH_SNIPPET_RM_SITE_ID, username, DataCreationState.Created);
        siteMember = siteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, username);
        EventDataObject responseData = new EventDataObject(STATUS.SUCCESS, siteMember);
        response = new EventProcessorResponse("Added RM site member", true, responseData);
        msg = MessageFormat.format(CREATED_RM_SITE_MEMBER_MSG_TEMPLATE, response);
        nextEvent = new Event(eventNameRMSiteMemberCreated, null);
    } catch (Exception e) {
        // Failure
        throw new RuntimeException("Create RM site member as user: admin failed (" + e.getMessage() + "): " + siteMember, e);
    }
    logger.debug(msg);
    EventResult result = new EventResult(msg, nextEvent);
    return result;
}
Also used : SiteMemberData(org.alfresco.bm.site.SiteMemberData) EventResult(org.alfresco.bm.event.EventResult) EventDataObject(org.alfresco.bm.event.selector.EventDataObject) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject) RMUserAPI(org.alfresco.rest.rm.community.requests.gscore.api.RMUserAPI) EventProcessorResponse(org.alfresco.bm.event.selector.EventProcessorResponse)

Example 10 with SiteMemberData

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

the class LoadSingleComponentUnitTest method mockSiteAndUserData.

/**
 * Helper methods
 */
/**
 * Helper method for mocking user data
 */
protected void mockSiteAndUserData() {
    SiteData mockedSiteData = mock(SiteData.class);
    when(mockedSiteDataService.getSite(PATH_SNIPPET_RM_SITE_ID)).thenReturn(mockedSiteData);
    SiteMemberData mockedSiteMemberData = mock(SiteMemberData.class);
    when(mockedSiteMemberData.getUsername()).thenReturn("aUser");
    when(mockedSiteDataService.randomSiteMember(PATH_SNIPPET_RM_SITE_ID, DataCreationState.Created, null, RMRole.Administrator.toString())).thenReturn(mockedSiteMemberData);
    UserData mockedUserData = mock(UserData.class);
    when(mockedUserData.getUsername()).thenReturn("aUser");
    when(mockedUserData.getPassword()).thenReturn("aUser");
    when(mockedUserDataService.findUserByUsername("aUser")).thenReturn(mockedUserData);
    when(mockedApplicationContext.getBean("restAPIFactory", RestAPIFactory.class)).thenReturn(mockedRestApiFactory);
    when(mockedRestApiFactory.getRecordsAPI(any(UserModel.class))).thenReturn(mockedRecordsAPI);
}
Also used : SiteData(org.alfresco.bm.site.SiteData) UserModel(org.alfresco.utility.model.UserModel) SiteMemberData(org.alfresco.bm.site.SiteMemberData) UserData(org.alfresco.bm.user.UserData)

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