Search in sources :

Example 21 with StringBlob

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

the class AuditChangeFinderTestSuite method testSection.

@Test
public void testSection() throws Exception {
    DocumentModel section;
    DocumentModel doc1;
    DocumentModel proxy1;
    DocumentModel proxy2;
    List<FileSystemItemChange> changes;
    try {
        // Create a Section and register it as a synchronization root
        section = session.createDocument(session.createDocumentModel("/", "sectionSyncRoot", "Section"));
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), section, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Check changes, expecting 2:
        // - rootRegistered for section
        // - documentCreated for section
        changes = getChanges(session.getPrincipal());
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(section.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + section.getId(), "sectionSyncRoot"));
        expectedChanges.add(new SimpleFileSystemItemChange(section.getId(), "documentCreated", "test", "defaultSyncRootFolderItemFactory#test#" + section.getId(), "sectionSyncRoot"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        // Publish 2 documents in the section
        doc1 = session.createDocumentModel("/folder1", "doc1", "File");
        doc1.setPropertyValue("file:content", new StringBlob("The content of file 1."));
        doc1 = session.createDocument(doc1);
        proxy1 = session.publishDocument(doc1, section);
        DocumentModel doc2 = session.createDocumentModel("/folder1", "doc2", "File");
        doc2.setPropertyValue("file:content", new StringBlob("The content of file 2."));
        doc2 = session.createDocument(doc2);
        proxy2 = session.publishDocument(doc2, section);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Check changes, expecting 4:
        // - documentProxyPublished for proxy2
        // - documentCreated for proxy2
        // - documentProxyPublished for proxy1
        // - documentCreated for proxy1
        changes = getChanges();
        assertEquals(4, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(proxy2.getId(), "documentProxyPublished", "test", "defaultFileSystemItemFactory#test#" + proxy2.getId(), "doc2"));
        expectedChanges.add(new SimpleFileSystemItemChange(proxy2.getId(), "documentCreated", "test", "defaultFileSystemItemFactory#test#" + proxy2.getId(), "doc2"));
        expectedChanges.add(new SimpleFileSystemItemChange(proxy1.getId(), "documentProxyPublished", "test", "defaultFileSystemItemFactory#test#" + proxy1.getId(), "doc1"));
        expectedChanges.add(new SimpleFileSystemItemChange(proxy1.getId(), "documentCreated", "test", "defaultFileSystemItemFactory#test#" + proxy1.getId(), "doc1"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        // Update an existing proxy
        doc1.setPropertyValue("file:content", new StringBlob("The updated content of file 1."));
        session.saveDocument(doc1);
        session.publishDocument(doc1, section);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Check changes, expecting 1:
        // - documentProxyPublished for proxy1
        changes = getChanges();
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(proxy1.getId(), "documentProxyPublished", "test", "defaultFileSystemItemFactory#test#" + proxy1.getId(), "doc1"), toSimpleFileSystemItemChange(changes.get(0)));
    } 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 22 with StringBlob

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

the class AuditChangeFinderTestSuite method testCollectionEvents.

@Test
public void testCollectionEvents() throws Exception {
    DocumentModel doc1;
    DocumentModel doc2;
    DocumentModel doc3;
    List<FileSystemItemChange> changes;
    DocumentModel locallyEditedCollection;
    try {
        log.trace("Create 2 test docs and them to the 'Locally Edited' collection");
        doc1 = session.createDocumentModel(folder1.getPathAsString(), "doc1", "File");
        doc1.setPropertyValue("file:content", new StringBlob("File content."));
        doc1 = session.createDocument(doc1);
        doc2 = session.createDocumentModel(folder1.getPathAsString(), "doc2", "File");
        doc2.setPropertyValue("file:content", new StringBlob("File content."));
        doc2 = session.createDocument(doc2);
        nuxeoDriveManager.addToLocallyEditedCollection(session, doc1);
        nuxeoDriveManager.addToLocallyEditedCollection(session, doc2);
        DocumentModel userCollections = collectionManager.getUserDefaultCollections(folder1, session);
        DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME);
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
        // Re-fetch documents to get rid of the disabled events in context
        // data
        doc1 = session.getDocument(doc1.getRef());
        doc2 = session.getDocument(doc2.getRef());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 8 (among which 7 distinct) changes:
        // - addedToCollection for doc2
        // - documentModified for 'Locally Edited' collection (2 occurrences)
        // - rootRegistered for 'Locally Edited' collection
        // - addedToCollection for doc1
        // - documentCreated for 'Locally Edited' collection
        // - documentCreated for doc2
        // - documentCreated for doc1
        changes = getChanges(session.getPrincipal());
        assertEquals(8, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Update doc1 member of the 'Locally Edited' collection");
        doc1.setPropertyValue("file:content", new StringBlob("Updated file content."));
        session.saveDocument(doc1);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: documentModified for doc1
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(doc1.getId(), "documentModified"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Remove doc1 from the 'Locally Edited' collection, delete doc2 and add doc 3 to the collection");
        collectionManager.removeFromCollection(locallyEditedCollection, doc1, session);
        doc2.followTransition(LifeCycleConstants.DELETE_TRANSITION);
        doc3 = session.createDocumentModel(folder1.getPathAsString(), "doc3", "File");
        doc3.setPropertyValue("file:content", new StringBlob("File content."));
        doc3 = session.createDocument(doc3);
        collectionManager.addToCollection(locallyEditedCollection, doc3, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 6 (among which 5 distinct) changes:
        // - addedToCollection for doc3
        // - documentModified for 'Locally Edited' collection (2 occurrences)
        // - documentCreated for doc3
        // - deleted for doc2
        // - deleted for doc1
        changes = getChanges(session.getPrincipal());
        assertEquals(6, changes.size());
        List<SimpleFileSystemItemChange> expectedChanges = new ArrayList<>();
        expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "deleted"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "deleted"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Unregister the 'Locally Edited' collection as a sync root");
        nuxeoDriveManager.unregisterSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: deleted for 'Locally Edited' collection
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "deleted"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Register the 'Locally Edited' collection back as a sync root");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: rootRegistered for 'Locally Edited'
        // collection
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "rootRegistered"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Delete the 'Locally Edited' collection");
        locallyEditedCollection.followTransition(LifeCycleConstants.DELETE_TRANSITION);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: deleted for 'Locally Edited' collection
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "deleted"), toSimpleFileSystemItemChange(changes.get(0)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) PathRef(org.nuxeo.ecm.core.api.PathRef) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) ArrayList(java.util.ArrayList) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 23 with StringBlob

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

the class AuditChangeFinderTestSuite method testGetChangeSummary.

@Test
public void testGetChangeSummary() throws Exception {
    FileSystemChangeSummary changeSummary;
    Principal admin = new NuxeoPrincipalImpl("Administrator");
    DocumentModel doc1;
    DocumentModel doc2;
    try {
        // No sync roots => shouldn't find any changes
        changeSummary = getChangeSummary(admin);
        assertNotNull(changeSummary);
        assertTrue(changeSummary.getFileSystemChanges().isEmpty());
        assertEquals(Boolean.FALSE, changeSummary.getHasTooManyChanges());
        // Register sync roots => should find changes: the newly
        // synchronized root folders as they are updated by the
        // synchronization
        // registration process
        nuxeoDriveManager.registerSynchronizationRoot(admin, folder1, session);
        nuxeoDriveManager.registerSynchronizationRoot(admin, folder2, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changeSummary = getChangeSummary(admin);
        assertEquals(2, changeSummary.getFileSystemChanges().size());
        assertEquals(Boolean.FALSE, changeSummary.getHasTooManyChanges());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    // Create 3 documents, only 2 in sync roots => should find 2 changes
    try {
        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);
        session.createDocument(session.createDocumentModel("/folder3", "doc3", "File"));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changeSummary = getChangeSummary(admin);
        List<FileSystemItemChange> changes = changeSummary.getFileSystemChanges();
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        SimpleFileSystemItemChange simpleChange = new SimpleFileSystemItemChange(doc2.getId(), "documentCreated", "test");
        simpleChange.setLifeCycleState("project");
        expectedChanges.add(simpleChange);
        simpleChange = new SimpleFileSystemItemChange(doc1.getId(), "documentCreated", "test");
        simpleChange.setLifeCycleState("project");
        expectedChanges.add(simpleChange);
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        assertEquals(Boolean.FALSE, changeSummary.getHasTooManyChanges());
        // Create a document that should not be synchronized because not
        // adaptable as a FileSystemItem (not Folderish nor a BlobHolder
        // with a
        // blob) => should not be considered as a change
        session.createDocument(session.createDocumentModel("/folder1", "notSynchronizableDoc", "NotSynchronizable"));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changeSummary = getChangeSummary(admin);
        assertTrue(changeSummary.getFileSystemChanges().isEmpty());
        assertEquals(Boolean.FALSE, changeSummary.getHasTooManyChanges());
        // Create 2 documents in the same sync root: "/folder1" and 1 document in another sync root => should find 2
        // changes for "/folder1"
        DocumentModel doc3 = session.createDocumentModel("/folder1", "doc3", "File");
        doc3.setPropertyValue("file:content", new StringBlob("The content of file 3."));
        doc3 = session.createDocument(doc3);
        DocumentModel doc4 = session.createDocumentModel("/folder1", "doc4", "File");
        doc4.setPropertyValue("file:content", new StringBlob("The content of file 4."));
        doc4 = session.createDocument(doc4);
        DocumentModel doc5 = session.createDocumentModel("/folder2", "doc5", "File");
        doc5.setPropertyValue("file:content", new StringBlob("The content of file 5."));
        doc5 = session.createDocument(doc5);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changeSummary = getChangeSummary(admin);
        assertEquals(Boolean.FALSE, changeSummary.getHasTooManyChanges());
        assertEquals(3, changeSummary.getFileSystemChanges().size());
        // No changes since last successful sync
        changeSummary = getChangeSummary(admin);
        assertTrue(changeSummary.getFileSystemChanges().isEmpty());
        assertEquals(Boolean.FALSE, changeSummary.getHasTooManyChanges());
        // Test 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");
    changeSummary = getChangeSummary(admin);
    assertTrue(changeSummary.getFileSystemChanges().isEmpty());
    assertEquals(Boolean.TRUE, changeSummary.getHasTooManyChanges());
}
Also used : NuxeoPrincipalImpl(org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl) FileSystemChangeSummary(org.nuxeo.drive.service.FileSystemChangeSummary) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) Principal(java.security.Principal) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with StringBlob

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

the class AuditChangeFinderClusteringEnabledTestSuite method testClusteringEnabled.

@Test
public void testClusteringEnabled() throws Exception {
    List<FileSystemItemChange> changes;
    DocumentModel file1;
    DocumentModel file2;
    try {
        // No sync roots
        changes = getChanges();
        assertNotNull(changes);
        assertTrue(changes.isEmpty());
        log.trace("Register a sync root and create a document inside it");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder1, session);
        file1 = session.createDocumentModel("/folder1", "file1", "File");
        file1.setPropertyValue("file:content", new StringBlob("The file content"));
        file1 = session.createDocument(file1);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // NXP-22284: Cannot expect to have no changes if the clustering delay is not expired since waiting for
        // async completion has an unknown duration
        // Wait for (2 * clustering delay + 1 second) then check changes, expecting at least 2:
        // - documentCreated for file1
        // - rootRegistered for folder1
        // The documentCreated event for folder1 might have already been "swallowed" by the first call to
        // #getChanges() if the test initialization takes too much time, sometimes happens with an Oracle database
        Thread.sleep(3000);
        changes = getChanges();
        assertTrue(changes.size() >= 2);
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(file1.getId(), "documentCreated", "test", "defaultFileSystemItemFactory#test#" + file1.getId(), "file1"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + folder1.getId(), "folder1"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "documentCreated", "test", "defaultSyncRootFolderItemFactory#test#" + folder1.getId(), "folder1"));
        assertTrue(CollectionUtils.isSubCollection(toSimpleFileSystemItemChanges(changes), expectedChanges));
        log.trace("Update existing document and create a new one");
        file1.setPropertyValue("dc:description", "Upated description");
        session.saveDocument(file1);
        file2 = session.createDocumentModel("/folder1", "file2", "File");
        file2.setPropertyValue("file:content", new StringBlob("The second file content"));
        file2 = session.createDocument(file2);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    // NXP-22284: Cannot expect to have no changes if the clustering delay is not expired since waiting for
    // async completion has an unknown duration
    // Wait for (2 * clustering delay + 1 second) then check changes, expecting 2:
    // - documentCreated for file2
    // - documentModified for file1
    Thread.sleep(3000);
    changes = getChanges();
    assertEquals(2, changes.size());
    Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
    expectedChanges.add(new SimpleFileSystemItemChange(file2.getId(), "documentCreated", "test", "defaultFileSystemItemFactory#test#" + file2.getId(), "file2"));
    expectedChanges.add(new SimpleFileSystemItemChange(file1.getId(), "documentModified", "test", "defaultFileSystemItemFactory#test#" + file1.getId(), "file1"));
    assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
}
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 25 with StringBlob

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

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