Search in sources :

Example 1 with VersionOptions

use of org.alfresco.rest.api.model.VersionOptions in project alfresco-remote-api by Alfresco.

the class NodeVersionsApiTest method testRevert.

/**
 * Tests revert (ie. promote older version to become the latest/most recent version).
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/versions/<versionId>/revert}
 */
@Test
public void testRevert() throws Exception {
    // As user 1 ...
    setRequestContext(user1);
    String sharedFolderNodeId = getSharedNodeId();
    // create folder
    String f1Id = null;
    try {
        f1Id = createFolder(sharedFolderNodeId, "testRevert-f1-" + System.currentTimeMillis()).getId();
        int majorVersion = 1;
        int minorVersion = 0;
        int verCnt = 1;
        String textContentSuffix = "The quick brown fox jumps over the lazy dog ";
        String contentName = "content " + System.currentTimeMillis();
        String content = textContentSuffix + verCnt;
        String updateVerCommentSuffix = "Update comment ";
        Map<String, String> params = new HashMap<>();
        params.put(Nodes.PARAM_VERSION_COMMENT, updateVerCommentSuffix + verCnt);
        // Upload text file - versioning is currently auto enabled on upload (create file via multi-part/form-data)
        Document documentResp = createTextFile(f1Id, contentName, content, "UTF-8", params);
        String d1Id = documentResp.getId();
        // Update the content
        int updateCnt = 3;
        for (int i = 1; i <= updateCnt; i++) {
            verCnt++;
            minorVersion++;
            // Update
            content = textContentSuffix + verCnt;
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
            File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
            PublicApiHttpClient.BinaryPayload payload = new PublicApiHttpClient.BinaryPayload(txtFile);
            params = new HashMap<>();
            params.put(Nodes.PARAM_VERSION_COMMENT, updateVerCommentSuffix + verCnt);
            putBinary(getNodeContentUrl(d1Id), payload, null, params, 200);
        }
        // check version history count - also get properties so that we can check version label etc
        params = new HashMap<>();
        params.put("include", "properties");
        HttpResponse response = getAll(getNodeVersionsUrl(d1Id), null, params, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(verCnt, nodes.size());
        // check version labels and content
        checkVersionHistoryAndContent(d1Id, nodes, verCnt, textContentSuffix, updateVerCommentSuffix, majorVersion, minorVersion, false);
        int revertMajorVersion = 1;
        int revertMinorVersion = 0;
        String revertVerCommentSuffix = "Revert comment ";
        int revertCnt = 3;
        for (int i = 1; i <= revertCnt; i++) {
            String revertVersionId = revertMajorVersion + "." + revertMinorVersion;
            VersionOptions versionOptions = new VersionOptions();
            versionOptions.setMajorVersion(true);
            versionOptions.setComment(revertVerCommentSuffix + i);
            post(getNodeVersionRevertUrl(d1Id, revertVersionId), toJsonAsStringNonNull(versionOptions), null, 200);
            verCnt++;
            revertMinorVersion++;
            majorVersion++;
        }
        // check version history count - also get properties so that we can check version label etc
        params = new HashMap<>();
        params.put("include", "properties");
        response = getAll(getNodeVersionsUrl(d1Id), null, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(verCnt, nodes.size());
        // check version labels and content - most recently reverted, eg. version labels 4.0, 3.0, 2.0
        List<Node> revertedNodes = nodes.subList(0, revertCnt);
        checkVersionHistoryAndContent(d1Id, revertedNodes, updateCnt, textContentSuffix, revertVerCommentSuffix, majorVersion, 0, true);
        // check version labels and content - the rest of the version history (prior to reverted), eg. version labels 1.3, 1.2, 1.1, 1.0
        minorVersion = 3;
        List<Node> originalUpdatedNodes = nodes.subList(revertCnt, nodes.size());
        checkVersionHistoryAndContent(d1Id, originalUpdatedNodes, updateCnt + 1, textContentSuffix, updateVerCommentSuffix, 1, minorVersion, false);
        // Currently, we also allow the most recent version to be reverted (ie. not disallowed by underlying VersionService)
        post(getNodeVersionRevertUrl(d1Id, majorVersion + ".0"), "{}", null, 200);
        {
            setRequestContext(null);
            // -ve test - unauthenticated - belts-and-braces ;-)
            post(getNodeVersionRevertUrl(d1Id, "1.0"), "{}", null, 401);
            setRequestContext(user1);
            // -ve test - unknown nodeId
            post(getNodeVersionRevertUrl("dummy", "1.0"), "{}", null, 404);
            // -ve test - unknown versionId
            post(getNodeVersionRevertUrl(d1Id, "15.0"), "{}", null, 404);
            // -ve test - permission denied
            setRequestContext(user2);
            post(getNodeVersionRevertUrl(d1Id, "1.0"), "{}", null, 403);
        }
    } finally {
        if (f1Id != null) {
            // some cleanup
            setRequestContext(user1);
            deleteNode(f1Id, true, 204);
        }
    }
}
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) Document(org.alfresco.rest.api.tests.client.data.Document) VersionOptions(org.alfresco.rest.api.model.VersionOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 HashMap (java.util.HashMap)1 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)1 VersionOptions (org.alfresco.rest.api.model.VersionOptions)1 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)1 PublicApiHttpClient (org.alfresco.rest.api.tests.client.PublicApiHttpClient)1 Document (org.alfresco.rest.api.tests.client.data.Document)1 Node (org.alfresco.rest.api.tests.client.data.Node)1 Test (org.junit.Test)1