Search in sources :

Example 46 with HttpResponse

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

the class NodeVersionsApiTest method testCreateEmptyFileVersionUpdate.

/**
 * Tests api when uploading a file and then updating with a new version
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 *
 * <p>PUT:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
 *
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/versions}
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/versions/<versionId>}
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/versions/<versionId>/content}
 */
@Test
public void testCreateEmptyFileVersionUpdate() throws Exception {
    // As user 1 ...
    setRequestContext(user1);
    String myFolderNodeId = getMyNodeId();
    // create folder
    String f1Id = createFolder(myFolderNodeId, "f1").getId();
    try {
        // create "empty" content node
        Node n = new Node();
        n.setName("d1");
        n.setNodeType(TYPE_CM_CONTENT);
        HttpResponse response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(n), 201);
        String d1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
        response = getSingle(URL_NODES, d1Id, 200);
        Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertFalse(nodeResp.getAspectNames().contains("cm:versionable"));
        Paging paging = getPaging(0, 100);
        // empty list - before
        response = getAll(getNodeVersionsUrl(d1Id), paging, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // note: we do not disallow listing version history on non-content node - however currently no API method to version say a folder
        response = getAll(getNodeVersionsUrl(f1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // Update the empty node's content a few times (before/without versioning)
        int cntBefore = 2;
        int verCnt = 1;
        String textContentSuffix = "The quick brown fox jumps over the lazy dog ";
        for (int i = 1; i <= cntBefore; i++) {
            String content = textContentSuffix + verCnt;
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
            File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
            PublicApiHttpClient.BinaryPayload payload = new PublicApiHttpClient.BinaryPayload(txtFile);
            putBinary(getNodeContentUrl(d1Id), payload, null, null, 200);
            verCnt++;
            response = getSingle(URL_NODES, d1Id, 200);
            nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
            assertFalse(nodeResp.getAspectNames().contains("cm:versionable"));
            response = getAll(getNodeVersionsUrl(d1Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(0, nodes.size());
        }
        // Enable versioning - done here by adding versionable aspect
        // note: alternatively can use version params ("comment" &/or "majorVersion") on update (see separate test below)
        Node nodeUpdate = new Node();
        nodeUpdate.setAspectNames(Collections.singletonList("cm:versionable"));
        put(URL_NODES, d1Id, toJsonAsStringNonNull(nodeUpdate), null, 200);
        String versionId = "1.0";
        Map<String, String> params = new HashMap<>();
        params.put("include", "properties");
        response = getAll(getNodeVersionsUrl(d1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(versionId, nodes.get(0).getProperties().get("cm:versionLabel"));
        assertEquals("MAJOR", nodes.get(0).getProperties().get("cm:versionType"));
        // get version info
        response = getSingle(getNodeVersionsUrl(d1Id), versionId, null, 200);
        Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals(versionId, node.getProperties().get("cm:versionLabel"));
        assertEquals("MAJOR", node.getProperties().get("cm:versionType"));
        // Update the content a few more times (after/with versioning)
        int cntAfter = 3;
        for (int i = 1; i <= cntAfter; i++) {
            // Update again
            String content = textContentSuffix + verCnt;
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
            File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
            PublicApiHttpClient.BinaryPayload payload = new PublicApiHttpClient.BinaryPayload(txtFile);
            putBinary(getNodeContentUrl(d1Id), payload, null, null, 200);
            verCnt++;
            // get version info
            versionId = "1." + i;
            response = getSingle(getNodeVersionsUrl(d1Id), versionId, null, 200);
            node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
            assertEquals(versionId, node.getProperties().get("cm:versionLabel"));
            assertEquals("MINOR", node.getProperties().get("cm:versionType"));
            response = getAll(getNodeVersionsUrl(d1Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(i + 1, nodes.size());
        }
        int totalVerCnt = cntAfter + 1;
        int minorVersion = totalVerCnt - 1;
        verCnt = cntBefore + cntAfter;
        params = new HashMap<>();
        params.put("include", "properties");
        response = getAll(getNodeVersionsUrl(d1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(totalVerCnt, nodes.size());
        checkVersionHistoryAndContent(d1Id, nodes, verCnt, textContentSuffix, null, 1, minorVersion, false);
        // delete to trashcan/archive ...
        deleteNode(d1Id);
        // -ve tests
        {
            getSingle(NodesEntityResource.class, d1Id, null, 404);
            getAll(getNodeVersionsUrl(d1Id), null, null, 404);
        }
        // ... and then restore again
        post(URL_DELETED_NODES + "/" + d1Id + "/restore", null, null, 200);
        response = getAll(getNodeVersionsUrl(d1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(cntAfter + 1, nodes.size());
        // 
        // -ve tests
        // 
        {
            setRequestContext(null);
            // -ve test - unauthenticated - belts-and-braces ;-)
            getAll(getNodeVersionsUrl(d1Id), paging, null, 401);
            setRequestContext(user1);
            // -ve test - unknown node
            getAll(getNodeVersionsUrl("dummy"), paging, null, 404);
            // 
            // -ve tests for non-versionable node with no version history (could be content, folder or some other node type)
            // 
            // folder node - no version history
            String f99Id = createFolder(f1Id, "f99").getId();
            getSingle(getNodeVersionsUrl(f99Id), "1.0", null, 404);
            getSingle(getNodeVersionsUrl(f99Id), "1.0/content", null, 404);
            // content node - no version history
            n = new Node();
            n.setName("z1");
            n.setNodeType(TYPE_CM_CONTENT);
            response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(n), 201);
            String z1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
            getSingle(getNodeVersionsUrl(z1Id), "1.0", null, 404);
            getSingle(getNodeVersionsUrl(z1Id), "1.0/content", null, 404);
        }
    } finally {
        // some cleanup
        setRequestContext(user1);
        deleteNode(f1Id, true, 204);
    }
}
Also used : HashMap(java.util.HashMap) Node(org.alfresco.rest.api.tests.client.data.Node) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) PublicApiHttpClient(org.alfresco.rest.api.tests.client.PublicApiHttpClient) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 47 with HttpResponse

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

the class NodeVersionsApiTest method updateFileVersions.

/**
 * This test helper method uses "update binary content" to create one or more new versions. The file must already exist.
 *
 * @param userId
 * @param contentNodeId
 * @param cnt
 * @param textContentPrefix
 * @param verCnt
 * @param majorVersion
 * @param currentVersionLabel
 * @return
 * @throws Exception
 */
private String updateFileVersions(String userId, String contentNodeId, int cnt, String textContentPrefix, int verCnt, Boolean majorVersion, String currentVersionLabel) throws Exception {
    String[] parts = currentVersionLabel.split("\\.");
    int majorVer = new Integer(parts[0]).intValue();
    int minorVer = new Integer(parts[1]).intValue();
    Map<String, String> params = new HashMap<>();
    params.put(Nodes.PARAM_OVERWRITE, "true");
    if (majorVersion != null) {
        params.put(Nodes.PARAM_VERSION_MAJOR, majorVersion.toString());
    } else {
        majorVersion = false;
    }
    if (majorVersion) {
        minorVer = 0;
    }
    for (int i = 1; i <= cnt; i++) {
        if (majorVersion) {
            majorVer++;
        } else {
            minorVer++;
        }
        verCnt++;
        params.put("comment", "my version " + verCnt);
        String textContent = textContentPrefix + verCnt;
        currentVersionLabel = majorVer + "." + minorVer;
        // Update
        ByteArrayInputStream inputStream = new ByteArrayInputStream(textContent.getBytes());
        File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
        PublicApiHttpClient.BinaryPayload payload = new PublicApiHttpClient.BinaryPayload(txtFile);
        HttpResponse response = putBinary(getNodeContentUrl(contentNodeId), payload, null, params, 200);
        Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertTrue(nodeResp.getAspectNames().contains("cm:versionable"));
        assertNotNull(nodeResp.getProperties());
        assertEquals(currentVersionLabel, nodeResp.getProperties().get("cm:versionLabel"));
        assertEquals((majorVersion ? "MAJOR" : "MINOR"), nodeResp.getProperties().get("cm:versionType"));
        // double-check - get version node info
        response = getSingle(getNodeVersionsUrl(contentNodeId), currentVersionLabel, null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals(currentVersionLabel, nodeResp.getProperties().get("cm:versionLabel"));
        assertEquals((majorVersion ? "MAJOR" : "MINOR"), nodeResp.getProperties().get("cm:versionType"));
    }
    return currentVersionLabel;
}
Also used : HashMap(java.util.HashMap) Node(org.alfresco.rest.api.tests.client.data.Node) PublicApiHttpClient(org.alfresco.rest.api.tests.client.PublicApiHttpClient) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File)

Example 48 with HttpResponse

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

the class NodeVersionsApiTest method testUploadFileVersionCreateWithOverwrite.

/**
 * Test version creation when uploading files (via multi-part/form-data with overwrite=true)
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 *
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/versions}
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/versions/<versionId>}
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/versions/<versionId>/content}
 */
@Test
public void testUploadFileVersionCreateWithOverwrite() throws Exception {
    setRequestContext(user1);
    String myFolderNodeId = getMyNodeId();
    // create folder
    String f1Id = createFolder(myFolderNodeId, "f1").getId();
    try {
        int verCnt = 1;
        String versionLabel = "1.0";
        // upload text file - versioning is currently auto enabled on upload (create file via multi-part/form-data)
        String textContentSuffix = "The quick brown fox jumps over the lazy dog ";
        String contentName = "content-1-" + System.currentTimeMillis();
        String content = textContentSuffix + verCnt;
        // create first version (ie. 1.0)
        Document documentResp = createTextFile(f1Id, contentName, content, "UTF-8", null);
        String docId = documentResp.getId();
        assertTrue(documentResp.getAspectNames().contains("cm:versionable"));
        assertNotNull(documentResp.getProperties());
        assertEquals(versionLabel, documentResp.getProperties().get("cm:versionLabel"));
        Map<String, String> params = null;
        // create some minor versions (note: majorVersion=null) (ie. 1.1, 1.2, 1.3)
        int cnt = 3;
        versionLabel = uploadTextFileVersions(user1, f1Id, contentName, cnt, textContentSuffix, verCnt, null, versionLabel).getFirst();
        verCnt = verCnt + cnt;
        // create some major versions  (ie. 2.0, 3.0)
        cnt = 2;
        versionLabel = uploadTextFileVersions(user1, f1Id, contentName, cnt, textContentSuffix, verCnt, true, versionLabel).getFirst();
        verCnt = verCnt + cnt;
        // create some more minor versions (ie. 3.1, 3.2, 3.3)
        cnt = 3;
        versionLabel = uploadTextFileVersions(user1, f1Id, contentName, cnt, textContentSuffix, verCnt, false, versionLabel).getFirst();
        verCnt = verCnt + cnt;
        assertEquals("3.3", versionLabel);
        assertEquals(9, verCnt);
        {
            // -ve test
            params = new HashMap<>();
            params.put(Nodes.PARAM_OVERWRITE, "true");
            params.put(Nodes.PARAM_AUTO_RENAME, "true");
            createTextFile(myFolderNodeId, contentName, content, "UTF-8", params, 400);
        }
        // remove versionable aspect
        List<String> aspectNames = documentResp.getAspectNames();
        aspectNames.remove("cm:versionable");
        Document dUpdate = new Document();
        dUpdate.setAspectNames(aspectNames);
        HttpResponse response = put(URL_NODES, docId, toJsonAsStringNonNull(dUpdate), null, 200);
        documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
        assertFalse(documentResp.getAspectNames().contains("cm:versionable"));
        // no properties (ie. no "cm:versionLabel")
        assertNull(documentResp.getProperties());
        // check no version history
        response = getAll(getNodeVersionsUrl(docId), null, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        {
            // -ve test - do not allow overwrite (using POST upload) if the file is not versionable
            cnt++;
            content = textContentSuffix + cnt;
            params = new HashMap<>();
            params.put(Nodes.PARAM_OVERWRITE, "true");
            createTextFile(f1Id, contentName, content, "UTF-8", params, 409);
        }
        // we do allow update of binary content with no versioning (after removing versionable)
        textContentSuffix = "Amazingly few discotheques provide jukeboxes ";
        for (int i = 1; i <= 4; i++) {
            content = textContentSuffix + i;
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
            File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
            PublicApiHttpClient.BinaryPayload payload = new PublicApiHttpClient.BinaryPayload(txtFile);
            putBinary(getNodeContentUrl(docId), payload, null, null, 200);
        }
        // check no version history
        response = getAll(getNodeVersionsUrl(docId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
    } finally {
        // some cleanup
        setRequestContext(user1);
        deleteNode(f1Id, true, 204);
    }
}
Also used : HashMap(java.util.HashMap) Node(org.alfresco.rest.api.tests.client.data.Node) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) PublicApiHttpClient(org.alfresco.rest.api.tests.client.PublicApiHttpClient) Document(org.alfresco.rest.api.tests.client.data.Document) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 49 with HttpResponse

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

the class NodeVersionsApiTest method checkVersionHistoryAndContent.

private void checkVersionHistoryAndContent(String docId, List<Node> nodesWithProps, int verCnt, String textContentSuffix, String verCommentSuffix, int majorVersion, int minorVersion, boolean majorVersions) throws Exception {
    String versionId = null;
    // also download and check the versioned content
    for (Node versionNode : nodesWithProps) {
        versionId = majorVersion + "." + minorVersion;
        assertEquals(versionId, versionNode.getId());
        assertEquals(versionId, versionNode.getProperties().get("cm:versionLabel"));
        if (versionId.endsWith(".0")) {
            assertEquals("MAJOR", versionNode.getProperties().get("cm:versionType"));
        } else {
            assertEquals("MINOR", versionNode.getProperties().get("cm:versionType"));
        }
        assertNull(versionNode.getParentId());
        assertNull(versionNode.getCreatedByUser());
        assertNull(versionNode.getCreatedAt());
        assertEquals((verCommentSuffix != null ? verCommentSuffix + verCnt : null), versionNode.getVersionComment());
        // Download version content - by default with Content-Disposition header
        HttpResponse response = getSingle(getNodeVersionsUrl(docId), versionId + "/content", null, 200);
        String textContent = response.getResponse();
        assertEquals(textContentSuffix + verCnt, textContent);
        if (majorVersions) {
            majorVersion--;
        } else {
            minorVersion--;
        }
        verCnt--;
    }
}
Also used : Node(org.alfresco.rest.api.tests.client.data.Node) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse)

Example 50 with HttpResponse

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

the class NodeVersionsApiTest method testVersionHistoryPaging.

/**
 * Test version history paging.
 *
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/versions}
 */
@Test
public void testVersionHistoryPaging() throws Exception {
    // create folder
    setRequestContext(user1);
    String f1Id = null;
    try {
        f1Id = createFolder(Nodes.PATH_MY, "testVersionHistoryPaging-f1").getId();
        String textContentSuffix = "Amazingly few discotheques provide jukeboxes ";
        String contentName = "content-1";
        int cnt = 6;
        Pair<String, String> pair = uploadTextFileVersions(user1, f1Id, contentName, cnt, textContentSuffix, 0, null, null);
        String versionLabel = pair.getFirst();
        String docId = pair.getSecond();
        // 1.0, 1.1, ... 1.5
        assertEquals("1.5", versionLabel);
        // check version history count (note: no paging => default max items => 100)
        HttpResponse response = getAll(getNodeVersionsUrl(docId), null, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(cnt, nodes.size());
        // Sanity Test paging
        // SkipCount=0,MaxItems=2
        Paging paging = getPaging(0, 2);
        response = getAll(getNodeVersionsUrl(docId), paging, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.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() >= cnt);
        assertTrue(expectedPaging.getHasMoreItems());
        // SkipCount=2,MaxItems=3
        paging = getPaging(2, 3);
        response = getAll(getNodeVersionsUrl(docId), paging, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(3, nodes.size());
        expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
        assertEquals(3, expectedPaging.getCount().intValue());
        assertEquals(2, expectedPaging.getSkipCount().intValue());
        assertEquals(3, expectedPaging.getMaxItems().intValue());
        assertTrue(expectedPaging.getTotalItems() >= cnt);
    } finally {
        if (f1Id != null) {
            // some cleanup
            setRequestContext(user1);
            deleteNode(f1Id, true, 204);
        }
    }
}
Also used : Node(org.alfresco.rest.api.tests.client.data.Node) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Aggregations

HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)162 Test (org.junit.Test)114 HashMap (java.util.HashMap)59 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)50 Document (org.alfresco.rest.api.tests.client.data.Document)49 Node (org.alfresco.rest.api.tests.client.data.Node)49 LinkedHashMap (java.util.LinkedHashMap)31 ArrayList (java.util.ArrayList)29 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)27 Folder (org.alfresco.rest.api.tests.client.data.Folder)26 File (java.io.File)25 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)25 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)22 CustomModel (org.alfresco.rest.api.model.CustomModel)16 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)15 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)15 JSONObject (org.json.simple.JSONObject)15 NodeRef (org.alfresco.service.cmr.repository.NodeRef)14 Map (java.util.Map)13 CustomAspect (org.alfresco.rest.api.model.CustomAspect)13