Search in sources :

Example 1 with UserInfo

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

the class NodeApiTest method testGetNodeInfo.

/**
 * Tests get node information.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>}
 */
@Test
public void testGetNodeInfo() throws Exception {
    setRequestContext(user1);
    HttpResponse response = getSingle(NodesEntityResource.class, Nodes.PATH_ROOT, null, 200);
    Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    String rootNodeId = node.getId();
    response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, null, 200);
    node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    String myFilesNodeId = node.getId();
    assertNotNull(myFilesNodeId);
    assertEquals(user1, node.getName());
    assertTrue(node.getIsFolder());
    assertFalse(node.getIsFile());
    String userHomesId = node.getParentId();
    // /Company Home/User Homes/user<timestamp>/folder<timestamp>_A
    String folderA = "folder" + RUNID + "_A";
    String folderA_Id = createFolder(myFilesNodeId, folderA).getId();
    // /Company Home/User Homes/user<timestamp>/folder<timestamp>_A/folder<timestamp>_B
    String folderB = "folder" + RUNID + "_B";
    String folderB_Id = createFolder(folderA_Id, folderB).getId();
    // /Company Home/User Homes/user<timestamp>/folder<timestamp>_A/folder<timestamp>_B/content<timestamp>
    String title = "test title";
    Map<String, String> docProps = new HashMap<>();
    docProps.put("cm:title", title);
    String contentName = "content " + RUNID + ".txt";
    String content1Id = createTextFile(folderB_Id, contentName, "The quick brown fox jumps over the lazy dog.", "UTF-8", docProps).getId();
    // get node info
    response = getSingle(NodesEntityResource.class, content1Id, null, 200);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String content_Id = documentResp.getId();
    // Expected result ...
    UserInfo expectedUser = new UserInfo(user1);
    Document d1 = new Document();
    d1.setId(content_Id);
    d1.setParentId(folderB_Id);
    d1.setName(contentName);
    d1.setNodeType(TYPE_CM_CONTENT);
    ContentInfo ciExpected = new ContentInfo();
    ciExpected.setMimeType("text/plain");
    ciExpected.setMimeTypeName("Plain Text");
    ciExpected.setSizeInBytes(44L);
    ciExpected.setEncoding("ISO-8859-1");
    d1.setContent(ciExpected);
    d1.setCreatedByUser(expectedUser);
    d1.setModifiedByUser(expectedUser);
    Map<String, Object> props = new HashMap<>();
    props.put("cm:title", title);
    props.put("cm:versionLabel", "1.0");
    props.put("cm:versionType", "MAJOR");
    d1.setProperties(props);
    d1.setAspectNames(Arrays.asList("cm:auditable", "cm:titled", "cm:versionable", "cm:author"));
    // Note: Path is not part of the default info
    d1.expected(documentResp);
    // get node info + path
    // ...nodes/nodeId?include=path
    Map<String, String> params = Collections.singletonMap("include", "path");
    response = getSingle(NodesEntityResource.class, content1Id, params, 200);
    documentResp = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
    // Expected path ...
    // note: the pathInfo should only include the parents (not the requested node)
    List<ElementInfo> elements = new ArrayList<>(5);
    elements.add(new ElementInfo(rootNodeId, "Company Home"));
    elements.add(new ElementInfo(userHomesId, "User Homes"));
    elements.add(new ElementInfo(myFilesNodeId, user1));
    elements.add(new ElementInfo(folderA_Id, folderA));
    elements.add(new ElementInfo(folderB_Id, folderB));
    PathInfo expectedPath = new PathInfo("/Company Home/User Homes/" + user1 + "/" + folderA + "/" + folderB, true, elements);
    d1.setPath(expectedPath);
    d1.expected(documentResp);
    // get node info via relativePath
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "/" + folderA + "/" + folderB);
    response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, params, 200);
    Folder folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderB_Id, folderResp.getId());
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, folderA + "/" + folderB + "/" + contentName);
    response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, params, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(content_Id, documentResp.getId());
    // test path with utf-8 encoded param (eg. ¢ => )
    String folderC = "folder" + RUNID + " ¢";
    String folderC_Id = createFolder(folderB_Id, folderC).getId();
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "/" + folderA + "/" + folderB + "/" + folderC);
    response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, params, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderC_Id, folderResp.getId());
    // -ve test - get info for unknown node should return 404
    getSingle(NodesEntityResource.class, UUID.randomUUID().toString(), null, 404);
    // -ve test - user2 tries to get node info about user1's home folder
    setRequestContext(user2);
    getSingle(NodesEntityResource.class, myFilesNodeId, null, 403);
    setRequestContext(user1);
    // -ve test - try to get node info using relative path to unknown node
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, folderA + "/unknown");
    getSingle(NodesEntityResource.class, Nodes.PATH_MY, params, 404);
    // -ve test - try to get node info 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);
    getSingle(NodesEntityResource.class, Nodes.PATH_ROOT, params, 404);
    // -ve test - attempt to get node info for non-folder node with relative path should return 400
    params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "/unknown");
    getSingle(NodesEntityResource.class, content_Id, params, 400);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ElementInfo(org.alfresco.rest.api.tests.client.data.PathInfo.ElementInfo) Node(org.alfresco.rest.api.tests.client.data.Node) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) UserInfo(org.alfresco.rest.api.tests.client.data.UserInfo) 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) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) JSONObject(org.json.simple.JSONObject) PathInfo(org.alfresco.rest.api.tests.client.data.PathInfo) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 2 with UserInfo

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

the class NodeApiTest method testUpdateNodeInfo.

/**
 * Tests update node info (file or folder)
 * <p>PUT:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>}
 */
@Test
public void testUpdateNodeInfo() throws Exception {
    setRequestContext(user1);
    // create folder f0
    String folder0Name = "f0-testUpdateNodeInfo-" + RUNID;
    String f0Id = createFolder(Nodes.PATH_MY, folder0Name).getId();
    UserInfo expectedUser = new UserInfo(user1);
    String postUrl = getNodeChildrenUrl(f0Id);
    String folderName = "My Folder";
    // create folder
    Folder folderResp = createFolder(f0Id, folderName);
    String fId = folderResp.getId();
    Folder f1 = new Folder();
    f1.setName(folderName);
    f1.setNodeType(TYPE_CM_FOLDER);
    f1.setIsFolder(true);
    f1.setParentId(f0Id);
    f1.setAspectNames(Collections.singletonList("cm:auditable"));
    f1.setCreatedByUser(expectedUser);
    f1.setModifiedByUser(expectedUser);
    f1.expected(folderResp);
    // create empty file
    Document d1 = new Document();
    d1.setName("d1.txt");
    d1.setNodeType(TYPE_CM_CONTENT);
    HttpResponse response = post(postUrl, toJsonAsStringNonNull(d1), 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String dId = 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);
    // update file - name (=> rename within current folder)
    Document dUpdate = new Document();
    dUpdate.setName("d1b.txt");
    response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    d1.setName("d1b.txt");
    d1.expected(documentResp);
    // update file - add some properties
    Map<String, Object> props = new HashMap<>();
    props.put("cm:title", "my file title");
    props.put("cm:description", "my file description");
    dUpdate = new Document();
    dUpdate.setProperties(props);
    response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    d1.setProperties(props);
    d1.setAspectNames(Arrays.asList("cm:auditable", "cm:titled"));
    d1.expected(documentResp);
    // update file - add versionable aspect
    dUpdate = new Document();
    dUpdate.setAspectNames(Arrays.asList("cm:auditable", "cm:titled", "cm:versionable"));
    response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    d1.getProperties().put("cm:versionLabel", "1.0");
    d1.getProperties().put("cm:versionType", "MAJOR");
    d1.setAspectNames(Arrays.asList("cm:auditable", "cm:titled", "cm:versionable"));
    d1.expected(documentResp);
    response = getSingle(URL_NODES, dId, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    d1.getProperties().put("cm:versionLabel", "1.0");
    d1.getProperties().put("cm:versionType", "MAJOR");
    d1.expected(documentResp);
    // update file - remove titled aspect (and it's related aspect properties)
    dUpdate = new Document();
    dUpdate.setAspectNames(Arrays.asList("cm:auditable", "cm:versionable"));
    response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    d1.getProperties().remove("cm:title");
    d1.getProperties().remove("cm:description");
    d1.setAspectNames(Arrays.asList("cm:auditable", "cm:versionable"));
    d1.expected(documentResp);
    // update folder - rename and add some properties
    props = new HashMap<>();
    props.put("cm:title", "my folder title");
    props.put("cm:description", "my folder description");
    folderName = "My Updated Folder";
    Folder fUpdate = new Folder();
    fUpdate.setProperties(props);
    fUpdate.setName(folderName);
    response = put(URL_NODES, fId, toJsonAsStringNonNull(fUpdate), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    f1.setName(folderName);
    f1.setAspectNames(Arrays.asList("cm:auditable", "cm:titled"));
    f1.setProperties(props);
    f1.expected(folderResp);
    // update folder - unset a property
    props = new HashMap<>();
    props.put("cm:title", null);
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    response = put(URL_NODES, fId, toJsonAsStringNonNull(fUpdate), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    f1.getProperties().remove("cm:title");
    f1.expected(folderResp);
    // update folder - specialise node type
    fUpdate = new Folder();
    fUpdate.setNodeType("app:glossary");
    response = put(URL_NODES, fId, toJsonAsStringNonNull(fUpdate), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    f1.setNodeType("app:glossary");
    f1.expected(folderResp);
    {
        // test versioning for metadata-only updates
        Map params = new HashMap<>();
        params.put("majorVersion", "true");
        params.put("comment", "Initial empty file :-)");
        String fileName = "My File";
        Node nodeResp = createEmptyTextFile(f0Id, fileName, params, 201);
        assertEquals("1.0", nodeResp.getProperties().get("cm:versionLabel"));
        props = new HashMap<>();
        props.put("cm:title", "my file title");
        dUpdate = new Document();
        dUpdate.setProperties(props);
        response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals("1.0", nodeResp.getProperties().get("cm:versionLabel"));
        // turn-off auto-version on metadata-only updates (OOTB this is now false by default, as per MNT-12226)
        props = new HashMap<>();
        props.put("cm:autoVersionOnUpdateProps", true);
        dUpdate = new Document();
        dUpdate.setProperties(props);
        response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals("1.1", nodeResp.getProperties().get("cm:versionLabel"));
        props = new HashMap<>();
        props.put("cm:title", "my file title 2");
        dUpdate = new Document();
        dUpdate.setProperties(props);
        response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals("1.2", nodeResp.getProperties().get("cm:versionLabel"));
        props = new HashMap<>();
        props.put("cm:title", "my file title 3");
        dUpdate = new Document();
        dUpdate.setProperties(props);
        response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals("1.3", nodeResp.getProperties().get("cm:versionLabel"));
        // turn-off auto-version on metadata-only updates
        props = new HashMap<>();
        props.put("cm:autoVersionOnUpdateProps", false);
        dUpdate = new Document();
        dUpdate.setProperties(props);
        response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals("1.3", nodeResp.getProperties().get("cm:versionLabel"));
        props = new HashMap<>();
        props.put("cm:title", "my file title 4");
        dUpdate = new Document();
        dUpdate.setProperties(props);
        response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals("1.3", nodeResp.getProperties().get("cm:versionLabel"));
    }
    // update folder(s) via well-known aliases rather than node id
    // note: as of now, the platform does allow a user to modify their home folder [this may change in the future, if so adjust the test accordingly]
    response = getSingle(URL_NODES, Nodes.PATH_MY, 200);
    Folder user1MyFolder = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    String user1MyFolderId = user1MyFolder.getId();
    String description = "my folder description " + RUNID;
    props = new HashMap<>();
    props.put("cm:description", description);
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    response = put(URL_NODES, Nodes.PATH_MY, toJsonAsStringNonNull(fUpdate), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(description, folderResp.getProperties().get("cm:description"));
    setRequestContext(networkAdmin);
    props = new HashMap<>();
    props.put("cm:description", description);
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    response = put(URL_NODES, Nodes.PATH_ROOT, toJsonAsStringNonNull(fUpdate), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(description, folderResp.getProperties().get("cm:description"));
    props = new HashMap<>();
    props.put("cm:description", description);
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    response = put(URL_NODES, Nodes.PATH_SHARED, toJsonAsStringNonNull(fUpdate), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(description, folderResp.getProperties().get("cm:description"));
    setRequestContext(user1);
    // -ve test - fail on unknown property
    props = new HashMap<>();
    props.put("cm:xyz", "my unknown property");
    dUpdate = new Document();
    dUpdate.setProperties(props);
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 400);
    // -ve test - fail on unknown aspect
    List<String> aspects = new ArrayList<>(d1.getAspectNames());
    aspects.add("cm:unknownAspect");
    dUpdate = new Document();
    dUpdate.setAspectNames(aspects);
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 400);
    // -ve test - duplicate name
    dUpdate = new Document();
    dUpdate.setName(folderName);
    put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 409);
    // -ve test - unknown node id
    dUpdate = new Document();
    dUpdate.setName("some.txt");
    put(URL_NODES, UUID.randomUUID().toString(), toJsonAsStringNonNull(dUpdate), null, 404);
    // -ve test - generalise node type
    fUpdate = new Folder();
    fUpdate.setNodeType(TYPE_CM_FOLDER);
    put(URL_NODES, fId, toJsonAsStringNonNull(fUpdate), null, 400);
    // -ve test - try to move to a different parent using PUT (note: should use new POST /nodes/{nodeId}/move operation instead)
    folderResp = createFolder(f0Id, "folder 2");
    String f2Id = folderResp.getId();
    fUpdate = new Folder();
    fUpdate.setParentId(f2Id);
    put(URL_NODES, fId, toJsonAsStringNonNull(fUpdate), null, 400);
    // ok - if parent does not change
    fUpdate = new Folder();
    fUpdate.setParentId(f0Id);
    put(URL_NODES, fId, toJsonAsStringNonNull(fUpdate), null, 200);
    // -ve test - minor: error code if trying to update property with invalid format (REPO-473)
    props = new HashMap<>();
    props.put("exif:pixelYDimension", "my unknown property");
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    put(URL_NODES, f2Id, toJsonAsStringNonNull(fUpdate), null, 400);
    // -ve test - minor: error code if trying to update property with invalid format (REPO-1635)
    props = new HashMap<>();
    props.put("exif:dateTimeOriginal", "25-11-2016");
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    put(URL_NODES, f2Id, toJsonAsStringNonNull(fUpdate), null, 400);
    // +ve test - try again with valid formats (REPO-473, REPO-1635)
    props = new HashMap<>();
    props.put("exif:pixelYDimension", "123");
    props.put("exif:dateTimeOriginal", "2016-11-21T16:26:19.037+0000");
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    put(URL_NODES, f2Id, toJsonAsStringNonNull(fUpdate), null, 200);
    // -ve test - non-admin cannot modify root (Company Home) folder
    props = new HashMap<>();
    props.put("cm:description", "my folder description");
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    put(URL_NODES, Nodes.PATH_ROOT, toJsonAsStringNonNull(fUpdate), null, 403);
    // -ve test - non-admin cannot modify "Shared" folder
    props = new HashMap<>();
    props.put("cm:description", "my folder description");
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    put(URL_NODES, Nodes.PATH_SHARED, toJsonAsStringNonNull(fUpdate), null, 403);
    setRequestContext(user2);
    // -ve test - user cannot modify another user's home folder
    props = new HashMap<>();
    props.put("cm:description", "my folder description");
    fUpdate = new Folder();
    fUpdate.setProperties(props);
    put(URL_NODES, user1MyFolderId, toJsonAsStringNonNull(fUpdate), null, 403);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.alfresco.rest.api.tests.client.data.Node) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) UserInfo(org.alfresco.rest.api.tests.client.data.UserInfo) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 3 with UserInfo

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

the class NodeApiTest method testCreateFolder.

/**
 * Tests create folder.
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 */
@Test
public void testCreateFolder() throws Exception {
    setRequestContext(user1);
    String myNodeId = getMyNodeId();
    UserInfo expectedUser = new UserInfo(user1);
    String postUrl = getNodeChildrenUrl(myNodeId);
    // create folder
    Folder folderResp = createFolder(myNodeId, "f1");
    String f1Id = folderResp.getId();
    Folder f1 = new Folder();
    f1.setName("f1");
    f1.setNodeType(TYPE_CM_FOLDER);
    f1.setIsFolder(true);
    f1.setParentId(myNodeId);
    f1.setAspectNames(Collections.singletonList("cm:auditable"));
    f1.setCreatedByUser(expectedUser);
    f1.setModifiedByUser(expectedUser);
    f1.expected(folderResp);
    // create sub-folder with properties
    Map<String, Object> props = new HashMap<>();
    props.put("cm:title", "my folder title");
    props.put("cm:description", "my folder description");
    folderResp = createFolder(f1Id, "f2", props);
    String f2Id = folderResp.getId();
    Folder f2 = new Folder();
    f2.setName("f2");
    f2.setNodeType(TYPE_CM_FOLDER);
    f2.setProperties(props);
    f2.setIsFolder(true);
    f2.setParentId(f1Id);
    f2.setAspectNames(Arrays.asList("cm:auditable", "cm:titled"));
    f2.setCreatedByUser(expectedUser);
    f2.setModifiedByUser(expectedUser);
    f2.expected(folderResp);
    // create another folder in a (partially existing) folder path
    Node n = new Node();
    n.setName("fZ");
    n.setNodeType(TYPE_CM_FOLDER);
    n.setRelativePath("/f1/f2/f3/f4");
    // create node
    HttpResponse response = post(getNodeChildrenUrl(myNodeId), RestApiUtil.toJsonAsStringNonNull(n), 201);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    // check parent hierarchy ...
    response = getSingle(NodesEntityResource.class, folderResp.getId(), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderResp.getName(), "fZ");
    response = getSingle(NodesEntityResource.class, folderResp.getParentId(), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderResp.getName(), "f4");
    response = getSingle(NodesEntityResource.class, folderResp.getParentId(), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderResp.getName(), "f3");
    response = getSingle(NodesEntityResource.class, folderResp.getParentId(), null, 200);
    folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(folderResp.getName(), "f2");
    assertEquals(folderResp.getId(), f2Id);
    // -ve test - name is mandatory
    Folder invalid = new Folder();
    invalid.setNodeType(TYPE_CM_FOLDER);
    post(postUrl, toJsonAsStringNonNull(invalid), 400);
    // -ve test - invalid name
    invalid = new Folder();
    invalid.setName("inv:alid");
    invalid.setNodeType(TYPE_CM_FOLDER);
    post(postUrl, toJsonAsStringNonNull(invalid), 422);
    // -ve test - node type is mandatory
    invalid = new Folder();
    invalid.setName("my folder");
    post(postUrl, toJsonAsStringNonNull(invalid), 400);
    // create empty file - used in -ve test below
    Document d1 = new Document();
    d1.setName("d1.txt");
    d1.setNodeType(TYPE_CM_CONTENT);
    response = post(postUrl, toJsonAsStringNonNull(d1), 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String d1Id = documentResp.getId();
    // -ve test - invalid (model integrity exception)
    Folder f3 = new Folder();
    f3.setName("f3");
    f3.setNodeType(TYPE_CM_FOLDER);
    post(getNodeChildrenUrl(d1Id), toJsonAsStringNonNull(f3), 422);
    // -ve test - it should not be possible to create a "system folder"
    invalid = new Folder();
    invalid.setName("my sys folder");
    invalid.setNodeType("cm:systemfolder");
    post(postUrl, toJsonAsStringNonNull(invalid), 400);
    // -ve test - unknown parent folder node id
    post(getNodeChildrenUrl(UUID.randomUUID().toString()), toJsonAsStringNonNull(f3), 404);
    // -ve test - duplicate name
    post(postUrl, toJsonAsStringNonNull(f1), 409);
    // Create a folder with a duplicate name (f1), but set the autoRename to true
    response = post(postUrl, toJsonAsStringNonNull(f1), "?" + Nodes.PARAM_AUTO_RENAME + "=true", 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals("f1-1", documentResp.getName());
    // Create a folder with a duplicate name (f1) again, but set the autoRename to true
    response = post(postUrl, toJsonAsStringNonNull(f1), "?" + Nodes.PARAM_AUTO_RENAME + "=true", 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals("f1-2", documentResp.getName());
    // -ve test - create a folder with a duplicate name (f1), but set the autoRename to false
    post(postUrl, toJsonAsStringNonNull(f1), "?" + Nodes.PARAM_AUTO_RENAME + "=false", 409);
    // Create folder using relative path
    n = new Node();
    n.setName("fX");
    n.setNodeType(TYPE_CM_FOLDER);
    n.setRelativePath("/f1/f2");
    response = post(postUrl, toJsonAsStringNonNull(n), "?" + Nodes.PARAM_AUTO_RENAME + "=true", 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals("fX", documentResp.getName());
    // Create a folder using relative path, with a duplicate name (fX) but set the autoRename to true
    response = post(postUrl, toJsonAsStringNonNull(n), "?" + Nodes.PARAM_AUTO_RENAME + "=true", 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals("fX-1", documentResp.getName());
    // -ve test - create a folder with a duplicate name (fX), but set the autoRename to false
    post(postUrl, toJsonAsStringNonNull(n), "?" + Nodes.PARAM_AUTO_RENAME + "=false", 409);
    // -ve test - invalid relative path
    n = new Node();
    n.setName("fX");
    n.setNodeType(TYPE_CM_FOLDER);
    n.setRelativePath("/f1/inv:alid");
    post(getNodeChildrenUrl(f2Id), RestApiUtil.toJsonAsStringNonNull(n), 422);
    // -ve test - invalid relative path - points to existing node that is not a folder
    n = new Node();
    n.setName("fY");
    n.setNodeType(TYPE_CM_FOLDER);
    n.setRelativePath("d1.txt");
    post(getNodeChildrenUrl(myNodeId), RestApiUtil.toJsonAsStringNonNull(n), 409);
    // -ve test - minor: error code if trying to create with property with invalid format (REPO-473)
    props = new HashMap<>();
    props.put("exif:pixelYDimension", "my unknown property");
    n = new Folder();
    n.setName("fZ");
    n.setNodeType(TYPE_CM_FOLDER);
    n.setProperties(props);
    post(getNodeChildrenUrl(myNodeId), RestApiUtil.toJsonAsStringNonNull(n), 400);
}
Also used : 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) 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 4 with UserInfo

use of org.alfresco.rest.api.tests.client.data.UserInfo 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 5 with UserInfo

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

the class NodeApiTest method testLinkCRUD.

// note: app:folderlink & app:filelink both extend cm:link (which in turn extends cm:cmobject)
// (see applicationModel.xml / contentModel.xml)
@Test
public void testLinkCRUD() throws Exception {
    setRequestContext(user1);
    String myNodeId = getMyNodeId();
    UserInfo expectedUser = new UserInfo(user1);
    String myChildrenUrl = getNodeChildrenUrl(myNodeId);
    // create folder f1
    Folder folderResp = createFolder(myNodeId, "f1 " + RUNID);
    String f1Id = folderResp.getId();
    // create empty file d1 in f1
    Document d1 = new Document();
    d1.setName("d1.txt");
    d1.setNodeType(TYPE_CM_CONTENT);
    HttpResponse response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(d1), 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String d1Id = documentResp.getId();
    // create folder f2
    folderResp = createFolder(myNodeId, "f2 " + RUNID);
    String f2Id = folderResp.getId();
    // create folder link node in f2 (pointing to f1)
    String nodeName = "f1 link";
    String nodeType = "app:folderlink";
    Map<String, Object> props = new HashMap<>();
    props.put("cm:destination", f1Id);
    Node nodeResp = createNode(f2Id, nodeName, nodeType, props);
    String n1Id = nodeResp.getId();
    Node n1 = new Node();
    n1.setName(nodeName);
    n1.setNodeType(nodeType);
    n1.setIsFolder(true);
    // note: parent of the link (not where it is pointing)
    n1.setParentId(f2Id);
    n1.setAspectNames(Collections.singletonList("cm:auditable"));
    n1.setProperties(props);
    n1.setCreatedByUser(expectedUser);
    n1.setModifiedByUser(expectedUser);
    n1.expected(nodeResp);
    // get node info
    response = getSingle(NodesEntityResource.class, n1Id, null, 200);
    nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    n1.expected(nodeResp);
    // create file link node in f2 pointing to d1
    nodeName = "d1 link";
    nodeType = "app:filelink";
    props = new HashMap<>();
    props.put("cm:destination", d1Id);
    nodeResp = createNode(f2Id, nodeName, nodeType, props);
    String n2Id = nodeResp.getId();
    Node n2 = new Node();
    n2.setName(nodeName);
    n2.setNodeType(nodeType);
    n2.setIsFolder(false);
    // note: parent of the link (not where it is pointing)
    n2.setParentId(f2Id);
    n2.setAspectNames(Collections.singletonList("cm:auditable"));
    n2.setProperties(props);
    n2.setCreatedByUser(expectedUser);
    n2.setModifiedByUser(expectedUser);
    n2.expected(nodeResp);
    // get node info
    response = getSingle(NodesEntityResource.class, n2Id, null, 200);
    nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    n2.expected(nodeResp);
    // update node - rename
    String updatedName = "f1 link renamed";
    Node nUpdate = new Node();
    nUpdate.setName(updatedName);
    response = put(URL_NODES, n1Id, toJsonAsStringNonNull(nUpdate), null, 200);
    nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    n1.setName(updatedName);
    n1.expected(nodeResp);
    // filtering, via where clause (nodeType + optionally including sub-types)
    List<String> linkIds = Arrays.asList(n1Id, n2Id);
    Map<String, String> params = new HashMap<>();
    params.put("where", "(nodeType='cm:link')");
    Paging paging = getPaging(0, Integer.MAX_VALUE);
    response = getAll(getNodeChildrenUrl(f2Id), paging, params, 200);
    List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
    assertEquals(0, nodes.size());
    // filter by including sub-types - note: includesubtypes is case-insensitive
    params = new HashMap<>();
    params.put("where", "(nodeType='cm:link INCLUDESUBTYPES')");
    paging = getPaging(0, Integer.MAX_VALUE);
    response = getAll(getNodeChildrenUrl(f2Id), paging, params, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
    assertEquals(linkIds.size(), nodes.size());
    assertTrue(linkIds.contains(nodes.get(0).getId()));
    assertTrue(linkIds.contains(nodes.get(1).getId()));
    params = new HashMap<>();
    params.put("where", "(nodeType='cm:link includeSubTypes')");
    paging = getPaging(0, Integer.MAX_VALUE);
    response = getAll(getNodeChildrenUrl(f2Id), paging, params, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
    assertEquals(linkIds.size(), nodes.size());
    assertTrue(linkIds.contains(nodes.get(0).getId()));
    assertTrue(linkIds.contains(nodes.get(1).getId()));
    // delete link
    deleteNode(n1Id);
    // -ve test - delete - cannot delete nonexistent link
    deleteNode(n1Id, 404);
    // -ve test - create - name is mandatory
    Node invalid = new Node();
    invalid.setNodeType("cm:link");
    post(myChildrenUrl, toJsonAsStringNonNull(invalid), 400);
    // -ve test - create - node type is mandatory
    invalid = new Node();
    invalid.setName("my node");
    post(myChildrenUrl, toJsonAsStringNonNull(invalid), 400);
    // -ve test - create - unsupported node type
    invalid = new Node();
    invalid.setName("my node");
    invalid.setNodeType("sys:base");
    post(myChildrenUrl, toJsonAsStringNonNull(invalid), 400);
    // -ve test - create - duplicate name
    post(getNodeChildrenUrl(f2Id), toJsonAsStringNonNull(n2), 409);
    // -ve test - unknown nodeType when filtering
    params = new HashMap<>();
    params.put("where", "(nodeType='my:unknown'");
    getAll(getNodeChildrenUrl(f2Id), paging, params, 400);
    // -ver test - invalid node type localname format and suffix is not ' includesubtypes'
    params = new HashMap<>();
    params.put("where", "(nodeType='cm:link ')");
    getAll(getNodeChildrenUrl(f2Id), paging, params, 400);
    params = new HashMap<>();
    params.put("where", "(nodeType='cm:link blah')");
    getAll(getNodeChildrenUrl(f2Id), 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) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) 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