Search in sources :

Example 6 with StringBlob

use of org.nuxeo.ecm.core.api.impl.blob.StringBlob in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testCollectionMembership.

@Test
public void testCollectionMembership() {
    DocumentModel doc = session.createDocumentModel(session.getRootDocument().getPathAsString(), "testDoc", "File");
    Blob blob = new StringBlob("Content of Joe's file.");
    blob.setFilename("Joe.odt");
    doc.setPropertyValue("file:content", (Serializable) blob);
    doc = session.createDocument(doc);
    log.trace("Try to adapt a document not member of any collection");
    try {
        defaultFileSystemItemFactory.getFileSystemItem(doc);
        fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
    } catch (RootlessItemException e) {
        log.trace(e);
    }
    log.trace("Try to adapt a document member of a non sync root collection");
    DocumentModel nonSyncrootCollection = collectionManager.createCollection(session, "Non sync root collection", "", session.getRootDocument().getPathAsString());
    collectionManager.addToCollection(nonSyncrootCollection, doc, session);
    try {
        defaultFileSystemItemFactory.getFileSystemItem(doc);
        fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
    } catch (RootlessItemException e) {
        log.trace(e);
    }
    log.trace("Adapt a document member of a non sync root colllection and a sync root collection");
    DocumentModel syncRootCollection = collectionManager.createCollection(session, "Sync root collection", "", session.getRootDocument().getPathAsString());
    nuxeoDriveManager.registerSynchronizationRoot(principal, syncRootCollection, session);
    collectionManager.addToCollection(syncRootCollection, doc, session);
    FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(doc);
    assertNotNull(fsItem);
    log.trace("Adapt a document member of a sync root collection only");
    collectionManager.removeFromCollection(nonSyncrootCollection, doc, session);
    assertEquals(fsItem, defaultFileSystemItemFactory.getFileSystemItem(doc));
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 7 with StringBlob

use of org.nuxeo.ecm.core.api.impl.blob.StringBlob 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 8 with StringBlob

use of org.nuxeo.ecm.core.api.impl.blob.StringBlob in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method createTestDocs.

@Before
public void createTestDocs() throws Exception {
    principal = session.getPrincipal();
    syncRootFolder = session.createDocumentModel("/", "syncRoot", "Folder");
    syncRootFolder = session.createDocument(syncRootFolder);
    nuxeoDriveManager.registerSynchronizationRoot(principal, syncRootFolder, session);
    // Expected sync root FS item id
    syncRootItemId = DEFAULT_SYNC_ROOT_ITEM_ID_PREFIX + syncRootFolder.getId();
    // File
    file = session.createDocumentModel(syncRootFolder.getPathAsString(), "aFile", "File");
    Blob blob = new StringBlob("Content of Joe's file.");
    blob.setFilename("Joe.odt");
    file.setPropertyValue("file:content", (Serializable) blob);
    file = session.createDocument(file);
    // Note
    note = session.createDocumentModel(syncRootFolder.getPathAsString(), "aNote", "Note");
    note.setPropertyValue("note:note", "Content of Bob's note.");
    note = session.createDocument(note);
    // Custom doc type with the "file" schema
    custom = session.createDocumentModel(syncRootFolder.getPathAsString(), "aCustomDoc", "Custom");
    blob = new StringBlob("Content of Bonnie's file.");
    blob.setFilename("Bonnie's file.odt");
    custom.setPropertyValue("file:content", (Serializable) blob);
    custom = session.createDocument(custom);
    // Folder
    folder = session.createDocumentModel(syncRootFolder.getPathAsString(), "aFolder", "Folder");
    folder.setPropertyValue("dc:title", "Jack's folder");
    folder = session.createDocument(folder);
    // FolderishFile: doc type with the "file" schema and the "Folderish"
    // facet
    folderishFile = session.createDocumentModel(syncRootFolder.getPathAsString(), "aFolderishFile", "FolderishFile");
    folderishFile.setPropertyValue("dc:title", "Sarah's folderish file");
    folderishFile = session.createDocument(folderishFile);
    // Doc not adaptable as a FileSystemItem (not Folderish nor a
    // BlobHolder)
    notAFileSystemItem = session.createDocumentModel(syncRootFolder.getPathAsString(), "notAFileSystemItem", "NotSynchronizable");
    notAFileSystemItem = session.createDocument(notAFileSystemItem);
    session.save();
    // Get default file system item factory
    defaultFileSystemItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("defaultFileSystemItemFactory");
}
Also used : 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) Before(org.junit.Before)

Example 9 with StringBlob

use of org.nuxeo.ecm.core.api.impl.blob.StringBlob in project nuxeo-drive-server by nuxeo.

the class TestCollectionSyncRootFolderItemFactory method testFactory.

@Test
public void testFactory() throws Exception {
    FileSystemItemFactory collectionSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("collectionSyncRootFolderItemFactory");
    DocumentModel collection = collectionManager.createCollection(session, "testCollection", "Test collection.", "/");
    DocumentModel doc1 = session.createDocumentModel("/", "doc1", "File");
    doc1.setPropertyValue("dc:title", "doc1");
    doc1.setPropertyValue("file:content", new StringBlob("Content of file 1."));
    doc1 = session.createDocument(doc1);
    collectionManager.addToCollection(collection, doc1, session);
    assertTrue(collectionManager.isInCollection(collection, doc1, session));
    DocumentModel doc2 = session.createDocumentModel("/", "doc2", "File");
    doc2.setPropertyValue("dc:title", "doc2");
    doc2.setPropertyValue("file:content", new StringBlob("Content of file 2."));
    doc2 = session.createDocument(doc2);
    collectionManager.addToCollection(collection, doc2, session);
    assertTrue(collectionManager.isInCollection(collection, doc2, session));
    log.trace("Check document that is not a Collection");
    assertFalse(collectionSyncRootFolderItemFactory.isFileSystemItem(session.getRootDocument()));
    log.trace("Check Collection not registered as a sync root");
    assertFalse(collectionSyncRootFolderItemFactory.isFileSystemItem(collection));
    log.trace("Check Collection registered as a sync root");
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collection, session);
    assertTrue(collectionSyncRootFolderItemFactory.isFileSystemItem(collection));
    log.trace("Adapt test collection as a FileSystemItem");
    FileSystemItem fsItem = collectionSyncRootFolderItemFactory.getFileSystemItem(collection);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof CollectionSyncRootFolderItem);
    log.trace("Check children");
    FolderItem collectionFSItem = (FolderItem) fsItem;
    List<FileSystemItem> collectionChildren = collectionFSItem.getChildren();
    assertEquals(2, collectionChildren.size());
    FileSystemItem child1 = collectionChildren.get(0);
    assertTrue(child1 instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc1.getId(), child1.getId());
    assertEquals(COLLECTION_SYNC_ROOT_ITEM_ID_PREFIX + collection.getId(), child1.getParentId());
    assertEquals("doc1", child1.getName());
    FileSystemItem child2 = collectionChildren.get(1);
    assertTrue(child2 instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc2.getId(), child2.getId());
    assertEquals(COLLECTION_SYNC_ROOT_ITEM_ID_PREFIX + collection.getId(), child2.getParentId());
    assertEquals("doc2", child2.getName());
    log.trace("Check FolderItem#getCanScrollDescendants");
    assertFalse(collectionFSItem.getCanScrollDescendants());
    log.trace("Check descendants");
    try {
        collectionFSItem.scrollDescendants(null, 10, 1000);
        fail("Should not be able to scroll through the descendants of a CollectionSyncRootFolderItem.");
    } catch (UnsupportedOperationException e) {
        assertEquals("Cannot scroll through the descendants of a collection sync root folder item, please call getChildren() instead.", e.getMessage());
    }
    log.trace("Check FolderItem#getCanCreateChild");
    assertFalse(collectionFSItem.getCanCreateChild());
    log.trace("Check FolderItem#createFile");
    try {
        collectionFSItem.createFile(new StringBlob("Child file content."));
        fail("Should not be able to create a file in a CollectionSyncRootFolderItem.");
    } catch (UnsupportedOperationException e) {
        assertEquals("Cannot create a file in a collection synchronization root.", e.getMessage());
    }
    log.trace("Check FolderItem#createFolder");
    try {
        collectionFSItem.createFolder("Child folder");
        fail("Should not be able to create a folder in a CollectionSyncRootFolderItem.");
    } catch (UnsupportedOperationException e) {
        assertEquals("Cannot create a folder in a collection synchronization root.", e.getMessage());
    }
    log.trace("Test AbstractDocumentBackedFileSystemItem#delete");
    child1.delete();
    doc1 = session.getDocument(doc1.getRef());
    assertFalse(doc1.isTrashed());
    assertFalse(collectionManager.isInCollection(collection, doc1, session));
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) CollectionSyncRootFolderItem(org.nuxeo.drive.adapter.impl.CollectionSyncRootFolderItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) CollectionSyncRootFolderItem(org.nuxeo.drive.adapter.impl.CollectionSyncRootFolderItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 10 with StringBlob

use of org.nuxeo.ecm.core.api.impl.blob.StringBlob in project nuxeo-drive-server by nuxeo.

the class TestDefaultTopLevelFolderItemFactory method createTestDocs.

@Before
public void createTestDocs() throws Exception {
    // Create and register 2 synchronization roots for Administrator
    syncRoot1 = session.createDocument(session.createDocumentModel("/", "syncRoot1", "Folder"));
    syncRoot2 = session.createDocument(session.createDocumentModel("/", "syncRoot2", "Folder"));
    Principal administrator = session.getPrincipal();
    nuxeoDriveManager.registerSynchronizationRoot(administrator, syncRoot1, session);
    nuxeoDriveManager.registerSynchronizationRoot(administrator, syncRoot2, session);
    // Add a child file to syncRoot1
    DocumentModel syncRoot1Child = session.createDocumentModel("/syncRoot1", "syncRoot1Child", "File");
    Blob blob = new StringBlob("Content of Joe's file.");
    blob.setFilename("Joe.odt");
    syncRoot1Child.setPropertyValue("file:content", (Serializable) blob);
    syncRoot1Child = session.createDocument(syncRoot1Child);
    // Flush the session so that the other session instances from the
    // FileSystemManager service.
    session.save();
    // Get default top level folder item factory
    defaultTopLevelFolderItemFactory = fileSystemItemAdapterService.getTopLevelFolderItemFactory();
    assertTrue(defaultTopLevelFolderItemFactory instanceof DefaultTopLevelFolderItemFactory);
    assertEquals("Nuxeo Drive", defaultTopLevelFolderItemFactory.getFolderName());
}
Also used : 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) DefaultTopLevelFolderItemFactory(org.nuxeo.drive.service.impl.DefaultTopLevelFolderItemFactory) Principal(java.security.Principal) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Before(org.junit.Before)

Aggregations

StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)32 Test (org.junit.Test)24 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)24 Blob (org.nuxeo.ecm.core.api.Blob)17 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)13 HashSet (java.util.HashSet)12 FileItem (org.nuxeo.drive.adapter.FileItem)7 Principal (java.security.Principal)5 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)5 FolderItem (org.nuxeo.drive.adapter.FolderItem)5 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)5 Before (org.junit.Before)4 FileSystemChangeSummary (org.nuxeo.drive.service.FileSystemChangeSummary)4 RootlessItemException (org.nuxeo.drive.adapter.RootlessItemException)2 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)2 SimpleFileSystemItemChange (org.nuxeo.drive.fixtures.SimpleFileSystemItemChange)2 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)2 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)2 PathRef (org.nuxeo.ecm.core.api.PathRef)2 Deploy (org.nuxeo.runtime.test.runner.Deploy)2