Search in sources :

Example 1 with ExpectedErrorResponse

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);
    }
}
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

ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)1 ExpectedErrorResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedErrorResponse)1 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)1 Document (org.alfresco.rest.api.tests.client.data.Document)1 Rendition (org.alfresco.rest.api.tests.client.data.Rendition)1 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)1 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)1 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)1 RestApiUtil.toJsonAsString (org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString)1 ThumbnailService (org.alfresco.service.cmr.thumbnail.ThumbnailService)1 Test (org.junit.Test)1