Search in sources :

Example 11 with Rendition

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

the class RenditionsTest method testCreateRenditionOnUpload.

/**
 * Tests create rendition when on upload/create of a file
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/children}
 */
@Test
public void testCreateRenditionOnUpload() throws Exception {
    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 renditionName = "doclib";
    String fileName = "quick.pdf";
    File file = getResourceFile(fileName);
    MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setRenditions(Collections.singletonList(renditionName)).build();
    // Upload quick.pdf file into 'folder' - including request to create 'doclib' thumbnail
    HttpResponse response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String contentNodeId = document.getId();
    // wait and check that rendition is created ...
    Rendition rendition = waitAndGetRendition(contentNodeId, renditionName);
    assertNotNull(rendition);
    assertEquals(RenditionStatus.CREATED, rendition.getStatus());
    Map<String, String> params = new HashMap<>();
    params.put("placeholder", "false");
    response = getSingle(getNodeRenditionsUrl(contentNodeId), ("doclib/content"), params, 200);
    assertNotNull(response.getResponseAsBytes());
    if (isOpenOfficeAvailable()) {
        // Create multipart request - Word doc file
        renditionName = "doclib";
        fileName = "farmers_markets_list_2003.doc";
        file = getResourceFile(fileName);
        reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setRenditions(Collections.singletonList(renditionName)).build();
        // Upload file into 'folder' - including request to create 'doclib' thumbnail
        response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
        document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
        contentNodeId = document.getId();
        // wait and check that rendition is created ...
        rendition = waitAndGetRendition(contentNodeId, renditionName);
        assertNotNull(rendition);
        assertEquals(RenditionStatus.CREATED, rendition.getStatus());
    }
    /* RA-834: commented-out since not currently applicable for empty file
        Document d1 = new Document();
        d1.setName("d1.txt");
        d1.setNodeType("cm:content");
        ContentInfo ci = new ContentInfo();
        ci.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
        d1.setContent(ci);

        // create empty file including request to generate a thumbnail
        renditionName = "doclib";
        response = post(getNodeChildrenUrl(folder_Id), userId, toJsonAsStringNonNull(d1), "?renditions="+renditionName, 201);
        Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
        String d1Id = documentResp.getId();

        // wait and check that rendition is created ...
        rendition = waitAndGetRendition(userId, d1Id, renditionName);
        assertNotNull(rendition);
        assertEquals(RenditionStatus.CREATED, rendition.getStatus());
        */
    /*
         * Per RA-1052, the failure of the async request to create a rendition
         * should NOT fail the upload.
         */
    // Currently we do not support multiple rendition requests on create
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setAutoRename(true).setRenditions(Arrays.asList(new String[] { "doclib,imgpreview" })).build();
    post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 400);
    // Unknown rendition
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setAutoRename(true).setRenditions(Arrays.asList(new String[] { "unknown" })).build();
    post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    // ThumbnailService is disabled
    ThumbnailService thumbnailService = applicationContext.getBean("thumbnailService", ThumbnailService.class);
    thumbnailService.setThumbnailsEnabled(false);
    try {
        // Create multipart request
        String txtFileName = "quick-1.txt";
        File txtFile = getResourceFile(fileName);
        reqBody = MultiPartBuilder.create().setFileData(new FileData(txtFileName, txtFile)).setRenditions(Arrays.asList(new String[] { "doclib" })).build();
        post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    } finally {
        thumbnailService.setThumbnailsEnabled(true);
    }
}
Also used : ThumbnailService(org.alfresco.service.cmr.thumbnail.ThumbnailService) HashMap(java.util.HashMap) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) 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) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test)

Example 12 with Rendition

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

the class RenditionsTest method testGetNodeRendition.

/**
 * Tests get node rendition.
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/renditions/<renditionId>}
 */
@Test
public void testGetNodeRendition() 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), "doclib", 200);
    Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
    assertNotNull(rendition);
    assertEquals(RenditionStatus.NOT_CREATED, rendition.getStatus());
    ContentInfo contentInfo = rendition.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
    assertEquals("PNG Image", contentInfo.getMimeTypeName());
    assertNull("Shouldn't have returned the encoding, as the rendition hasn't been created yet.", contentInfo.getEncoding());
    assertNull("Shouldn't have returned the size, as the rendition hasn't been created yet.", contentInfo.getSizeInBytes());
    // Create and get 'doclib' rendition
    rendition = createAndGetRendition(contentNodeId, "doclib");
    assertNotNull(rendition);
    assertEquals(RenditionStatus.CREATED, rendition.getStatus());
    contentInfo = rendition.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_IMAGE_PNG, contentInfo.getMimeType());
    assertEquals("PNG Image", contentInfo.getMimeTypeName());
    assertNotNull(contentInfo.getEncoding());
    assertTrue(contentInfo.getSizeInBytes() > 0);
    // nodeId in the path parameter does not represent a file
    getSingle(getNodeRenditionsUrl(folder_Id), "doclib", 400);
    // nodeId in the path parameter does not exist
    getSingle(getNodeRenditionsUrl(UUID.randomUUID().toString()), "doclib", 404);
    // renditionId in the path parameter is not registered/available
    getSingle(getNodeRenditionsUrl(contentNodeId), ("renditionId" + System.currentTimeMillis()), 404);
    // Create a node without any content. Test only if OpenOffice is available
    if (isOpenOfficeAvailable()) {
        String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId());
        getSingle(getNodeRenditionsUrl(emptyContentNodeId), "doclib", 200);
    }
    // Create multipart request
    String jpgFileName = "quick.jpg";
    File jpgFile = getResourceFile(fileName);
    reqBody = MultiPartBuilder.create().setFileData(new FileData(jpgFileName, jpgFile)).build();
    // Upload quick.jpg file into 'folder'
    response = post(getNodeChildrenUrl(folder_Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document jpgImage = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    String jpgImageNodeId = jpgImage.getId();
    // List all available renditions (includes those that have been created and those that are yet to be created)
    response = getAll(getNodeRenditionsUrl(jpgImageNodeId), getPaging(0, 50), 200);
    List<Rendition> renditions = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Rendition.class);
    // Check there is no pdf rendition is available for the jpg file
    Rendition pdf = getRendition(renditions, "pdf");
    assertNull(pdf);
    // The renditionId (pdf) is registered but it is not applicable for the node's mimeType
    getSingle(getNodeRenditionsUrl(jpgImageNodeId), "pdf", 404);
}
Also used : MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) Rendition(org.alfresco.rest.api.tests.client.data.Rendition) 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) 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