Search in sources :

Example 6 with DocumentBackedFileItem

use of org.nuxeo.drive.adapter.impl.DocumentBackedFileItem in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemOperations method testGetChildren.

@Test
public void testGetChildren() throws Exception {
    // Get children of sub-folder of sync root 1
    Blob childrenJSON = (Blob) clientSession.newRequest(NuxeoDriveGetChildren.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder1.getId()).execute();
    assertNotNull(childrenJSON);
    List<DocumentBackedFileItem> children = mapper.readValue(childrenJSON.getStream(), new TypeReference<List<DocumentBackedFileItem>>() {
    });
    assertNotNull(children);
    assertEquals(2, children.size());
    // Ordered
    checkChildren(children, subFolder1.getId(), file3.getId(), file4.getId(), true);
}
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) List(java.util.List) ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) Test(org.junit.Test)

Example 7 with DocumentBackedFileItem

use of org.nuxeo.drive.adapter.impl.DocumentBackedFileItem in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemOperations method testRename.

@Test
public void testRename() throws Exception {
    // ------------------------------------------------------
    // File
    // ------------------------------------------------------
    Blob renamedFSItemJSON = (Blob) clientSession.newRequest(NuxeoDriveRename.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file1.getId()).set("name", "Renamed file 1.odt").execute();
    assertNotNull(renamedFSItemJSON);
    DocumentBackedFileItem renamedFileItem = mapper.readValue(renamedFSItemJSON.getStream(), DocumentBackedFileItem.class);
    assertNotNull(renamedFileItem);
    assertEquals("Renamed file 1.odt", renamedFileItem.getName());
    // Need to flush VCS cache to be aware of changes in the session used by the file system item
    session.save();
    DocumentModel renamedFileDoc = session.getDocument(new IdRef(file1.getId()));
    assertEquals("file1", renamedFileDoc.getTitle());
    org.nuxeo.ecm.core.api.Blob renamedFileBlob = (org.nuxeo.ecm.core.api.Blob) renamedFileDoc.getPropertyValue("file:content");
    assertNotNull(renamedFileBlob);
    assertEquals("Renamed file 1.odt", renamedFileBlob.getFilename());
    assertEquals("nxfile/test/" + file1.getId() + "/blobholder:0/Renamed%20file%201.odt", renamedFileItem.getDownloadURL());
    assertEquals("MD5", renamedFileItem.getDigestAlgorithm());
    assertEquals(renamedFileBlob.getDigest(), renamedFileItem.getDigest());
    // ------------------------------------------------------
    // Folder
    // ------------------------------------------------------
    renamedFSItemJSON = (Blob) clientSession.newRequest(NuxeoDriveRename.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder1.getId()).set("name", "Renamed sub-folder 1").execute();
    assertNotNull(renamedFSItemJSON);
    DocumentBackedFolderItem renamedFolderItem = mapper.readValue(renamedFSItemJSON.getStream(), DocumentBackedFolderItem.class);
    assertNotNull(renamedFolderItem);
    assertEquals("Renamed sub-folder 1", renamedFolderItem.getName());
    // Need to flush VCS cache to be aware of changes in the session used by the file system item
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    DocumentModel renamedFolderDoc = session.getDocument(new IdRef(subFolder1.getId()));
    assertEquals("Renamed sub-folder 1", renamedFolderDoc.getTitle());
    // ------------------------------------------------------
    // Sync root
    // ------------------------------------------------------
    renamedFSItemJSON = (Blob) clientSession.newRequest(NuxeoDriveRename.ID).set("id", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId()).set("name", "New name for sync root").execute();
    assertNotNull(renamedFSItemJSON);
    DefaultSyncRootFolderItem renamedSyncRootItem = mapper.readValue(renamedFSItemJSON.getStream(), DefaultSyncRootFolderItem.class);
    assertNotNull(renamedSyncRootItem);
    assertEquals("New name for sync root", renamedSyncRootItem.getName());
    // Need to flush VCS cache to be aware of changes in the session used by the file system item
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    DocumentModel renamedSyncRootDoc = session.getDocument(new IdRef(syncRoot1.getId()));
    assertEquals("New name for sync root", renamedSyncRootDoc.getTitle());
    // ------------------------------------------------------
    try {
        clientSession.newRequest(NuxeoDriveRename.ID).set("id", fileSystemItemAdapterService.getTopLevelFolderItemFactory().getTopLevelFolderItem(session.getPrincipal()).getId()).set("name", "New name for top level folder").execute();
        fail("Top level folder renaming shoud be unsupported.");
    } catch (Exception e) {
        assertEquals("Failed to invoke operation: NuxeoDrive.Rename", e.getMessage());
    }
}
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) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) DocumentBackedFolderItem(org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem) Test(org.junit.Test)

Example 8 with DocumentBackedFileItem

use of org.nuxeo.drive.adapter.impl.DocumentBackedFileItem in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemOperations method testGetFileSystemItem.

@Test
public void testGetFileSystemItem() throws Exception {
    // Get top level folder
    String topLevelFolderItemId = fileSystemItemAdapterService.getTopLevelFolderItemFactory().getTopLevelFolderItem(session.getPrincipal()).getId();
    Blob fileSystemItemJSON = (Blob) clientSession.newRequest(NuxeoDriveGetFileSystemItem.ID).set("id", topLevelFolderItemId).execute();
    assertNotNull(fileSystemItemJSON);
    DefaultTopLevelFolderItem topLevelFolderItem = mapper.readValue(fileSystemItemJSON.getStream(), DefaultTopLevelFolderItem.class);
    assertNotNull(topLevelFolderItem);
    assertEquals(topLevelFolderItemId, topLevelFolderItem.getId());
    assertNull(topLevelFolderItem.getParentId());
    assertEquals("Nuxeo Drive", topLevelFolderItem.getName());
    assertTrue(topLevelFolderItem.isFolder());
    assertEquals("system", topLevelFolderItem.getCreator());
    assertEquals("system", topLevelFolderItem.getLastContributor());
    assertFalse(topLevelFolderItem.getCanRename());
    assertFalse(topLevelFolderItem.getCanDelete());
    assertFalse(topLevelFolderItem.getCanCreateChild());
    // Get sync root
    fileSystemItemJSON = (Blob) clientSession.newRequest(NuxeoDriveGetFileSystemItem.ID).set("id", SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId()).execute();
    assertNotNull(fileSystemItemJSON);
    DefaultSyncRootFolderItem syncRootFolderItem = mapper.readValue(fileSystemItemJSON.getStream(), DefaultSyncRootFolderItem.class);
    assertNotNull(syncRootFolderItem);
    assertEquals(SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId(), syncRootFolderItem.getId());
    assertTrue(syncRootFolderItem.getParentId().endsWith("DefaultTopLevelFolderItemFactory#"));
    assertEquals("folder1", syncRootFolderItem.getName());
    assertTrue(syncRootFolderItem.isFolder());
    assertEquals("Administrator", syncRootFolderItem.getCreator());
    assertEquals("Administrator", syncRootFolderItem.getLastContributor());
    assertTrue(syncRootFolderItem.getCanRename());
    assertTrue(syncRootFolderItem.getCanDelete());
    assertTrue(syncRootFolderItem.getCanCreateChild());
    // Get file in sync root
    fileSystemItemJSON = (Blob) clientSession.newRequest(NuxeoDriveGetFileSystemItem.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file1.getId()).execute();
    assertNotNull(fileSystemItemJSON);
    DocumentBackedFileItem fileItem = mapper.readValue(fileSystemItemJSON.getStream(), DocumentBackedFileItem.class);
    assertNotNull(fileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file1.getId(), fileItem.getId());
    assertEquals(SYNC_ROOT_FOLDER_ITEM_ID_PREFIX + syncRoot1.getId(), fileItem.getParentId());
    assertEquals("First file.odt", fileItem.getName());
    assertFalse(fileItem.isFolder());
    assertEquals("Administrator", fileItem.getCreator());
    assertEquals("Administrator", fileItem.getLastContributor());
    assertTrue(fileItem.getCanRename());
    assertTrue(fileItem.getCanDelete());
    assertTrue(fileItem.getCanUpdate());
    assertEquals("nxfile/test/" + file1.getId() + "/blobholder:0/First%20file.odt", fileItem.getDownloadURL());
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    assertEquals(((org.nuxeo.ecm.core.api.Blob) file1.getPropertyValue("file:content")).getDigest(), fileItem.getDigest());
    // Get deleted file
    file1.followTransition("delete");
    // Need to flush VCS cache to be aware of changes in the session used by the file system item
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    fileSystemItemJSON = (Blob) clientSession.newRequest(NuxeoDriveGetFileSystemItem.ID).set("id", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file1.getId()).execute();
    assertNotNull(fileSystemItemJSON);
    assertNull(mapper.readValue(fileSystemItemJSON.getStream(), Object.class));
}
Also used : Blob(org.nuxeo.ecm.automation.client.model.Blob) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) DefaultTopLevelFolderItem(org.nuxeo.drive.adapter.impl.DefaultTopLevelFolderItem) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) Test(org.junit.Test)

Example 9 with DocumentBackedFileItem

use of org.nuxeo.drive.adapter.impl.DocumentBackedFileItem in project nuxeo-drive-server by nuxeo.

the class TestFileSystemItemOperations method testCreateFile.

@Test
public void testCreateFile() throws Exception {
    StringBlob blob = new StringBlob("This is the content of a new file.");
    blob.setFileName("New file.odt");
    Blob newFileJSON = (Blob) clientSession.newRequest(NuxeoDriveCreateFile.ID).set("parentId", DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder1.getId()).setInput(blob).execute();
    assertNotNull(newFileJSON);
    DocumentBackedFileItem newFile = mapper.readValue(newFileJSON.getStream(), DocumentBackedFileItem.class);
    assertNotNull(newFile);
    // Need to flush VCS cache to be aware of changes in the session used by the file system item
    session.save();
    DocumentModel newFileDoc = session.getDocument(new PathRef("/folder1/subFolder1/New file.odt"));
    assertEquals("File", newFileDoc.getType());
    assertEquals("New file.odt", newFileDoc.getTitle());
    org.nuxeo.ecm.core.api.Blob newFileBlob = (org.nuxeo.ecm.core.api.Blob) newFileDoc.getPropertyValue("file:content");
    assertNotNull(newFileBlob);
    assertEquals("New file.odt", newFileBlob.getFilename());
    assertEquals("This is the content of a new file.", newFileBlob.getString());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + newFileDoc.getId(), newFile.getId());
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + subFolder1.getId(), newFile.getParentId());
    assertEquals("New file.odt", newFile.getName());
    assertFalse(newFile.isFolder());
    assertEquals("Administrator", newFile.getCreator());
    assertEquals("Administrator", newFile.getLastContributor());
    assertTrue(newFile.getCanRename());
    assertTrue(newFile.getCanDelete());
    assertTrue(newFile.getCanUpdate());
    assertEquals("nxfile/test/" + newFileDoc.getId() + "/blobholder:0/New%20file.odt", newFile.getDownloadURL());
    assertEquals("MD5", newFile.getDigestAlgorithm());
    assertEquals(newFileBlob.getDigest(), newFile.getDigest());
}
Also used : Blob(org.nuxeo.ecm.automation.client.model.Blob) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) PathRef(org.nuxeo.ecm.core.api.PathRef) StringBlob(org.nuxeo.ecm.automation.client.model.StringBlob) DocumentBackedFileItem(org.nuxeo.drive.adapter.impl.DocumentBackedFileItem) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Aggregations

DocumentBackedFileItem (org.nuxeo.drive.adapter.impl.DocumentBackedFileItem)9 Blob (org.nuxeo.ecm.automation.client.model.Blob)9 Test (org.junit.Test)8 StringBlob (org.nuxeo.ecm.automation.client.model.StringBlob)7 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)5 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)4 List (java.util.List)3 DocumentBackedFolderItem (org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 IOException (java.io.IOException)2 TopLevelFolderItemFactory (org.nuxeo.drive.service.TopLevelFolderItemFactory)2 IdRef (org.nuxeo.ecm.core.api.IdRef)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1