Search in sources :

Example 6 with Document

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

the class NodeApiTest method testLock.

/**
 * Tests lock of a node
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/lock}
 */
@Test
public void testLock() throws Exception {
    setRequestContext(user1);
    // create folder
    Folder folderResp = createFolder(Nodes.PATH_MY, "folder" + RUNID);
    String folderId = folderResp.getId();
    // try to lock the folder and check that is not allowed
    LockInfo lockInfo = new LockInfo();
    lockInfo.setTimeToExpire(60);
    lockInfo.setType("FULL");
    lockInfo.setLifetime("PERSISTENT");
    HttpResponse response = post(getNodeOperationUrl(folderId, "lock"), toJsonAsStringNonNull(lockInfo), null, 400);
    // create document d1
    String d1Name = "content" + RUNID + "_1l";
    Document d1 = createTextFile(folderId, d1Name, "The quick brown fox jumps over the lazy dog 1.");
    String d1Id = d1.getId();
    // lock d1 document
    lockInfo = new LockInfo();
    lockInfo.setTimeToExpire(30);
    lockInfo.setType("FULL");
    lockInfo.setLifetime("PERSISTENT");
    response = post(getNodeOperationUrl(d1Id, "lock"), toJsonAsStringNonNull(lockInfo), null, 200);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1Name, documentResp.getName());
    assertEquals(d1Id, documentResp.getId());
    assertEquals(LockType.READ_ONLY_LOCK.toString(), documentResp.getProperties().get("cm:lockType"));
    assertNotNull(documentResp.getProperties().get("cm:lockOwner"));
    assertNull(documentResp.getIsLocked());
    // invalid test - delete a locked node
    deleteNode(d1Id, true, 409);
    // wait for expiration time set to pass and delete node
    TimeUnit.SECONDS.sleep(30);
    deleteNode(d1Id, true, 204);
    // create doc d2
    String d2Name = "content" + RUNID + "_2l";
    Document d2 = createTextFile(folderId, d2Name, "The quick brown fox jumps over the lazy dog 2.");
    String d2Id = d2.getId();
    response = getSingle(URL_NODES, d2Id, null, null, 200);
    Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    assertNull(node.getProperties().get("cm:lockType"));
    assertNull(node.getProperties().get("cm:lockOwner"));
    assertNull(node.getIsLocked());
    Map<String, String> params = Collections.singletonMap("include", "isLocked");
    response = getSingle(URL_NODES, d2Id, params, null, 200);
    node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    assertNull(node.getProperties().get("cm:lockType"));
    assertNull(node.getProperties().get("cm:lockOwner"));
    assertFalse(node.getIsLocked());
    lockInfo = new LockInfo();
    lockInfo.setTimeToExpire(60);
    lockInfo.setType("FULL");
    lockInfo.setLifetime("PERSISTENT");
    response = post(getNodeOperationUrl(d2Id, "lock"), toJsonAsStringNonNull(lockInfo), null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d2Name, documentResp.getName());
    assertEquals(d2Id, documentResp.getId());
    assertEquals(LockType.READ_ONLY_LOCK.toString(), documentResp.getProperties().get("cm:lockType"));
    assertNotNull(documentResp.getProperties().get("cm:lockOwner"));
    assertNull(documentResp.getIsLocked());
    unlock(d2Id);
    // Empty lock body, the default values are used
    post(getNodeOperationUrl(d2Id, "lock"), EMPTY_BODY, null, 200);
    // Lock on already locked node
    post(getNodeOperationUrl(d2Id, "lock"), toJsonAsStringNonNull(lockInfo), null, 200);
    // Test delete on a folder which contains a locked node - NodeLockedException
    deleteNode(folderId, true, 409);
    // Content update on a locked node
    updateTextFile(d2Id, "Updated text", null, 409);
    unlock(d2Id);
    // Test lock file
    // create folder
    String folderAName = "folder" + RUNID + "_A";
    Folder folderA = createFolder(Nodes.PATH_MY, folderAName);
    String folderAId = folderA.getId();
    // create a file in the folderA
    Document dA1 = createTextFile(folderAId, "content" + RUNID + "_A1", "A1 content");
    String dA1Id = dA1.getId();
    lockInfo = new LockInfo();
    lockInfo.setTimeToExpire(60);
    lockInfo.setType("ALLOW_OWNER_CHANGES");
    lockInfo.setLifetime("EPHEMERAL");
    // lock the file
    post(getNodeOperationUrl(dA1Id, "lock"), toJsonAsStringNonNull(lockInfo), null, 200);
    params = Collections.singletonMap("include", "aspectNames,properties,isLocked");
    response = getSingle(URL_NODES, dA1Id, params, null, 200);
    node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    assertTrue(node.getIsLocked());
    // note: this can be updated by the owner since the lock type is "ALLOW_OWNER_CHANGES"
    updateTextFile(node.getId(), "Updated text", null, 200);
    // Lock body properties - boundary values
    Document dB1 = createTextFile(folderAId, "content" + RUNID + "_dB1", "dB1 content");
    String dB1Id = dB1.getId();
    lockInfo = new LockInfo();
    // values lower than 0 are considered as no expiry time
    lockInfo.setTimeToExpire(-100);
    post(getNodeOperationUrl(dB1Id, "lock"), toJsonAsStringNonNull(lockInfo), null, 200);
    // Lock node by a different user than the owner
    setRequestContext(user1);
    Folder folder1Resp = createFolder(Nodes.PATH_SHARED, "folder1" + RUNID);
    String folder1Id = folder1Resp.getId();
    String f1d1Name = "content f1" + RUNID + "_1l";
    Document f1d1 = createTextFile(folder1Id, f1d1Name, "The quick brown fox jumps over the lazy dog 1.");
    String f1d1Id = f1d1.getId();
    // use admin for now (might be better to use a user with given WRITE permission)
    setRequestContext(networkAdmin);
    post(getNodeOperationUrl(f1d1Id, "lock"), EMPTY_BODY, null, 200);
    unlock(f1d1Id);
    // -ve tests
    // Missing target node
    lockInfo = new LockInfo();
    post(getNodeOperationUrl("fakeId", "lock"), toJsonAsStringNonNull(lockInfo), null, 404);
    // Cannot lock Data Dictionary node
    params = new HashMap<>();
    params.put(Nodes.PARAM_RELATIVE_PATH, "/Data Dictionary");
    response = getSingle(NodesEntityResource.class, getRootNodeId(), params, 200);
    Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    String ddNodeId = nodeResp.getId();
    setRequestContext(networkAdmin);
    post(getNodeOperationUrl(ddNodeId, "lock"), toJsonAsStringNonNull(lockInfo), null, 403);
    // Lock node already locked by another user - UnableToAquireLockException
    post(getNodeOperationUrl(dB1Id, "lock"), EMPTY_BODY, null, 422);
    // Lock node without permission (node created by user 1 in the Home folder)
    setRequestContext(user1);
    Folder folder2Resp = createFolder(Nodes.PATH_MY, "folder2" + RUNID);
    String folder2Id = folder2Resp.getId();
    String f2d1Name = "content f2" + RUNID + "_1l";
    Document f2d1 = createTextFile(folder2Id, f2d1Name, "The quick brown fox jumps over the lazy dog 1.");
    String f2d1Id = f2d1.getId();
    setRequestContext(user2);
    post(getNodeOperationUrl(f2d1Id, "lock"), EMPTY_BODY, null, 403);
    // Invalid lock body values
    setRequestContext(user1);
    Folder folderC = createFolder(Nodes.PATH_MY, "folder" + RUNID + "_C");
    String folderCId = folderC.getId();
    Document dC1 = createTextFile(folderCId, "content" + RUNID + "_dC1", "dC1 content");
    String dC1Id = dC1.getId();
    Map<String, String> body = new HashMap<>();
    body.put("type", "FULL123");
    post(getNodeOperationUrl(dC1Id, "lock"), toJsonAsStringNonNull(body), null, 400);
    body = new HashMap<>();
    body.put("type", "ALLOW_ADD_CHILDREN");
    post(getNodeOperationUrl(dC1Id, "lock"), toJsonAsStringNonNull(body), null, 400);
    body = new HashMap<>();
    body.put("lifetime", "PERSISTENT123");
    post(getNodeOperationUrl(dC1Id, "lock"), toJsonAsStringNonNull(body), null, 400);
    body = new HashMap<>();
    body.put("includeChildren", "true");
    post(getNodeOperationUrl(dC1Id, "lock"), toJsonAsStringNonNull(body), null, 400);
    body = new HashMap<>();
    body.put("timeToExpire", "NaN");
    post(getNodeOperationUrl(dC1Id, "lock"), toJsonAsStringNonNull(body), null, 400);
    body = new HashMap<>();
    body.put("invalid_property", "true");
    post(getNodeOperationUrl(dC1Id, "lock"), toJsonAsStringNonNull(body), null, 400);
    // Invalid lock of a folder
    post(getNodeOperationUrl(folderId, "lock"), toJsonAsStringNonNull(lockInfo), null, 400);
    // cleanup
    // all locks were made by user1
    setRequestContext(user1);
    unlock(dB1Id);
    deleteNode(dB1Id);
    deleteNode(folderId);
    deleteNode(folderAId);
    deleteNode(folderCId);
    deleteNode(folder1Id);
    deleteNode(folder2Id);
}
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) LockInfo(org.alfresco.rest.api.model.LockInfo) 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 7 with Document

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

the class NodeApiTest method testUpdatePermissionInvalidName.

/**
 * Test attempt to set permission with an invalid name
 *
 * @throws Exception
 */
private void testUpdatePermissionInvalidName() 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, "InvalidName", AccessStatus.DENIED.toString()));
    nodePermissions.setLocallySet(locallySetPermissions);
    dUpdate.setPermissions(nodePermissions);
    // "Cannot set permissions on this node - unknown permission name:
    // InvalidName"
    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)

Example 8 with Document

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

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

use of org.alfresco.rest.api.tests.client.data.Document 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)

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