Search in sources :

Example 66 with NodeRef

use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.

the class PutMethodTest method testPutContentBadPath.

/**
 * Updating a file in an non-existent path
 */
@Test
public void testPutContentBadPath() throws Exception {
    String fileName = "file-" + GUID.generate();
    NodeRef fileNoderef = null;
    try {
        // Add non-existent path
        executeMethod(WebDAV.METHOD_PUT, "non/existent/path" + fileName, testDataFile, null);
        fail("The PUT execution should fail with a 400 error");
    } catch (WebDAVServerException wse) {
        // The execution failed and it is expected
        assertTrue(wse.getHttpStatusCode() == HttpServletResponse.SC_CONFLICT);
    } catch (Exception e) {
        fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
    } finally {
        if (fileNoderef != null) {
            nodeService.deleteNode(fileNoderef);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) Test(org.junit.Test)

Example 67 with NodeRef

use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.

the class PutMethodTest method testPutNoContentFileAndUpdate.

/**
 * Putting a zero content file and update it
 * <p>
 * Put an empty file
 * <p>
 * Lock the file
 * <p>
 * Put the contents
 * <p>
 * Unlock the node
 */
@Test
public void testPutNoContentFileAndUpdate() throws Exception {
    String fileName = "file-" + GUID.generate();
    NodeRef fileNoderef = null;
    try {
        executeMethod(WebDAV.METHOD_PUT, fileName, new byte[0], null);
        List<NodeRef> refs = searchService.selectNodes(nodeService.getRootNode(storeRef), "/app:company_home/cm:" + fileName, null, namespaceService, false);
        fileNoderef = refs.get(0);
        assertTrue("File should exist.", nodeService.exists(fileNoderef));
        assertEquals("Filename is not correct", fileName, nodeService.getProperty(fileNoderef, ContentModel.PROP_NAME));
        assertTrue("Expected return status is " + HttpServletResponse.SC_CREATED + ", but returned is " + response.getStatus(), HttpServletResponse.SC_CREATED == response.getStatus());
        byte[] updatedFile = IOUtils.toByteArray(fileFolderService.getReader(fileNoderef).getContentInputStream());
        assertTrue("The content should be empty", updatedFile.length == 0);
    } catch (Exception e) {
        throw new RuntimeException("Failed to upload a file", e);
    }
    try {
        executeMethod(WebDAV.METHOD_LOCK, fileName, davLockInfoAdminFile, null);
        assertEquals("File should be locked", LockStatus.LOCK_OWNER, lockService.getLockStatus(fileNoderef));
    } catch (Exception e) {
        throw new RuntimeException("Failed to lock a file", e);
    }
    // Construct IF HEADER
    String lockToken = fileNoderef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + AuthenticationUtil.getAdminUserName();
    String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(WebDAV.HEADER_IF, lockHeaderValue);
    try {
        executeMethod(WebDAV.METHOD_PUT, fileName, testDataFile, headers);
        assertTrue("File does not exist.", nodeService.exists(fileNoderef));
        assertEquals("Filename is not correct", fileName, nodeService.getProperty(fileNoderef, ContentModel.PROP_NAME));
        assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(), HttpServletResponse.SC_NO_CONTENT == response.getStatus());
        assertTrue("File should have NO_CONTENT aspect", nodeService.hasAspect(fileNoderef, ContentModel.ASPECT_NO_CONTENT));
        InputStream updatedFileIS = fileFolderService.getReader(fileNoderef).getContentInputStream();
        byte[] updatedFile = IOUtils.toByteArray(updatedFileIS);
        updatedFileIS.close();
        assertTrue("The content has to be equal", ArrayUtils.isEquals(testDataFile, updatedFile));
    } catch (Exception e) {
        throw new RuntimeException("Failed to upload a file", e);
    }
    headers = new HashMap<String, String>();
    headers.put(WebDAV.HEADER_LOCK_TOKEN, "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">");
    try {
        executeMethod(WebDAV.METHOD_UNLOCK, fileName, null, headers);
        assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(), HttpServletResponse.SC_NO_CONTENT == response.getStatus());
        assertFalse("File should not have NO_CONTENT aspect", nodeService.hasAspect(fileNoderef, ContentModel.ASPECT_NO_CONTENT));
        assertEquals("File should be unlocked", LockStatus.NO_LOCK, lockService.getLockStatus(fileNoderef));
    } catch (Exception e) {
        throw new RuntimeException("Failed to unlock a file", e);
    }
    if (fileNoderef != null) {
        nodeService.deleteNode(fileNoderef);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) Test(org.junit.Test)

Example 68 with NodeRef

use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.

the class FormRestApiJsonPost_Test method testAddNewAssociationsToNode.

/**
 * This test method attempts to add new associations between existing nodes.
 */
public void testAddNewAssociationsToNode() throws Exception {
    List<NodeRef> associatedNodes;
    checkOriginalAssocsBeforeChanges();
    // Add three additional associations
    JSONObject jsonPostData = new JSONObject();
    String assocsToAdd = associatedDoc_C + "," + associatedDoc_D + "," + associatedDoc_E;
    jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
    // Check the now updated associations via the node service
    List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
    assertEquals(5, modifiedAssocs.size());
    // Extract the target nodeRefs to make them easier to examine
    associatedNodes = new ArrayList<NodeRef>(5);
    for (AssociationRef assocRef : modifiedAssocs) {
        associatedNodes.add(assocRef.getTargetRef());
    }
    assertTrue(associatedNodes.contains(associatedDoc_A));
    assertTrue(associatedNodes.contains(associatedDoc_B));
    assertTrue(associatedNodes.contains(associatedDoc_C));
    assertTrue(associatedNodes.contains(associatedDoc_D));
    assertTrue(associatedNodes.contains(associatedDoc_E));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(referencingNodeUpdateUrl), 200);
        String jsonRspString = response.getContentAsString();
        JSONObject jsonGetResponse = new JSONObject(jsonRspString);
        JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
        assertNotNull(jsonData);

        JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
        assertNotNull(jsonFormData);
        
        String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
        
        // We expect exactly 5 assocs on the test node
        assertEquals(5, jsonAssocs.split(",").length);
        for (AssociationRef assocRef : modifiedAssocs)
        {
            assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
        }*/
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 69 with NodeRef

use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.

the class FormRestApiJsonPost_Test method testRemoveChildAssociationsFromNode.

/**
 * This test method attempts to remove an existing child association between two
 * existing nodes.
 */
public void testRemoveChildAssociationsFromNode() throws Exception {
    List<NodeRef> associatedNodes;
    checkOriginalChildAssocsBeforeChanges();
    // Remove an association
    JSONObject jsonPostData = new JSONObject();
    String assocsToRemove = childDoc_B.toString();
    jsonPostData.put(ASSOC_SYS_CHILDREN_REMOVED, assocsToRemove);
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(containingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
    // Check the now updated child associations via the node service
    List<ChildAssociationRef> modifiedAssocs = nodeService.getChildAssocs(containerNodeRef);
    assertEquals(1, modifiedAssocs.size());
    // Extract the target nodeRefs to make them easier to examine
    associatedNodes = new ArrayList<NodeRef>(5);
    for (ChildAssociationRef assocRef : modifiedAssocs) {
        associatedNodes.add(assocRef.getChildRef());
    }
    assertTrue(associatedNodes.contains(childDoc_A));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(containingNodeUpdateUrl), 200);
        String jsonRspString = response.getContentAsString();
        JSONObject jsonGetResponse = new JSONObject(jsonRspString);
        JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
        assertNotNull(jsonData);

        JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
        assertNotNull(jsonFormData);
        
        String jsonAssocs = (String)jsonFormData.get(ASSOC_SYS_CHILDREN);
        
        // We expect exactly 1 assoc on the test node
        assertEquals(1, jsonAssocs.split(",").length);
        for (ChildAssociationRef assocRef : modifiedAssocs)
        {
            assertTrue(jsonAssocs.contains(assocRef.getChildRef().toString()));
        }*/
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 70 with NodeRef

use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.

the class InvitationWebScriptTest method makeAvatar.

public static String makeAvatar(final NodeService nodeService, final NodeRef person) {
    nodeService.addAspect(person, ContentModel.ASPECT_PREFERENCES, null);
    ChildAssociationRef assoc = nodeService.createNode(person, ContentModel.ASSOC_PREFERENCE_IMAGE, avatarQName, ContentModel.TYPE_CONTENT);
    NodeRef avatar = assoc.getChildRef();
    nodeService.createAssociation(person, avatar, ContentModel.ASSOC_AVATAR);
    return "api/node/" + avatar + "/content/thumbnails/avatar";
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Aggregations

NodeRef (org.alfresco.service.cmr.repository.NodeRef)1239 HashMap (java.util.HashMap)244 QName (org.alfresco.service.namespace.QName)242 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)209 Test (org.junit.Test)195 ArrayList (java.util.ArrayList)159 Serializable (java.io.Serializable)136 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)104 BaseUnitTest (org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest)101 FileInfo (org.alfresco.service.cmr.model.FileInfo)82 Map (java.util.Map)81 Node (org.alfresco.web.bean.repository.Node)81 JSONObject (org.json.JSONObject)80 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)74 FacesContext (javax.faces.context.FacesContext)61 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)59 List (java.util.List)58 IOException (java.io.IOException)55 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)52 Date (java.util.Date)51