Search in sources :

Example 31 with Document

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

the class DeletedNodesTest method testListRenditions.

/**
 * Test retrieve renditions for deleted nodes
 * <p>post:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/renditions}
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/rendition/<renditionId>}
 */
@Test
public void testListRenditions() throws Exception {
    setRequestContext(user1);
    Date now = new Date();
    String folder1 = "folder" + now.getTime() + "_1";
    Folder createdFolder = createFolder(tDocLibNodeId, folder1, null);
    assertNotNull(createdFolder);
    String f1Id = createdFolder.getId();
    // Create multipart request
    String fileName = "quick.pdf";
    File file = getResourceFile(fileName);
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName, file));
    MultiPartBuilder.MultiPartRequest reqBody = multiPartBuilder.build();
    // Upload quick.pdf file into the folder previously created
    HttpResponse response = post(getNodeChildrenUrl(f1Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String contentNodeId = document.getId();
    // create doclib rendition and move node to trashcan
    createAndGetRendition(contentNodeId, "doclib");
    deleteNode(contentNodeId);
    // List all renditions and check for results
    PublicApiClient.Paging paging = getPaging(0, 50);
    response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, 200);
    List<Rendition> renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 3);
    // +ve test - get previously created 'doclib' rendition
    response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib", 200);
    Rendition doclibRendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
    assertNotNull(doclibRendition);
    assertEquals(Rendition.RenditionStatus.CREATED, doclibRendition.getStatus());
    ContentInfo contentInfo = doclibRendition.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
    assertEquals("PNG Image", contentInfo.getMimeTypeName());
    assertNotNull(contentInfo.getEncoding());
    assertTrue(contentInfo.getSizeInBytes() > 0);
    // +ve test - Add a filter on rendition 'status' and list only 'NOT_CREATED' renditions
    Map<String, String> params = new HashMap<>(1);
    params.put("where", "(status='NOT_CREATED')");
    response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 2);
    // +ve test - Add a filter on rendition 'status' and list only the CREATED renditions
    params.put("where", "(status='CREATED')");
    response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals("Only 'doclib' rendition should be returned.", 1, renditions.size());
    // SkipCount=0,MaxItems=2
    paging = getPaging(0, 2);
    // List all available renditions
    response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals(2, renditions.size());
    PublicApiClient.ExpectedPaging expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
    assertEquals(2, expectedPaging.getCount().intValue());
    assertEquals(0, expectedPaging.getSkipCount().intValue());
    assertEquals(2, expectedPaging.getMaxItems().intValue());
    assertTrue(expectedPaging.getTotalItems() >= 3);
    assertTrue(expectedPaging.getHasMoreItems());
    // SkipCount=1,MaxItems=3
    paging = getPaging(1, 3);
    // List all available renditions
    response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals(3, renditions.size());
    expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
    assertEquals(3, expectedPaging.getCount().intValue());
    assertEquals(1, expectedPaging.getSkipCount().intValue());
    assertEquals(3, expectedPaging.getMaxItems().intValue());
    assertTrue(expectedPaging.getTotalItems() >= 3);
    // +ve test - Test returned renditions are ordered (natural sort order)
    response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(Ordering.natural().isOrdered(renditions));
    // Check again to make sure the ordering wasn't coincidental
    response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(Ordering.natural().isOrdered(renditions));
    // -ve - nodeId in the path parameter does not exist
    getAll(getDeletedNodeRenditionsUrl(UUID.randomUUID().toString()), paging, params, 404);
    // -ve test - Create an empty text file
    Document emptyDoc = createEmptyTextFile(f1Id, "d1.txt");
    getAll(getDeletedNodeRenditionsUrl(emptyDoc.getId()), paging, params, 404);
    // -ve - nodeId in the path parameter does not represent a file
    deleteNode(f1Id);
    getAll(getDeletedNodeRenditionsUrl(f1Id), paging, params, 400);
    // -ve - Invalid status value
    params.put("where", "(status='WRONG')");
    getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 400);
    // -ve - Invalid filter (only 'status' is supported)
    params.put("where", "(id='doclib')");
    getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 400);
    // -ve test - Authentication failed
    setRequestContext(null);
    getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 401);
    // -ve - Current user does not have permission for nodeId
    setRequestContext(user2);
    getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 403);
    // Test get single node rendition
    setRequestContext(user1);
    // -ve - nodeId in the path parameter does not exist
    getSingle(getDeletedNodeRenditionsUrl(UUID.randomUUID().toString()), "doclib", 404);
    // -ve - renditionId in the path parameter is not registered/available
    getSingle(getNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis()), 404);
    // -ve - nodeId in the path parameter does not represent a file
    getSingle(getDeletedNodeRenditionsUrl(f1Id), "doclib", 400);
    // -ve test - Authentication failed
    setRequestContext(null);
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib", 401);
    // -ve - Current user does not have permission for nodeId
    setRequestContext(user2);
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib", 403);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) Date(java.util.Date) 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) File(java.io.File) Test(org.junit.Test)

Example 32 with Document

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

the class DeletedNodesTest method testDownloadFileContent.

/**
 * Tests download of file/content.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/content}
 */
@Test
public void testDownloadFileContent() throws Exception {
    setRequestContext(user1);
    // Use existing test file
    String fileName = "quick-1.txt";
    File file = getResourceFile(fileName);
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName, file));
    MultiPartBuilder.MultiPartRequest reqBody = multiPartBuilder.build();
    // Upload text content
    HttpResponse response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String contentNodeId = document.getId();
    // Check the upload response
    assertEquals(fileName, document.getName());
    ContentInfo contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    // move the node to Trashcan
    deleteNode(document.getId());
    // Download text content - by default with Content-Disposition header
    response = getSingle(TrashcanEntityResource.class, contentNodeId + "/content", null, 200);
    String textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
    Map<String, String> responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    assertEquals("attachment; filename=\"quick-1.txt\"; filename*=UTF-8''quick-1.txt", responseHeaders.get("Content-Disposition"));
    String cacheControl = responseHeaders.get("Cache-Control");
    assertNotNull(cacheControl);
    assertTrue(cacheControl.contains("must-revalidate"));
    assertTrue(cacheControl.contains("max-age=0"));
    assertNotNull(responseHeaders.get("Expires"));
    String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
    assertNotNull(lastModifiedHeader);
    Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
    // Test 304 response
    getSingle(URL_DELETED_NODES + "/" + contentNodeId + "/content", null, null, headers, 304);
    // Use existing pdf test file
    fileName = "quick.pdf";
    file = getResourceFile(fileName);
    byte[] originalBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
    multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName, file));
    reqBody = multiPartBuilder.build();
    // Upload binary content
    response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // move the node to Trashcan
    deleteNode(document.getId());
    contentNodeId = document.getId();
    // Check the upload response
    assertEquals(fileName, document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    // Download binary content (as bytes) - without Content-Disposition
    // header (attachment=false)
    Map<String, String> params = new LinkedHashMap<>();
    params.put("attachment", "false");
    response = getSingle(TrashcanEntityResource.class, contentNodeId + "/content", params, 200);
    byte[] bytes = response.getResponseAsBytes();
    assertArrayEquals(originalBytes, bytes);
    responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    assertNull(responseHeaders.get("Content-Disposition"));
    assertNotNull(responseHeaders.get("Cache-Control"));
    assertNotNull(responseHeaders.get("Expires"));
    lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
    assertNotNull(lastModifiedHeader);
    headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
    // Test 304 response
    getSingle(URL_DELETED_NODES + "/" + contentNodeId + "/content", null, null, headers, 304);
    // -ve - nodeId in the path parameter does not exist
    getSingle(TrashcanEntityResource.class, UUID.randomUUID().toString() + "/content", params, 404);
    // -ve test - Authentication failed
    setRequestContext(null);
    getSingle(TrashcanEntityResource.class, contentNodeId + "/content", params, 401);
    // -ve - Current user does not have permission for nodeId
    setRequestContext(user2);
    getSingle(TrashcanEntityResource.class, contentNodeId + "/content", params, 403);
}
Also used : HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) TrashcanEntityResource(org.alfresco.rest.api.trashcan.TrashcanEntityResource) Document(org.alfresco.rest.api.tests.client.data.Document) LinkedHashMap(java.util.LinkedHashMap) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) File(java.io.File) Test(org.junit.Test)

Example 33 with Document

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

the class DeletedNodesTest method testDownloadRendition.

/**
 * Tests download rendition.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/deleted-nodes/<nodeId>/renditions/<renditionId>/content}
 */
@Test
public void testDownloadRendition() throws Exception {
    setRequestContext(user1);
    // Create a folder within the site document's library
    Date now = new Date();
    String folder1 = "folder" + now.getTime() + "_1";
    Folder createdFolder = createFolder(tDocLibNodeId, folder1, null);
    assertNotNull(createdFolder);
    String f1Id = createdFolder.getId();
    // Create multipart request using an existing file
    String fileName = "quick.pdf";
    File file = getResourceFile(fileName);
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName, file));
    MultiPartBuilder.MultiPartRequest reqBody = multiPartBuilder.build();
    // Upload quick.pdf file into 'folder'
    HttpResponse response = post(getNodeChildrenUrl(f1Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String contentNodeId = document.getId();
    Rendition rendition = createAndGetRendition(contentNodeId, "doclib");
    assertNotNull(rendition);
    assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
    deleteNode(contentNodeId);
    // Download rendition - by default with Content-Disposition header
    response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", 200);
    assertNotNull(response.getResponseAsBytes());
    Map<String, String> responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    String contentDisposition = responseHeaders.get("Content-Disposition");
    assertNotNull(contentDisposition);
    assertTrue(contentDisposition.contains("filename=\"doclib\""));
    String contentType = responseHeaders.get("Content-Type");
    assertNotNull(contentType);
    assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
    // Download rendition - without Content-Disposition header
    // (attachment=false)
    Map<String, String> params = new HashMap<>();
    params = Collections.singletonMap("attachment", "false");
    response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", params, 200);
    assertNotNull(response.getResponseAsBytes());
    responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    assertNull(responseHeaders.get("Content-Disposition"));
    contentType = responseHeaders.get("Content-Type");
    assertNotNull(contentType);
    assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
    // Download rendition - with Content-Disposition header
    // (attachment=true) same as default
    params = Collections.singletonMap("attachment", "true");
    response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", params, 200);
    assertNotNull(response.getResponseAsBytes());
    responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    String cacheControl = responseHeaders.get("Cache-Control");
    assertNotNull(cacheControl);
    assertFalse(cacheControl.contains("must-revalidate"));
    assertTrue(cacheControl.contains("max-age=31536000"));
    contentDisposition = responseHeaders.get("Content-Disposition");
    assertNotNull(contentDisposition);
    assertTrue(contentDisposition.contains("filename=\"doclib\""));
    contentType = responseHeaders.get("Content-Type");
    assertNotNull(contentType);
    assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
    // Test 304 response - doclib rendition (attachment=true)
    String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
    assertNotNull(lastModifiedHeader);
    Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", params, headers, 304);
    // -ve tests
    // nodeId in the path parameter does not represent a file
    deleteNode(f1Id);
    getSingle(getDeletedNodeRenditionsUrl(f1Id), "doclib/content", 400);
    // nodeId in the path parameter does not exist
    getSingle(getDeletedNodeRenditionsUrl(UUID.randomUUID().toString()), "doclib/content", 404);
    // renditionId in the path parameter is not registered/available
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis() + "/content"), 404);
    // The rendition does not exist, a placeholder is not available and the
    // placeholder parameter has a value of "true"
    params = Collections.singletonMap("placeholder", "true");
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis() + "/content"), params, 404);
}
Also used : MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) File(java.io.File) Date(java.util.Date) Test(org.junit.Test)

Example 34 with Document

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

the class DeletedNodesTest method testCreateAndRestore.

/**
 * Tests restoring deleted nodes
 * <p>post:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/restore}
 */
@Test
public void testCreateAndRestore() throws Exception {
    setRequestContext(user1);
    Date now = new Date();
    String folder1 = "folder" + now.getTime() + "_1";
    Folder createdFolder = createFolder(tDocLibNodeId, folder1, null);
    assertNotNull(createdFolder);
    String f1Id = createdFolder.getId();
    // Create a folder outside a site
    Folder createdFolderNonSite = createFolder(Nodes.PATH_MY, folder1, null);
    assertNotNull(createdFolderNonSite);
    Document document = createEmptyTextFile(f1Id, "restoreme.txt");
    deleteNode(document.getId());
    // Create another document with the same name
    Document documentSameName = createEmptyTextFile(f1Id, "restoreme.txt");
    // Can't restore a node of the same name
    post(URL_DELETED_NODES + "/" + document.getId() + "/restore", null, null, Status.STATUS_CONFLICT);
    deleteNode(documentSameName.getId());
    // Now we can restore it.
    post(URL_DELETED_NODES + "/" + document.getId() + "/restore", null, null, 200);
    deleteNode(document.getId());
    // Create a new nodeTargetAssoc containing target id and association type
    NodeTargetAssoc nodeTargetAssoc = new NodeTargetAssoc();
    nodeTargetAssoc.setTargetParentId(f1Id);
    nodeTargetAssoc.setAssocType(ASSOC_TYPE_CM_CONTAINS);
    // restore to new location
    post(URL_DELETED_NODES + "/" + document.getId() + "/restore", toJsonAsStringNonNull(nodeTargetAssoc), null, 200);
    deleteNode(document.getId());
    // restore to nonexistent nodeId as the new location
    nodeTargetAssoc.setTargetParentId("nonexistentTargetNode");
    post(URL_DELETED_NODES + "/" + document.getId() + "/restore", toJsonAsStringNonNull(nodeTargetAssoc), null, 404);
    // restore to new location and using an invalid assocType
    nodeTargetAssoc.setTargetParentId(f1Id);
    nodeTargetAssoc.setAssocType("invalidAssociationType");
    post(URL_DELETED_NODES + "/" + document.getId() + "/restore", toJsonAsStringNonNull(nodeTargetAssoc), null, 400);
    // restore to new location without adding an association type
    nodeTargetAssoc.setTargetParentId(f1Id);
    nodeTargetAssoc.setAssocType(null);
    post(URL_DELETED_NODES + "/" + document.getId() + "/restore", toJsonAsStringNonNull(nodeTargetAssoc), null, 400);
    // create an folder as an admin
    setRequestContext(networkAdmin);
    String folderAdmin = "adminsFolder" + now.getTime() + "_1";
    Folder adminCreatedFolder = createFolder(Nodes.PATH_MY, folderAdmin, null);
    assertNotNull(adminCreatedFolder);
    String adminf1Id = adminCreatedFolder.getId();
    // switch context, re-delete the document and try to restore it to a
    // folder user1 does not have permissions to
    setRequestContext(user1);
    nodeTargetAssoc.setTargetParentId(adminf1Id);
    nodeTargetAssoc.setAssocType(ASSOC_TYPE_CM_CONTAINS);
    post(URL_DELETED_NODES + "/" + document.getId() + "/restore", toJsonAsStringNonNull(nodeTargetAssoc), null, 403);
    deleteNode(createdFolder.getId());
    // We deleted the parent folder so lets see if we can restore a child
    // doc, hopefully not.
    post(URL_DELETED_NODES + "/" + documentSameName.getId() + "/restore", null, null, Status.STATUS_NOT_FOUND);
    // Can't delete "nonsense" noderef
    post("deleted-nodes/nonsense/restore", null, null, Status.STATUS_NOT_FOUND);
    // User 2 can't restore it but user 1 can.
    setRequestContext(user2);
    post(URL_DELETED_NODES + "/" + createdFolder.getId() + "/restore", null, null, Status.STATUS_FORBIDDEN);
    setRequestContext(user1);
    post(URL_DELETED_NODES + "/" + createdFolder.getId() + "/restore", null, null, 200);
}
Also used : Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) Date(java.util.Date) NodeTargetAssoc(org.alfresco.rest.api.model.NodeTargetAssoc) Test(org.junit.Test)

Example 35 with Document

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

the class NodeApiTest method testDownloadFileContentReadPermission.

/**
 * Tests download of file/content - basic read permission
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
 */
@Test
public void testDownloadFileContentReadPermission() throws Exception {
    setRequestContext(user1);
    String fileName = "quick-1.txt";
    File file = getResourceFile(fileName);
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    MultiPartRequest reqBody = multiPartBuilder.build();
    // Upload text content
    HttpResponse response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String contentNodeId = document.getId();
    // Download text content
    response = getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 200);
    String textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
    // Also test versions endpoint (1.0 in this case)
    response = getSingle(NodesEntityResource.class, contentNodeId + "/versions/1.0/content", null, 200);
    textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
    // -ve test: user2 does not have read permission
    setRequestContext(user2);
    getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 403);
    getSingle(NodesEntityResource.class, contentNodeId + "/versions/1.0/content", null, 403);
    // add Consumer (~ Read) permission
    setRequestContext(user1);
    Document dUpdate = new Document();
    NodePermissions nodePermissions = new NodePermissions();
    List<NodePermissions.NodePermission> locallySetPermissions = new ArrayList<>();
    locallySetPermissions.add(new NodePermissions.NodePermission(user2, PermissionService.CONSUMER, AccessStatus.ALLOWED.toString()));
    nodePermissions.setLocallySet(locallySetPermissions);
    dUpdate.setPermissions(nodePermissions);
    // update node
    response = put(URL_NODES, contentNodeId, toJsonAsStringNonNull(dUpdate), null, 200);
    setRequestContext(user2);
    // Download text content
    response = getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 200);
    textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
    // Also test versions endpoint (1.0 in this case)
    response = getSingle(NodesEntityResource.class, contentNodeId + "/versions/1.0/content", null, 200);
    textContent = response.getResponse();
    assertEquals("The quick brown fox jumps over the lazy dog", textContent);
}
Also used : NodePermissions(org.alfresco.rest.api.model.NodePermissions) ArrayList(java.util.ArrayList) 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) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

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