use of org.alfresco.rest.api.tests.client.data.Folder in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testCopy.
/**
* Tests copy (file or folder)
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/copy}
*/
@Test
public void testCopy() throws Exception {
setRequestContext(user1);
// create folder
Folder folderResp = createFolder(Nodes.PATH_MY, "fsource");
String sourceId = folderResp.getId();
// create folder
folderResp = createFolder(Nodes.PATH_MY, "ftarget");
String targetId = folderResp.getId();
// create doc d1
String d1Name = "content" + RUNID + "_1";
String d1Id = createTextFile(sourceId, d1Name, "The quick brown fox jumps over the lazy dog 1.").getId();
// create doc d2
String d2Name = "content" + RUNID + "_2";
String d2Id = createTextFile(sourceId, d2Name, "The quick brown fox jumps over the lazy dog 2.").getId();
Map<String, String> body = new HashMap<>();
body.put("targetParentId", targetId);
HttpResponse response = post(URL_NODES, d1Id, "copy", toJsonAsStringNonNull(body).getBytes(), null, null, 201);
Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(d1Name, documentResp.getName());
assertEquals(targetId, documentResp.getParentId());
// copy file (with rename)
String newD2Name = d2Name + " updated !!";
body = new HashMap<>();
body.put("targetParentId", targetId);
body.put("name", newD2Name);
response = post(URL_NODES, d2Id, "copy", toJsonAsStringNonNull(body).getBytes(), null, null, 201);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(newD2Name, documentResp.getName());
assertEquals(targetId, documentResp.getParentId());
// -ve tests
// missing target
NodeTarget tgt = new NodeTarget();
tgt.setName("new name");
post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 400);
// name already exists - different parent
tgt = new NodeTarget();
tgt.setName(newD2Name);
tgt.setTargetParentId(targetId);
post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 409);
// name already exists - same parent
tgt = new NodeTarget();
tgt.setTargetParentId(sourceId);
post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 409);
// unknown source nodeId
tgt = new NodeTarget();
tgt.setTargetParentId(targetId);
post("nodes/" + UUID.randomUUID().toString() + "/copy", toJsonAsStringNonNull(tgt), null, 404);
// unknown target nodeId
tgt = new NodeTarget();
tgt.setTargetParentId(UUID.randomUUID().toString());
post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 404);
// target is not a folder
tgt = new NodeTarget();
tgt.setTargetParentId(d2Id);
post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 400);
String rootNodeId = getRootNodeId();
// no (write/create) permissions to copy to target
tgt = new NodeTarget();
tgt.setTargetParentId(rootNodeId);
post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 403);
// -ve test - cannot copy to multiple destinations in single POST call (unsupported)
List<NodeTarget> nodeDestinations = new ArrayList<>(2);
NodeTarget nodeTarget = new NodeTarget();
nodeTarget.setTargetParentId(sourceId);
nodeDestinations.add(nodeTarget);
nodeTarget = new NodeTarget();
nodeTarget.setTargetParentId(targetId);
nodeDestinations.add(nodeTarget);
post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(nodeDestinations), null, 405);
}
use of org.alfresco.rest.api.tests.client.data.Folder 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);
}
use of org.alfresco.rest.api.tests.client.data.Folder in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testUpdateType.
/**
* Tests update type
* <p>PUT:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>}
*/
@Test
public void testUpdateType() throws Exception {
setRequestContext(user1);
// create folder f0
String folderName = "f0-testUpdateOwner-" + RUNID;
Folder folderResp = createFolder(Nodes.PATH_SHARED, folderName);
String f0Id = folderResp.getId();
// owner is implied
assertNull(user1, folderResp.getProperties());
// non-update case
Folder fUpdate = new Folder();
fUpdate.setNodeType(folderResp.getNodeType());
HttpResponse response = put(URL_NODES, f0Id, toJsonAsStringNonNull(fUpdate), null, 200);
folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
assertEquals(TYPE_CM_FOLDER, folderResp.getNodeType());
// set type to an incompatible type
fUpdate.setNodeType(TYPE_CM_CONTENT);
put(URL_NODES, f0Id, toJsonAsStringNonNull(fUpdate), null, 400);
// set type to system folder (a special, unsupported case)
fUpdate.setNodeType("cm:systemfolder");
put(URL_NODES, f0Id, toJsonAsStringNonNull(fUpdate), null, 400);
// set type to supported folder sub-type
// (none exists in contentModel, so forumsModel it is)
fUpdate.setNodeType("fm:forums");
response = put(URL_NODES, f0Id, toJsonAsStringNonNull(fUpdate), null, 200);
folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
assertEquals("fm:forums", folderResp.getNodeType());
// set type to a generalised type (unsupported case)
fUpdate.setNodeType(TYPE_CM_FOLDER);
put(URL_NODES, f0Id, toJsonAsStringNonNull(fUpdate), null, 400);
}
use of org.alfresco.rest.api.tests.client.data.Folder in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testMoveFileCreatedByDeletedUser.
@Test
public void testMoveFileCreatedByDeletedUser() throws Exception {
// Create temp user
String user = createUser("userTstMove-" + RUNID, "userRNDPassword", networkOne);
setRequestContext(user);
// create folder f0
String folder0Name = "f0-testMove-" + RUNID;
String f0Id = createFolder(Nodes.PATH_MY, folder0Name).getId();
// create folder f1
Folder folderResp = createFolder(f0Id, "f1");
String f1Id = folderResp.getId();
// create folder f2
folderResp = createFolder(f0Id, "f2");
String f2Id = folderResp.getId();
// create doc d1
String d1Name = "content" + RUNID + "_1";
String d1Id = createTextFile(f1Id, d1Name, "The quick brown fox jumps over the lazy dog 1.").getId();
setRequestContext(networkAdmin);
transactionHelper.doInTransaction(() -> {
deleteUser(user, networkOne);
return null;
});
// move file (without rename)
NodeTarget tgt = new NodeTarget();
tgt.setTargetParentId(f2Id);
HttpResponse response = post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 200);
Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(d1Name, documentResp.getName());
assertEquals(f2Id, documentResp.getParentId());
// Get node info (ensure rollback didn't happen)
response = getSingle(NodesEntityResource.class, d1Id, null, 200);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(f2Id, documentResp.getParentId());
// move file (with rename)
String d1NewName = d1Name + " updated !!";
tgt = new NodeTarget();
tgt.setName(d1NewName);
tgt.setTargetParentId(f1Id);
response = post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 200);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(d1NewName, documentResp.getName());
assertEquals(f1Id, documentResp.getParentId());
// Get node info (ensure rollback didn't happen)
response = getSingle(NodesEntityResource.class, d1Id, null, 200);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(f1Id, documentResp.getParentId());
}
use of org.alfresco.rest.api.tests.client.data.Folder in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testUploadToSite.
/**
* Tests Multipart upload to a Site.
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/children}
*/
@Test
public void testUploadToSite() throws Exception {
setRequestContext(user1);
final String fileName = "quick-1.txt";
final File file = getResourceFile(fileName);
String folderA = "folder" + RUNID + "_A";
String folderA_id = createFolder(tDocLibNodeId, folderA).getId();
Paging paging = getPaging(0, Integer.MAX_VALUE);
HttpResponse response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
PublicApiClient.ExpectedPaging pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
final int numOfNodes = pagingResult.getCount();
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
// Try to upload
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName, document.getName());
ContentInfo contentInfo = document.getContent();
assertNotNull(contentInfo);
// As the client didn't set the mimeType, the API must guess it.
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Retrieve the uploaded file
response = getSingle(NodesEntityResource.class, document.getId(), null, 200);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Check 'get children' is confirming the upload
response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals(numOfNodes + 1, pagingResult.getCount().intValue());
// Upload the same file again to check the name conflicts handling
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 409);
response = getAll(getNodeChildrenUrl(folderA_id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals(numOfNodes + 1, pagingResult.getCount().intValue());
setRequestContext(user2);
final String fileName2 = "quick-2.txt";
final File file2 = getResourceFile(fileName2);
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).build();
// user2 tries to upload a new file into the folderA of user1
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 403);
setRequestContext(user1);
// Test upload with properties
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName2, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertNotNull(document.getProperties());
assertNull(document.getProperties().get("cm:title"));
assertNull(document.getProperties().get("cm:description"));
// upload a file with properties. Also, set autoRename=true
Map<String, String> props = new HashMap<>(2);
props.put("cm:title", "test title");
props.put("cm:description", "test description");
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setProperties(props).build();
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
// "quick-2-1.txt" => fileName2 + autoRename
assertEquals("quick-2-1.txt", document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertNotNull(document.getProperties());
assertEquals("test title", document.getProperties().get("cm:title"));
assertEquals("test description", document.getProperties().get("cm:description"));
// Test unknown property name
props = new HashMap<>(1);
props.put("unknownPrefix" + System.currentTimeMillis() + ":description", "test description");
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setProperties(props).build();
// Prop prefix is unknown
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 400);
// Test relativePath multi-part field.
// Any folders in the relativePath that do not exist, are created before the content is created.
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setRelativePath("X/Y/Z");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), "?include=path", reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Check the uploaded file parent folders
PathInfo pathInfo = document.getPath();
assertNotNull(pathInfo);
List<ElementInfo> elementInfos = pathInfo.getElements();
assertNotNull(elementInfos);
// /Company Home/Sites/RandomSite<timestamp>/documentLibrary/folder<timestamp>_A/X/Y/Z
assertEquals(8, elementInfos.size());
assertEquals(document.getParentId(), elementInfos.get(7).getId());
assertEquals("Z", elementInfos.get(7).getName());
assertEquals("Y", elementInfos.get(6).getName());
assertEquals("X", elementInfos.get(5).getName());
assertEquals(folderA, elementInfos.get(4).getName());
// Try to create a folder with the same name as the document within the 'Z' folder.
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("X/Y/Z/" + document.getName()).build();
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 409);
// Test the same functionality as "mkdir -p x/y/z" which the folders should be created
// as needed but no errors thrown if the path or any part of the path already exists.
// NOTE: white spaces, leading and trailing "/" are ignored.
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("/X/ Y/Z /CoolFolder/").build();
response = post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// Retrieve the uploaded file parent folder
response = getSingle(NodesEntityResource.class, document.getParentId(), null, 200);
Folder coolFolder = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
assertEquals(document.getParentId(), coolFolder.getId());
assertEquals("CoolFolder", coolFolder.getName());
// Try to upload quick-1.txt within coolFolder and set the relativePath to a blank string.
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath(// blank
" ").build();
// 409 -> as the blank string is ignored and quick-1.txt already exists in the coolFolder
post(getNodeChildrenUrl(coolFolder.getId()), reqBody.getBody(), null, reqBody.getContentType(), 409);
setRequestContext(user2);
// user2 tries to upload the same file by creating sub-folders in the folderA of user1
reqBody = MultiPartBuilder.copy(multiPartBuilder).setRelativePath("userTwoFolder1/userTwoFolder2").build();
post(getNodeChildrenUrl(folderA_id), reqBody.getBody(), null, reqBody.getContentType(), 403);
// -ve test: integrity error
setRequestContext(user1);
reqBody = MultiPartBuilder.create().setFileData(new FileData("invalid:name", file)).build();
// 422 -> invalid name (includes a ':' in this example)
post(getNodeChildrenUrl(coolFolder.getId()), reqBody.getBody(), null, reqBody.getContentType(), 422);
}
Aggregations