Search in sources :

Example 81 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel 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 82 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel 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 83 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel 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 84 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class AuditChangeFinderTestSuite method testSyncRootParentPermissionChange.

@Test
public void testSyncRootParentPermissionChange() throws Exception {
    List<FileSystemItemChange> changes;
    DocumentModel subFolder;
    try {
        // No sync roots
        changes = getChanges();
        assertTrue(changes.isEmpty());
        // Create a subfolder in a sync root
        subFolder = session.createDocumentModel("/folder1", "subFolder", "Folder");
        subFolder = session.createDocument(subFolder);
        // Grant READ_WRITE permission to user1 on the subfolder
        setPermissions(subFolder, new ACE("user1", SecurityConstants.READ_WRITE));
        // Mark subfolder as a sync root for user1
        nuxeoDriveManager.registerSynchronizationRoot(user1Session.getPrincipal(), subFolder, user1Session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Get changes for user1
        changes = getChanges(user1Session.getPrincipal());
        // Folder creation and sync root registration events
        assertEquals(2, changes.size());
        // Remove READ_WRITE permission granted to user1 on the subfolder
        resetPermissions(subFolder, "user1");
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges(user1Session.getPrincipal());
        // Expecting 1 change: securityUpdated for subFolder with a non-null FileSystemItem and FileSystemItem name
        // since the user can still access it by inheritance
        assertEquals(1, changes.size());
        FileSystemItemChange change = changes.get(0);
        assertEquals(new SimpleFileSystemItemChange(subFolder.getId(), "securityUpdated", "test", "defaultSyncRootFolderItemFactory#test#" + subFolder.getId(), "subFolder"), toSimpleFileSystemItemChange(change));
        assertNotNull(change);
        assertEquals("subFolder", change.getFileSystemItemName());
        // Remove READ_WRITE permission granted to user1 on the parent folder
        resetPermissions(folder1, "user1");
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges(user1Session.getPrincipal());
        // Expecting 1 change: securityUpdated for subFolder with a null FileSystemItem and FileSystemItem name
        // since the user cannot access it anymore
        assertEquals(1, changes.size());
        FileSystemItemChange change = changes.get(0);
        assertEquals(new SimpleFileSystemItemChange(subFolder.getId(), "securityUpdated", "test", "test#" + subFolder.getId(), "subFolder"), toSimpleFileSystemItemChange(change));
        assertNull(change.getFileSystemItem());
        assertNull(change.getFileSystemItemName());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : ACE(org.nuxeo.ecm.core.api.security.ACE) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 85 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class GroupChangesTestSuite method createGroup.

protected void createGroup(String name, List<String> members, List<String> subGroups) {
    DocumentModel group = userManager.getBareGroupModel();
    group.setPropertyValue("group:groupname", name);
    if (members != null) {
        group.setPropertyValue("group:members", (Serializable) members);
    }
    if (subGroups != null) {
        group.setPropertyValue("group:subGroups", (Serializable) subGroups);
    }
    userManager.createGroup(group);
}
Also used : DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Aggregations

DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)128 Test (org.junit.Test)58 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)26 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)25 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)24 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)22 PathRef (org.nuxeo.ecm.core.api.PathRef)21 IdRef (org.nuxeo.ecm.core.api.IdRef)18 HashSet (java.util.HashSet)17 Blob (org.nuxeo.ecm.core.api.Blob)17 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)17 ArrayList (java.util.ArrayList)16 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)11 Principal (java.security.Principal)10 ACE (org.nuxeo.ecm.core.api.security.ACE)10 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)9 FileItem (org.nuxeo.drive.adapter.FileItem)8 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)8 HashMap (java.util.HashMap)7