Java Code Examples for org.alfresco.repo.jscript.ScriptNode#revert()

The following examples show how to use org.alfresco.repo.jscript.ScriptNode#revert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VersionServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Test reverting from Share with changing type
 * see MNT-14688
 * <li>
 *     <ul>1) Create a node and a version (simulates upload a doc to Share)</ul>
 *     <ul>2) Change the node's type to a custom with mandatory aspect</ul>
 *     <ul>3) Create a new version via upload</ul>
 *     <ul>4) Try to revert to original document and see if the type is reverted, too</ul>
 * </li>
 */
@SuppressWarnings("unused")
@Commit
@Test
public void testScriptNodeRevertWithChangeType()
{
    CheckOutCheckInService checkOutCheckInService =
            (CheckOutCheckInService) applicationContext.getBean("checkOutCheckInService");

    // Create a versionable node
    NodeRef versionableNode = createNewVersionableNode();
    Version version1 = createVersion(versionableNode);
    //Set new type
    nodeService.setType(versionableNode, TEST_TYPE_WITH_MANDATORY_ASPECT_QNAME);
    // Create a new version
    NodeRef checkedOut = checkOutCheckInService.checkout(versionableNode);
    ContentWriter contentWriter = this.contentService.getWriter(checkedOut, ContentModel.PROP_CONTENT, true);
    assertNotNull(contentWriter);
    contentWriter.putContent(UPDATED_CONTENT_1);
    nodeService.setProperty(checkedOut, PROP_1, VALUE_1);
    checkOutCheckInService.checkin(checkedOut, null, contentWriter.getContentUrl(), false);
    Version version2 = createVersion(versionableNode);

    // Create a ScriptNode as used in Share
    ServiceRegistry services = applicationContext.getBean(ServiceRegistry.class);
    ScriptNode scriptNode = new ScriptNode(versionableNode, services);
    assertEquals("0.2", nodeService.getProperty(scriptNode.getNodeRef(), ContentModel.PROP_VERSION_LABEL));
    assertEquals(TEST_TYPE_WITH_MANDATORY_ASPECT_QNAME, nodeService.getType(scriptNode.getNodeRef()));

    // Revert to version1
    ScriptNode newNode = scriptNode.revert("History", false, version1.getVersionLabel());
    assertEquals("0.3", nodeService.getProperty(newNode.getNodeRef(), ContentModel.PROP_VERSION_LABEL));
    assertEquals(TEST_TYPE_QNAME, nodeService.getType(newNode.getNodeRef()));
}
 
Example 2
Source File: VersionServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test reverting from Share
 */
@SuppressWarnings("unused")
@Commit
@Test
public void testScriptNodeRevert()
{
    CheckOutCheckInService checkOutCheckIn =
        (CheckOutCheckInService) applicationContext.getBean("checkOutCheckInService");
    
    // Create a versionable node
    NodeRef versionableNode = createNewVersionableNode();
    NodeRef checkedOut = checkOutCheckIn.checkout(versionableNode);
    
    Version versionC1 = createVersion(checkedOut);

    
    // Create a new, first proper version
    ContentWriter contentWriter = this.contentService.getWriter(checkedOut, ContentModel.PROP_CONTENT, true);
    assertNotNull(contentWriter);
    contentWriter.putContent(UPDATED_CONTENT_1);
    nodeService.setProperty(checkedOut, PROP_1, VALUE_1);
    checkOutCheckIn.checkin(checkedOut, null, contentWriter.getContentUrl(), false);
    Version version1 = createVersion(versionableNode);
    checkedOut = checkOutCheckIn.checkout(versionableNode);
    
    
    // Create another new version
    contentWriter = this.contentService.getWriter(checkedOut, ContentModel.PROP_CONTENT, true);
    assertNotNull(contentWriter);
    contentWriter.putContent(UPDATED_CONTENT_2);
    nodeService.setProperty(checkedOut, PROP_1, VALUE_2);
    checkOutCheckIn.checkin(checkedOut, null, contentWriter.getContentUrl(), false);
    Version version2 = createVersion(versionableNode);
    checkedOut = checkOutCheckIn.checkout(versionableNode);
    
    // Check we're now up to two versions
    // (The version created on the working copy doesn't count)
    VersionHistory history = versionService.getVersionHistory(versionableNode);
    assertEquals(version2.getVersionLabel(), history.getHeadVersion().getVersionLabel());
    assertEquals(version2.getVersionedNodeRef(), history.getHeadVersion().getVersionedNodeRef());
    assertEquals(2, history.getAllVersions().size());
    
    Version[] versions = history.getAllVersions().toArray(new Version[2]);
    assertEquals("0.2", versions[0].getVersionLabel());
    assertEquals("0.1", versions[1].getVersionLabel());
    
    
    // Add yet another version
    contentWriter = this.contentService.getWriter(checkedOut, ContentModel.PROP_CONTENT, true);
    assertNotNull(contentWriter);
    contentWriter.putContent(UPDATED_CONTENT_3);
    nodeService.setProperty(checkedOut, PROP_1, VALUE_3);
    checkOutCheckIn.checkin(checkedOut, null, contentWriter.getContentUrl(), false);
    Version version3 = createVersion(versionableNode);
    
    // Verify that the version labels are as we expect them to be
    history = versionService.getVersionHistory(versionableNode);
    assertEquals(version3.getVersionLabel(), history.getHeadVersion().getVersionLabel());
    assertEquals(version3.getVersionedNodeRef(), history.getHeadVersion().getVersionedNodeRef());
    assertEquals(3, history.getAllVersions().size());
    
    versions = history.getAllVersions().toArray(new Version[3]);
    assertEquals("0.3", versions[0].getVersionLabel());
    assertEquals("0.2", versions[1].getVersionLabel());
    assertEquals("0.1", versions[2].getVersionLabel());
    
    
    // Create a ScriptNode as used in Share
    ServiceRegistry services = applicationContext.getBean(ServiceRegistry.class); 
    ScriptNode scriptNode = new ScriptNode(versionableNode, services);
    assertEquals("0.3", nodeService.getProperty(scriptNode.getNodeRef(), ContentModel.PROP_VERSION_LABEL));
    assertEquals(VALUE_3, nodeService.getProperty(scriptNode.getNodeRef(), PROP_1));
    
    // Revert to version2
    // The content and properties will be the same as on Version 2, but we'll
    //  actually be given a new version number for it
    ScriptNode newNode = scriptNode.revert("History", false, version2.getVersionLabel());
    ContentReader contentReader = this.contentService.getReader(newNode.getNodeRef(), ContentModel.PROP_CONTENT);
    assertNotNull(contentReader);
    assertEquals(UPDATED_CONTENT_2, contentReader.getContentString());
    assertEquals(VALUE_2, nodeService.getProperty(newNode.getNodeRef(), PROP_1));
    // Will be a new version though - TODO Is this correct?
    assertEquals("0.4", nodeService.getProperty(newNode.getNodeRef(), ContentModel.PROP_VERSION_LABEL));
    
    // Revert to version1
    newNode = scriptNode.revert("History", false, version1.getVersionLabel());
    contentReader = this.contentService.getReader(newNode.getNodeRef(), ContentModel.PROP_CONTENT);
    assertNotNull(contentReader);
    assertEquals(UPDATED_CONTENT_1, contentReader.getContentString());
    assertEquals(VALUE_1, nodeService.getProperty(newNode.getNodeRef(), PROP_1));
    // Will be a new version though - TODO Is this correct?
    assertEquals("0.5", nodeService.getProperty(newNode.getNodeRef(), ContentModel.PROP_VERSION_LABEL));
}