Search in sources :

Example 41 with Document

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

the class QueriesNodesApiTest method testLiveSearchNodes_FTS_and_Metadata.

/**
 * Tests basic api for nodes live search - metadata (name, title, description) &/or full text search of file/content
 *
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/queries/nodes}
 */
@Test
public void testLiveSearchNodes_FTS_and_Metadata() throws Exception {
    setRequestContext(user1);
    int f1Count = 5;
    List<String> f1NodeIds = new ArrayList<>(f1Count);
    int f2Count = 3;
    List<String> f2NodeIds = new ArrayList<>(f2Count);
    int f3Count = 4;
    List<String> f3NodeIds = new ArrayList<>(f3Count);
    int totalCount = f1Count + f2Count + f3Count;
    List<String> allIds = new ArrayList<>(totalCount);
    String testTerm = "abc123basic";
    try {
        // As user 1 ...
        Paging paging = getPaging(0, 100);
        Map<String, String> params = new HashMap<>(1);
        params.put(Queries.PARAM_TERM, testTerm);
        // We can no longer check the assumption that there is a clean repo (ie. no nodes with search term 'abc123')
        // in the same way as before which used search - Generally ok not to check.
        String myFolderNodeId = getMyNodeId();
        String f1Id = createFolder(myFolderNodeId, "folder 1").getId();
        String f2Id = createFolder(myFolderNodeId, "folder 2").getId();
        String name = "name";
        String title = "title";
        String descrip = "descrip";
        String folderNameSuffix = " " + testTerm + " folder";
        String txtSuffix = ".txt";
        Map<String, String> idNameMap = new HashMap<>();
        Map<String, List<String>> textIdMap = new HashMap<>();
        int nameIdx = f1Count;
        for (int i = 1; i <= f1Count; i++) {
            // create doc - in folder 1
            String contentText = "f1 " + testTerm + " test document " + user1 + " document " + i;
            String num = String.format("%05d", nameIdx);
            String docName = name + num + name + txtSuffix;
            Map<String, String> docProps = new HashMap<>(2);
            docProps.put("cm:title", title + num + title);
            docProps.put("cm:description", descrip + num + descrip);
            Document doc = createTextFile(f1Id, docName, contentText, "UTF-8", docProps);
            f1NodeIds.add(doc.getId());
            idNameMap.put(doc.getId(), docName);
            addTo(textIdMap, name + num + name, doc.getId());
            addTo(textIdMap, docName, doc.getId());
            addTo(textIdMap, docProps.get("cm:title"), doc.getId());
            addTo(textIdMap, docProps.get("cm:description"), doc.getId());
            nameIdx--;
        }
        nameIdx = f2Count;
        for (int i = 1; i <= f2Count; i++) {
            // create doc - in folder 2
            String contentText = "f2 " + testTerm + " test document";
            String num = String.format("%05d", nameIdx);
            String docName = name + num + name + txtSuffix;
            Map<String, String> props = new HashMap<>(2);
            props.put("cm:title", title + num + title);
            props.put("cm:description", descrip + num + descrip);
            Document doc = createTextFile(f2Id, docName, contentText, "UTF-8", props);
            f2NodeIds.add(doc.getId());
            idNameMap.put(doc.getId(), docName);
            addTo(textIdMap, name + num + name, doc.getId());
            addTo(textIdMap, docName, doc.getId());
            addTo(textIdMap, props.get("cm:title"), doc.getId());
            addTo(textIdMap, props.get("cm:description"), doc.getId());
            nameIdx--;
        }
        nameIdx = f3Count;
        for (int i = 1; i <= f3Count; i++) {
            // create folders - in folder 3
            String num = String.format("%05d", nameIdx);
            String folderName = name + num + name + folderNameSuffix;
            Map<String, Object> props = new HashMap<>(2);
            props.put("cm:title", title + num + title);
            props.put("cm:description", descrip + num + descrip);
            Node node = createFolder(myFolderNodeId, folderName, props);
            f3NodeIds.add(node.getId());
            idNameMap.put(node.getId(), folderName);
            addTo(textIdMap, name + num + name, node.getId());
            addTo(textIdMap, folderName, node.getId());
            addTo(textIdMap, (String) props.get("cm:title"), node.getId());
            addTo(textIdMap, (String) props.get("cm:description"), node.getId());
            nameIdx--;
        }
        allIds.addAll(idNameMap.keySet());
        // 
        // find nodes
        // 
        // Search hits based on FTS (content) and also name (in case of folder nodes)
        checkApiCall(null, DEAFULT_QUERY, testTerm, null, null, null, null, paging, 200, null, true, allIds);
        // Search - include optional fields - eg. aspectNames, properties, path, isLink
        checkApiCall(null, DEAFULT_QUERY, testTerm, null, null, "aspectNames,properties,path,isLink", null, paging, 200, null, false, allIds);
        // Search hits restricted by node type
        checkApiCall(null, NODE_TYPE_QUERY, testTerm, "cm:folder", null, null, null, paging, 200, null, null, f3NodeIds);
        // Search - with -root- as the root node (for path-based / in-tree search)
        checkApiCall("/app:company_home", ROOT_NODE_QUERY_SUFFIX, testTerm, null, Nodes.PATH_ROOT, null, null, paging, 200, null, null, allIds);
        // Search - with -shared- as the root node (for path-based / in-tree search)
        checkApiCall("/app:company_home/app:shared", ROOT_NODE_QUERY_SUFFIX, testTerm, null, Nodes.PATH_SHARED, null, null, paging, 200, null, null, Collections.EMPTY_LIST);
        // Search - with folder 1 as root node (for path-based / in-tree search)
        checkApiCall("/app:company_home/app:user_homes/cm:user1-[0-9]*_x...._org.alfresco.rest.api.tests.queriesnodesapitest/cm:folder_x...._1", ROOT_NODE_QUERY_SUFFIX, testTerm, null, f1Id, null, null, paging, 200, null, null, f1NodeIds);
        // Search - with folder 2 as the root node (for path-based / in-tree search)
        checkApiCall("/app:company_home/app:user_homes/cm:user1-[0-9]*_x...._org.alfresco.rest.api.tests.queriesnodesapitest/cm:folder_x...._2", ROOT_NODE_QUERY_SUFFIX, testTerm, null, f2Id, null, null, paging, 200, null, null, f2NodeIds);
        // Search - with -my- as the root node (for path-based / in-tree search)
        checkApiCall("/app:company_home/app:user_homes/cm:user1-[0-9]*_x...._org.alfresco.rest.api.tests.queriesnodesapitest", ROOT_NODE_QUERY_SUFFIX, name + "*", null, Nodes.PATH_MY, null, null, paging, 200, null, null, allIds);
        // Search hits based on cm:name
        String term = name + String.format("%05d", 1) + name;
        List<String> ids = textIdMap.get(term);
        assertEquals(term, 3, ids.size());
        List<Node> nodes = checkApiCall(null, DEAFULT_QUERY, "\"" + term + "\"", null, null, null, null, paging, 200, null, null, ids);
        for (Node node : nodes) {
            if (node.getIsFolder()) {
                assertEquals(term + folderNameSuffix, node.getName());
            } else {
                assertEquals(term + txtSuffix, node.getName());
            }
        }
        // search for name with . (eg. ".txt") without double quotes
        term = name + String.format("%05d", 1) + name + txtSuffix;
        ids = textIdMap.get(term);
        assertEquals(term, 2, ids.size());
        checkApiCall(null, DEAFULT_QUERY, term, null, null, null, null, paging, 200, null, null, ids);
        // search for name with . (eg. ".txt") with double quotes
        term = name + String.format("%05d", 1) + name + txtSuffix;
        ids = textIdMap.get(term);
        assertEquals(term, 2, ids.size());
        checkApiCall(null, DEAFULT_QUERY, "\"" + term + "\"", null, null, null, null, paging, 200, null, null, ids);
        // Search hits based on cm:title
        term = title + String.format("%05d", 2) + title;
        ids = textIdMap.get(term);
        assertEquals(term, 3, ids.size());
        nodes = checkApiCall(null, DEAFULT_QUERY, "\"" + term + "\"", null, null, "properties", null, paging, 200, null, null, ids);
        assertEquals(term, nodes.get(0).getProperties().get("cm:title"));
        assertEquals(term, nodes.get(1).getProperties().get("cm:title"));
        assertEquals(term, nodes.get(2).getProperties().get("cm:title"));
        // Search hits based on cm:description
        term = descrip + String.format("%05d", 3) + descrip;
        ids = textIdMap.get(term);
        assertEquals(term, 3, ids.size());
        nodes = checkApiCall(null, DEAFULT_QUERY, "\"" + term + "\"", null, null, "properties", null, paging, 200, null, null, ids);
        assertEquals(term, nodes.get(0).getProperties().get("cm:description"));
        assertEquals(term, nodes.get(1).getProperties().get("cm:description"));
        assertEquals(term, nodes.get(2).getProperties().get("cm:description"));
        // -ve test - no params (ie. no term)
        getAll(URL_QUERIES_LSN, paging, null, 400);
        // -ve test - no term
        params = new HashMap<>(1);
        params.put(Queries.PARAM_ROOT_NODE_ID, f1Id);
        getAll(URL_QUERIES_LSN, paging, params, 400);
        // -ve test - unknown root node id
        params = new HashMap<>(2);
        params.put(Queries.PARAM_TERM, "abc");
        params.put(Queries.PARAM_ROOT_NODE_ID, "dummy");
        getAll(URL_QUERIES_LSN, paging, params, 404);
        // -ve test - unknown node type
        params = new HashMap<>(2);
        params.put(Queries.PARAM_TERM, "abc");
        params.put(Queries.PARAM_NODE_TYPE, "cm:dummy");
        getAll(URL_QUERIES_LSN, paging, params, 400);
        // -ve test - term too short
        params = new HashMap<>(1);
        params.put(Queries.PARAM_TERM, "ab");
        getAll(URL_QUERIES_LSN, paging, params, 400);
        // -ve test - term is still too short
        params = new HashMap<>(1);
        params.put(Queries.PARAM_TERM, "  \"a b *\"  ");
        getAll(URL_QUERIES_LSN, paging, params, 400);
        // -ve test - unauthenticated - belts-and-braces ;-)
        setRequestContext(null);
        getAll(URL_QUERIES_LSN, paging, params, 401);
    } finally {
        // some cleanup
        setRequestContext(user1);
        for (String docId : allIds) {
            deleteNode(docId, true, 204);
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) Node(org.alfresco.rest.api.tests.client.data.Node) ArrayList(java.util.ArrayList) Document(org.alfresco.rest.api.tests.client.data.Document) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 42 with Document

use of org.alfresco.rest.api.tests.client.data.Document 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 43 with Document

use of org.alfresco.rest.api.tests.client.data.Document 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)

Example 44 with Document

use of org.alfresco.rest.api.tests.client.data.Document 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 45 with Document

use of org.alfresco.rest.api.tests.client.data.Document 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

Document (org.alfresco.rest.api.tests.client.data.Document)60 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)49 Test (org.junit.Test)46 HashMap (java.util.HashMap)34 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)30 Folder (org.alfresco.rest.api.tests.client.data.Folder)25 LinkedHashMap (java.util.LinkedHashMap)22 File (java.io.File)20 ArrayList (java.util.ArrayList)20 Node (org.alfresco.rest.api.tests.client.data.Node)19 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)18 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)15 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)14 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)13 NodePermissions (org.alfresco.rest.api.model.NodePermissions)12 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)11 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)11 Rendition (org.alfresco.rest.api.tests.client.data.Rendition)10 JSONObject (org.json.simple.JSONObject)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6