Search in sources :

Example 16 with DocumentModel

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

the class AuditChangeFinderTestSuite method testRegisterParentSyncRoot.

@Test
public void testRegisterParentSyncRoot() throws Exception {
    DocumentModel subFolder;
    List<FileSystemItemChange> changes;
    try {
        // Create a subfolder in folder1
        subFolder = session.createDocument(session.createDocumentModel(folder1.getPathAsString(), "subFolder", "Folder"));
        // Register subfolder as a sync root
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), subFolder, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Check changes, expecting 2:
        // - rootRegistered for subfolder
        // - documentCreated for subFolder
        changes = getChanges(session.getPrincipal());
        assertEquals(2, changes.size());
        // Register folder1 as a sync root
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder1, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Check changes, expecting 2:
        // - rootRegistered for folder1
        // - deleted for subFolder
        changes = getChanges(session.getPrincipal());
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + folder1.getId(), "folder1"));
        expectedChanges.add(new SimpleFileSystemItemChange(subFolder.getId(), "deleted", "test", "test#" + subFolder.getId(), "subFolder"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with DocumentModel

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

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

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

the class GroupChangesTestSuite method init.

@Override
@Before
public void init() throws Exception {
    lastEventLogId = 0;
    lastSyncActiveRootDefinitions = "";
    DocumentModel user = userManager.getBareUserModel();
    user.setPropertyValue("user:username", "user");
    userManager.createUser(user);
    List<String> members = Arrays.asList("user");
    createGroup("group1", members, null);
    createGroup("group2", members, null);
    createGroup("parentGroup", null, Arrays.asList("group1"));
    createGroup("grandParentGroup", null, Arrays.asList("parentGroup"));
    commitAndWaitForAsyncCompletion();
    userSession = CoreInstance.openCoreSession(session.getRepositoryName(), userManager.getPrincipal("user"));
}
Also used : DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Before(org.junit.Before)

Example 20 with DocumentModel

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

the class GroupChangesTestSuite method testGroupChangesOnSyncRootParent.

/**
 * Tests changes on a group that has access to the parent of a synchronization root.
 */
@Test
public void testGroupChangesOnSyncRootParent() throws Exception {
    DocumentModel syncRoot;
    try {
        DocumentModel parent = session.createDocument(session.createDocumentModel("/", "parent", "Folder"));
        syncRoot = session.createDocument(session.createDocumentModel("/parent", "syncRoot", "Folder"));
        log.trace("Grant ReadWrite to group1 on parent");
        setPermissions(parent, new ACE("group1", SecurityConstants.READ_WRITE));
        nuxeoDriveManager.registerSynchronizationRoot(userSession.getPrincipal(), syncRoot, userSession);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        List<FileSystemItemChange> changes = getChanges(userSession.getPrincipal());
        // Sync root creation and registration events
        assertEquals(2, changes.size());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    testGroupChanges(syncRoot, "defaultSyncRootFolderItemFactory", "group1", false);
}
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)

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