use of org.nuxeo.drive.adapter.FolderItem in project nuxeo-drive-server by nuxeo.
the class TestDriveVersioning method testFolderVersioning.
@Test
public void testFolderVersioning() throws Exception {
// Create a Folder in the synchronization root
DocumentModel folder = session.createDocumentModel("/syncRoot", "aFolder", "Folder");
folder = session.createDocument(folder);
session.save();
// Expect no versions initially
// Cannot use DocumentModel#getVersionLabel since it relies on the uid schema not held by the Folder type
assertTrue(session.getVersions(folder.getRef()).isEmpty());
// Wait for the versioning delay and update the folder
Thread.sleep(VERSIONING_DELAY);
FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
folderItem.rename("aFolderRenamed");
folder = session.getDocument(folder.getRef());
// Expect no versions since the "versioning-delay" policy doesn't apply to folderish documents
assertTrue(session.getVersions(folder.getRef()).isEmpty());
}
use of org.nuxeo.drive.adapter.FolderItem in project nuxeo-drive-server by nuxeo.
the class DefaultFileSystemItemFactoryFixture method testPermissionCheckNotOptimized.
@Test
@Deploy({ "org.nuxeo.drive.core:OSGI-INF/test-nuxeodrive-permissions-contrib.xml", "org.nuxeo.drive.core:OSGI-INF/test-nuxeodrive-permission-check-not-optimized-contrib.xml" })
public void testPermissionCheckNotOptimized() {
setPermission(syncRootFolder, "joe", SecurityConstants.READ, true);
try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
log.trace("Register the sync root for Joe's account");
nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, joeSession);
folder = joeSession.getDocument(folder.getRef());
log.trace("Check canDelete/canCreateChild flags on folder for user joe with Read granted on parent folder");
FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
assertFalse(folderItem.getCanDelete());
assertFalse(folderItem.getCanCreateChild());
log.trace("Check canDelete/canCreateChild flags on folder for user joe with Write granted on folder, AddChildren not granted on folder and RemoveChildren not granted on parent folder");
setPermission(folder, "joe", SecurityConstants.WRITE, true);
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
// False here as not optimized => explicit check of RemoveChildren
// on parent folder and AddChildren on
// folder
assertFalse(folderItem.getCanDelete());
assertFalse(folderItem.getCanCreateChild());
log.trace("Check canDelete flag on folder for user joe with Write (thus RemoveChildren) granted on parent folder");
setPermission(syncRootFolder, "joe", SecurityConstants.WRITE, true);
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
// True here thanks to RemoveChildren on the parent folder
assertTrue(folderItem.getCanDelete());
// Still false here because of missing AddChildren on folder
assertFalse(folderItem.getCanCreateChild());
log.trace("Check canCreateChild flag on folder for user joe with AddChildren granted on folder");
setPermission(folder, "joe", SecurityConstants.ADD_CHILDREN, true);
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
// True here thanks to AddChildren on folder
assertTrue(folderItem.getCanCreateChild());
}
resetPermissions(folder, "joe");
resetPermissions(syncRootFolder, "joe");
}
use of org.nuxeo.drive.adapter.FolderItem in project nuxeo-drive-server by nuxeo.
the class DefaultFileSystemItemFactoryFixture method testFolderItem.
@Test
public void testFolderItem() throws Exception {
// ------------------------------------------------------
// FolderItem#canCreateChild
// ------------------------------------------------------
// As Administrator
FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
assertTrue(folderItem.getCanCreateChild());
// As a user with READ permission
DocumentModel rootDoc = session.getRootDocument();
setPermission(rootDoc, "joe", SecurityConstants.READ, true);
// Under Oracle, the READ ACL optims are not visible from the joe
// session while the transaction has not been committed.
TransactionHelper.commitOrRollbackTransaction();
TransactionHelper.startTransaction();
try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
folder = joeSession.getDocument(folder.getRef());
// should not be mappable as an fs item.
try {
defaultFileSystemItemFactory.getFileSystemItem(folder);
fail("Should have raised RootlessItemException as ");
} catch (RootlessItemException e) {
// expected
}
// Register the sync root for Joe's account
nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
assertFalse(folderItem.getCanCreateChild());
// As a user with WRITE permission
setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
assertTrue(folderItem.getCanCreateChild());
}
resetPermissions(rootDoc, "joe");
// ------------------------------------------------------
// FolderItem#createFile and FolderItem#createFolder
// ------------------------------------------------------
folder = session.getDocument(folder.getRef());
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
// Note
Blob childBlob = new StringBlob("This is the Note child.");
childBlob.setFilename("Note child.txt");
folderItem.createFile(childBlob);
// File
childBlob = new StringBlob("This is the File child.");
childBlob.setFilename("File child.odt");
childBlob.setMimeType("application/vnd.oasis.opendocument.text");
folderItem.createFile(childBlob);
// Folder
folderItem.createFolder("Sub-folder");
DocumentModelList children = session.query(String.format("select * from Document where ecm:parentId = '%s' order by ecm:primaryType asc", folder.getId()));
assertEquals(3, children.size());
// Check File
DocumentModel file = children.get(0);
assertEquals("File", file.getType());
assertEquals("File child.odt", file.getTitle());
childBlob = (Blob) file.getPropertyValue("file:content");
assertEquals("File child.odt", childBlob.getFilename());
assertEquals("This is the File child.", childBlob.getString());
// Check Folder
DocumentModel subFolder = children.get(1);
assertEquals("Folder", subFolder.getType());
assertEquals("Sub-folder", subFolder.getTitle());
// Check Note
DocumentModel note = children.get(2);
assertEquals("Note", note.getType());
assertEquals("Note child.txt", note.getTitle());
childBlob = note.getAdapter(BlobHolder.class).getBlob();
assertEquals("Note child.txt", childBlob.getFilename());
assertEquals("This is the Note child.", childBlob.getString());
// --------------------------------------------------------------------------------------------
// FolderItem#getChildren, FolderItem#getCanScrollDescendants and
// FolderItem#scrollDescendants
// --------------------------------------------------------------------------------------------
// Create another child adaptable as a FileSystemItem => should be
// retrieved
DocumentModel adaptableChild = session.createDocumentModel("/syncRoot/aFolder", "adaptableChild", "File");
Blob adaptableChildBlob = new StringBlob("Content of another file.");
adaptableChildBlob.setFilename("Another file.odt");
adaptableChild.setPropertyValue("file:content", (Serializable) adaptableChildBlob);
adaptableChild = session.createDocument(adaptableChild);
// Create another child not adaptable as a FileSystemItem => should
// not be retrieved
session.createDocument(session.createDocumentModel("/syncRoot/aFolder", "notAdaptableChild", "NotSynchronizable"));
session.save();
// Check getChildren
List<FileSystemItem> folderChildren = folderItem.getChildren();
assertEquals(4, folderChildren.size());
// Ordered
checkChildren(folderChildren, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), true);
// Check scrollDescendants
assertTrue(folderItem.getCanScrollDescendants());
// Scroll through all descendants in one breath
ScrollFileSystemItemList folderDescendants = folderItem.scrollDescendants(null, 10, 1000);
String scrollId = folderDescendants.getScrollId();
assertNotNull(scrollId);
assertEquals(4, folderDescendants.size());
// Order is not determined
checkChildren(folderDescendants, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
// Check that next call to scrollDescendants returns an empty list
assertTrue(folderItem.scrollDescendants(scrollId, 10, 1000).isEmpty());
// Scroll through descendants in several steps
folderDescendants.clear();
ScrollFileSystemItemList descendantsBatch;
int batchSize = 2;
scrollId = null;
while (!(descendantsBatch = folderItem.scrollDescendants(scrollId, batchSize, 1000)).isEmpty()) {
assertTrue(descendantsBatch.size() > 0);
scrollId = descendantsBatch.getScrollId();
folderDescendants.addAll(descendantsBatch);
}
assertEquals(4, folderDescendants.size());
// Order is not determined
checkChildren(folderDescendants, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
// Check batch size limit
try {
folderItem.scrollDescendants(null, 10000, 1000);
fail("Should not be able to scroll through more descendants than the maximum batch size allowed.");
} catch (NuxeoException e) {
log.trace(e);
}
}
use of org.nuxeo.drive.adapter.FolderItem in project nuxeo-drive-server by nuxeo.
the class TestCollectionSyncRootFolderItemFactory method testFactory.
@Test
public void testFactory() throws Exception {
FileSystemItemFactory collectionSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("collectionSyncRootFolderItemFactory");
DocumentModel collection = collectionManager.createCollection(session, "testCollection", "Test collection.", "/");
DocumentModel doc1 = session.createDocumentModel("/", "doc1", "File");
doc1.setPropertyValue("dc:title", "doc1");
doc1.setPropertyValue("file:content", new StringBlob("Content of file 1."));
doc1 = session.createDocument(doc1);
collectionManager.addToCollection(collection, doc1, session);
assertTrue(collectionManager.isInCollection(collection, doc1, session));
DocumentModel doc2 = session.createDocumentModel("/", "doc2", "File");
doc2.setPropertyValue("dc:title", "doc2");
doc2.setPropertyValue("file:content", new StringBlob("Content of file 2."));
doc2 = session.createDocument(doc2);
collectionManager.addToCollection(collection, doc2, session);
assertTrue(collectionManager.isInCollection(collection, doc2, session));
log.trace("Check document that is not a Collection");
assertFalse(collectionSyncRootFolderItemFactory.isFileSystemItem(session.getRootDocument()));
log.trace("Check Collection not registered as a sync root");
assertFalse(collectionSyncRootFolderItemFactory.isFileSystemItem(collection));
log.trace("Check Collection registered as a sync root");
nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collection, session);
assertTrue(collectionSyncRootFolderItemFactory.isFileSystemItem(collection));
log.trace("Adapt test collection as a FileSystemItem");
FileSystemItem fsItem = collectionSyncRootFolderItemFactory.getFileSystemItem(collection);
assertNotNull(fsItem);
assertTrue(fsItem instanceof CollectionSyncRootFolderItem);
log.trace("Check children");
FolderItem collectionFSItem = (FolderItem) fsItem;
List<FileSystemItem> collectionChildren = collectionFSItem.getChildren();
assertEquals(2, collectionChildren.size());
FileSystemItem child1 = collectionChildren.get(0);
assertTrue(child1 instanceof FileItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc1.getId(), child1.getId());
assertEquals(COLLECTION_SYNC_ROOT_ITEM_ID_PREFIX + collection.getId(), child1.getParentId());
assertEquals("doc1", child1.getName());
FileSystemItem child2 = collectionChildren.get(1);
assertTrue(child2 instanceof FileItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + doc2.getId(), child2.getId());
assertEquals(COLLECTION_SYNC_ROOT_ITEM_ID_PREFIX + collection.getId(), child2.getParentId());
assertEquals("doc2", child2.getName());
log.trace("Check FolderItem#getCanScrollDescendants");
assertFalse(collectionFSItem.getCanScrollDescendants());
log.trace("Check descendants");
try {
collectionFSItem.scrollDescendants(null, 10, 1000);
fail("Should not be able to scroll through the descendants of a CollectionSyncRootFolderItem.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot scroll through the descendants of a collection sync root folder item, please call getChildren() instead.", e.getMessage());
}
log.trace("Check FolderItem#getCanCreateChild");
assertFalse(collectionFSItem.getCanCreateChild());
log.trace("Check FolderItem#createFile");
try {
collectionFSItem.createFile(new StringBlob("Child file content."));
fail("Should not be able to create a file in a CollectionSyncRootFolderItem.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot create a file in a collection synchronization root.", e.getMessage());
}
log.trace("Check FolderItem#createFolder");
try {
collectionFSItem.createFolder("Child folder");
fail("Should not be able to create a folder in a CollectionSyncRootFolderItem.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot create a folder in a collection synchronization root.", e.getMessage());
}
log.trace("Test AbstractDocumentBackedFileSystemItem#delete");
child1.delete();
doc1 = session.getDocument(doc1.getRef());
assertFalse(doc1.isTrashed());
assertFalse(collectionManager.isInCollection(collection, doc1, session));
}
use of org.nuxeo.drive.adapter.FolderItem in project nuxeo-drive-server by nuxeo.
the class TestDefaultTopLevelFolderItemFactory method testFactory.
@Test
public void testFactory() throws Exception {
// -------------------------------------------------------------
// Check TopLevelFolderItemFactory#getTopLevelFolderItem(String
// Principal)
// -------------------------------------------------------------
FolderItem topLevelFolderItem = defaultTopLevelFolderItemFactory.getTopLevelFolderItem(session.getPrincipal());
assertNotNull(topLevelFolderItem);
assertTrue(topLevelFolderItem instanceof DefaultTopLevelFolderItem);
assertTrue(topLevelFolderItem.getId().endsWith("DefaultTopLevelFolderItemFactory#"));
assertTrue(topLevelFolderItem.getPath().endsWith("DefaultTopLevelFolderItemFactory#"));
assertTrue(topLevelFolderItem.getPath().startsWith("/"));
assertNull(topLevelFolderItem.getParentId());
assertEquals("Nuxeo Drive", topLevelFolderItem.getName());
assertTrue(topLevelFolderItem.isFolder());
assertEquals("system", topLevelFolderItem.getCreator());
assertEquals("system", topLevelFolderItem.getLastContributor());
assertFalse(topLevelFolderItem.getCanRename());
try {
topLevelFolderItem.rename("newName");
fail("Should not be able to rename the default top level folder item.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot rename a virtual folder item.", e.getMessage());
}
assertFalse(topLevelFolderItem.getCanDelete());
try {
topLevelFolderItem.delete();
fail("Should not be able to delete the default top level folder item.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot delete a virtual folder item.", e.getMessage());
}
assertFalse(topLevelFolderItem.canMove(null));
try {
topLevelFolderItem.move(null);
fail("Should not be able to move the default top level folder item.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot move a virtual folder item.", e.getMessage());
}
List<FileSystemItem> children = topLevelFolderItem.getChildren();
assertNotNull(children);
assertEquals(2, children.size());
assertFalse(topLevelFolderItem.getCanScrollDescendants());
try {
topLevelFolderItem.scrollDescendants(null, 10, 1000);
fail("Should not be able to scroll through the descendants of the default top level folder item.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot scroll through the descendants of a virtual folder item, please call getChildren() instead.", e.getMessage());
}
assertFalse(topLevelFolderItem.getCanCreateChild());
for (FileSystemItem child : children) {
assertEquals(topLevelFolderItem.getPath() + '/' + child.getId(), child.getPath());
}
try {
topLevelFolderItem.createFile(new StringBlob("Child file content."));
fail("Should not be able to create a file in the default top level folder item.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot create a file in a virtual folder item.", e.getMessage());
}
try {
topLevelFolderItem.createFolder("subFolder");
fail("Should not be able to create a folder in the default top level folder item.");
} catch (UnsupportedOperationException e) {
assertEquals("Cannot create a folder in a virtual folder item.", e.getMessage());
}
// -------------------------------------------------------------
// Check VirtualFolderItemFactory#getVirtualFolderItem(Principal
// userName)
// -------------------------------------------------------------
assertEquals(topLevelFolderItem, defaultTopLevelFolderItemFactory.getVirtualFolderItem(session.getPrincipal()));
}
Aggregations