Search in sources :

Example 46 with Document

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

the class ActivitiesPostingTest method testCreateUpdate.

/**
 * Tests the main activites, added, updated, deleted, downloaded
 */
@Test
public void testCreateUpdate() throws Exception {
    setRequestContext(user1);
    List<Activity> activities = getMyActivities();
    int beforeCount = activities.size();
    String folder1 = "folder" + System.currentTimeMillis() + "_1";
    Folder createdFolder = createFolder(tDocLibNodeId, folder1, null);
    assertNotNull(createdFolder);
    String f1Id = createdFolder.getId();
    String docName = "d1.txt";
    Document documentResp = createEmptyTextFile(f1Id, docName);
    // Update the file
    Document dUpdate = new Document();
    dUpdate.setName("d1b.txt");
    put(URL_NODES, documentResp.getId(), toJsonAsStringNonNull(dUpdate), null, 200);
    // Now download it
    HttpResponse response = getSingle(NodesEntityResource.class, documentResp.getId() + "/content", null, 200);
    String textContent = response.getResponse();
    assertNotNull(textContent);
    deleteNode(documentResp.getId());
    deleteNode(createdFolder.getId());
    activities = getMyActivities();
    assertEquals(beforeCount + 6, activities.size());
    Activity act = matchActivity(activities, ActivityType.FOLDER_ADDED, user1, tSiteId, tDocLibNodeId, folder1);
    assertNotNull(act);
    act = matchActivity(activities, ActivityType.FILE_ADDED, user1, tSiteId, createdFolder.getId(), docName);
    assertNotNull(act);
    act = matchActivity(activities, ActivityType.FILE_UPDATED, user1, tSiteId, createdFolder.getId(), dUpdate.getName());
    assertNotNull(act);
    act = matchActivity(activities, ActivityType.FOLDER_DELETED, user1, tSiteId, tDocLibNodeId, folder1);
    assertNotNull(act);
    act = matchActivity(activities, ActivityType.FILE_DELETED, user1, tSiteId, createdFolder.getId(), dUpdate.getName());
    assertNotNull(act);
    act = matchActivity(activities, ActivityPoster.DOWNLOADED, user1, tSiteId, createdFolder.getId(), dUpdate.getName());
    assertNotNull(act);
}
Also used : Activity(org.alfresco.rest.api.tests.client.data.Activity) 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) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 47 with Document

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

the class NodeApiTest method testUpdatePermissionsPermissionDeniedUser.

/**
 * Tests updating permissions on a node that user doesn't have permission for
 *
 * @throws Exception
 */
private void testUpdatePermissionsPermissionDeniedUser() throws Exception {
    // create folder with 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(groupA, PermissionService.CONSUMER, AccessStatus.DENIED.toString()));
    nodePermissions.setLocallySet(locallySetPermissions);
    dUpdate.setPermissions(nodePermissions);
    setRequestContext(user2);
    // "Permission Denied" expected
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 403);
}
Also used : NodePermissions(org.alfresco.rest.api.model.NodePermissions) ArrayList(java.util.ArrayList) Document(org.alfresco.rest.api.tests.client.data.Document)

Example 48 with Document

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

the class NodeApiTest method testDownloadFileContent.

/**
 * Tests download of file/content.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
 */
@Test
public void testDownloadFileContent() throws Exception {
    setRequestContext(user1);
    // 
    // Test plain text
    // 
    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();
    // Check the upload response
    assertEquals(fileName, document.getName());
    ContentInfo contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    // Download text content - by default with Content-Disposition header
    response = getSingle(NodesEntityResource.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(getNodeContentUrl(contentNodeId), null, null, headers, 304);
    // Update the content to change the node's modified date
    Document docUpdate = new Document();
    docUpdate.setProperties(Collections.singletonMap("cm:description", (Object) "desc updated!"));
    // Wait a second then update, as the dates will be rounded to
    // ignore millisecond when checking for If-Modified-Since
    Thread.sleep(1000L);
    response = put(URL_NODES, contentNodeId, toJsonAsStringNonNull(docUpdate), null, 200);
    Document updatedDocument = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(contentNodeId, updatedDocument.getId());
    // The requested "If-Modified-Since" date is older than node's modified date
    response = getSingle(getNodeContentUrl(contentNodeId), null, null, headers, 200);
    responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    assertNotNull(responseHeaders.get("Cache-Control"));
    assertNotNull(responseHeaders.get("Expires"));
    String newLastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
    assertNotNull(newLastModifiedHeader);
    assertNotEquals(lastModifiedHeader, newLastModifiedHeader);
    // 
    // Test binary (eg. PDF)
    // 
    fileName = "quick.pdf";
    file = getResourceFile(fileName);
    byte[] originalBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
    multiPartBuilder = MultiPartBuilder.create().setFileData(new 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);
    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(NodesEntityResource.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(getNodeContentUrl(contentNodeId), null, null, headers, 304);
}
Also used : 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) LinkedHashMap(java.util.LinkedHashMap) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) JSONObject(org.json.simple.JSONObject) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 49 with Document

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

the class NodeApiTest method testCopy.

/**
 * Tests copy (file or folder)
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/copy}
 */
@Test
public void testCopy() throws Exception {
    setRequestContext(user1);
    // create folder
    Folder folderResp = createFolder(Nodes.PATH_MY, "fsource");
    String sourceId = folderResp.getId();
    // create folder
    folderResp = createFolder(Nodes.PATH_MY, "ftarget");
    String targetId = folderResp.getId();
    // create doc d1
    String d1Name = "content" + RUNID + "_1";
    String d1Id = createTextFile(sourceId, d1Name, "The quick brown fox jumps over the lazy dog 1.").getId();
    // create doc d2
    String d2Name = "content" + RUNID + "_2";
    String d2Id = createTextFile(sourceId, d2Name, "The quick brown fox jumps over the lazy dog 2.").getId();
    Map<String, String> body = new HashMap<>();
    body.put("targetParentId", targetId);
    HttpResponse response = post(URL_NODES, d1Id, "copy", toJsonAsStringNonNull(body).getBytes(), null, null, 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1Name, documentResp.getName());
    assertEquals(targetId, documentResp.getParentId());
    // copy file (with rename)
    String newD2Name = d2Name + " updated !!";
    body = new HashMap<>();
    body.put("targetParentId", targetId);
    body.put("name", newD2Name);
    response = post(URL_NODES, d2Id, "copy", toJsonAsStringNonNull(body).getBytes(), null, null, 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(newD2Name, documentResp.getName());
    assertEquals(targetId, documentResp.getParentId());
    // -ve tests
    // missing target
    NodeTarget tgt = new NodeTarget();
    tgt.setName("new name");
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 400);
    // name already exists - different parent
    tgt = new NodeTarget();
    tgt.setName(newD2Name);
    tgt.setTargetParentId(targetId);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 409);
    // name already exists - same parent
    tgt = new NodeTarget();
    tgt.setTargetParentId(sourceId);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 409);
    // unknown source nodeId
    tgt = new NodeTarget();
    tgt.setTargetParentId(targetId);
    post("nodes/" + UUID.randomUUID().toString() + "/copy", toJsonAsStringNonNull(tgt), null, 404);
    // unknown target nodeId
    tgt = new NodeTarget();
    tgt.setTargetParentId(UUID.randomUUID().toString());
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 404);
    // target is not a folder
    tgt = new NodeTarget();
    tgt.setTargetParentId(d2Id);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 400);
    String rootNodeId = getRootNodeId();
    // no (write/create) permissions to copy to target
    tgt = new NodeTarget();
    tgt.setTargetParentId(rootNodeId);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 403);
    // -ve test - cannot copy to multiple destinations in single POST call (unsupported)
    List<NodeTarget> nodeDestinations = new ArrayList<>(2);
    NodeTarget nodeTarget = new NodeTarget();
    nodeTarget.setTargetParentId(sourceId);
    nodeDestinations.add(nodeTarget);
    nodeTarget = new NodeTarget();
    nodeTarget.setTargetParentId(targetId);
    nodeDestinations.add(nodeTarget);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(nodeDestinations), null, 405);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) NodeTarget(org.alfresco.rest.api.model.NodeTarget) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 50 with Document

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

the class NodeApiTest method testUpdatePermissionMissingFields.

/**
 * Tests updating permissions on a node without providing mandatory
 * properties
 *
 * @throws Exception
 */
private void testUpdatePermissionMissingFields() throws Exception {
    // create folder with an empty document
    String postUrl = createFolder();
    String dId = createDocument(postUrl);
    // update permissions
    Document dUpdate = new Document();
    // Add same permission with different access status
    NodePermissions nodePermissions = new NodePermissions();
    List<NodePermissions.NodePermission> locallySetPermissions = new ArrayList<>();
    locallySetPermissions.add(new NodePermissions.NodePermission(null, PermissionService.CONSUMER, AccessStatus.ALLOWED.toString()));
    nodePermissions.setLocallySet(locallySetPermissions);
    dUpdate.setPermissions(nodePermissions);
    // "Authority Id is expected."
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 400);
    locallySetPermissions.clear();
    locallySetPermissions.add(new NodePermissions.NodePermission("", PermissionService.CONSUMER, AccessStatus.ALLOWED.toString()));
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 400);
    locallySetPermissions.clear();
    locallySetPermissions.add(new NodePermissions.NodePermission(groupA, null, AccessStatus.ALLOWED.toString()));
    // "Permission name is expected."
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 400);
    locallySetPermissions.clear();
    locallySetPermissions.add(new NodePermissions.NodePermission(groupA, "", AccessStatus.ALLOWED.toString()));
    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