use of org.alfresco.rest.api.model.LockInfo 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);
}
Aggregations