Search in sources :

Example 1 with ScrollFileSystemItemList

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

the class DefaultFileSystemItemFactoryFixture method testScrollDescendantsIncludingCollections.

@Test
public void testScrollDescendantsIncludingCollections() {
    log.trace("Add a document to a new collection \"testCollection\" created in \"/default-domain/UserWorkspaces/Administrator/Collections\"");
    collectionManager.addToNewCollection("testCollection", null, file, session);
    DocumentModel userCollections = collectionManager.getUserDefaultCollections(null, session);
    DocumentModel userWorkspace = session.getParentDocument(userCollections.getRef());
    log.trace("Create \"testFolder\" in \"/default-domain/UserWorkspaces/Administrator\"");
    DocumentModel testFolder = session.createDocumentModel(userWorkspace.getPathAsString(), "testFolder", "Folder");
    testFolder = session.createDocument(testFolder);
    log.trace("Register \"/default-domain/UserWorkspaces/Administrator\" as a synchronization root for Administrator");
    nuxeoDriveManager.registerSynchronizationRoot(principal, userWorkspace, session);
    log.trace("Scroll through the descendants of \"/default-domain/UserWorkspaces/Administrator\", expecting one: \"testFolder\", " + "the \"Collections\" folder and its descendants being ignored");
    FileSystemItemFactory defaultSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("defaultSyncRootFolderItemFactory");
    FolderItem userWorkspaceFolderItem = (FolderItem) defaultSyncRootFolderItemFactory.getFileSystemItem(userWorkspace);
    ScrollFileSystemItemList descendants = userWorkspaceFolderItem.scrollDescendants(null, 10, 1000);
    assertEquals(1, descendants.size());
    FileSystemItem descendant = descendants.get(0);
    assertTrue(descendant.isFolder());
    assertEquals("testFolder", descendant.getName());
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) DefaultFileSystemItemFactory(org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) FolderItem(org.nuxeo.drive.adapter.FolderItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 2 with ScrollFileSystemItemList

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

the class DefaultFileSystemItemFactoryFixture method testFolderItem.

@Test
public void testFolderItem() throws Exception {
    // ------------------------------------------------------
    // FolderItem#canCreateChild
    // ------------------------------------------------------
    // As Administrator
    FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
    assertTrue(folderItem.getCanCreateChild());
    // 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")) {
        folder = joeSession.getDocument(folder.getRef());
        // should not be mappable as an fs item.
        try {
            defaultFileSystemItemFactory.getFileSystemItem(folder);
            fail("Should have raised RootlessItemException as ");
        } catch (RootlessItemException e) {
        // expected
        }
        // Register the sync root for Joe's account
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        assertFalse(folderItem.getCanCreateChild());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        assertTrue(folderItem.getCanCreateChild());
    }
    resetPermissions(rootDoc, "joe");
    // ------------------------------------------------------
    // FolderItem#createFile and FolderItem#createFolder
    // ------------------------------------------------------
    folder = session.getDocument(folder.getRef());
    folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
    // Note
    Blob childBlob = new StringBlob("This is the Note child.");
    childBlob.setFilename("Note child.txt");
    folderItem.createFile(childBlob);
    // File
    childBlob = new StringBlob("This is the File child.");
    childBlob.setFilename("File child.odt");
    childBlob.setMimeType("application/vnd.oasis.opendocument.text");
    folderItem.createFile(childBlob);
    // Folder
    folderItem.createFolder("Sub-folder");
    DocumentModelList children = session.query(String.format("select * from Document where ecm:parentId = '%s' order by ecm:primaryType asc", folder.getId()));
    assertEquals(3, children.size());
    // Check File
    DocumentModel file = children.get(0);
    assertEquals("File", file.getType());
    assertEquals("File child.odt", file.getTitle());
    childBlob = (Blob) file.getPropertyValue("file:content");
    assertEquals("File child.odt", childBlob.getFilename());
    assertEquals("This is the File child.", childBlob.getString());
    // Check Folder
    DocumentModel subFolder = children.get(1);
    assertEquals("Folder", subFolder.getType());
    assertEquals("Sub-folder", subFolder.getTitle());
    // Check Note
    DocumentModel note = children.get(2);
    assertEquals("Note", note.getType());
    assertEquals("Note child.txt", note.getTitle());
    childBlob = note.getAdapter(BlobHolder.class).getBlob();
    assertEquals("Note child.txt", childBlob.getFilename());
    assertEquals("This is the Note child.", childBlob.getString());
    // --------------------------------------------------------------------------------------------
    // FolderItem#getChildren, FolderItem#getCanScrollDescendants and
    // FolderItem#scrollDescendants
    // --------------------------------------------------------------------------------------------
    // Create another child adaptable as a FileSystemItem => should be
    // retrieved
    DocumentModel adaptableChild = session.createDocumentModel("/syncRoot/aFolder", "adaptableChild", "File");
    Blob adaptableChildBlob = new StringBlob("Content of another file.");
    adaptableChildBlob.setFilename("Another file.odt");
    adaptableChild.setPropertyValue("file:content", (Serializable) adaptableChildBlob);
    adaptableChild = session.createDocument(adaptableChild);
    // Create another child not adaptable as a FileSystemItem => should
    // not be retrieved
    session.createDocument(session.createDocumentModel("/syncRoot/aFolder", "notAdaptableChild", "NotSynchronizable"));
    session.save();
    // Check getChildren
    List<FileSystemItem> folderChildren = folderItem.getChildren();
    assertEquals(4, folderChildren.size());
    // Ordered
    checkChildren(folderChildren, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), true);
    // Check scrollDescendants
    assertTrue(folderItem.getCanScrollDescendants());
    // Scroll through all descendants in one breath
    ScrollFileSystemItemList folderDescendants = folderItem.scrollDescendants(null, 10, 1000);
    String scrollId = folderDescendants.getScrollId();
    assertNotNull(scrollId);
    assertEquals(4, folderDescendants.size());
    // Order is not determined
    checkChildren(folderDescendants, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
    // Check that next call to scrollDescendants returns an empty list
    assertTrue(folderItem.scrollDescendants(scrollId, 10, 1000).isEmpty());
    // Scroll through descendants in several steps
    folderDescendants.clear();
    ScrollFileSystemItemList descendantsBatch;
    int batchSize = 2;
    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(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
    // Check batch size limit
    try {
        folderItem.scrollDescendants(null, 10000, 1000);
        fail("Should not be able to scroll through more descendants than the maximum batch size allowed.");
    } catch (NuxeoException e) {
        log.trace(e);
    }
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FolderItem(org.nuxeo.drive.adapter.FolderItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) DocumentModelList(org.nuxeo.ecm.core.api.DocumentModelList) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) Test(org.junit.Test)

Example 3 with ScrollFileSystemItemList

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

the class TestFileSystemItemOperations method testScrollDescendants.

@Test
public void testScrollDescendants() throws Exception {
    // Get descendants of sync root 1
    // Scroll through all descendants in one breath
    Blob descendantsJSON = (Blob) clientSession.newRequest(NuxeoDriveScrollDescendants.ID).set("id", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId()).set("batchSize", 10).execute();
    assertNotNull(descendantsJSON);
    ScrollFileSystemItemList descendants = mapper.readValue(descendantsJSON.getStream(), ScrollFileSystemItemListImpl.class);
    assertNotNull(descendants);
    assertNotNull(descendants.getScrollId());
    assertEquals(4, descendants.size());
    List<String> expectedIds = Arrays.asList(file1, subFolder1, file3, file4).stream().map(doc -> DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc.getId()).collect(Collectors.toList());
    List<String> descendantIds = descendants.stream().map(fsItem -> fsItem.getId()).collect(Collectors.toList());
    // Note that order is not determined
    assertTrue(CollectionUtils.isEqualCollection(expectedIds, descendantIds));
    // Scroll through descendants in several steps
    descendantIds.clear();
    ScrollFileSystemItemList descendantsBatch;
    int batchSize = 2;
    String scrollId = null;
    while (!(descendantsBatch = mapper.readValue(((Blob) clientSession.newRequest(NuxeoDriveScrollDescendants.ID).set("id", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId()).set("batchSize", batchSize).set("scrollId", scrollId).execute()).getStream(), ScrollFileSystemItemListImpl.class)).isEmpty()) {
        assertTrue(descendantsBatch.size() > 0);
        scrollId = descendantsBatch.getScrollId();
        descendantIds.addAll(descendantsBatch.stream().map(fsItem -> fsItem.getId()).collect(Collectors.toList()));
    }
    assertEquals(4, descendantIds.size());
    // Note that order is not determined
    assertTrue(CollectionUtils.isEqualCollection(expectedIds, descendantIds));
    // Check descendants of sub-folder of sync root 1
    assertTrue(CollectionUtils.isEqualCollection(Arrays.asList(file3, file4).stream().map(doc -> DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc.getId()).collect(Collectors.toList()), mapper.readValue(((Blob) clientSession.newRequest(NuxeoDriveScrollDescendants.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder1.getId()).set("batchSize", 10).execute()).getStream(), JsonNode.class).findValuesAsText("id")));
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) NuxeoPrincipal(org.nuxeo.ecm.core.api.NuxeoPrincipal) ACE(org.nuxeo.ecm.core.api.security.ACE) Arrays(java.util.Arrays) DirectoryService(org.nuxeo.ecm.directory.api.DirectoryService) Map(java.util.Map) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) Assert.fail(org.junit.Assert.fail) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Session(org.nuxeo.ecm.automation.client.Session) ScrollFileSystemItemListImpl(org.nuxeo.drive.adapter.impl.ScrollFileSystemItemListImpl) SecurityConstants(org.nuxeo.ecm.core.api.security.SecurityConstants) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) IdRef(org.nuxeo.ecm.core.api.IdRef) HttpAutomationClient(org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Blob(org.nuxeo.ecm.automation.client.model.Blob) List(java.util.List) Features(org.nuxeo.runtime.test.runner.Features) Principal(java.security.Principal) Assert.assertFalse(org.junit.Assert.assertFalse) ACP(org.nuxeo.ecm.core.api.security.ACP) Jetty(org.nuxeo.runtime.test.runner.Jetty) ACL(org.nuxeo.ecm.core.api.security.ACL) CoreFeature(org.nuxeo.ecm.core.test.CoreFeature) PathRef(org.nuxeo.ecm.core.api.PathRef) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Inject(javax.inject.Inject) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FeaturesRunner(org.nuxeo.runtime.test.runner.FeaturesRunner) CollectionUtils(org.apache.commons.collections.CollectionUtils) FileSystemItemAdapterService(org.nuxeo.drive.service.FileSystemItemAdapterService) TransactionHelper(org.nuxeo.runtime.transaction.TransactionHelper) Framework(org.nuxeo.runtime.api.Framework) DocumentBackedFolderItem(org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem) UserManager(org.nuxeo.ecm.platform.usermanager.UserManager) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) Before(org.junit.Before) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FolderItem(org.nuxeo.drive.adapter.FolderItem) Assert.assertNull(org.junit.Assert.assertNull) ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) DefaultTopLevelFolderItem(org.nuxeo.drive.adapter.impl.DefaultTopLevelFolderItem) CoreSession(org.nuxeo.ecm.core.api.CoreSession) Assert.assertEquals(org.junit.Assert.assertEquals) Blob(org.nuxeo.ecm.automation.client.model.Blob) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 4 with ScrollFileSystemItemList

use of org.nuxeo.drive.adapter.ScrollFileSystemItemList 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 5 with ScrollFileSystemItemList

use of org.nuxeo.drive.adapter.ScrollFileSystemItemList 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

ScrollFileSystemItemList (org.nuxeo.drive.adapter.ScrollFileSystemItemList)8 FolderItem (org.nuxeo.drive.adapter.FolderItem)7 Test (org.junit.Test)6 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)6 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)6 Blob (org.nuxeo.ecm.core.api.Blob)4 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)4 FileItem (org.nuxeo.drive.adapter.FileItem)3 Serializable (java.io.Serializable)2 Principal (java.security.Principal)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Before (org.junit.Before)2 RunWith (org.junit.runner.RunWith)2 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)2 FileSystemItemAdapterService (org.nuxeo.drive.service.FileSystemItemAdapterService)2