Search in sources :

Example 6 with Rendition

use of org.alfresco.rest.api.tests.client.data.Rendition 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);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) Date(java.util.Date) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) File(java.io.File) Test(org.junit.Test)

Example 7 with Rendition

use of org.alfresco.rest.api.tests.client.data.Rendition in project alfresco-remote-api by Alfresco.

the class DeletedNodesTest method testDownloadRendition.

/**
 * Tests download rendition.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/deleted-nodes/<nodeId>/renditions/<renditionId>/content}
 */
@Test
public void testDownloadRendition() throws Exception {
    setRequestContext(user1);
    // Create a folder within the site document's library
    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 using an existing file
    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 'folder'
    HttpResponse response = post(getNodeChildrenUrl(f1Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String contentNodeId = document.getId();
    Rendition rendition = createAndGetRendition(contentNodeId, "doclib");
    assertNotNull(rendition);
    assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
    deleteNode(contentNodeId);
    // Download rendition - by default with Content-Disposition header
    response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", 200);
    assertNotNull(response.getResponseAsBytes());
    Map<String, String> responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    String contentDisposition = responseHeaders.get("Content-Disposition");
    assertNotNull(contentDisposition);
    assertTrue(contentDisposition.contains("filename=\"doclib\""));
    String contentType = responseHeaders.get("Content-Type");
    assertNotNull(contentType);
    assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
    // Download rendition - without Content-Disposition header
    // (attachment=false)
    Map<String, String> params = new HashMap<>();
    params = Collections.singletonMap("attachment", "false");
    response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", params, 200);
    assertNotNull(response.getResponseAsBytes());
    responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    assertNull(responseHeaders.get("Content-Disposition"));
    contentType = responseHeaders.get("Content-Type");
    assertNotNull(contentType);
    assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
    // Download rendition - with Content-Disposition header
    // (attachment=true) same as default
    params = Collections.singletonMap("attachment", "true");
    response = getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", params, 200);
    assertNotNull(response.getResponseAsBytes());
    responseHeaders = response.getHeaders();
    assertNotNull(responseHeaders);
    String cacheControl = responseHeaders.get("Cache-Control");
    assertNotNull(cacheControl);
    assertFalse(cacheControl.contains("must-revalidate"));
    assertTrue(cacheControl.contains("max-age=31536000"));
    contentDisposition = responseHeaders.get("Content-Disposition");
    assertNotNull(contentDisposition);
    assertTrue(contentDisposition.contains("filename=\"doclib\""));
    contentType = responseHeaders.get("Content-Type");
    assertNotNull(contentType);
    assertTrue(contentType.startsWith(MimetypeMap.MIMETYPE_IMAGE_PNG));
    // Test 304 response - doclib rendition (attachment=true)
    String lastModifiedHeader = responseHeaders.get(LAST_MODIFIED_HEADER);
    assertNotNull(lastModifiedHeader);
    Map<String, String> headers = Collections.singletonMap(IF_MODIFIED_SINCE_HEADER, lastModifiedHeader);
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), "doclib/content", params, headers, 304);
    // -ve tests
    // nodeId in the path parameter does not represent a file
    deleteNode(f1Id);
    getSingle(getDeletedNodeRenditionsUrl(f1Id), "doclib/content", 400);
    // nodeId in the path parameter does not exist
    getSingle(getDeletedNodeRenditionsUrl(UUID.randomUUID().toString()), "doclib/content", 404);
    // renditionId in the path parameter is not registered/available
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis() + "/content"), 404);
    // The rendition does not exist, a placeholder is not available and the
    // placeholder parameter has a value of "true"
    params = Collections.singletonMap("placeholder", "true");
    getSingle(getDeletedNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis() + "/content"), params, 404);
}
Also used : MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) File(java.io.File) Date(java.util.Date) Test(org.junit.Test)

Example 8 with Rendition

use of org.alfresco.rest.api.tests.client.data.Rendition in project alfresco-remote-api by Alfresco.

the class AbstractBaseApiTest method createAndGetRendition.

protected Rendition createAndGetRendition(String sourceNodeId, String renditionId) throws Exception {
    Rendition renditionRequest = new Rendition();
    renditionRequest.setId(renditionId);
    int retryCount = 0;
    while (retryCount < MAX_RETRY) {
        try {
            HttpResponse res = post(getNodeRenditionsUrl(sourceNodeId), toJsonAsString(renditionRequest), 202);
            assertNull(res.getJsonResponse());
            break;
        } catch (AssertionError ex) {
            // If no transformer is currently available,
            // wait for 'PAUSE_TIME' and try again.
            retryCount++;
            System.out.println("waitAndGetRendition: " + retryCount);
            Thread.sleep(PAUSE_TIME);
        }
    }
    return waitAndGetRendition(sourceNodeId, renditionId);
}
Also used : Rendition(org.alfresco.rest.api.tests.client.data.Rendition) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse)

Example 9 with Rendition

use of org.alfresco.rest.api.tests.client.data.Rendition in project alfresco-remote-api by Alfresco.

the class RenditionsTest method testListNodeRenditions.

/**
 * Tests get node renditions.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/renditions}
 */
@Test
public void testListNodeRenditions() 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();
    Paging paging = getPaging(0, 50);
    // List all available renditions (includes those that have been created and those that are yet to be created)
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, 200);
    List<Rendition> renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 5);
    Rendition docLib = getRendition(renditions, "doclib");
    assertNotNull(docLib);
    assertEquals(RenditionStatus.NOT_CREATED, docLib.getStatus());
    ContentInfo contentInfo = docLib.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
    assertEquals("PNG Image", contentInfo.getMimeTypeName());
    assertNull(contentInfo.getEncoding());
    assertNull(contentInfo.getSizeInBytes());
    // Add a filter to select the renditions based on the given status
    Map<String, String> params = new HashMap<>(1);
    params.put("where", "(status='NOT_CREATED')");
    // List only the NOT_CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 5);
    params.put("where", "(status='CREATED')");
    // List only the CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals("There is no rendition created yet.", 0, renditions.size());
    // Test paging
    // SkipCount=0,MaxItems=2
    paging = getPaging(0, 2);
    // List all available renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals(2, renditions.size());
    ExpectedPaging expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
    assertEquals(2, expectedPaging.getCount().intValue());
    assertEquals(0, expectedPaging.getSkipCount().intValue());
    assertEquals(2, expectedPaging.getMaxItems().intValue());
    assertTrue(expectedPaging.getTotalItems() >= 5);
    assertTrue(expectedPaging.getHasMoreItems());
    // SkipCount=2,MaxItems=3
    paging = getPaging(2, 3);
    // List all available renditions
    response = getAll(getNodeRenditionsUrl(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(2, expectedPaging.getSkipCount().intValue());
    assertEquals(3, expectedPaging.getMaxItems().intValue());
    assertTrue(expectedPaging.getTotalItems() >= 5);
    // Create 'doclib' rendition
    createAndGetRendition(contentNodeId, docLib.getId());
    // List all available renditions (includes those that have been created and those that are yet to be created)
    paging = getPaging(0, 50);
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() >= 5);
    docLib = getRendition(renditions, "doclib");
    assertNotNull(docLib);
    assertEquals(RenditionStatus.CREATED, docLib.getStatus());
    contentInfo = docLib.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
    assertEquals("PNG Image", contentInfo.getMimeTypeName());
    assertNotNull(contentInfo.getEncoding());
    assertTrue(contentInfo.getSizeInBytes() > 0);
    // List only the CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertEquals("Should've only returned the 'doclib' rendition.", 1, renditions.size());
    params.put("where", "(status='NOT_CREATED')");
    // List only the NOT_CREATED renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(renditions.size() > 0);
    docLib = getRendition(renditions, "doclib");
    assertNull("'doclib' rendition has already been created.", docLib);
    // Test returned renditions are ordered (natural sort order)
    // List all renditions
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(Ordering.natural().isOrdered(renditions));
    // Try again to make sure the ordering wasn't coincidental
    response = getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 200);
    renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    assertTrue(Ordering.natural().isOrdered(renditions));
    // nodeId in the path parameter does not represent a file
    getAll(getNodeRenditionsUrl(folder_Id), paging, params, 400);
    // nodeId in the path parameter does not exist
    getAll(getNodeRenditionsUrl(UUID.randomUUID().toString()), paging, params, 404);
    // Create a node without any content
    String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId());
    getAll(getNodeRenditionsUrl(emptyContentNodeId), paging, params, 200);
    // Invalid status value
    params.put("where", "(status='WRONG')");
    getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 400);
    // Invalid filter (only 'status' is supported)
    params.put("where", "(id='doclib')");
    getAll(getNodeRenditionsUrl(contentNodeId), paging, params, 400);
}
Also used : HashMap(java.util.HashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) RestApiUtil.toJsonAsString(org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString) Document(org.alfresco.rest.api.tests.client.data.Document) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test)

Example 10 with Rendition

use of org.alfresco.rest.api.tests.client.data.Rendition 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);
    }
}
Also used : ExpectedErrorResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedErrorResponse) ThumbnailService(org.alfresco.service.cmr.thumbnail.ThumbnailService) HashMap(java.util.HashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) RestApiUtil.toJsonAsString(org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString) Document(org.alfresco.rest.api.tests.client.data.Document) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test)

Aggregations

HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)12 Rendition (org.alfresco.rest.api.tests.client.data.Rendition)12 File (java.io.File)10 Document (org.alfresco.rest.api.tests.client.data.Document)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)8 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)8 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)6 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)6 RestApiUtil.toJsonAsString (org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString)6 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)5 Date (java.util.Date)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 QuickShareLink (org.alfresco.rest.api.model.QuickShareLink)2 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)2 QuickShareLinkEntityResource (org.alfresco.rest.api.quicksharelinks.QuickShareLinkEntityResource)2 Folder (org.alfresco.rest.api.tests.client.data.Folder)2 ThumbnailService (org.alfresco.service.cmr.thumbnail.ThumbnailService)2 InputStream (java.io.InputStream)1