use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class AbstractBaseApiTest method delete.
protected HttpResponse delete(String url, String entityId, Map<String, String> params, Map<String, String> headers, String apiName, int expectedStatus) throws Exception {
RequestBuilder requestBuilder = httpClient.new DeleteRequestBuilder().setRequestContext(publicApiClient.getRequestContext()).setScope(getScope()).setApiName(apiName).setEntityCollectionName(url).setEntityId(entityId).setParams(params).setHeaders(headers);
HttpResponse response = publicApiClient.execute(requestBuilder);
checkStatus(expectedStatus, response.getStatusCode());
return response;
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class AbstractBaseApiTest method getMyNodeId.
// -my- (eg. User's Home for on-prem)
protected String getMyNodeId() throws Exception {
HttpResponse response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, null, 200);
Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
return node.getId();
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class DeletedNodesTest method testListRenditions.
/**
* Test retrieve renditions for deleted nodes
* <p>post:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/renditions}
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/rendition/<renditionId>}
*/
@Test
public void testListRenditions() throws Exception {
setRequestContext(user1);
Date now = new Date();
String folder1 = "folder" + now.getTime() + "_1";
Folder createdFolder = createFolder(tDocLibNodeId, folder1, null);
assertNotNull(createdFolder);
String f1Id = createdFolder.getId();
// Create multipart request
String fileName = "quick.pdf";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName, file));
MultiPartBuilder.MultiPartRequest reqBody = multiPartBuilder.build();
// Upload quick.pdf file into the folder previously created
HttpResponse response = post(getNodeChildrenUrl(f1Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document.getId();
// create doclib rendition and move node to trashcan
createAndGetRendition(contentNodeId, "doclib");
deleteNode(contentNodeId);
// List all renditions and check for results
PublicApiClient.Paging paging = getPaging(0, 50);
response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, 200);
List<Rendition> renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertTrue(renditions.size() >= 3);
// +ve test - get previously created 'doclib' rendition
response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib", 200);
Rendition doclibRendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(doclibRendition);
assertEquals(Rendition.RenditionStatus.CREATED, doclibRendition.getStatus());
ContentInfo contentInfo = doclibRendition.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
assertEquals("PNG Image", contentInfo.getMimeTypeName());
assertNotNull(contentInfo.getEncoding());
assertTrue(contentInfo.getSizeInBytes() > 0);
// +ve test - Add a filter on rendition 'status' and list only 'NOT_CREATED' renditions
Map<String, String> params = new HashMap<>(1);
params.put("where", "(status='NOT_CREATED')");
response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertTrue(renditions.size() >= 2);
// +ve test - Add a filter on rendition 'status' and list only the CREATED renditions
params.put("where", "(status='CREATED')");
response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertEquals("Only 'doclib' rendition should be returned.", 1, renditions.size());
// SkipCount=0,MaxItems=2
paging = getPaging(0, 2);
// List all available renditions
response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertEquals(2, renditions.size());
PublicApiClient.ExpectedPaging expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
assertEquals(2, expectedPaging.getCount().intValue());
assertEquals(0, expectedPaging.getSkipCount().intValue());
assertEquals(2, expectedPaging.getMaxItems().intValue());
assertTrue(expectedPaging.getTotalItems() >= 3);
assertTrue(expectedPaging.getHasMoreItems());
// SkipCount=1,MaxItems=3
paging = getPaging(1, 3);
// List all available renditions
response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertEquals(3, renditions.size());
expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
assertEquals(3, expectedPaging.getCount().intValue());
assertEquals(1, expectedPaging.getSkipCount().intValue());
assertEquals(3, expectedPaging.getMaxItems().intValue());
assertTrue(expectedPaging.getTotalItems() >= 3);
// +ve test - Test returned renditions are ordered (natural sort order)
response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertTrue(Ordering.natural().isOrdered(renditions));
// Check again to make sure the ordering wasn't coincidental
response = getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 200);
renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
assertTrue(Ordering.natural().isOrdered(renditions));
// -ve - nodeId in the path parameter does not exist
getAll(getDeletedNodeRenditionsUrl(UUID.randomUUID().toString()), paging, params, 404);
// -ve test - Create an empty text file
Document emptyDoc = createEmptyTextFile(f1Id, "d1.txt");
getAll(getDeletedNodeRenditionsUrl(emptyDoc.getId()), paging, params, 404);
// -ve - nodeId in the path parameter does not represent a file
deleteNode(f1Id);
getAll(getDeletedNodeRenditionsUrl(f1Id), paging, params, 400);
// -ve - Invalid status value
params.put("where", "(status='WRONG')");
getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 400);
// -ve - Invalid filter (only 'status' is supported)
params.put("where", "(id='doclib')");
getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 400);
// -ve test - Authentication failed
setRequestContext(null);
getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 401);
// -ve - Current user does not have permission for nodeId
setRequestContext(user2);
getAll(getDeletedNodeRenditionsUrl(contentNodeId), paging, params, 403);
// Test get single node rendition
setRequestContext(user1);
// -ve - nodeId in the path parameter does not exist
getSingle(getDeletedNodeRenditionsUrl(UUID.randomUUID().toString()), "doclib", 404);
// -ve - renditionId in the path parameter is not registered/available
getSingle(getNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis()), 404);
// -ve - nodeId in the path parameter does not represent a file
getSingle(getDeletedNodeRenditionsUrl(f1Id), "doclib", 400);
// -ve test - Authentication failed
setRequestContext(null);
getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib", 401);
// -ve - Current user does not have permission for nodeId
setRequestContext(user2);
getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib", 403);
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class DeletedNodesTest method testDownloadFileContent.
/**
* Tests download of file/content.
* <p>GET:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/content}
*/
@Test
public void testDownloadFileContent() throws Exception {
setRequestContext(user1);
// Use existing test file
String fileName = "quick-1.txt";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName, file));
MultiPartBuilder.MultiPartRequest reqBody = multiPartBuilder.build();
// Upload text content
HttpResponse response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document.getId();
// Check the upload response
assertEquals(fileName, document.getName());
ContentInfo contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
// move the node to Trashcan
deleteNode(document.getId());
// Download text content - by default with Content-Disposition header
response = getSingle(TrashcanEntityResource.class, contentNodeId + "/content", null, 200);
String textContent = response.getResponse();
assertEquals("The quick brown fox jumps over the lazy dog", textContent);
Map<String, String> responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertEquals("attachment; filename=\"quick-1.txt\"; filename*=UTF-8''quick-1.txt", responseHeaders.get("Content-Disposition"));
String cacheControl = responseHeaders.get("Cache-Control");
assertNotNull(cacheControl);
assertTrue(cacheControl.contains("must-revalidate"));
assertTrue(cacheControl.contains("max-age=0"));
assertNotNull(responseHeaders.get("Expires"));
String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
// Test 304 response
getSingle(URL_DELETED_NODES + "/" + contentNodeId + "/content", null, null, headers, 304);
// Use existing pdf test file
fileName = "quick.pdf";
file = getResourceFile(fileName);
byte[] originalBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
multiPartBuilder = MultiPartBuilder.create().setFileData(new MultiPartBuilder.FileData(fileName, file));
reqBody = multiPartBuilder.build();
// Upload binary content
response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// move the node to Trashcan
deleteNode(document.getId());
contentNodeId = document.getId();
// Check the upload response
assertEquals(fileName, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
// Download binary content (as bytes) - without Content-Disposition
// header (attachment=false)
Map<String, String> params = new LinkedHashMap<>();
params.put("attachment", "false");
response = getSingle(TrashcanEntityResource.class, contentNodeId + "/content", params, 200);
byte[] bytes = response.getResponseAsBytes();
assertArrayEquals(originalBytes, bytes);
responseHeaders = response.getHeaders();
assertNotNull(responseHeaders);
assertNull(responseHeaders.get("Content-Disposition"));
assertNotNull(responseHeaders.get("Cache-Control"));
assertNotNull(responseHeaders.get("Expires"));
lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
assertNotNull(lastModifiedHeader);
headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
// Test 304 response
getSingle(URL_DELETED_NODES + "/" + contentNodeId + "/content", null, null, headers, 304);
// -ve - nodeId in the path parameter does not exist
getSingle(TrashcanEntityResource.class, UUID.randomUUID().toString() + "/content", params, 404);
// -ve test - Authentication failed
setRequestContext(null);
getSingle(TrashcanEntityResource.class, contentNodeId + "/content", params, 401);
// -ve - Current user does not have permission for nodeId
setRequestContext(user2);
getSingle(TrashcanEntityResource.class, contentNodeId + "/content", params, 403);
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class DeletedNodesTest method testCreateAndPurge.
/**
* Tests purging a deleted node
* <p>delete:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/deleted-nodes/<nodeId>/}
*/
@Test
public void testCreateAndPurge() throws Exception {
setRequestContext(user1);
Date now = new Date();
String folder1 = "folder" + now.getTime() + "_1";
Folder createdFolder = createFolder(tDocLibNodeId, folder1, null);
assertNotNull(createdFolder);
deleteNode(createdFolder.getId());
HttpResponse response = getSingle(URL_DELETED_NODES, createdFolder.getId(), 200);
Folder fNode = jacksonUtil.parseEntry(response.getJsonResponse(), Folder.class);
assertNotNull(fNode);
// try purging "nonsense"
delete(URL_DELETED_NODES, "nonsense", 404);
// User 2 can't do it
setRequestContext(user2);
delete(URL_DELETED_NODES, createdFolder.getId(), Status.STATUS_FORBIDDEN);
setRequestContext(user1);
// Now purge the folder
delete(URL_DELETED_NODES, createdFolder.getId(), 204);
// This time we can't find it.
getSingle(URL_DELETED_NODES, createdFolder.getId(), 404);
}
Aggregations