use of org.alfresco.rest.api.tests.client.data.ContentInfo 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);
}
use of org.alfresco.rest.api.tests.client.data.ContentInfo 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);
}
use of org.alfresco.rest.api.tests.client.data.ContentInfo in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testUploadToMyFiles.
/**
* Tests Multipart upload to user's home (a.k.a My Files).
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
*/
@Test
public void testUploadToMyFiles() throws Exception {
setRequestContext(user1);
// create folder f0
String folder0Name = "f0-testUploadToMyFiles-" + RUNID;
Folder folderResp = createFolder(Nodes.PATH_MY, folder0Name);
String f0Id = folderResp.getId();
final String fileName = "quick.pdf";
final File file = getResourceFile(fileName);
Paging paging = getPaging(0, Integer.MAX_VALUE);
HttpResponse response = getAll(getNodeChildrenUrl(f0Id), 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 into a non-existent folder
post(getNodeChildrenUrl(UUID.randomUUID().toString()), reqBody.getBody(), null, reqBody.getContentType(), 404);
// Upload
response = post(getNodeChildrenUrl(f0Id), 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);
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
// Default encoding
assertEquals("UTF-8", contentInfo.getEncoding());
// Check there is no path info returned.
// The path info should only be returned when it is requested via a include statement.
assertNull(document.getPath());
// 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_PDF, contentInfo.getMimeType());
// Check 'get children' is confirming the upload
response = getAll(getNodeChildrenUrl(f0Id), 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(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 409);
response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals("Duplicate file name. The file shouldn't have been uploaded.", numOfNodes + 1, pagingResult.getCount().intValue());
// Set autoRename=true and upload the same file again
reqBody = MultiPartBuilder.copy(multiPartBuilder).setAutoRename(true).build();
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals("quick-1.pdf", document.getName());
// upload the same file again, and request the path info to be present in the response
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), "?include=path", reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals("quick-2.pdf", document.getName());
assertNotNull(document.getPath());
response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals(numOfNodes + 3, pagingResult.getCount().intValue());
// upload without specifying content type or without overriding filename - hence guess mimetype and use file's name
final String fileName1 = "quick-1.txt";
final File file1 = getResourceFile(fileName1);
reqBody = MultiPartBuilder.create().setFileData(new FileData(null, file1)).build();
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName1, document.getName());
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, document.getContent().getMimeType());
// upload with "default" binary content type and override filename - hence guess mimetype & use overridden name
final String fileName2 = "quick-2.txt";
final String fileName2b = "quick-2b.txt";
final File file2 = getResourceFile(fileName2);
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2b, file2)).build();
response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName2b, document.getName());
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, document.getContent().getMimeType());
response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, null, 200);
Folder user1Home = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
// User2 tries to upload a new file into the user1's home folder.
setRequestContext(user2);
final File file3 = getResourceFile(fileName2);
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file3)).build();
post(getNodeChildrenUrl(user1Home.getId()), reqBody.getBody(), null, reqBody.getContentType(), 403);
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 403);
setRequestContext(user1);
response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
pagingResult = parsePaging(response.getJsonResponse());
assertNotNull(paging);
assertEquals("Access Denied. The file shouldn't have been uploaded.", numOfNodes + 5, pagingResult.getCount().intValue());
// User1 tries to upload a file into a document rather than a folder!
post(getNodeChildrenUrl(document.getId()), reqBody.getBody(), null, reqBody.getContentType(), 400);
// Try to upload a file without defining the required formData
reqBody = MultiPartBuilder.create().setAutoRename(true).build();
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 400);
// Test unsupported node type
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setNodeType("cm:link").build();
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 400);
// User1 uploads a new file
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).build();
response = post(getNodeChildrenUrl(f0Id), 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());
assertEquals("ISO-8859-1", contentInfo.getEncoding());
// Test content size limit
final SimpleFixedLimitProvider limitProvider = applicationContext.getBean("defaultContentLimitProvider", SimpleFixedLimitProvider.class);
final long defaultSizeLimit = limitProvider.getSizeLimit();
// 20 KB
limitProvider.setSizeLimitString("20000");
try {
// quick.pdf size is about 23 KB
reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setAutoRename(true).build();
// Try to upload a file larger than the configured size limit
post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 413);
} finally {
limitProvider.setSizeLimitString(Long.toString(defaultSizeLimit));
}
}
use of org.alfresco.rest.api.tests.client.data.ContentInfo in project alfresco-remote-api by Alfresco.
the class RenditionsTest method testCreateRenditionForNewVersion.
/**
* Tests create rendition after uploading new version(s)
*/
@Test
public void testCreateRenditionForNewVersion() throws Exception {
String PROP_LTM = "cm:lastThumbnailModification";
String RENDITION_NAME = "imgpreview";
String userId = userOneN1.getId();
setRequestContext(networkN1.getId(), userOneN1.getId(), null);
// Create a folder within the site document's library
String folderName = "folder" + System.currentTimeMillis();
String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, TYPE_CM_FOLDER, userId);
// Create multipart request - pdf file
String fileName = "quick.pdf";
File file = getResourceFile(fileName);
MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).build();
Map<String, String> params = Collections.singletonMap("include", "properties");
// Upload quick.pdf file into 'folder' - do not include request to create 'doclib' thumbnail
HttpResponse response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), params, null, "alfresco", reqBody.getContentType(), 201);
Document document1 = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document1.getId();
assertNotNull(document1.getProperties());
assertNull(document1.getProperties().get(PROP_LTM));
// pause briefly
Thread.sleep(DELAY_IN_MS);
// Get rendition (not created yet) information for node
response = getSingle(getNodeRenditionsUrl(contentNodeId), RENDITION_NAME, 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(RenditionStatus.NOT_CREATED, rendition.getStatus());
params = new HashMap<>();
params.put("placeholder", "false");
getSingle(getNodeRenditionsUrl(contentNodeId), (RENDITION_NAME + "/content"), params, 404);
// TODO add test to request creation of rendition as another user (that has read-only access on the content, not write)
// Create and get 'imgpreview' rendition
rendition = createAndGetRendition(contentNodeId, RENDITION_NAME);
assertNotNull(rendition);
assertEquals(RenditionStatus.CREATED, rendition.getStatus());
ContentInfo contentInfo = rendition.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_JPEG, contentInfo.getMimeType());
assertEquals("JPEG Image", contentInfo.getMimeTypeName());
assertNotNull(contentInfo.getEncoding());
assertTrue(contentInfo.getSizeInBytes() > 0);
params = new HashMap<>();
params.put("placeholder", "false");
response = getSingle(getNodeRenditionsUrl(contentNodeId), (RENDITION_NAME + "/content"), params, 200);
byte[] renditionBytes1 = response.getResponseAsBytes();
assertNotNull(renditionBytes1);
// check node details ...
params = Collections.singletonMap("include", "properties");
response = getSingle(NodesEntityResource.class, contentNodeId, params, 200);
Document document1b = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(document1b.getModifiedAt(), document1.getModifiedAt());
assertEquals(document1b.getModifiedByUser().getId(), document1.getModifiedByUser().getId());
assertEquals(document1b.getModifiedByUser().getDisplayName(), document1.getModifiedByUser().getDisplayName());
assertNotEquals(document1b.getProperties().get(PROP_LTM), document1.getProperties().get(PROP_LTM));
// upload another version of "quick.pdf" and check again
fileName = "quick-2.pdf";
file = getResourceFile(fileName);
reqBody = MultiPartBuilder.create().setFileData(new FileData("quick.pdf", file)).setOverwrite(true).build();
response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, null, "alfresco", reqBody.getContentType(), 201);
Document document2 = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(contentNodeId, document2.getId());
// wait to allow new version of the rendition to be created ...
Thread.sleep(DELAY_IN_MS * 4);
params = new HashMap<>();
params.put("placeholder", "false");
response = getSingle(getNodeRenditionsUrl(contentNodeId), (RENDITION_NAME + "/content"), params, 200);
assertNotNull(response.getResponseAsBytes());
// check rendition binary has changed
assertNotEquals(renditionBytes1, response.getResponseAsBytes());
// check node details ...
params = Collections.singletonMap("include", "properties");
response = getSingle(NodesEntityResource.class, contentNodeId, params, 200);
Document document2b = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertTrue(document2b.getModifiedAt().after(document1.getModifiedAt()));
assertEquals(document2b.getModifiedByUser().getId(), document1.getModifiedByUser().getId());
assertEquals(document2b.getModifiedByUser().getDisplayName(), document1.getModifiedByUser().getDisplayName());
// check last thumbnail modification property has changed ! (REPO-1644)
assertNotEquals(document2b.getProperties().get(PROP_LTM), document1b.getProperties().get(PROP_LTM));
}
use of org.alfresco.rest.api.tests.client.data.ContentInfo in project alfresco-remote-api by Alfresco.
the class AbstractBaseApiTest method createEmptyTextFile.
protected Document createEmptyTextFile(String parentFolderId, String docName, Map<String, String> params, int expectedStatus) throws Exception {
Document d1 = new Document();
d1.setName(docName);
d1.setNodeType("cm:content");
ContentInfo ci = new ContentInfo();
ci.setMimeType("text/plain");
d1.setContent(ci);
// create empty file
HttpResponse response = post(getNodeChildrenUrl(parentFolderId), toJsonAsStringNonNull(d1), params, null, "alfresco", expectedStatus);
if (expectedStatus != 201) {
return null;
}
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
}
Aggregations