Search in sources :

Example 1 with Blob

use of org.nuxeo.ecm.automation.client.model.Blob in project nuxeo-drive-server by nuxeo.

the class TestGetChangeSummary method getChangeSummary.

/**
 * Gets the changes summary for the user bound to the {@link #session} using the {@link NuxeoDriveGetChangeSummary}
 * automation operation and updates the {@link #lastSyncDate} date.
 */
protected FileSystemChangeSummary getChangeSummary() throws Exception {
    // Wait 1 second as the mock change finder relies on steps of 1 second
    Thread.sleep(1000);
    Blob changeSummaryJSON = (Blob) clientSession.newRequest(NuxeoDriveGetChangeSummary.ID).set("lastSyncDate", lastSyncDate).set("lastSyncActiveRootDefinitions", lastSyncActiveRoots).execute();
    assertNotNull(changeSummaryJSON);
    FileSystemChangeSummary changeSummary = mapper.readValue(changeSummaryJSON.getStream(), FileSystemChangeSummaryImpl.class);
    assertNotNull(changeSummary);
    lastSyncDate = changeSummary.getSyncDate();
    lastSyncActiveRoots = changeSummary.getActiveSynchronizationRootDefinitions();
    return changeSummary;
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.automation.client.model.Blob) FileSystemChangeSummary(org.nuxeo.drive.service.FileSystemChangeSummary)

Example 2 with Blob

use of org.nuxeo.ecm.automation.client.model.Blob in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemOperations method testMove.

@Test
public void testMove() throws Exception {
    // ------------------------------------------------------
    try {
        clientSession.newRequest(NuxeoDriveMove.ID).set("srcId", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file1.getId()).set("destId", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file2.getId()).execute();
        fail("Move to a non folder item should fail.");
    } catch (Exception e) {
        String expectedMessage = String.format("Failed to invoke operation: NuxeoDrive.Move, Failed to invoke operation NuxeoDrive.Move, " + "Cannot move a file system item to file system item with id %s because it is not a folder.", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file2.getId());
        assertEquals(expectedMessage, e.getMessage());
    }
    // ------------------------------------------------------
    try {
        clientSession.newRequest(NuxeoDriveMove.ID).set("srcId", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId()).set("destId", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot2.getId()).execute();
        fail("Should not be able to move a synchronization root folder item.");
    } catch (Exception e) {
        assertEquals("Failed to invoke operation: NuxeoDrive.Move", e.getMessage());
    }
    // ------------------------------------------------------
    try {
        clientSession.newRequest(NuxeoDriveMove.ID).set("srcId", fileSystemItemAdapterService.getTopLevelFolderItemFactory().getTopLevelFolderItem(session.getPrincipal()).getId()).set("destId", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot2.getId()).execute();
        fail("Should not be able to move the top level folder item.");
    } catch (Exception e) {
        assertEquals("Failed to invoke operation: NuxeoDrive.Move", e.getMessage());
    }
    // ------------------------------------------------------
    // File to Folder => succeed
    // ------------------------------------------------------
    Blob movedFSItemJSON = (Blob) clientSession.newRequest(NuxeoDriveMove.ID).set("srcId", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file1.getId()).set("destId", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot2.getId()).execute();
    assertNotNull(movedFSItemJSON);
    DocumentBackedFileItem movedFileItem = mapper.readValue(movedFSItemJSON.getStream(), DocumentBackedFileItem.class);
    assertNotNull(movedFileItem);
    assertEquals("First file.odt", movedFileItem.getName());
    // Need to flush VCS cache to be aware of changes in the session used by the file system item
    session.save();
    DocumentModel movedFileDoc = session.getDocument(new IdRef(file1.getId()));
    assertEquals("/folder2/file1", movedFileDoc.getPathAsString());
    assertEquals("file1", movedFileDoc.getTitle());
    org.nuxeo.ecm.core.api.Blob movedFileBlob = (org.nuxeo.ecm.core.api.Blob) movedFileDoc.getPropertyValue("file:content");
    assertNotNull(movedFileBlob);
    assertEquals("First file.odt", movedFileBlob.getFilename());
    assertEquals("MD5", movedFileItem.getDigestAlgorithm());
    assertEquals(movedFileBlob.getDigest(), movedFileItem.getDigest());
}
Also used : Blob(org.nuxeo.ecm.automation.client.model.Blob) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 3 with Blob

use of org.nuxeo.ecm.automation.client.model.Blob in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemOperations method testUpdateFile.

@Test
public void testUpdateFile() throws Exception {
    StringBlob blob = new StringBlob("This is the updated content of file 1.");
    blob.setFileName("Updated file 1.odt");
    Blob updatedFileJSON = (Blob) clientSession.newRequest(NuxeoDriveUpdateFile.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file1.getId()).setInput(blob).execute();
    assertNotNull(updatedFileJSON);
    DocumentBackedFileItem updatedFile = mapper.readValue(updatedFileJSON.getStream(), DocumentBackedFileItem.class);
    assertNotNull(updatedFile);
    // Need to flush VCS cache to be aware of changes in the session used by the file system item
    session.save();
    DocumentModel updatedFileDoc = session.getDocument(new IdRef(file1.getId()));
    assertEquals("File", updatedFileDoc.getType());
    assertEquals("file1", updatedFileDoc.getTitle());
    org.nuxeo.ecm.core.api.Blob updatedFileBlob = (org.nuxeo.ecm.core.api.Blob) updatedFileDoc.getPropertyValue("file:content");
    assertNotNull(updatedFileBlob);
    assertEquals("Updated file 1.odt", updatedFileBlob.getFilename());
    assertEquals("This is the updated content of file 1.", updatedFileBlob.getString());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + updatedFileDoc.getId(), updatedFile.getId());
    assertEquals(SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId(), updatedFile.getParentId());
    assertEquals("Updated file 1.odt", updatedFile.getName());
    assertFalse(updatedFile.isFolder());
    assertEquals("Administrator", updatedFile.getCreator());
    assertEquals("Administrator", updatedFile.getLastContributor());
    assertTrue(updatedFile.getCanRename());
    assertTrue(updatedFile.getCanDelete());
    assertTrue(updatedFile.getCanUpdate());
    assertEquals("nxfile/test/" + updatedFileDoc.getId() + "/blobholder:0/Updated%20file%201.odt", updatedFile.getDownloadURL());
    assertEquals("MD5", updatedFile.getDigestAlgorithm());
    assertEquals(updatedFileBlob.getDigest(), updatedFile.getDigest());
}
Also used : Blob(org.nuxeo.ecm.automation.client.model.Blob) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 4 with Blob

use of org.nuxeo.ecm.automation.client.model.Blob in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemOperations method testScrollDescendants.

@Test
public void testScrollDescendants() throws Exception {
    // Get descendants of sync root 1
    // Scroll through all descendants in one breath
    Blob descendantsJSON = (Blob) clientSession.newRequest(NuxeoDriveScrollDescendants.ID).set("id", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId()).set("batchSize", 10).execute();
    assertNotNull(descendantsJSON);
    ScrollFileSystemItemList descendants = mapper.readValue(descendantsJSON.getStream(), ScrollFileSystemItemListImpl.class);
    assertNotNull(descendants);
    assertNotNull(descendants.getScrollId());
    assertEquals(4, descendants.size());
    List<String> expectedIds = Arrays.asList(file1, subFolder1, file3, file4).stream().map(doc -> DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc.getId()).collect(Collectors.toList());
    List<String> descendantIds = descendants.stream().map(fsItem -> fsItem.getId()).collect(Collectors.toList());
    // Note that order is not determined
    assertTrue(CollectionUtils.isEqualCollection(expectedIds, descendantIds));
    // Scroll through descendants in several steps
    descendantIds.clear();
    ScrollFileSystemItemList descendantsBatch;
    int batchSize = 2;
    String scrollId = null;
    while (!(descendantsBatch = mapper.readValue(((Blob) clientSession.newRequest(NuxeoDriveScrollDescendants.ID).set("id", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId()).set("batchSize", batchSize).set("scrollId", scrollId).execute()).getStream(), ScrollFileSystemItemListImpl.class)).isEmpty()) {
        assertTrue(descendantsBatch.size() > 0);
        scrollId = descendantsBatch.getScrollId();
        descendantIds.addAll(descendantsBatch.stream().map(fsItem -> fsItem.getId()).collect(Collectors.toList()));
    }
    assertEquals(4, descendantIds.size());
    // Note that order is not determined
    assertTrue(CollectionUtils.isEqualCollection(expectedIds, descendantIds));
    // Check descendants of sub-folder of sync root 1
    assertTrue(CollectionUtils.isEqualCollection(Arrays.asList(file3, file4).stream().map(doc -> DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc.getId()).collect(Collectors.toList()), mapper.readValue(((Blob) clientSession.newRequest(NuxeoDriveScrollDescendants.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder1.getId()).set("batchSize", 10).execute()).getStream(), JsonNode.class).findValuesAsText("id")));
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) NuxeoPrincipal(org.nuxeo.ecm.core.api.NuxeoPrincipal) ACE(org.nuxeo.ecm.core.api.security.ACE) Arrays(java.util.Arrays) DirectoryService(org.nuxeo.ecm.directory.api.DirectoryService) Map(java.util.Map) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) Assert.fail(org.junit.Assert.fail) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Session(org.nuxeo.ecm.automation.client.Session) ScrollFileSystemItemListImpl(org.nuxeo.drive.adapter.impl.ScrollFileSystemItemListImpl) SecurityConstants(org.nuxeo.ecm.core.api.security.SecurityConstants) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) IdRef(org.nuxeo.ecm.core.api.IdRef) HttpAutomationClient(org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Blob(org.nuxeo.ecm.automation.client.model.Blob) List(java.util.List) Features(org.nuxeo.runtime.test.runner.Features) Principal(java.security.Principal) Assert.assertFalse(org.junit.Assert.assertFalse) ACP(org.nuxeo.ecm.core.api.security.ACP) Jetty(org.nuxeo.runtime.test.runner.Jetty) ACL(org.nuxeo.ecm.core.api.security.ACL) CoreFeature(org.nuxeo.ecm.core.test.CoreFeature) PathRef(org.nuxeo.ecm.core.api.PathRef) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Inject(javax.inject.Inject) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FeaturesRunner(org.nuxeo.runtime.test.runner.FeaturesRunner) CollectionUtils(org.apache.commons.collections.CollectionUtils) FileSystemItemAdapterService(org.nuxeo.drive.service.FileSystemItemAdapterService) TransactionHelper(org.nuxeo.runtime.transaction.TransactionHelper) Framework(org.nuxeo.runtime.api.Framework) DocumentBackedFolderItem(org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem) UserManager(org.nuxeo.ecm.platform.usermanager.UserManager) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) Before(org.junit.Before) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FolderItem(org.nuxeo.drive.adapter.FolderItem) Assert.assertNull(org.junit.Assert.assertNull) ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) DefaultTopLevelFolderItem(org.nuxeo.drive.adapter.impl.DefaultTopLevelFolderItem) CoreSession(org.nuxeo.ecm.core.api.CoreSession) Assert.assertEquals(org.junit.Assert.assertEquals) Blob(org.nuxeo.ecm.automation.client.model.Blob) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 5 with Blob

use of org.nuxeo.ecm.automation.client.model.Blob in project nuxeo-drive-server by nuxeo.

the class TestPermissionHierarchy method testClientSideUser1.

@Test
public void testClientSideUser1() throws Exception {
    // ---------------------------------------------
    // Check active factories
    // ---------------------------------------------
    TopLevelFolderItemFactory topLevelFolderItemFactory = fileSystemItemAdapterService.getTopLevelFolderItemFactory();
    assertEquals("org.nuxeo.drive.hierarchy.permission.factory.PermissionTopLevelFactory", topLevelFolderItemFactory.getName());
    Set<String> activeFactories = fileSystemItemAdapterService.getActiveFileSystemItemFactories();
    assertEquals(5, activeFactories.size());
    assertTrue(activeFactories.contains("collectionSyncRootFolderItemFactory"));
    assertTrue(activeFactories.contains("defaultFileSystemItemFactory"));
    assertTrue(activeFactories.contains("userSyncRootParentFactory"));
    assertTrue(activeFactories.contains("permissionSyncRootFactory"));
    assertTrue(activeFactories.contains("sharedSyncRootParentFactory"));
    // ---------------------------------------------
    // Check top level folder: "Nuxeo Drive"
    // ---------------------------------------------
    Blob topLevelFolderJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetTopLevelFolder.ID).execute();
    assertNotNull(topLevelFolderJSON);
    PermissionTopLevelFolderItem topLevelFolder = mapper.readValue(topLevelFolderJSON.getStream(), PermissionTopLevelFolderItem.class);
    assertNotNull(topLevelFolder);
    assertEquals(TOP_LEVEL_ID, topLevelFolder.getId());
    assertNull(topLevelFolder.getParentId());
    assertEquals("/" + TOP_LEVEL_ID, topLevelFolder.getPath());
    assertEquals("Nuxeo Drive", topLevelFolder.getName());
    assertTrue(topLevelFolder.isFolder());
    assertEquals("system", topLevelFolder.getCreator());
    assertEquals("system", topLevelFolder.getLastContributor());
    assertFalse(topLevelFolder.getCanRename());
    assertFalse(topLevelFolder.getCanDelete());
    assertFalse(topLevelFolder.getCanCreateChild());
    /**
     * <pre>
     * ===================================================
     * User workspace registered as a synchronization root
     * ===================================================
     * => Expected client side for user1:
     *
     * Nuxeo Drive
     *   |-- My Docs
     *   |     |-- user1File2
     *   |     |-- user1Folder1
     *   |     |     |-- user1File1
     *   |     |     |-- user1Folder2
     *   |     |-- user1Folder3
     *   |     |      |-- user1File3
     *   |     |-- user1Folder4
     *   |
     *   |-- Other Docs
     *   |     |-- user2Folder1
     *   |     |     |-- user2File1
     *   |     |     |-- user2Folder2
     *   |     |-- user2Folder3
     *   |     |     |-- user2File3
     * </pre>
     */
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    nuxeoDriveManager.registerSynchronizationRoot(session1.getPrincipal(), userWorkspace1, session1);
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // ---------------------------------------------
    // Check top level folder children
    // ---------------------------------------------
    // Check descendants
    assertFalse(topLevelFolder.getCanScrollDescendants());
    try {
        clientSession1.newRequest(NuxeoDriveScrollDescendants.ID).set("id", topLevelFolder.getId()).set("batchSize", 10).execute();
        fail("Scrolling through the descendants of the permission top level folder item should be unsupported.");
    } catch (Exception e) {
        assertEquals("Failed to invoke operation: NuxeoDrive.ScrollDescendants", e.getMessage());
    }
    // Get children
    Blob topLevelChildrenJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", topLevelFolder.getId()).execute();
    ArrayNode topLevelChildren = mapper.readValue(topLevelChildrenJSON.getStream(), ArrayNode.class);
    assertNotNull(topLevelChildren);
    assertEquals(2, topLevelChildren.size());
    // Check "My Docs"
    UserSyncRootParentFolderItem userSyncRootParent = readValue(topLevelChildren.get(0), UserSyncRootParentFolderItem.class);
    assertEquals(userWorkspace1ItemId, userSyncRootParent.getId());
    assertEquals(TOP_LEVEL_ID, userSyncRootParent.getParentId());
    assertEquals(userWorkspace1ItemPath, userSyncRootParent.getPath());
    assertEquals("My Docs", userSyncRootParent.getName());
    assertTrue(userSyncRootParent.isFolder());
    assertEquals("user1", userSyncRootParent.getCreator());
    assertEquals("user1", userSyncRootParent.getLastContributor());
    assertFalse(userSyncRootParent.getCanRename());
    assertFalse(userSyncRootParent.getCanDelete());
    // Can create a child since "My Docs" is the user workspace
    assertTrue(userSyncRootParent.getCanCreateChild());
    // Check "Other Docs"
    SharedSyncRootParentFolderItem sharedSyncRootParent = readValue(topLevelChildren.get(1), SharedSyncRootParentFolderItem.class);
    assertEquals(SHARED_SYNC_ROOT_PARENT_ID, sharedSyncRootParent.getId());
    assertEquals(TOP_LEVEL_ID, sharedSyncRootParent.getParentId());
    assertEquals("/" + TOP_LEVEL_ID + "/" + SHARED_SYNC_ROOT_PARENT_ID, sharedSyncRootParent.getPath());
    assertEquals("Other Docs", sharedSyncRootParent.getName());
    assertTrue(sharedSyncRootParent.isFolder());
    assertEquals("system", sharedSyncRootParent.getCreator());
    assertEquals("system", sharedSyncRootParent.getLastContributor());
    assertFalse(sharedSyncRootParent.getCanRename());
    assertFalse(sharedSyncRootParent.getCanDelete());
    assertFalse(sharedSyncRootParent.getCanCreateChild());
    // --------------------------------------------
    // Check user synchronization roots
    // --------------------------------------------
    // Check descendants
    assertTrue(userSyncRootParent.getCanScrollDescendants());
    assertTrue(CollectionUtils.isEqualCollection(Arrays.asList(user1File2, user1Folder1, user1File1, user1Folder2, user1Folder3, user1File3, user1Folder4).stream().map(doc -> DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc.getId()).collect(Collectors.toList()), mapper.readValue(((Blob) clientSession1.newRequest(NuxeoDriveScrollDescendants.ID).set("id", userSyncRootParent.getId()).set("batchSize", 10).execute()).getStream(), JsonNode.class).findValuesAsText("id")));
    // Get children
    Blob userSyncRootsJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", userSyncRootParent.getId()).execute();
    ArrayNode userSyncRoots = mapper.readValue(userSyncRootsJSON.getStream(), ArrayNode.class);
    assertNotNull(userSyncRoots);
    assertEquals(4, userSyncRoots.size());
    DocumentBackedFolderItem folderItem;
    DocumentBackedFileItem childFileItem;
    DocumentBackedFileItem fileItem;
    DocumentBackedFolderItem childFolderItem;
    JsonNode[] rootNodes = sortNodeByName(userSyncRoots);
    // user1File2
    fileItem = readValue(rootNodes[0], DocumentBackedFileItem.class);
    checkFileItem(fileItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1File2, userWorkspace1ItemId, userWorkspace1ItemPath, "user1File2.txt", "user1", "user1");
    // user1Folder1
    folderItem = readValue(rootNodes[1], DocumentBackedFolderItem.class);
    checkFolderItem(folderItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1Folder1, userWorkspace1ItemId, userWorkspace1ItemPath, "user1Folder1", "user1", "user1");
    Blob folderItemChildrenJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", folderItem.getId()).execute();
    ArrayNode folderItemChildren = mapper.readValue(folderItemChildrenJSON.getStream(), ArrayNode.class);
    assertNotNull(folderItemChildren);
    assertEquals(2, folderItemChildren.size());
    {
        JsonNode[] nodes = sortNodeByName(folderItemChildren);
        // user1File1
        childFileItem = readValue(nodes[0], DocumentBackedFileItem.class);
        checkFileItem(childFileItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1File1, folderItem.getId(), folderItem.getPath(), "user1File1.txt", "user1", "user1");
        // user1Folder2
        childFolderItem = readValue(nodes[1], DocumentBackedFolderItem.class);
        checkFolderItem(childFolderItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1Folder2, folderItem.getId(), folderItem.getPath(), "user1Folder2", "user1", "user1");
    }
    // user1Folder3
    folderItem = readValue(rootNodes[2], DocumentBackedFolderItem.class);
    checkFolderItem(folderItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1Folder3, userWorkspace1ItemId, userWorkspace1ItemPath, "user1Folder3", "user1", "user1");
    {
        folderItemChildrenJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", folderItem.getId()).execute();
        folderItemChildren = mapper.readValue(folderItemChildrenJSON.getStream(), ArrayNode.class);
        assertNotNull(folderItemChildren);
        assertEquals(1, folderItemChildren.size());
        // user1File3
        childFileItem = readValue(folderItemChildren.get(0), DocumentBackedFileItem.class);
        checkFileItem(childFileItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1File3, folderItem.getId(), folderItem.getPath(), "user1File3.txt", "user1", "user1");
    }
    // user1Folder4
    folderItem = readValue(rootNodes[3], DocumentBackedFolderItem.class);
    checkFolderItem(folderItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1Folder4, userWorkspace1ItemId, userWorkspace1ItemPath, "user1Folder4", "user1", "user1");
    // ---------------------------------------------
    // Check shared synchronization roots
    // ---------------------------------------------
    // Check descendants
    assertFalse(topLevelFolder.getCanScrollDescendants());
    try {
        clientSession1.newRequest(NuxeoDriveScrollDescendants.ID).set("id", sharedSyncRootParent.getId()).set("batchSize", 10).execute();
        fail("Scrolling through the descendants of the shared sync root parent folder item should be unsupported.");
    } catch (Exception e) {
        assertEquals("Failed to invoke operation: NuxeoDrive.ScrollDescendants", e.getMessage());
    }
    // Get children
    Blob sharedSyncRootsJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", sharedSyncRootParent.getId()).execute();
    List<DefaultSyncRootFolderItem> sharedSyncRoots = mapper.readValue(sharedSyncRootsJSON.getStream(), new TypeReference<List<DefaultSyncRootFolderItem>>() {
    });
    Collections.sort(sharedSyncRoots);
    assertNotNull(sharedSyncRoots);
    assertEquals(2, sharedSyncRoots.size());
    // user2Folder1
    DefaultSyncRootFolderItem sharedSyncRoot = sharedSyncRoots.get(0);
    checkFolderItem(sharedSyncRoot, SYNC_ROOT_ID_PREFIX, session1.getDocument(user2Folder1.getRef()), sharedSyncRootParent.getId(), sharedSyncRootParent.getPath(), "user2Folder1", "user2", "user1");
    Blob sharedSyncRootChildrenJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", sharedSyncRoot.getId()).execute();
    ArrayNode sharedSyncRootChildren = mapper.readValue(sharedSyncRootChildrenJSON.getStream(), ArrayNode.class);
    assertNotNull(sharedSyncRootChildren);
    assertEquals(2, sharedSyncRootChildren.size());
    DocumentBackedFolderItem sharedSyncRootChildFolderItem;
    DocumentBackedFileItem sharedSyncRootChildFileItem;
    {
        JsonNode[] nodes = sortNodeByName(sharedSyncRootChildren);
        // user2File1
        sharedSyncRootChildFileItem = readValue(nodes[0], DocumentBackedFileItem.class);
        checkFileItem(sharedSyncRootChildFileItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, session1.getDocument(user2File1.getRef()), sharedSyncRoot.getId(), sharedSyncRoot.getPath(), "user2File1.txt", "user2", "user2");
        // user2Folder2
        sharedSyncRootChildFolderItem = readValue(nodes[1], DocumentBackedFolderItem.class);
        checkFolderItem(sharedSyncRootChildFolderItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, session1.getDocument(user2Folder2.getRef()), sharedSyncRoot.getId(), sharedSyncRoot.getPath(), "user2Folder2", "user2", "user2");
    }
    // user2Folder3
    sharedSyncRoot = sharedSyncRoots.get(1);
    checkFolderItem(sharedSyncRoot, SYNC_ROOT_ID_PREFIX, session1.getDocument(user2Folder3.getRef()), sharedSyncRootParent.getId(), sharedSyncRootParent.getPath(), "user2Folder3", "user2", "user1");
    sharedSyncRootChildrenJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", sharedSyncRoot.getId()).execute();
    sharedSyncRootChildren = mapper.readValue(sharedSyncRootChildrenJSON.getStream(), ArrayNode.class);
    assertNotNull(sharedSyncRootChildren);
    assertEquals(1, sharedSyncRootChildren.size());
    // user2File3
    sharedSyncRootChildFileItem = readValue(sharedSyncRootChildren.get(0), DocumentBackedFileItem.class);
    checkFileItem(sharedSyncRootChildFileItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, session1.getDocument(user2File3.getRef()), sharedSyncRoot.getId(), sharedSyncRoot.getPath(), "user2File3.txt", "user2", "user2");
    /**
     * <pre>
     * =======================================================
     * User workspace NOT registered as a synchronization root
     * =======================================================
     * => Expected client side for user1:
     *
     * Nuxeo Drive
     *   |-- My Docs
     *   |
     *   |-- Other Docs (unchanged)
     *   |     |-- user2Folder1
     *   |     |     |-- user2File1
     *   |     |     |-- user2Folder2
     *   |     |-- user2Folder3
     *   |     |     |-- user2File3
     * </pre>
     */
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    nuxeoDriveManager.unregisterSynchronizationRoot(session1.getPrincipal(), userWorkspace1, session1);
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // ---------------------------------------------
    // Check "My Docs"
    // ---------------------------------------------
    Blob userSyncRootParentJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetFileSystemItem.ID).set("id", userWorkspace1ItemId).execute();
    assertNotNull(userSyncRootParentJSON);
    userSyncRootParent = mapper.readValue(userSyncRootParentJSON.getStream(), UserSyncRootParentFolderItem.class);
    assertEquals(userWorkspace1ItemId, userSyncRootParent.getId());
    assertEquals(TOP_LEVEL_ID, userSyncRootParent.getParentId());
    assertEquals(userWorkspace1ItemPath, userSyncRootParent.getPath());
    assertEquals("My Docs", userSyncRootParent.getName());
    assertTrue(userSyncRootParent.isFolder());
    assertEquals("user1", userSyncRootParent.getCreator());
    assertEquals("user1", userSyncRootParent.getLastContributor());
    assertFalse(userSyncRootParent.getCanRename());
    assertFalse(userSyncRootParent.getCanDelete());
    // Cannot create a child since "My Docs" is only the parent of the
    // synchronization roots, not the user workspace
    assertFalse(userSyncRootParent.getCanCreateChild());
    // --------------------------------------------
    // Check user synchronization roots
    // --------------------------------------------
    // Check descendants
    assertFalse(topLevelFolder.getCanScrollDescendants());
    try {
        clientSession1.newRequest(NuxeoDriveScrollDescendants.ID).set("id", userSyncRootParent.getId()).set("batchSize", 10).execute();
        fail("Scrolling through the descendants of the user sync root parent folder item not registered as a sync root should be unsupported.");
    } catch (Exception e) {
        assertEquals("Failed to invoke operation: NuxeoDrive.ScrollDescendants", e.getMessage());
    }
    // Get children
    userSyncRootsJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", userSyncRootParent.getId()).execute();
    userSyncRoots = mapper.readValue(userSyncRootsJSON.getStream(), ArrayNode.class);
    assertNotNull(userSyncRoots);
    assertEquals(0, userSyncRoots.size());
    /**
     * <pre>
     * =======================================================
     * User workspace NOT registered as a synchronization root
     * but specific folders yes: user1Folder3, user1Folder4
     * =======================================================
     * => Expected client side for user1:
     *
     * Nuxeo Drive
     *   |-- My Docs
     *   |     |-- user1Folder3
     *   |     |      |-- user1File3
     *   |     |-- user1Folder4
     *   |
     *   |-- Other Docs (unchanged)
     *   |     |-- user2Folder1
     *   |     |     |-- user2File1
     *   |     |     |-- user2Folder2
     *   |     |-- user2Folder3
     *   |     |     |-- user2File3
     * </pre>
     */
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    nuxeoDriveManager.registerSynchronizationRoot(session1.getPrincipal(), user1Folder3, session1);
    nuxeoDriveManager.registerSynchronizationRoot(session1.getPrincipal(), user1Folder4, session1);
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // --------------------------------------------
    // Check user synchronization roots
    // --------------------------------------------
    userSyncRootsJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", userSyncRootParent.getId()).execute();
    userSyncRoots = mapper.readValue(userSyncRootsJSON.getStream(), ArrayNode.class);
    assertNotNull(userSyncRoots);
    assertEquals(2, userSyncRoots.size());
    // user1Folder3
    folderItem = readValue(userSyncRoots.get(0), DocumentBackedFolderItem.class);
    checkFolderItem(folderItem, SYNC_ROOT_ID_PREFIX, user1Folder3, userWorkspace1ItemId, userWorkspace1ItemPath, "user1Folder3", "user1", "user1");
    folderItemChildrenJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", folderItem.getId()).execute();
    folderItemChildren = mapper.readValue(folderItemChildrenJSON.getStream(), ArrayNode.class);
    assertNotNull(folderItemChildren);
    assertEquals(1, folderItemChildren.size());
    // user1File3
    childFileItem = readValue(folderItemChildren.get(0), DocumentBackedFileItem.class);
    checkFileItem(childFileItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, user1File3, folderItem.getId(), folderItem.getPath(), "user1File3.txt", "user1", "user1");
    // user1Folder4
    folderItem = readValue(userSyncRoots.get(1), DocumentBackedFolderItem.class);
    checkFolderItem(folderItem, SYNC_ROOT_ID_PREFIX, user1Folder4, userWorkspace1ItemId, userWorkspace1ItemPath, "user1Folder4", "user1", "user1");
    /**
     * <pre>
     * =======================================================
     * Unregister a shared folder: user2Folder1
     * =======================================================
     * => Expected client side for user1:
     *
     * Nuxeo Drive
     *   |-- My Docs (unchanged)
     *   |     |-- user1Folder3
     *   |     |      |-- user1File3
     *   |     |-- user1Folder4
     *   |
     *   |-- Other Docs
     *   |     |-- user2Folder3
     *   |     |     |-- user2File3
     * </pre>
     */
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    nuxeoDriveManager.unregisterSynchronizationRoot(session1.getPrincipal(), session1.getDocument(user2Folder1.getRef()), session1);
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // ---------------------------------------------
    // Check shared synchronization roots
    // ---------------------------------------------
    sharedSyncRootsJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", sharedSyncRootParent.getId()).execute();
    sharedSyncRoots = mapper.readValue(sharedSyncRootsJSON.getStream(), new TypeReference<List<DefaultSyncRootFolderItem>>() {
    });
    assertNotNull(sharedSyncRoots);
    assertEquals(1, sharedSyncRoots.size());
    // user2Folder3
    sharedSyncRoot = sharedSyncRoots.get(0);
    checkFolderItem(sharedSyncRoot, SYNC_ROOT_ID_PREFIX, session1.getDocument(user2Folder3.getRef()), sharedSyncRootParent.getId(), sharedSyncRootParent.getPath(), "user2Folder3", "user2", "user1");
    sharedSyncRootChildrenJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", sharedSyncRoot.getId()).execute();
    sharedSyncRootChildren = mapper.readValue(sharedSyncRootChildrenJSON.getStream(), ArrayNode.class);
    assertNotNull(sharedSyncRootChildren);
    assertEquals(1, sharedSyncRootChildren.size());
    // user2File3
    sharedSyncRootChildFileItem = readValue(sharedSyncRootChildren.get(0), DocumentBackedFileItem.class);
    checkFileItem(sharedSyncRootChildFileItem, DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX, session1.getDocument(user2File3.getRef()), sharedSyncRoot.getId(), sharedSyncRoot.getPath(), "user2File3.txt", "user2", "user2");
    /**
     * <pre>
     * =======================================================
     * Remove permission on a shared folder: user2Folder3
     * =======================================================
     * => Expected client side for user1:
     *
     * Nuxeo Drive
     *   |-- My Docs (unchanged)
     *   |     |-- user1Folder3
     *   |     |      |-- user1File3
     *   |     |-- user1Folder4
     *   |
     *   |-- Other Docs
     * </pre>
     */
    resetPermissions(user2Folder3.getRef(), "user1");
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // ---------------------------------------------
    // Check shared synchronization roots
    // ---------------------------------------------
    sharedSyncRootsJSON = (Blob) clientSession1.newRequest(NuxeoDriveGetChildren.ID).set("id", sharedSyncRootParent.getId()).execute();
    sharedSyncRoots = mapper.readValue(sharedSyncRootsJSON.getStream(), new TypeReference<List<DefaultSyncRootFolderItem>>() {
    });
    assertNotNull(sharedSyncRoots);
    assertEquals(0, sharedSyncRoots.size());
}
Also used : ACE(org.nuxeo.ecm.core.api.security.ACE) Arrays(java.util.Arrays) DirectoryService(org.nuxeo.ecm.directory.api.DirectoryService) NuxeoDriveAutomationFeature(org.nuxeo.drive.operations.NuxeoDriveAutomationFeature) Map(java.util.Map) After(org.junit.After) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) Assert.fail(org.junit.Assert.fail) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Session(org.nuxeo.ecm.automation.client.Session) JsonParseException(com.fasterxml.jackson.core.JsonParseException) SecurityConstants(org.nuxeo.ecm.core.api.security.SecurityConstants) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) Set(java.util.Set) HttpAutomationClient(org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Blob(org.nuxeo.ecm.automation.client.model.Blob) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) Features(org.nuxeo.runtime.test.runner.Features) NuxeoDriveGetFileSystemItem(org.nuxeo.drive.operations.NuxeoDriveGetFileSystemItem) UserSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.UserSyncRootParentFolderItem) Assert.assertFalse(org.junit.Assert.assertFalse) UserWorkspaceService(org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService) ACP(org.nuxeo.ecm.core.api.security.ACP) Jetty(org.nuxeo.runtime.test.runner.Jetty) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) FileItem(org.nuxeo.drive.adapter.FileItem) ACL(org.nuxeo.ecm.core.api.security.ACL) CoreFeature(org.nuxeo.ecm.core.test.CoreFeature) PermissionTopLevelFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.PermissionTopLevelFolderItem) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Inject(javax.inject.Inject) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) NuxeoDriveScrollDescendants(org.nuxeo.drive.operations.NuxeoDriveScrollDescendants) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FeaturesRunner(org.nuxeo.runtime.test.runner.FeaturesRunner) CollectionUtils(org.apache.commons.collections.CollectionUtils) FileSystemItemAdapterService(org.nuxeo.drive.service.FileSystemItemAdapterService) TransactionHelper(org.nuxeo.runtime.transaction.TransactionHelper) DocumentBackedFolderItem(org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem) NuxeoDriveGetChildren(org.nuxeo.drive.operations.NuxeoDriveGetChildren) TopLevelFolderItemFactory(org.nuxeo.drive.service.TopLevelFolderItemFactory) Before(org.junit.Before) Iterator(java.util.Iterator) JsonParser(com.fasterxml.jackson.core.JsonParser) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) SharedSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.SharedSyncRootParentFolderItem) IOException(java.io.IOException) Test(org.junit.Test) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) FolderItem(org.nuxeo.drive.adapter.FolderItem) Deploy(org.nuxeo.runtime.test.runner.Deploy) Assert.assertNull(org.junit.Assert.assertNull) NuxeoDriveGetTopLevelFolder(org.nuxeo.drive.operations.NuxeoDriveGetTopLevelFolder) CoreSession(org.nuxeo.ecm.core.api.CoreSession) Comparator(java.util.Comparator) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Blob(org.nuxeo.ecm.automation.client.model.Blob) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) JsonNode(com.fasterxml.jackson.databind.JsonNode) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) TopLevelFolderItemFactory(org.nuxeo.drive.service.TopLevelFolderItemFactory) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) PermissionTopLevelFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.PermissionTopLevelFolderItem) DocumentBackedFolderItem(org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem) List(java.util.List) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) UserSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.UserSyncRootParentFolderItem) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SharedSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.SharedSyncRootParentFolderItem) Test(org.junit.Test)

Aggregations

Blob (org.nuxeo.ecm.automation.client.model.Blob)18 Test (org.junit.Test)16 StringBlob (org.nuxeo.ecm.automation.client.model.StringBlob)12 DocumentBackedFileItem (org.nuxeo.drive.adapter.impl.DocumentBackedFileItem)9 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)9 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)6 DocumentBackedFolderItem (org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem)6 List (java.util.List)5 Session (org.nuxeo.ecm.automation.client.Session)5 CoreSession (org.nuxeo.ecm.core.api.CoreSession)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 FolderItem (org.nuxeo.drive.adapter.FolderItem)3 ScrollFileSystemItemList (org.nuxeo.drive.adapter.ScrollFileSystemItemList)3 DefaultTopLevelFolderItem (org.nuxeo.drive.adapter.impl.DefaultTopLevelFolderItem)3 IdRef (org.nuxeo.ecm.core.api.IdRef)3 PathRef (org.nuxeo.ecm.core.api.PathRef)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 IOException (java.io.IOException)2