Search in sources :

Example 1 with StringBlob

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

the class AuditChangeFinderTestSuite method testFolderishCollection2.

/**
 * <pre>
 * /folder1                 -> synchronization root && isMemberOf(collectionFolder)
 *   |-- collectionFolder
 * /collectionSyncRoot      -> synchronization root
 * /testDoc                 -> isMemberOf(collectionFolder, collectionSyncRoot)
 * </pre>
 */
@Test
public void testFolderishCollection2() throws Exception {
    DocumentModel collectionFolder;
    DocumentModel collectionSyncRoot;
    DocumentModel testDoc;
    List<FileSystemItemChange> changes;
    try {
        log.trace("testFolderishCollection2():" + "\nCreate a folder with the Collection facet (\"collectionFolder\") inside a folder (\"folder1\");" + "\nAdd \"folder1\" to the \"collectionFolder\" collection;" + "\nRegister \"folder1\" as a synchronization root;" + "\nCreate a collection \"collectionSyncRoot\" and register it as a synchronization root;" + "\nCreate a document \"testDoc\" and add it to both collections \"collectionFolder\" and \"collectionSyncRoot\".\n");
        collectionFolder = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder = session.createDocument(collectionFolder);
        collectionManager.addToCollection(collectionFolder, folder1, session);
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder1, session);
        collectionSyncRoot = collectionManager.createCollection(session, "collectionSyncRoot", null, "/");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collectionSyncRoot, session);
        testDoc = session.createDocumentModel("/", "testDoc", "File");
        testDoc.setPropertyValue("file:content", new StringBlob("The content of testDoc."));
        testDoc = session.createDocument(testDoc);
        collectionManager.addToCollection(collectionFolder, testDoc, session);
        collectionManager.addToCollection(collectionSyncRoot, testDoc, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 12 (among which 10 distinct) changes:
        // - addedToCollection for testDoc
        // - documentModified for collectionSyncRoot
        // - addedToCollection for testDoc
        // - documentModified for collectionFolder
        // - documentCreated for testDoc
        // - rootRegistered for collectionSyncRoot
        // - documentCreated for collectionSyncRoot
        // - rootRegistered for folder1
        // - addedToCollection for folder1
        // - documentModified for collectionFolder
        // - documentCreated for collectionFolder
        // - documentCreated for folder1
        changes = getChanges(session.getPrincipal());
        assertEquals(12, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionFolder.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionFolder.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with StringBlob

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

the class AuditChangeFinderTestSuite method testFolderishCollection1.

/**
 * <pre>
 * /folder1                 -> isMemberOf(collectionFolder2)
 *   |-- collectionFolder1
 *   |-- collectionFolder2
 * /collectionSyncRoot      -> synchronization root
 * /testDoc                 -> isMemberOf(collectionFolder1, collectionSyncRoot)
 * </pre>
 */
@Test
public void testFolderishCollection1() throws Exception {
    DocumentModel collectionSyncRoot;
    DocumentModel testDoc;
    List<FileSystemItemChange> changes;
    try {
        log.trace("testFolderishCollection1():" + "\nCreate a folder with the Collection facet (\"collectionFolder1\") inside a folder (\"folder1\");" + "\nCreate a folder with the Collection facet (\"collectionFolder2\") inside a folder (\"folder1\");" + "\nAdd \"folder1\" to the \"collectionFolder2\" collection;" + "\nCreate a collection \"collectionSyncRoot\" and register it as a synchronization root;" + "\nCreate a document \"testDoc\" and add it to both collections \"collectionFolder1\" and \"collectionSyncRoot\".\n");
        DocumentModel collectionFolder1 = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder1 = session.createDocument(collectionFolder1);
        DocumentModel collectionFolder2 = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder2 = session.createDocument(collectionFolder2);
        collectionManager.addToCollection(collectionFolder2, folder1, session);
        collectionSyncRoot = collectionManager.createCollection(session, "collectionSyncRoot", null, "/");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collectionSyncRoot, session);
        testDoc = session.createDocumentModel("/", "testDoc", "File");
        testDoc.setPropertyValue("file:content", new StringBlob("The content of testDoc."));
        testDoc = session.createDocument(testDoc);
        collectionManager.addToCollection(collectionFolder1, testDoc, session);
        collectionManager.addToCollection(collectionSyncRoot, testDoc, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 6 (among which 5 distinct) changes:
        // - addedToCollection for testDoc
        // - documentModified for collectionSyncRoot
        // - addedToCollection for testDoc
        // - documentCreated for testDoc
        // - rootRegistered for collectionSyncRoot
        // - documentCreated for collectionSyncRoot
        changes = getChanges(session.getPrincipal());
        assertEquals(6, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with StringBlob

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

the class AuditChangeFinderTestSuite method testFolderishCollection.

/**
 * <pre>
 * /folder1                 -> isMemberOf(collectionFolder)
 *   |-- collectionFolder
 * /collectionSyncRoot      -> synchronization root
 * /testDoc                 -> isMemberOf(collectionFolder, collectionSyncRoot)
 * </pre>
 */
@Test
public void testFolderishCollection() throws Exception {
    DocumentModel collectionSyncRoot;
    DocumentModel testDoc;
    List<FileSystemItemChange> changes;
    try {
        log.trace("testFolderishCollection():" + "\nCreate a folder with the Collection facet (\"collectionFolder\") inside a folder (\"folder1\");" + "\nAdd \"folder1\" to the \"collectionFolder\" collection;" + "\nCreate a collection \"collectionSyncRoot\" and register it as a synchronization root;" + "\nCreate a document \"testDoc\" and add it to both collections \"collectionFolder\" and \"collectionSyncRoot\".\n");
        DocumentModel collectionFolder = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder = session.createDocument(collectionFolder);
        collectionManager.addToCollection(collectionFolder, folder1, session);
        collectionSyncRoot = collectionManager.createCollection(session, "collectionSyncRoot", null, "/");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collectionSyncRoot, session);
        testDoc = session.createDocumentModel("/", "testDoc", "File");
        testDoc.setPropertyValue("file:content", new StringBlob("The content of testDoc."));
        testDoc = session.createDocument(testDoc);
        collectionManager.addToCollection(collectionFolder, testDoc, session);
        collectionManager.addToCollection(collectionSyncRoot, testDoc, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 6 (among which 5 distinct) changes:
        // - addedToCollection for testDoc
        // - documentModified for collectionSyncRoot
        // - addedToCollection for testDoc
        // - documentCreated for testDoc
        // - rootRegistered for collectionSyncRoot
        // - documentCreated for collectionSyncRoot
        changes = getChanges(session.getPrincipal());
        assertEquals(6, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with StringBlob

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

the class AuditChangeFinderTestSuite method testFindChanges.

@Test
public void testFindChanges() throws Exception {
    List<FileSystemItemChange> changes;
    DocumentModel doc1;
    DocumentModel doc2;
    DocumentModel doc3;
    DocumentModel docToCopy;
    DocumentModel copiedDoc;
    DocumentModel docToVersion;
    commitAndWaitForAsyncCompletion();
    try {
        // No sync roots
        changes = getChanges();
        assertNotNull(changes);
        assertTrue(changes.isEmpty());
        log.trace("Sync roots for Administrator");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder1, session);
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder2, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Get changes for Administrator
        changes = getChanges();
        // Root registration events
        assertEquals(2, changes.size());
        log.trace("Create 3 documents, only 2 in sync roots");
        doc1 = session.createDocumentModel("/folder1", "doc1", "File");
        doc1.setPropertyValue("file:content", new StringBlob("The content of file 1."));
        doc1 = session.createDocument(doc1);
        doc2 = session.createDocumentModel("/folder2", "doc2", "File");
        doc2.setPropertyValue("file:content", new StringBlob("The content of file 2."));
        doc2 = session.createDocument(doc2);
        doc3 = session.createDocumentModel("/folder3", "doc3", "File");
        doc3.setPropertyValue("file:content", new StringBlob("The content of file 3."));
        doc3 = session.createDocument(doc3);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "documentCreated", "test"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "documentCreated", "test"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        // No changes since last successful sync
        changes = getChanges();
        assertTrue(changes.isEmpty());
        log.trace("Update both synchronized documents and unsynchronize a root");
        doc1.setPropertyValue("file:content", new StringBlob("The content of file 1, updated."));
        session.saveDocument(doc1);
        doc2.setPropertyValue("file:content", new StringBlob("The content of file 2, updated."));
        session.saveDocument(doc2);
        nuxeoDriveManager.unregisterSynchronizationRoot(session.getPrincipal(), folder2, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(2, changes.size());
        // The root unregistration is mapped to a fake deletion from the
        // client's point of view
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "deleted", "test"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "documentModified", "test"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Delete a document with a lifecycle transition (trash)");
        session.followTransition(doc1.getRef(), "delete");
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(doc1.getId(), "deleted", "test", "test#" + doc1.getId()), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Restore a deleted document and move a document in a newly synchronized root");
        session.followTransition(doc1.getRef(), "undelete");
        session.move(doc3.getRef(), folder2.getRef(), null);
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder2, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(3, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + folder2.getId()));
        expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "documentMoved", "test"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "lifecycle_transition_event", "test"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Physical deletion without triggering the delete transition first");
        session.removeDocument(doc3.getRef());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(doc3.getId(), "deleted", "test", "test#" + doc3.getId()), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Create a doc and copy it from a sync root to another one");
        docToCopy = session.createDocumentModel("/folder1", "docToCopy", "File");
        docToCopy.setPropertyValue("file:content", new StringBlob("The content of file to copy."));
        docToCopy = session.createDocument(docToCopy);
        copiedDoc = session.copy(docToCopy.getRef(), folder2.getRef(), null);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(copiedDoc.getId(), "documentCreatedByCopy", "test", "defaultFileSystemItemFactory#test#" + copiedDoc.getId(), "docToCopy"));
        expectedChanges.add(new SimpleFileSystemItemChange(docToCopy.getId(), "documentCreated", "test", "defaultFileSystemItemFactory#test#" + docToCopy.getId(), "docToCopy"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Remove file from a document, mapped to a fake deletion from the client's point of view");
        doc1.setPropertyValue("file:content", null);
        session.saveDocument(doc1);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(doc1.getId(), "deleted", "test"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Move a doc from a sync root to another sync root");
        session.move(copiedDoc.getRef(), folder1.getRef(), null);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(copiedDoc.getId(), "documentMoved", "test"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Move a doc from a sync root to a non synchronized folder");
        session.move(copiedDoc.getRef(), folder3.getRef(), null);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(copiedDoc.getId(), "deleted", "test"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Create a doc, create a version of it, update doc and restore the version");
        docToVersion = session.createDocumentModel("/folder1", "docToVersion", "File");
        docToVersion.setPropertyValue("file:content", new StringBlob("The content of file to version."));
        docToVersion = session.createDocument(docToVersion);
        docToVersion.putContextData(VersioningService.VERSIONING_OPTION, VersioningOption.MAJOR);
        session.saveDocument(docToVersion);
        docToVersion.setPropertyValue("file:content", new StringBlob("Updated content of the versioned file."));
        session.saveDocument(docToVersion);
        List<DocumentModel> versions = session.getVersions(docToVersion.getRef());
        assertEquals(1, versions.size());
        DocumentModel version = versions.get(0);
        session.restoreToVersion(docToVersion.getRef(), version.getRef());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges();
        // Expecting 4 (among which 3 distinct) changes:
        // - documentRestored for docToVersion
        // - documentModified for docToVersion (2 occurrences)
        // - documentCreated for docToVersion
        assertEquals(4, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(docToVersion.getId(), "documentRestored"));
        expectedChanges.add(new SimpleFileSystemItemChange(docToVersion.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(docToVersion.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Too many changes");
        session.followTransition(doc1.getRef(), "delete");
        session.followTransition(doc2.getRef(), "delete");
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    Framework.getProperties().put("org.nuxeo.drive.document.change.limit", "1");
    FileSystemChangeSummary changeSummary = getChangeSummary(session.getPrincipal());
    assertEquals(true, changeSummary.getHasTooManyChanges());
}
Also used : FileSystemChangeSummary(org.nuxeo.drive.service.FileSystemChangeSummary) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with StringBlob

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

the class DefaultFileSystemItemFactoryFixture method testFileItem.

@Test
public void testFileItem() throws Exception {
    // ------------------------------------------------------
    // FileItem#getDownloadURL
    // ------------------------------------------------------
    FileItem fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
    String downloadURL = fileItem.getDownloadURL();
    assertEquals("nxfile/test/" + file.getId() + "/blobholder:0/Joe.odt", downloadURL);
    // ------------------------------------------------------------
    // FileItem#getDigestAlgorithm
    // ------------------------------------------------------------
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    FileItem noteItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(note);
    assertEquals("MD5", noteItem.getDigestAlgorithm());
    // ------------------------------------------------------------
    // FileItem#getDigest
    // ------------------------------------------------------------
    assertEquals(file.getAdapter(BlobHolder.class).getBlob().getDigest(), fileItem.getDigest());
    String noteDigest = FileSystemItemHelper.getMD5Digest(note.getAdapter(BlobHolder.class).getBlob());
    assertEquals(noteDigest, noteItem.getDigest());
    assertEquals(custom.getAdapter(BlobHolder.class).getBlob().getDigest(), ((FileItem) defaultFileSystemItemFactory.getFileSystemItem(custom)).getDigest());
    // ------------------------------------------------------------
    // FileItem#getCanUpdate
    // ------------------------------------------------------------
    // As Administrator
    assertTrue(fileItem.getCanUpdate());
    // 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);
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertFalse(fileItem.getCanUpdate());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertTrue(fileItem.getCanUpdate());
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        // ------------------------------------------------------
        // FileItem#getBlob
        // ------------------------------------------------------
        Blob fileItemBlob = fileItem.getBlob();
        assertEquals("Joe.odt", fileItemBlob.getFilename());
        assertEquals("Content of Joe's file.", fileItemBlob.getString());
        // Check versioning
        assertVersion("0.0", file);
        // ------------------------------------------------------
        // FileItem#setBlob and versioning
        // ------------------------------------------------------
        Blob newBlob = new StringBlob("This is a new file.");
        newBlob.setFilename("New blob.txt");
        ensureJustModified(file, session);
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        Blob updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("New blob.txt", updatedBlob.getFilename());
        assertEquals("This is a new file.", updatedBlob.getString());
        // Check versioning => should not be versioned since same
        // contributor
        // and last modification was done before the versioning delay
        assertVersion("0.0", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        newBlob.setFilename("File name modified.txt");
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.1", file);
        List<DocumentModel> fileVersions = session.getVersions(file.getRef());
        assertEquals(1, fileVersions.size());
        DocumentModel lastFileVersion = fileVersions.get(0);
        Blob versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("New blob.txt", versionedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        newBlob.setFilename("File name modified by Joe.txt");
        fileItem.setBlob(newBlob);
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.2", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(2, fileVersions.size());
        lastFileVersion = fileVersions.get(1);
        versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("File name modified.txt", versionedBlob.getFilename());
        // ------------------------------------------------------
        // DocumentBackedFileItem#rename and versioning
        // ------------------------------------------------------
        // Save document to trigger the DublinCoreListener and update
        // dc:lastContributor to "Administrator"
        // rename the file to enable dc listener (disable if not dirty)
        file.setPropertyValue("file:content/name", "newTitle");
        file = session.saveDocument(file);
        // Check versioning => should be versioned cause last contributor has changed
        assertVersion("0.3", file);
        // Switch back to Administrator as last contributor
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        ensureJustModified(file, session);
        fileItem.rename("Renamed file.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Check versioning => should not be versioned since same
        // contributor and last modification was done before the
        // versioning delay
        assertVersion("0.3", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        fileItem.rename("Renamed again.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.4", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(4, fileVersions.size());
        lastFileVersion = fileVersions.get(3);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        fileItem.rename("File renamed by Joe.txt");
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File renamed by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.5", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(5, fileVersions.size());
        lastFileVersion = fileVersions.get(4);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
    }
    resetPermissions(rootDoc, "joe");
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

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