Search in sources :

Example 6 with FileItem

use of org.nuxeo.drive.adapter.FileItem in project nuxeo-drive-server by nuxeo.

the class FileSystemItemManagerImpl method updateFile.

protected FileItem updateFile(FileSystemItem fsItem, Blob blob) {
    if (fsItem == null) {
        throw new NuxeoException("Cannot update the content of file system item because it doesn't exist.");
    }
    if (!(fsItem instanceof FileItem)) {
        throw new NuxeoException(String.format("Cannot update the content of file system item with id %s because it is not a file.", fsItem.getId()));
    }
    FileItem file = (FileItem) fsItem;
    file.setBlob(blob);
    return file;
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException)

Example 7 with FileItem

use of org.nuxeo.drive.adapter.FileItem in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testGetFileSystemItem.

@Test
public void testGetFileSystemItem() throws Exception {
    // ------------------------------------------------------
    // Check downloadable FileSystemItems
    // ------------------------------------------------------
    // File
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(file));
    FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Joe.odt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    Blob fileItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("Joe.odt", fileItemBlob.getFilename());
    assertEquals("Content of Joe's file.", fileItemBlob.getString());
    // Note
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(note));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("aNote.txt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    fileItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("aNote.txt", fileItemBlob.getFilename());
    assertEquals("Content of Bob's note.", fileItemBlob.getString());
    // Custom doc type with the "file" schema
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(custom));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(custom);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Bonnie's file.odt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    fileItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("Bonnie's file.odt", fileItemBlob.getFilename());
    assertEquals("Content of Bonnie's file.", fileItemBlob.getString());
    // File without a blob => not adaptable as a FileSystemItem
    file.setPropertyValue("file:content", null);
    file = session.saveDocument(file);
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(file));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
    assertNull(fsItem);
    // Deleted file => not adaptable as a FileSystemItem
    custom.followTransition("delete");
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(custom));
    assertNull(defaultFileSystemItemFactory.getFileSystemItem(custom));
    // Deleted file with explicit "includeDeleted" => adaptable as a
    // FileSystemItem
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(custom, true));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(custom, true);
    assertNotNull(fsItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), fsItem.getId());
    assertEquals("Bonnie's file.odt", fsItem.getName());
    // Version
    // Note is now automatically versioned at each save
    assertEquals("0.1", note.getVersionLabel());
    note.checkOut();
    DocumentRef versionRef = session.checkIn(note.getRef(), VersioningOption.MINOR, null);
    DocumentModel version = session.getDocument(versionRef);
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(version));
    // Proxy
    DocumentModel proxy = session.createProxy(note.getRef(), folder.getRef());
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(proxy));
    // HiddenInNavigation
    note.addFacet("HiddenInNavigation");
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(note));
    note.removeFacet("HiddenInNavigation");
    // ------------------------------------------------------
    // Check folderish FileSystemItems
    // ------------------------------------------------------
    // Folder
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(folder));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(folder);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Jack's folder", fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    FolderItem folderItem = (FolderItem) fsItem;
    List<FileSystemItem> children = folderItem.getChildren();
    assertNotNull(children);
    assertEquals(0, children.size());
    assertTrue(folderItem.getCanScrollDescendants());
    ScrollFileSystemItemList descendants = folderItem.scrollDescendants(null, 10, 1000);
    assertNotNull(descendants);
    assertNotNull(descendants.getScrollId());
    assertEquals(0, descendants.size());
    // FolderishFile => adaptable as a FolderItem since the default
    // FileSystemItem factory gives precedence to the Folderish facet
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(folderishFile));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(folderishFile);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folderishFile.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Sarah's folderish file", fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    // ------------------------------------------------------
    // Check not downloadable nor folderish
    // ------------------------------------------------------
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(notAFileSystemItem));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(notAFileSystemItem);
    assertNull(fsItem);
    // -------------------------------------------------------------
    // Check #getFileSystemItem(DocumentModel doc, FolderItem parentItem)
    // -------------------------------------------------------------
    FolderItem syncRootSystemItem = (FolderItem) fileSystemItemAdapterService.getFileSystemItemFactoryForId(syncRootItemId).getFileSystemItemById(syncRootItemId, principal);
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note, syncRootSystemItem);
    assertEquals(syncRootItemId, fsItem.getParentId());
    // Passing a null parent will force a null parentId
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note, null);
    assertNull(fsItem.getParentId());
    // ------------------------------------------------------------------
    // Check FileSystemItem#getCanRename and FileSystemItem#getCanDelete
    // ------------------------------------------------------------------
    // As Administrator
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
    assertTrue(fsItem.getCanRename());
    assertTrue(fsItem.getCanDelete());
    // As a user with READ permission
    DocumentModel rootDoc = session.getRootDocument();
    setPermission(rootDoc, "joe", SecurityConstants.READ, true);
    // Under Oracle, the READ ACL optims are not visible from the joe
    // session while the transaction has not been committed.
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
        note = joeSession.getDocument(note.getRef());
        fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
        assertFalse(fsItem.getCanRename());
        assertFalse(fsItem.getCanDelete());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
    }
    resetPermissions(rootDoc, "joe");
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 8 with FileItem

use of org.nuxeo.drive.adapter.FileItem in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testCreateFilesWithSameName.

@Test
public void testCreateFilesWithSameName() {
    FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
    Blob blob = new StringBlob("This is a blob.");
    blob.setFilename("File01.txt");
    FileItem fileItem = folderItem.createFile(blob);
    FileItem fileItem2 = folderItem.createFile(blob);
    assertNotEquals(fileItem.getId(), fileItem2.getId());
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) FolderItem(org.nuxeo.drive.adapter.FolderItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Test(org.junit.Test)

Example 9 with FileItem

use of org.nuxeo.drive.adapter.FileItem in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testGetFileSystemItemById.

@Test
public void testGetFileSystemItemById() throws Exception {
    // Non existent doc id, must return null
    assertNull(defaultFileSystemItemFactory.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + "nonExistentDocId", principal));
    // File without a blob
    file.setPropertyValue("file:content", null);
    file = session.saveDocument(file);
    session.save();
    FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), principal);
    assertNull(fsItem);
    // Note
    fsItem = defaultFileSystemItemFactory.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId(), principal);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("aNote.txt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    Blob fileItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("aNote.txt", fileItemBlob.getFilename());
    assertEquals("Content of Bob's note.", fileItemBlob.getString());
    // Folder
    fsItem = defaultFileSystemItemFactory.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), principal);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Jack's folder", fsItem.getName());
    assertTrue(fsItem.isFolder());
    FolderItem folderItem = (FolderItem) fsItem;
    assertTrue(folderItem.getChildren().isEmpty());
    assertTrue(folderItem.getCanScrollDescendants());
    assertTrue(folderItem.scrollDescendants(null, 10, 1000).isEmpty());
    // Not adaptable as a FileSystemItem
    fsItem = defaultFileSystemItemFactory.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + notAFileSystemItem.getId(), principal);
    assertNull(fsItem);
    // Deleted
    custom.followTransition("delete");
    fsItem = defaultFileSystemItemFactory.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), principal);
    assertNull(fsItem);
    // Use parent id
    fsItem = defaultFileSystemItemFactory.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId(), syncRootItemId, principal);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) Test(org.junit.Test)

Example 10 with FileItem

use of org.nuxeo.drive.adapter.FileItem in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemManagerService method testReadOperations.

@Test
public void testReadOperations() throws Exception {
    // ------------------------------------------------------
    // Check #getTopLevelFolder
    // ------------------------------------------------------
    List<FileSystemItem> topLevelChildren = fileSystemItemManagerService.getTopLevelFolder(principal).getChildren();
    assertNotNull(topLevelChildren);
    assertEquals(2, topLevelChildren.size());
    FileSystemItem childFsItem = topLevelChildren.get(0);
    assertTrue(childFsItem instanceof DefaultSyncRootFolderItem);
    assertEquals("defaultSyncRootFolderItemFactory#test#" + syncRoot1.getId(), childFsItem.getId());
    assertTrue(childFsItem.getParentId().endsWith("DefaultTopLevelFolderItemFactory#"));
    assertEquals("syncRoot1", childFsItem.getName());
    childFsItem = topLevelChildren.get(1);
    assertTrue(childFsItem instanceof DefaultSyncRootFolderItem);
    assertEquals("defaultSyncRootFolderItemFactory#test#" + syncRoot2.getId(), childFsItem.getId());
    assertTrue(childFsItem.getParentId().endsWith("DefaultTopLevelFolderItemFactory#"));
    assertEquals("syncRoot2", childFsItem.getName());
    // ------------------------------------------------------
    // Check #exists
    // ------------------------------------------------------
    // Non existent doc id
    assertFalse(fileSystemItemManagerService.exists(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + "nonExistentId", principal));
    // File
    assertTrue(fileSystemItemManagerService.exists(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), principal));
    // Not adaptable as a FileSystemItem
    assertFalse(fileSystemItemManagerService.exists(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + notAFileSystemItem.getId(), principal));
    // Deleted
    custom.followTransition("delete");
    assertFalse(fileSystemItemManagerService.exists(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), principal));
    // ------------------------------------------------------------
    // Check #getFileSystemItemById(String id, Principal principal)
    // ------------------------------------------------------------
    // Folder
    FileSystemItem fsItem = fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), principal);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getId());
    String expectedSyncRoot1Id = DEFAULT_SYNC_ROOT_ITEM_ID_PREFIX + syncRoot1.getId();
    assertEquals(expectedSyncRoot1Id, fsItem.getParentId());
    assertEquals("Jack's folder", fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertTrue(fsItem.getCanRename());
    assertTrue(fsItem.getCanDelete());
    assertTrue(((FolderItem) fsItem).getCanCreateChild());
    List<FileSystemItem> children = ((FolderItem) fsItem).getChildren();
    assertNotNull(children);
    assertEquals(4, children.size());
    // File
    fsItem = fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), principal);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), fsItem.getId());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getParentId());
    assertEquals("Joe.odt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertTrue(fsItem.getCanRename());
    assertTrue(fsItem.getCanDelete());
    FileItem fileFsItem = (FileItem) fsItem;
    assertTrue(fileFsItem.getCanUpdate());
    assertEquals("nxfile/test/" + file.getId() + "/blobholder:0/Joe.odt", fileFsItem.getDownloadURL());
    assertEquals("MD5", fileFsItem.getDigestAlgorithm());
    assertEquals(file.getAdapter(BlobHolder.class).getBlob().getDigest(), fileFsItem.getDigest());
    Blob fileItemBlob = fileFsItem.getBlob();
    assertEquals("Joe.odt", fileItemBlob.getFilename());
    assertEquals("Content of Joe's file.", fileItemBlob.getString());
    // FolderishFile
    fsItem = fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folderishFile.getId(), principal);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folderishFile.getId(), fsItem.getId());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getParentId());
    assertEquals("Sarah's folderish file", fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertTrue(fsItem.getCanRename());
    assertTrue(fsItem.getCanDelete());
    assertTrue(((FolderItem) fsItem).getCanCreateChild());
    assertTrue(((FolderItem) fsItem).getChildren().isEmpty());
    // Not adaptable as a FileSystemItem
    fsItem = fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + notAFileSystemItem.getId(), principal);
    assertNull(fsItem);
    // Deleted
    assertNull(fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), principal));
    // Sub folder
    fsItem = fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder.getId(), principal);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder.getId(), fsItem.getId());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getParentId());
    assertEquals("Tony's sub folder", fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertTrue(fsItem.getCanRename());
    assertTrue(fsItem.getCanDelete());
    assertTrue(((FolderItem) fsItem).getCanCreateChild());
    assertTrue(((FolderItem) fsItem).getChildren().isEmpty());
    // -------------------------------------------------------------------
    // Check #getFileSystemItemById(String id, String parentId, Principal
    // principal)
    // -------------------------------------------------------------------
    fsItem = fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), principal);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), fsItem.getId());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getParentId());
    // ------------------------------------------------------
    // Check #getChildren
    // ------------------------------------------------------
    // Need to flush VCS cache for the session used in DocumentBackedFolderItem#getChildren() to be aware of changes
    // in the current session
    session.save();
    children = fileSystemItemManagerService.getChildren(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), principal);
    assertNotNull(children);
    assertEquals(4, children.size());
    // Ordered
    checkChildren(children, folder.getId(), file.getId(), note.getId(), folderishFile.getId(), subFolder.getId(), true);
    children = fileSystemItemManagerService.getChildren(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder.getId(), principal);
    assertTrue(children.isEmpty());
    // ------------------------------------------------------
    // Check #scrollDescendants
    // ------------------------------------------------------
    // Need to flush VCS cache for the session used in DocumentBackedFolderItem#scrollDescendants to be aware of
    // changes in the current session
    session.save();
    FolderItem folderItem = (FolderItem) fileSystemItemManagerService.getFileSystemItemById(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), principal);
    assertTrue(folderItem.getCanScrollDescendants());
    // Scroll through all descendants in one breath
    ScrollFileSystemItemList folderDescendants = fileSystemItemManagerService.scrollDescendants(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), principal, null, 10, 1000);
    assertNotNull(folderDescendants);
    assertNotNull(folderDescendants.getScrollId());
    assertEquals(4, folderDescendants.size());
    // Order is not determined
    checkChildren(folderDescendants, folder.getId(), file.getId(), note.getId(), folderishFile.getId(), subFolder.getId(), false);
    // Scroll through descendants in several steps
    folderDescendants.clear();
    ScrollFileSystemItemList descendantsBatch;
    int batchSize = 2;
    String scrollId = null;
    while (!(descendantsBatch = folderItem.scrollDescendants(scrollId, batchSize, 1000)).isEmpty()) {
        assertTrue(descendantsBatch.size() > 0);
        scrollId = descendantsBatch.getScrollId();
        folderDescendants.addAll(descendantsBatch);
    }
    assertEquals(4, folderDescendants.size());
    // Order is not determined
    checkChildren(folderDescendants, folder.getId(), file.getId(), note.getId(), folderishFile.getId(), subFolder.getId(), false);
    folderDescendants = fileSystemItemManagerService.scrollDescendants(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder.getId(), principal, null, 10, 1000);
    assertTrue(folderDescendants.isEmpty());
    // ------------------------------------------------------
    // Check #canMove
    // ------------------------------------------------------
    // Not allowed to move a file system item to a non FolderItem
    String srcFsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId();
    String destFsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId();
    assertFalse(fileSystemItemManagerService.canMove(srcFsItemId, destFsItemId, principal));
    // Not allowed to move a file system item if no REMOVE permission on the
    // source backing doc
    Principal joePrincipal = new NuxeoPrincipalImpl("joe");
    DocumentModel rootDoc = session.getRootDocument();
    setPermission(rootDoc, "joe", SecurityConstants.READ, true);
    nuxeoDriveManager.registerSynchronizationRoot(joePrincipal, syncRoot1, session);
    // Under Oracle, the READ ACL optims are not visible from the joe
    // session while the transaction has not been committed.
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    destFsItemId = DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder.getId();
    assertFalse(fileSystemItemManagerService.canMove(srcFsItemId, destFsItemId, joePrincipal));
    // Not allowed to move a file system item if no ADD_CHILDREN permission
    // on the destination backing doc
    setPermission(folder, "joe", SecurityConstants.WRITE, true);
    setPermission(subFolder, "joe", SecurityConstants.READ, true);
    setPermission(subFolder, SecurityConstants.ADMINISTRATOR, SecurityConstants.EVERYTHING, true);
    setPermission(subFolder, SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false);
    assertFalse(fileSystemItemManagerService.canMove(srcFsItemId, destFsItemId, joePrincipal));
    // OK: REMOVE permission on the source backing doc + REMOVE_CHILDREN
    // permission on its parent + ADD_CHILDREN permission on the destination
    // backing doc
    resetPermissions(subFolder, SecurityConstants.EVERYONE);
    resetPermissions(subFolder, "joe");
    setPermission(subFolder, "joe", SecurityConstants.WRITE, true);
    assertTrue(fileSystemItemManagerService.canMove(srcFsItemId, destFsItemId, joePrincipal));
    // Reset permissions
    resetPermissions(rootDoc, "joe");
    resetPermissions(folder, "joe");
    resetPermissions(subFolder, "joe");
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FileItem(org.nuxeo.drive.adapter.FileItem) NuxeoPrincipalImpl(org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) Principal(java.security.Principal) Test(org.junit.Test)

Aggregations

FileItem (org.nuxeo.drive.adapter.FileItem)16 Test (org.junit.Test)12 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)12 Blob (org.nuxeo.ecm.core.api.Blob)11 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)9 FolderItem (org.nuxeo.drive.adapter.FolderItem)8 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)7 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)6 ScrollFileSystemItemList (org.nuxeo.drive.adapter.ScrollFileSystemItemList)3 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)3 Deploy (org.nuxeo.runtime.test.runner.Deploy)3 FileSystemItemFactory (org.nuxeo.drive.service.FileSystemItemFactory)2 FileSystemItemAdapterServiceImpl (org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl)2 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1