Search in sources :

Example 61 with EventResult

use of org.alfresco.bm.event.EventResult in project records-management by Alfresco.

the class LoadRecordUnitTest method testLoadRecordOperationWithRestAPiException.

@Test
public void testLoadRecordOperationWithRestAPiException() throws Exception {
    loadSingleComponent.setEventNameComplete("recordLoaded");
    Event mockedEvent = mock(Event.class);
    DBObject mockedData = mock(DBObject.class);
    when(mockedData.get(FIELD_CONTEXT)).thenReturn("someContext");
    when(mockedData.get(FIELD_PATH)).thenReturn("/aPath");
    when(mockedData.get(FIELD_LOAD_OPERATION)).thenReturn(LOAD_RECORD_OPERATION);
    when(mockedEvent.getData()).thenReturn(mockedData);
    FolderData mockedFolder = mock(FolderData.class);
    when(mockedFolder.getId()).thenReturn("folderId");
    when(mockedFolder.getPath()).thenReturn("/aPath");
    when(mockedFolder.getContext()).thenReturn("someContext");
    when(mockedFileFolderService.getFolder("someContext", "/aPath")).thenReturn(mockedFolder);
    when(mockedEvent.getSessionId()).thenReturn("someId");
    when(mockedRestApiFactory.getRecordFolderAPI(any(UserModel.class))).thenReturn(mockedRecordFolderAPI);
    RecordFolder mockedFilePlanComponent = mock(RecordFolder.class);
    when(mockedRecordFolderAPI.getRecordFolder("folderId")).thenReturn(mockedFilePlanComponent);
    File mockedFile = mock(File.class);
    when(mockedTestFileService.getFile()).thenReturn(mockedFile);
    Mockito.doThrow(new Exception("someError")).when(mockedRecordFolderAPI).createRecord(any(Record.class), any(String.class), any(File.class));
    mockSiteAndUserData();
    EventResult result = loadSingleComponent.processEvent(mockedEvent, new StopWatch());
    verify(mockedTestFileService, times(1)).getFile();
    verify(mockedRecordFolderAPI, times(1)).createRecord(any(Record.class), eq("folderId"), any(File.class));
    verify(mockedFileFolderService, never()).incrementFileCount(any(String.class), any(String.class), any(Long.class));
    assertEquals(false, result.isSuccess());
    DBObject data = (DBObject) result.getData();
    assertNotNull(data.get("error"));
    assertEquals("someError", data.get("error"));
    assertEquals("aUser", data.get("username"));
    assertEquals(mockedFolder.getPath(), data.get("path"));
    assertNotNull(data.get("stack"));
    assertEquals(0, result.getNextEvents().size());
}
Also used : UserModel(org.alfresco.utility.model.UserModel) EventResult(org.alfresco.bm.event.EventResult) RecordFolder(org.alfresco.rest.rm.community.model.recordfolder.RecordFolder) FolderData(org.alfresco.bm.cm.FolderData) Event(org.alfresco.bm.event.Event) Record(org.alfresco.rest.rm.community.model.record.Record) DBObject(com.mongodb.DBObject) File(java.io.File) StopWatch(org.apache.commons.lang3.time.StopWatch) LoadSingleComponentUnitTest(org.alfresco.bm.dataload.LoadSingleComponentUnitTest) Test(org.junit.Test)

Example 62 with EventResult

use of org.alfresco.bm.event.EventResult in project records-management by Alfresco.

the class LoadRootUnfiledRecordFolderUnitTest method testLoadRootUnfiledRecordFolderOperation.

@Test
public void testLoadRootUnfiledRecordFolderOperation() throws Exception {
    loadSingleComponent.setEventNameComplete(EVENT_ROOT_UNFILED_RECORD_FOLDER_LOADED);
    Event mockedEvent = mock(Event.class);
    DBObject mockedData = mock(DBObject.class);
    when(mockedData.get(FIELD_CONTEXT)).thenReturn("someContext");
    when(mockedData.get(FIELD_PATH)).thenReturn("/aPath");
    when(mockedData.get(FIELD_LOAD_OPERATION)).thenReturn(LOAD_ROOT_UNFILED_RECORD_FOLDER_OPERATION);
    when(mockedEvent.getData()).thenReturn(mockedData);
    FolderData mockedFolder = mock(FolderData.class);
    when(mockedFolder.getId()).thenReturn("folderId");
    when(mockedFolder.getPath()).thenReturn("/aPath");
    when(mockedFileFolderService.getFolder("someContext", "/aPath")).thenReturn(mockedFolder);
    when(mockedEvent.getSessionId()).thenReturn("someId");
    when(mockedRestApiFactory.getUnfiledContainersAPI(any(UserModel.class))).thenReturn(mockedUnfiledContainerAPI);
    UnfiledContainerChild mockedChildFilePlanComponent = mock(UnfiledContainerChild.class);
    when(mockedChildFilePlanComponent.getId()).thenReturn(UUID.randomUUID().toString());
    when(mockedUnfiledContainerAPI.createUnfiledContainerChild(any(UnfiledContainerChild.class), eq("folderId"))).thenReturn(mockedChildFilePlanComponent);
    mockSiteAndUserData();
    EventResult result = loadSingleComponent.processEvent(mockedEvent, new StopWatch());
    verify(mockedFileFolderService, times(1)).createNewFolder(any(String.class), any(String.class), any(String.class));
    verify(mockedFileFolderService, times(1)).incrementFolderCount(any(String.class), any(String.class), any(Long.class));
    assertEquals(true, result.isSuccess());
    DBObject data = (DBObject) result.getData();
    assertEquals("Created 1 root unfiled record folder.", data.get("msg"));
    assertEquals("/aPath", data.get(FIELD_PATH));
    assertEquals("aUser", data.get("username"));
    assertEquals(1, result.getNextEvents().size());
    Event event = result.getNextEvents().get(0);
    assertEquals(EVENT_ROOT_UNFILED_RECORD_FOLDER_LOADED, event.getName());
}
Also used : UserModel(org.alfresco.utility.model.UserModel) EventResult(org.alfresco.bm.event.EventResult) UnfiledContainerChild(org.alfresco.rest.rm.community.model.unfiledcontainer.UnfiledContainerChild) FolderData(org.alfresco.bm.cm.FolderData) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject) StopWatch(org.apache.commons.lang3.time.StopWatch) LoadSingleComponentUnitTest(org.alfresco.bm.dataload.LoadSingleComponentUnitTest) Test(org.junit.Test)

Example 63 with EventResult

use of org.alfresco.bm.event.EventResult in project records-management by Alfresco.

the class RMAbstractLoadComponent method processEvent.

@Override
protected EventResult processEvent(Event event) throws Exception {
    super.suspendTimer();
    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_PATH);
    }
    String context = (String) dataObj.get(FIELD_CONTEXT);
    String path = (String) dataObj.get(FIELD_PATH);
    String operation = (String) dataObj.get(FIELD_LOAD_OPERATION);
    if (context == null || path == null || isBlank(operation)) {
        return new EventResult("Request data not complete for filing unfiled record: " + dataObj, false);
    }
    // Get the folder
    FolderData folder = fileFolderService.getFolder(context, path);
    if (folder == null) {
        throw new IllegalStateException("No such folder recorded: " + dataObj);
    }
    // Get the session
    String sessionId = event.getSessionId();
    if (sessionId == null) {
        return new EventResult("Load scheduling should create a session for each loader.", false);
    }
    switch(operation) {
        case FILE_RECORD_OPERATION:
        case LOAD_UNFILED_RECORD_OPERATION:
        case LOAD_RECORD_OPERATION:
        case LOAD_ROOT_CATEGORY_OPERATION:
        case LOAD_SUB_CATEGORY_OPERATION:
        case LOAD_RECORD_FOLDER_OPERATION:
        case LOAD_ROOT_UNFILED_RECORD_FOLDER_OPERATION:
        case LOAD_UNFILED_RECORD_FOLDER_OPERATION:
            return loadOperation(folder, dataObj, operation);
        default:
            throw new IllegalStateException("Unsuported operation: " + operation);
    }
}
Also used : EventResult(org.alfresco.bm.event.EventResult) FolderData(org.alfresco.bm.cm.FolderData) DBObject(com.mongodb.DBObject)

Example 64 with EventResult

use of org.alfresco.bm.event.EventResult in project records-management by Alfresco.

the class RMAbstractLoadComponent method loadOperation.

/**
 * Helper method to load component specified by operation parameter in specified folder.
 *
 * @param folder - the folder to load component in
 * @param dataObj - the query object that has record id information when filing one record
 * @param operation - load operation, that specifies which type of component to load
 * @return EventResult - the loading result or error if there was an exception on loading
 */
private EventResult loadOperation(FolderData folder, DBObject dataObj, String operation) {
    if (FILE_RECORD_OPERATION.equals(operation)) {
        String recordId = (String) dataObj.get(FIELD_RECORD_ID);
        if (isBlank(recordId)) {
            return new EventResult("Request data not complete for filing unfiled record: " + dataObj, false);
        }
    }
    UserData user = getRandomUser(logger);
    String username = user.getUsername();
    String password = user.getPassword();
    UserModel userModel = new UserModel(username, password);
    try {
        List<Event> scheduleEvents = new ArrayList<Event>();
        String message = executeOperation(folder, dataObj, operation, userModel);
        DBObject eventData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, folder.getContext()).add(FIELD_PATH, folder.getPath()).get();
        Event nextEvent = new Event(getEventNameComplete(), eventData);
        scheduleEvents.add(nextEvent);
        DBObject resultData = BasicDBObjectBuilder.start().add("msg", message).add("path", folder.getPath()).add("username", username).get();
        return new EventResult(resultData, scheduleEvents);
    } catch (Exception e) {
        String error = e.getMessage();
        String stack = ExceptionUtils.getStackTrace(e);
        // Grab REST API information
        DBObject data = BasicDBObjectBuilder.start().append("error", error).append("username", username).append("path", folder.getPath()).append("stack", stack).get();
        // Build failure result
        return new EventResult(data, false);
    }
}
Also used : UserModel(org.alfresco.utility.model.UserModel) EventResult(org.alfresco.bm.event.EventResult) UserData(org.alfresco.bm.user.UserData) ArrayList(java.util.ArrayList) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject)

Example 65 with EventResult

use of org.alfresco.bm.event.EventResult in project records-management by Alfresco.

the class ScheduleUnfiledRecordFolderLoaders method processEvent.

@Override
protected EventResult processEvent(Event arg0) throws Exception {
    // Are there still sessions active?
    long sessionCount = sessionService.getActiveSessionsCount();
    int loaderSessionsToCreate = maxActiveLoaders - (int) sessionCount;
    List<Event> nextEvents = new ArrayList<Event>(maxActiveLoaders);
    // Do we actually need to do anything
    if (!createUnfiledRecordFolderStructure) {
        return new EventResult("Unfiled Record Folders structure creation not wanted, continue with loading data.", new Event(getEventNameLoadingComplete(), null));
    }
    if (unfiledRecordFolderDepth > 0) {
        // Load root unfiled record folders
        prepareRootUnfiledRecordFolders(loaderSessionsToCreate, nextEvents);
    }
    if (unfiledRecordFolderDepth > 1) {
        // Load unfiled record folder children
        prepareUnfiledRecordFolders(loaderSessionsToCreate, nextEvents);
    }
    // If there are no events, then we have finished
    String msg = null;
    if (loaderSessionsToCreate > 0 && nextEvents.isEmpty()) {
        rootUnfiledRecordFoldersToLoad = null;
        auxFileFolderService.drop();
        // There are no files or folders to load even though there are sessions available
        Event nextEvent = new Event(getEventNameLoadingComplete(), null);
        nextEvents.add(nextEvent);
        msg = "Loading completed.  Raising 'done' event.";
    } else {
        // Reschedule self
        Event nextEvent = new Event(getEventNameScheduleLoaders(), System.currentTimeMillis() + loadCheckDelay, null);
        nextEvents.add(nextEvent);
        msg = "Raised further " + (nextEvents.size() - 1) + " events and rescheduled self.";
    }
    if (logger.isDebugEnabled()) {
        logger.debug(msg);
    }
    EventResult result = new EventResult(msg, nextEvents);
    return result;
}
Also used : EventResult(org.alfresco.bm.event.EventResult) ArrayList(java.util.ArrayList) Event(org.alfresco.bm.event.Event)

Aggregations

EventResult (org.alfresco.bm.event.EventResult)133 Test (org.junit.Test)119 Event (org.alfresco.bm.event.Event)109 DBObject (com.mongodb.DBObject)101 StopWatch (org.apache.commons.lang3.time.StopWatch)97 FolderData (org.alfresco.bm.cm.FolderData)77 UserModel (org.alfresco.utility.model.UserModel)35 LoadSingleComponentUnitTest (org.alfresco.bm.dataload.LoadSingleComponentUnitTest)23 SiteData (org.alfresco.bm.site.SiteData)20 SiteMemberData (org.alfresco.bm.site.SiteMemberData)17 RestCoreAPI (org.alfresco.rest.requests.coreAPI.RestCoreAPI)15 RecordData (org.alfresco.bm.dataload.rm.services.RecordData)14 UnfiledContainerChild (org.alfresco.rest.rm.community.model.unfiledcontainer.UnfiledContainerChild)12 UserData (org.alfresco.bm.user.UserData)11 Node (org.alfresco.rest.requests.Node)10 Site (org.alfresco.rest.requests.Site)10 File (java.io.File)9 ArrayList (java.util.ArrayList)9 UnfiledContainer (org.alfresco.rest.rm.community.model.unfiledcontainer.UnfiledContainer)9 RestWrapper (org.alfresco.rest.core.RestWrapper)8