Search in sources :

Example 51 with Document

use of org.alfresco.rest.api.tests.client.data.Document in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testGetPathElements_DocLib.

/**
 * Tests get node with path information.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>?include=path}
 */
@Test
public void testGetPathElements_DocLib() throws Exception {
    setRequestContext(user1);
    // user1 creates a private site and adds user2 as a site consumer
    String site1Title = "site-testGetPathElements_DocLib-" + RUNID;
    String site1Id = createSite(site1Title, SiteVisibility.PRIVATE).getId();
    addSiteMember(site1Id, user2, SiteRole.SiteConsumer);
    String site1DocLibNodeId = getSiteContainerNodeId(site1Id, "documentLibrary");
    // /Company Home/Sites/RandomSite<timestamp>/documentLibrary/folder<timestamp>_A
    String folderA = "folder" + RUNID + "_A";
    String folderA_Id = createFolder(site1DocLibNodeId, folderA).getId();
    // /Company Home/Sites/RandomSite<timestamp>/documentLibrary/folder<timestamp>_A/folder<timestamp>_B
    String folderB = "folder" + RUNID + "_B";
    String folderB_Id = createFolder(folderA_Id, folderB).getId();
    NodeRef folderB_Ref = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, folderB_Id);
    // /Company Home/Sites/RandomSite<timestamp>/documentLibrary/folder<timestamp>_A/folder<timestamp>_B/folder<timestamp>_C
    String folderC = "folder" + RUNID + "_C";
    String folderC_Id = createFolder(folderB_Id, folderC).getId();
    NodeRef folderC_Ref = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, folderC_Id);
    // /Company Home/Sites/RandomSite<timestamp>/documentLibrary/folder<timestamp>_A/folder<timestamp>_B/folder<timestamp>_C/content<timestamp>
    String content = "content" + RUNID;
    String content1_Id = createTextFile(folderC_Id, content, "The quick brown fox jumps over the lazy dog.").getId();
    // TODO refactor with remote permission api calls (maybe use v0 until we have v1 ?)
    final String tenantDomain = (networkOne != null ? networkOne.getId() : TenantService.DEFAULT_DOMAIN);
    AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            return TenantUtil.runAsTenant(new TenantUtil.TenantRunAsWork<Void>() {

                public Void doWork() throws Exception {
                    // Revoke folderB inherited permissions
                    permissionService.setInheritParentPermissions(folderB_Ref, false);
                    // Grant user2 permission for folderC
                    permissionService.setPermission(folderC_Ref, user2, PermissionService.CONSUMER, true);
                    return null;
                }
            }, tenantDomain);
        }
    }, user1);
    // ...nodes/nodeId?include=path
    Map<String, String> params = Collections.singletonMap("include", "path");
    HttpResponse response = getSingle(NodesEntityResource.class, content1_Id, params, 200);
    Document node = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
    PathInfo path = node.getPath();
    assertNotNull(path);
    assertTrue(path.getIsComplete());
    assertNotNull(path.getName());
    // the path should only include the parents (not the requested node)
    assertFalse(path.getName().endsWith(content));
    assertTrue(path.getName().startsWith("/Company Home"));
    List<ElementInfo> pathElements = path.getElements();
    assertEquals(7, pathElements.size());
    // Check path element names and types, and one or two random aspects.
    assertEquals("Company Home", pathElements.get(0).getName());
    assertEquals("cm:folder", pathElements.get(0).getNodeType());
    assertEquals("Sites", pathElements.get(1).getName());
    assertEquals("st:sites", pathElements.get(1).getNodeType());
    assertEquals(site1Id, pathElements.get(2).getName());
    assertEquals("st:site", pathElements.get(2).getNodeType());
    assertTrue(pathElements.get(2).getAspectNames().contains("cm:titled"));
    // Check that sys:* is filtered out - to be consistent with other aspect name lists
    // e.g. /nodes/{nodeId}/children?include=aspectNames
    assertFalse(pathElements.get(2).getAspectNames().contains("sys:undeletable"));
    assertFalse(pathElements.get(2).getAspectNames().contains("sys:unmovable"));
    assertEquals("documentLibrary", pathElements.get(3).getName());
    assertEquals("cm:folder", pathElements.get(3).getNodeType());
    assertTrue(pathElements.get(3).getAspectNames().contains("st:siteContainer"));
    assertEquals(folderA, pathElements.get(4).getName());
    assertEquals("cm:folder", pathElements.get(4).getNodeType());
    assertEquals(folderB, pathElements.get(5).getName());
    assertEquals("cm:folder", pathElements.get(5).getNodeType());
    assertEquals(folderC, pathElements.get(6).getName());
    assertEquals("cm:folder", pathElements.get(6).getNodeType());
    // Try the above tests with user2 (site consumer)
    setRequestContext(user2);
    response = getSingle(NodesEntityResource.class, content1_Id, params, 200);
    node = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
    path = node.getPath();
    assertNotNull(path);
    assertFalse("The path is not complete as the user doesn't have permission to access the full path.", path.getIsComplete());
    assertNotNull(path.getName());
    // site consumer (user2) dose not have access to the folderB
    assertFalse("site consumer (user2) dose not have access to the folderB", path.getName().contains(folderB));
    assertFalse(path.getName().startsWith("/Company Home"));
    // Go up as far as they can, before getting access denied (i.e. "/folderC")
    assertTrue(path.getName().endsWith(folderC));
    pathElements = path.getElements();
    assertEquals(1, pathElements.size());
    assertEquals(folderC, pathElements.get(0).getName());
    // cleanup
    setRequestContext(user1);
    deleteSite(site1Id, true, 204);
}
Also used : ElementInfo(org.alfresco.rest.api.tests.client.data.PathInfo.ElementInfo) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Document(org.alfresco.rest.api.tests.client.data.Document) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) PathInfo(org.alfresco.rest.api.tests.client.data.PathInfo) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 52 with Document

use of org.alfresco.rest.api.tests.client.data.Document in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testUploadToSite.

/**
 * Tests Multipart upload to a Site.
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/children}
 */
@Test
public void testUploadToSite() throws Exception {
    setRequestContext(user1);
    final String fileName = "quick-1.txt";
    final File file = getResourceFile(fileName);
    String folderA = "folder" + RUNID + "_A";
    String folderA_id = createFolder(tDocLibNodeId, folderA).getId();
    Paging paging = getPaging(0, Integer.MAX_VALUE);
    HttpResponse response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
    PublicApiClient.ExpectedPaging pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    final int numOfNodes = pagingResult.getCount();
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    MultiPartRequest reqBody = multiPartBuilder.build();
    // Try to upload
    response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName, document.getName());
    ContentInfo contentInfo = document.getContent();
    assertNotNull(contentInfo);
    // As the client didn't set the mimeType, the API must guess it.
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    // Retrieve the uploaded file
    response = getSingle(NodesEntityResource.class, document.getId(), null, 200);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(fileName, document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    // Check 'get children' is confirming the upload
    response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
    pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    assertEquals(numOfNodes + 1, pagingResult.getCount().intValue());
    // Upload the same file again to check the name conflicts handling
    post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 409);
    response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
    pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    assertEquals(numOfNodes + 1, pagingResult.getCount().intValue());
    setRequestContext(user2);
    final String fileName2 = "quick-2.txt";
    final File file2 = getResourceFile(fileName2);
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).build();
    // user2 tries to upload a new file into the folderA of user1
    post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 403);
    setRequestContext(user1);
    // Test upload with properties
    response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName2, document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertNotNull(document.getProperties());
    assertNull(document.getProperties().get("cm:title"));
    assertNull(document.getProperties().get("cm:description"));
    // upload a file with properties. Also, set autoRename=true
    Map<String, String> props = new HashMap<>(2);
    props.put("cm:title", "test title");
    props.put("cm:description", "test description");
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setProperties(props).build();
    response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    // "quick-2-1.txt" => fileName2 + autoRename
    assertEquals("quick-2-1.txt", document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertNotNull(document.getProperties());
    assertEquals("test title", document.getProperties().get("cm:title"));
    assertEquals("test description", document.getProperties().get("cm:description"));
    // Test unknown property name
    props = new HashMap<>(1);
    props.put("unknownPrefix" + System.currentTimeMillis() + ":description", "test description");
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setProperties(props).build();
    // Prop prefix is unknown
    post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 400);
    // Test relativePath multi-part field.
    // Any folders in the relativePath that do not exist, are created before the content is created.
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setRelativePath("X/Y/Z");
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), "?include=path", reqBody.getContentType(), 201);
    document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName, document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    // Check the uploaded file parent folders
    PathInfo pathInfo = document.getPath();
    assertNotNull(pathInfo);
    List<ElementInfo> elementInfos = pathInfo.getElements();
    assertNotNull(elementInfos);
    // /Company Home/Sites/RandomSite<timestamp>/documentLibrary/folder<timestamp>_A/X/Y/Z
    assertEquals(8, elementInfos.size());
    assertEquals(document.getParentId(), elementInfos.get(7).getId());
    assertEquals("Z", elementInfos.get(7).getName());
    assertEquals("Y", elementInfos.get(6).getName());
    assertEquals("X", elementInfos.get(5).getName());
    assertEquals(folderA, elementInfos.get(4).getName());
    // Try to create a folder with the same name as the document within the 'Z' folder.
    reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("X/Y/Z/" + document.getName()).build();
    post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 409);
    // Test the same functionality as "mkdir -p x/y/z" which the folders should be created
    // as needed but no errors thrown if the path or any part of the path already exists.
    // NOTE: white spaces, leading and trailing "/" are ignored.
    reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("/X/ Y/Z /CoolFolder/").build();
    response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName, document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    // Retrieve the uploaded file parent folder
    response = getSingle(NodesEntityResource.class, document.getParentId(), null, 200);
    Folder coolFolder = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(document.getParentId(), coolFolder.getId());
    assertEquals("CoolFolder", coolFolder.getName());
    // Try to upload quick-1.txt within coolFolder and set the relativePath to a blank string.
    reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath(// blank
    "  ").build();
    // 409 -> as the blank string is ignored and quick-1.txt already exists in the coolFolder
    post(getNodeChildrenUrl(coolFolder.getId()), reqBody.getBody(), null, reqBody.getContentType(), 409);
    setRequestContext(user2);
    // user2 tries to upload the same file by creating sub-folders in the folderA of user1
    reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("userTwoFolder1/userTwoFolder2").build();
    post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 403);
    // -ve test: integrity error
    setRequestContext(user1);
    reqBody = MultiPartBuilder.create().setFileData(new FileData("invalid:name", file)).build();
    // 422 -> invalid name (includes a ':' in this example)
    post(getNodeChildrenUrl(coolFolder.getId()), reqBody.getBody(), null, reqBody.getContentType(), 422);
}
Also used : ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ElementInfo(org.alfresco.rest.api.tests.client.data.PathInfo.ElementInfo) RestApiUtil.parsePaging(org.alfresco.rest.api.tests.util.RestApiUtil.parsePaging) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Document(org.alfresco.rest.api.tests.client.data.Document) Folder(org.alfresco.rest.api.tests.client.data.Folder) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) PathInfo(org.alfresco.rest.api.tests.client.data.PathInfo) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 53 with Document

use of org.alfresco.rest.api.tests.client.data.Document in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testCreateEmptyFile.

/**
 * Tests create empty file.
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 */
@Test
public void testCreateEmptyFile() throws Exception {
    setRequestContext(user1);
    // create folder f0
    String folder0Name = "f0-testCreateEmptyFile-" + RUNID;
    String f0Id = createFolder(Nodes.PATH_MY, folder0Name).getId();
    UserInfo expectedUser = new UserInfo(user1);
    String postUrl = getNodeChildrenUrl(f0Id);
    Document d1 = new Document();
    d1.setName("d1.txt");
    d1.setNodeType(TYPE_CM_CONTENT);
    // create empty file
    HttpResponse response = post(postUrl, toJsonAsStringNonNull(d1), 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String d1Id = documentResp.getId();
    d1.setIsFolder(false);
    d1.setParentId(f0Id);
    d1.setAspectNames(Collections.singletonList("cm:auditable"));
    d1.setCreatedByUser(expectedUser);
    d1.setModifiedByUser(expectedUser);
    ContentInfo ciExpected = new ContentInfo();
    ciExpected.setMimeType("text/plain");
    ciExpected.setMimeTypeName("Plain Text");
    ciExpected.setSizeInBytes(0L);
    ciExpected.setEncoding("UTF-8");
    d1.setContent(ciExpected);
    d1.expected(documentResp);
    // create empty file with properties
    Map<String, Object> props = new HashMap<>();
    props.put("cm:title", "my file title");
    props.put("cm:description", "my file description");
    Document d2 = new Document();
    d2.setName("d2.txt");
    d2.setNodeType(TYPE_CM_CONTENT);
    d2.setProperties(props);
    response = post(postUrl, toJsonAsStringNonNull(d2), 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    d2.setIsFolder(false);
    d2.setParentId(f0Id);
    d2.setAspectNames(Arrays.asList("cm:auditable", "cm:titled"));
    d2.setCreatedByUser(expectedUser);
    d2.setModifiedByUser(expectedUser);
    ciExpected = new ContentInfo();
    ciExpected.setMimeType("text/plain");
    ciExpected.setMimeTypeName("Plain Text");
    ciExpected.setSizeInBytes(0L);
    ciExpected.setEncoding("UTF-8");
    d2.setContent(ciExpected);
    d2.expected(documentResp);
    // create another empty file in a (partially existing) folder path
    Node n = new Node();
    n.setName("d3.txt");
    n.setNodeType(TYPE_CM_CONTENT);
    n.setRelativePath("/f1/f2");
    // create node
    response = post(getNodeChildrenUrl(f0Id), RestApiUtil.toJsonAsStringNonNull(n), 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // check parent hierarchy ...
    response = getSingle(NodesEntityResource.class, documentResp.getId(), null, 200);
    Folder folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderResp.getName(), "d3.txt");
    response = getSingle(NodesEntityResource.class, folderResp.getParentId(), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderResp.getName(), "f2");
    response = getSingle(NodesEntityResource.class, folderResp.getParentId(), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderResp.getName(), "f1");
    response = getSingle(NodesEntityResource.class, folderResp.getParentId(), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(f0Id, folderResp.getId());
    // -ve test - name is mandatory
    Document invalid = new Document();
    invalid.setNodeType(TYPE_CM_CONTENT);
    post(postUrl, toJsonAsStringNonNull(invalid), 400);
    // -ve test - node type is mandatory
    invalid = new Document();
    invalid.setName("my file.txt");
    post(postUrl, toJsonAsStringNonNull(invalid), 400);
    // -ve test - invalid (model integrity exception)
    Document d3 = new Document();
    d3.setName("d3.txt");
    d3.setNodeType(TYPE_CM_CONTENT);
    post(getNodeChildrenUrl(d1Id), toJsonAsStringNonNull(d3), 422);
    // -ve test - unknown parent folder node id
    post(getNodeChildrenUrl(UUID.randomUUID().toString()), toJsonAsStringNonNull(d3), 404);
    // -ve test - duplicate name
    post(postUrl, toJsonAsStringNonNull(d1), 409);
    // Create a file with a duplicate name (d1.txt), but set the autoRename to true
    response = post(postUrl, toJsonAsStringNonNull(d1), "?" + Nodes.PARAM_AUTO_RENAME + "=true", 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals("d1-1.txt", documentResp.getName());
    // Create a file with a duplicate name (d1.txt) again, but set the autoRename to true
    response = post(postUrl, toJsonAsStringNonNull(d1), "?" + Nodes.PARAM_AUTO_RENAME + "=true", 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals("d1-2.txt", documentResp.getName());
    // Create a file with a duplicate name (d1-2.txt) again, but set the autoRename to true
    d1.setName("d1-2.txt");
    response = post(postUrl, toJsonAsStringNonNull(d1), "?" + Nodes.PARAM_AUTO_RENAME + "=true", 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals("d1-2-1.txt", documentResp.getName());
    // -ve test - create a file with a duplicate name (d1-2.txt), but set the autoRename to false
    post(postUrl, toJsonAsStringNonNull(d1), "?" + Nodes.PARAM_AUTO_RENAME + "=false", 409);
}
Also used : ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.alfresco.rest.api.tests.client.data.Node) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) UserInfo(org.alfresco.rest.api.tests.client.data.UserInfo) JSONObject(org.json.simple.JSONObject) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Document(org.alfresco.rest.api.tests.client.data.Document) Folder(org.alfresco.rest.api.tests.client.data.Folder) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 54 with Document

use of org.alfresco.rest.api.tests.client.data.Document in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testGuessMimeTypeAndEncoding.

/**
 * Tests guess mimetype & guess encoding when uploading file/content (create or update) - also empty file/content
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 * <p>PUT:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
 */
@Test
public void testGuessMimeTypeAndEncoding() throws Exception {
    setRequestContext(user1);
    String fId = createFolder(getMyNodeId(), "test-folder-guess-" + RUNID).getId();
    // create empty files
    Document d = new Document();
    d.setName("my doc");
    d.setNodeType(TYPE_CM_CONTENT);
    HttpResponse response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(MimetypeMap.MIMETYPE_BINARY, documentResp.getContent().getMimeType());
    assertEquals("Binary File (Octet Stream)", documentResp.getContent().getMimeTypeName());
    assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
    assertEquals("UTF-8", documentResp.getContent().getEncoding());
    d = new Document();
    d.setName("my doc.txt");
    d.setNodeType(TYPE_CM_CONTENT);
    response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, documentResp.getContent().getMimeType());
    assertEquals("Plain Text", documentResp.getContent().getMimeTypeName());
    assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
    assertEquals("UTF-8", documentResp.getContent().getEncoding());
    d = new Document();
    d.setName("my doc.pdf");
    d.setNodeType(TYPE_CM_CONTENT);
    response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(MimetypeMap.MIMETYPE_PDF, documentResp.getContent().getMimeType());
    assertEquals("Adobe PDF Document", documentResp.getContent().getMimeTypeName());
    assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
    assertEquals("UTF-8", documentResp.getContent().getEncoding());
    // upload files
    String fileName = "quick-2.pdf";
    File file = getResourceFile(fileName);
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    MultiPartRequest reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    ContentInfo contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "quick-2.pdf";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(// note: we've deliberately dropped the file ext here
    new FileData("quick-2", file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "example-1.txt";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("ISO-8859-1", contentInfo.getEncoding());
    fileName = "example-2.txt";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "shift-jis.txt";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("Shift_JIS", contentInfo.getEncoding());
    fileName = "example-1.xml";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
    assertEquals("ISO-8859-1", contentInfo.getEncoding());
    fileName = "example-2.xml";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // upload file, rename and then update file
    fileName = "quick.pdf";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String docId = document.getId();
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // rename (mimeType remains unchanged, binary has not changed)
    Document dUpdate = new Document();
    dUpdate.setName("quick.docx");
    response = put(URL_NODES, docId, toJsonAsStringNonNull(dUpdate), null, 200);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    fileName = "quick.docx";
    file = getResourceFile(fileName);
    BinaryPayload payload = new BinaryPayload(file);
    response = putBinary(getNodeContentUrl(docId), payload, null, null, 200);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_OPENXML_WORDPROCESSING, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // additional test
    fileName = "CMIS-Delete.json";
    file = getResourceFile(fileName);
    multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData("special-" + GUID.generate(), file));
    reqBody = multiPartBuilder.build();
    response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    contentInfo = document.getContent();
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("UTF-8", contentInfo.getEncoding());
    // cleanup
    deleteNode(fId);
}
Also used : MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) BinaryPayload(org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) Document(org.alfresco.rest.api.tests.client.data.Document) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 55 with Document

use of org.alfresco.rest.api.tests.client.data.Document in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testUpdatePermissionInvalidAuthority.

/**
 * Test attempt to set permission with an invalid authority
 *
 * @throws Exception
 */
private void testUpdatePermissionInvalidAuthority() throws Exception {
    // create folder containing an empty document
    String postUrl = createFolder();
    String dId = createDocument(postUrl);
    // update permissions
    Document dUpdate = new Document();
    NodePermissions nodePermissions = new NodePermissions();
    List<NodePermissions.NodePermission> locallySetPermissions = new ArrayList<>();
    locallySetPermissions.add(new NodePermissions.NodePermission("NonExistingAuthority", PermissionService.CONSUMER, AccessStatus.DENIED.toString()));
    nodePermissions.setLocallySet(locallySetPermissions);
    dUpdate.setPermissions(nodePermissions);
    // "Cannot set permissions on this node - unknown authority:
    // NonExistingAuthority"
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 400);
}
Also used : NodePermissions(org.alfresco.rest.api.model.NodePermissions) ArrayList(java.util.ArrayList) Document(org.alfresco.rest.api.tests.client.data.Document)

Aggregations

Document (org.alfresco.rest.api.tests.client.data.Document)60 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)49 Test (org.junit.Test)46 HashMap (java.util.HashMap)34 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)30 Folder (org.alfresco.rest.api.tests.client.data.Folder)25 LinkedHashMap (java.util.LinkedHashMap)22 File (java.io.File)20 ArrayList (java.util.ArrayList)20 Node (org.alfresco.rest.api.tests.client.data.Node)19 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)18 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)15 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)14 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)13 NodePermissions (org.alfresco.rest.api.model.NodePermissions)12 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)11 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)11 Rendition (org.alfresco.rest.api.tests.client.data.Rendition)10 JSONObject (org.json.simple.JSONObject)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6