use of org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedErrorResponse in project alfresco-remote-api by Alfresco.
the class RenditionsTest method testCreateRendition.
/**
* Tests create rendition.
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/renditions}
*/
@Test
public void testCreateRendition() throws Exception {
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, userOneN1.getId());
// Create multipart request
String fileName = "quick.pdf";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
// Upload quick.pdf file into 'folder'
HttpResponse response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String contentNodeId = document.getId();
// pause briefly
Thread.sleep(DELAY_IN_MS);
// Get rendition (not created yet) information for node
response = getSingle(getNodeRenditionsUrl(contentNodeId), "imgpreview", 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(RenditionStatus.NOT_CREATED, rendition.getStatus());
// Try to download non-existent rendition (and no placeholder)
Map<String, String> params = new HashMap<>();
params.put("placeholder", "false");
getSingle(getNodeRenditionsUrl(contentNodeId), ("doclib/content"), params, 404);
// Download placeholder instead
params = new HashMap<>();
params.put("placeholder", "true");
response = getSingle(getNodeRenditionsUrl(contentNodeId), ("doclib/content"), params, 200);
assertNotNull(response.getResponseAsBytes());
// Create and get 'imgpreview' rendition
rendition = createAndGetRendition(contentNodeId, "imgpreview");
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);
// Create 'doclib' rendition request
Rendition renditionRequest = new Rendition().setId("doclib");
// Test only if OpenOffice is available
if (isOpenOfficeAvailable()) {
// Create a node without any content
String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId());
post(getNodeRenditionsUrl(emptyContentNodeId), toJsonAsString(renditionRequest), 202);
// Rendition for binary content
String content = "The quick brown fox jumps over the lazy dog.";
file = TempFileProvider.createTempFile(new ByteArrayInputStream(content.getBytes()), getClass().getSimpleName(), ".bin");
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData("binaryFileName", file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document binaryDocument = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
post(getNodeRenditionsUrl(binaryDocument.getId()), toJsonAsString(renditionRequest), 202);
}
//
// -ve Tests
//
// The rendition requested already exists
response = post(getNodeRenditionsUrl(contentNodeId), toJsonAsString(new Rendition().setId("imgpreview")), 409);
ExpectedErrorResponse errorResponse = RestApiUtil.parseErrorResponse(response.getJsonResponse());
assertNotNull(errorResponse);
assertNotNull(errorResponse.getErrorKey());
assertNotNull(errorResponse.getBriefSummary());
assertNotNull(errorResponse.getStackTrace());
assertNotNull(errorResponse.getDescriptionURL());
assertEquals(409, errorResponse.getStatusCode());
// nodeId in the path parameter does not represent a file
post(getNodeRenditionsUrl(folder_Id), toJsonAsString(renditionRequest), 400);
// nodeId in the path parameter does not exist
response = post(getNodeRenditionsUrl(UUID.randomUUID().toString()), toJsonAsString(renditionRequest), 404);
// EntityNotFoundException
errorResponse = RestApiUtil.parseErrorResponse(response.getJsonResponse());
assertNotNull(errorResponse);
assertNotNull(errorResponse.getErrorKey());
assertNotNull(errorResponse.getBriefSummary());
assertNotNull(errorResponse.getStackTrace());
assertNotNull(errorResponse.getDescriptionURL());
assertEquals(404, errorResponse.getStatusCode());
// renditionId is not registered
final String randomRenditionId = "renditionId" + System.currentTimeMillis();
post(getNodeRenditionsUrl(contentNodeId), toJsonAsString(new Rendition().setId(randomRenditionId)), 404);
// renditionId is null
post(getNodeRenditionsUrl(contentNodeId), toJsonAsString(new Rendition().setId(null)), 400);
// renditionId is empty
post(getNodeRenditionsUrl(contentNodeId), toJsonAsString(new Rendition().setId("")), 400);
// -ve test - we do not currently accept multiple create entities
List<Rendition> multipleRenditionRequest = new ArrayList<>(2);
multipleRenditionRequest.add(new Rendition().setId("doclib"));
multipleRenditionRequest.add(new Rendition().setId("imgpreview"));
post(getNodeRenditionsUrl(contentNodeId), toJsonAsString(multipleRenditionRequest), 400);
ThumbnailService thumbnailService = applicationContext.getBean("thumbnailService", ThumbnailService.class);
// Disable thumbnail generation
thumbnailService.setThumbnailsEnabled(false);
try {
// Create multipart request
String txtFileName = "quick-1.txt";
File txtFile = getResourceFile(fileName);
reqBody = MultiPartBuilder.create().setFileData(new FileData(txtFileName, txtFile)).build();
// Upload quick-1.txt file into 'folder'
response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document txtDocument = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Thumbnail generation has been disabled
response = post(getNodeRenditionsUrl(txtDocument.getId()), toJsonAsString(renditionRequest), 501);
errorResponse = RestApiUtil.parseErrorResponse(response.getJsonResponse());
assertNotNull(errorResponse);
assertNotNull(errorResponse.getErrorKey());
assertNotNull(errorResponse.getBriefSummary());
assertNotNull(errorResponse.getStackTrace());
assertNotNull(errorResponse.getDescriptionURL());
assertEquals(501, errorResponse.getStatusCode());
} finally {
thumbnailService.setThumbnailsEnabled(true);
}
}
Aggregations