Search in sources :

Example 6 with UserInfo

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

the class NodeApiTest method testListChildrenWithinMyFiles.

/**
 * Tests list children.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 */
@Test
public void testListChildrenWithinMyFiles() throws Exception {
    setRequestContext(user1);
    String myNodeId = getMyNodeId();
    String rootChildrenUrl = getNodeChildrenUrl(Nodes.PATH_ROOT);
    String folder0Name = "folder " + RUNID + " 0";
    String folder0Id = createFolder(myNodeId, folder0Name, null).getId();
    String childrenUrl = getNodeChildrenUrl(folder0Id);
    Map<String, Object> props = new HashMap<>(1);
    props.put("cm:title", "This is folder 1");
    String folder1 = "folder " + RUNID + " 1";
    String folder1_Id = createFolder(folder0Id, folder1, props).getId();
    String contentF1 = "content" + RUNID + " in folder 1";
    String contentF1_Id = createTextFile(folder1_Id, contentF1, "The quick brown fox jumps over the lazy dog 1.").getId();
    props = new HashMap<>(1);
    props.put("cm:title", "This is folder 2");
    String folder2 = "folder " + RUNID + " 2";
    String folder2_Id = createFolder(folder0Id, folder2, props).getId();
    String contentF2 = "content" + RUNID + " in folder 2.txt";
    String contentF2_Id = createTextFile(folder2_Id, contentF2, "The quick brown fox jumps over the lazy dog 2.").getId();
    String content1 = "content" + RUNID + " 1.txt";
    String content1_Id = createTextFile(folder0Id, content1, "The quick brown fox jumps over the lazy dog.").getId();
    props = new HashMap<>();
    props.put(PROP_OWNER, user1);
    props.put("cm:lastThumbnailModification", Collections.singletonList("doclib:1444660852296"));
    Node nodeUpdate = new Node();
    nodeUpdate.setProperties(props);
    put(URL_NODES, content1_Id, toJsonAsStringNonNull(nodeUpdate), null, 200);
    List<String> folderIds = Arrays.asList(folder1_Id, folder2_Id);
    List<String> contentIds = Arrays.asList(content1_Id);
    Paging paging = getPaging(0, 100);
    HttpResponse response = getAll(childrenUrl, paging, 200);
    List<Document> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    assertEquals(3, nodes.size());
    // Order by folders and modified date first
    Map<String, String> orderBy = Collections.singletonMap("orderBy", "isFolder DESC,modifiedAt DESC");
    response = getAll(childrenUrl, paging, orderBy, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    assertEquals(3, nodes.size());
    assertEquals(folder2, nodes.get(0).getName());
    assertEquals(folder1, nodes.get(1).getName());
    Document node = nodes.get(2);
    assertEquals(content1, node.getName());
    assertEquals(TYPE_CM_CONTENT, node.getNodeType());
    assertEquals(content1_Id, node.getId());
    UserInfo createdByUser = node.getCreatedByUser();
    assertEquals(user1, createdByUser.getId());
    assertEquals(UserInfo.getTestDisplayName(user1), createdByUser.getDisplayName());
    UserInfo modifiedByUser = node.getModifiedByUser();
    assertEquals(user1, modifiedByUser.getId());
    assertEquals(UserInfo.getTestDisplayName(user1), modifiedByUser.getDisplayName());
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, node.getContent().getMimeType());
    assertNotNull(node.getContent().getMimeTypeName());
    assertNotNull(node.getContent().getEncoding());
    assertTrue(node.getContent().getSizeInBytes() > 0);
    // request without "include"
    Map<String, String> params = new HashMap<>();
    response = getAll(childrenUrl, paging, params, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    for (Node n : nodes) {
        assertNull("There shouldn't be a 'properties' object in the response.", n.getProperties());
        assertNull("There shouldn't be a 'isLink' object in the response.", n.getIsLink());
        assertNull("There shouldn't be a 'path' object in the response.", n.getPath());
        assertNull("There shouldn't be a 'aspectNames' object in the response.", n.getAspectNames());
    }
    // request with include - example 1
    params = new HashMap<>();
    params.put("include", "isLink");
    response = getAll(childrenUrl, paging, params, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    for (Node n : nodes) {
        assertNotNull("There should be a 'isLink' object in the response.", n.getIsLink());
    }
    // request with include - example 2
    params = new HashMap<>();
    params.put("include", "aspectNames,properties,path,isLink");
    response = getAll(childrenUrl, paging, params, 200);
    nodes = jacksonUtil.parseEntries(response.getJsonResponse(), Document.class);
    for (Node n : nodes) {
        // eg. cm:title, see above
        assertNotNull("There should be a 'properties' object in the response.", n.getProperties());
        assertNotNull("There should be a 'isLink' object in the response.", n.getIsLink());
        assertNotNull("There should be a 'path' object in the response.", n.getPath());
        assertNotNull("There should be a 'aspectNames' object in the response.", n.getAspectNames());
    }
    // request specific property via include
    params = new HashMap<>();
    params.put("include", "cm:lastThumbnailModification");
    params.put("orderBy", "isFolder DESC,modifiedAt DESC");
    response = getAll(childrenUrl, paging, params, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    assertEquals(3, nodes.size());
    assertNull("There shouldn't be a 'properties' object in the response.", nodes.get(0).getProperties());
    assertNull("There shouldn't be a 'properties' object in the response.", nodes.get(1).getProperties());
    assertNotNull("There should be a 'properties' object in the response.", nodes.get(2).getProperties());
    Set<Entry<String, Object>> propsSet = nodes.get(2).getProperties().entrySet();
    assertEquals(1, propsSet.size());
    Entry<String, Object> entry = propsSet.iterator().next();
    assertEquals("cm:lastThumbnailModification", entry.getKey());
    assertEquals("doclib:1444660852296", ((List<?>) entry.getValue()).get(0));
    // filtering, via where clause - folders only
    params = new HashMap<>();
    params.put("where", "(" + Nodes.PARAM_ISFOLDER + "=true)");
    response = getAll(childrenUrl, paging, params, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    assertEquals(2, nodes.size());
    assertTrue(nodes.get(0).getIsFolder());
    assertFalse(nodes.get(0).getIsFile());
    assertTrue(folderIds.contains(nodes.get(0).getId()));
    assertTrue(nodes.get(1).getIsFolder());
    assertFalse(nodes.get(1).getIsFile());
    assertTrue(folderIds.contains(nodes.get(1).getId()));
    // filtering, via where clause - content only
    params = new HashMap<>();
    params.put("where", "(" + Nodes.PARAM_ISFILE + "=true)");
    response = getAll(childrenUrl, paging, params, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    assertEquals(1, nodes.size());
    assertFalse(nodes.get(0).getIsFolder());
    assertTrue(nodes.get(0).getIsFile());
    assertTrue(contentIds.contains(nodes.get(0).getId()));
    // list children via relativePath
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, folder1);
    response = getAll(childrenUrl, paging, params, 200);
    JSONObject jsonResponse = response.getJsonResponse();
    nodes = RestApiUtil.parseRestApiEntries(jsonResponse, Document.class);
    assertEquals(1, nodes.size());
    assertEquals(contentF1_Id, nodes.get(0).getId());
    JSONObject jsonList = (JSONObject) jsonResponse.get("list");
    assertNotNull(jsonList);
    JSONObject jsonSrcObj = (JSONObject) jsonResponse.get("source");
    assertNull(jsonSrcObj);
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "User Homes/" + user1 + "/" + folder0Name + "/" + folder2);
    response = getAll(rootChildrenUrl, paging, params, 200);
    jsonResponse = response.getJsonResponse();
    nodes = RestApiUtil.parseRestApiEntries(jsonResponse, Document.class);
    assertEquals(1, nodes.size());
    assertEquals(contentF2_Id, nodes.get(0).getId());
    jsonList = (JSONObject) jsonResponse.get("list");
    assertNotNull(jsonList);
    jsonSrcObj = (JSONObject) jsonResponse.get("source");
    assertNull(jsonSrcObj);
    // list children via relativePath and also return the source entity
    params = new HashMap<>();
    params.put(Nodes.PARAM_RELATIVE_PATH, "User Homes/" + user1 + "/" + folder0Name + "/" + folder2);
    params.put("includeSource", "true");
    params.put("include", "path,isLink");
    params.put("fields", "id");
    response = getAll(rootChildrenUrl, paging, params, 200);
    jsonResponse = response.getJsonResponse();
    nodes = RestApiUtil.parseRestApiEntries(jsonResponse, Document.class);
    assertEquals(1, nodes.size());
    Document doc = nodes.get(0);
    assertEquals(contentF2_Id, doc.getId());
    assertNotNull(doc.getPath());
    assertEquals(Boolean.FALSE, doc.getIsLink());
    assertNull(doc.getName());
    jsonList = (JSONObject) jsonResponse.get("list");
    assertNotNull(jsonList);
    // source is not affected by include (or fields for that matter) - returns the default node response
    Folder src = RestApiUtil.parsePojo("source", jsonList, Folder.class);
    assertEquals(folder2_Id, src.getId());
    assertNull(src.getPath());
    assertNull(src.getIsLink());
    assertNotNull(src.getName());
    assertNotNull(src.getAspectNames());
    assertNotNull(src.getProperties());
    // -ve test - Invalid QName (Namespace prefix cm... is not mapped to a namespace URI) for the orderBy parameter.
    params = Collections.singletonMap("orderBy", Nodes.PARAM_ISFOLDER + " DESC,cm" + System.currentTimeMillis() + ":modified DESC");
    getAll(childrenUrl, paging, params, 400);
    paging = getPaging(0, 10);
    // -ve test - list folder children for unknown node should return 404
    getAll(getNodeChildrenUrl(UUID.randomUUID().toString()), paging, 404);
    // -ve test - user2 tries to access user1's home folder
    setRequestContext(user2);
    getAll(getNodeChildrenUrl(myNodeId), paging, 403);
    setRequestContext(user1);
    // -ve test - try to list children using relative path to unknown node
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "User Homes/" + user1 + "/unknown");
    getAll(rootChildrenUrl, paging, params, 404);
    // -ve test - try to list children using relative path to node for which user does not have read permission (expect 404 instead of 403)
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "User Homes/" + user2);
    getAll(rootChildrenUrl, paging, params, 404);
    // -ve test - list folder children with relative path to unknown node should return 400
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "/unknown");
    getAll(getNodeChildrenUrl(content1_Id), paging, params, 400);
    // filtering, via where clause - negated comparison
    params = new HashMap<>();
    params.put("where", "(NOT " + Nodes.PARAM_ISFILE + "=true)");
    getAll(childrenUrl, paging, params, 400);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.alfresco.rest.api.tests.client.data.Node) 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) UserInfo(org.alfresco.rest.api.tests.client.data.UserInfo) Document(org.alfresco.rest.api.tests.client.data.Document) Folder(org.alfresco.rest.api.tests.client.data.Folder) Entry(java.util.Map.Entry) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Aggregations

HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)6 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)6 Document (org.alfresco.rest.api.tests.client.data.Document)6 Folder (org.alfresco.rest.api.tests.client.data.Folder)6 Node (org.alfresco.rest.api.tests.client.data.Node)6 UserInfo (org.alfresco.rest.api.tests.client.data.UserInfo)6 JSONObject (org.json.simple.JSONObject)6 Test (org.junit.Test)6 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)4 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)3 ArrayList (java.util.ArrayList)2 ExpectedPaging (org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging)2 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)2 RestApiUtil.parsePaging (org.alfresco.rest.api.tests.util.RestApiUtil.parsePaging)2 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 MimetypeMap (org.alfresco.repo.content.MimetypeMap)1 PathInfo (org.alfresco.rest.api.tests.client.data.PathInfo)1