Search in sources :

Example 71 with NodeRef

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

the class LinksRestApiTest method pushLinkCreatedDateBack.

/**
 * Monkeys with the created date on a link
 */
private void pushLinkCreatedDateBack(String name, int daysAgo) throws Exception {
    NodeRef container = siteService.getContainer(SITE_SHORT_NAME_LINKS, "links");
    NodeRef node = nodeService.getChildByName(container, ContentModel.ASSOC_CONTAINS, name);
    Date created = (Date) nodeService.getProperty(node, ContentModel.PROP_CREATED);
    Date newCreated = new Date(created.getTime() - daysAgo * 24 * 60 * 60 * 1000);
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    this.policyBehaviourFilter.disableBehaviour(ContentModel.ASPECT_AUDITABLE);
    internalNodeService.setProperty(node, ContentModel.PROP_CREATED, newCreated);
    this.policyBehaviourFilter.enableBehaviour(ContentModel.ASPECT_AUDITABLE);
    txn.commit();
    // Now chance something else on the node to have it re-indexed
    nodeService.setProperty(node, ContentModel.PROP_CREATED, newCreated);
    nodeService.setProperty(node, ContentModel.PROP_DESCRIPTION, "Forced change");
}
Also used : UserTransaction(javax.transaction.UserTransaction) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Date(java.util.Date)

Example 72 with NodeRef

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

the class NodeWebScripTest method testLinkCreation.

@SuppressWarnings("unchecked")
public void testLinkCreation() throws Exception {
    // Create a folder within the DocLib
    NodeRef siteDocLib = siteService.getContainer(TEST_SITE.getShortName(), SiteService.DOCUMENT_LIBRARY);
    String testFolder1Name = "testingLinkCreationFolder1";
    Map<QName, Serializable> testFolderProps = new HashMap<QName, Serializable>();
    testFolderProps.put(ContentModel.PROP_NAME, testFolder1Name);
    NodeRef testFolder1 = nodeService.createNode(siteDocLib, ContentModel.ASSOC_CONTAINS, QName.createQName("testingLinkCreationFolder1"), ContentModel.TYPE_FOLDER, testFolderProps).getChildRef();
    JSONObject jsonReq = null;
    JSONObject json = null;
    JSONArray jsonArray = new JSONArray();
    JSONArray jsonLinkNodes = null;
    JSONObject jsonLinkNode = null;
    // Create files in the testFolder1
    NodeRef testFile1 = createNode(testFolder1, "testingLinkCreationFile1", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    NodeRef testFile2 = createNode(testFolder1, "testingLinkCreationFile2", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    NodeRef testFile3 = createNode(testFolder1, "testingLinkCreationFile3", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    // Create testFolder2 in the testFolder1
    String testFolder2Name = "testingLinkCreationFolder2";
    testFolderProps = new HashMap<QName, Serializable>();
    testFolderProps.put(ContentModel.PROP_NAME, testFolder2Name);
    NodeRef testFolder2 = nodeService.createNode(siteDocLib, ContentModel.ASSOC_CONTAINS, QName.createQName("testingLinkCreationFolder2"), ContentModel.TYPE_FOLDER, testFolderProps).getChildRef();
    // Create link to file1 in same folder - testFolder1
    Request req = new Request("POST", CREATE_LINK_API + testFile1.getStoreRef().getProtocol() + "/" + testFile1.getStoreRef().getIdentifier() + "/" + testFile1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray.add(testFile1.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    String nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef file1Link = new NodeRef(nodeRef);
    // Check that app:linked aspect is added on sourceNode
    assertEquals(true, nodeService.hasAspect(testFile1, ApplicationModel.ASPECT_LINKED));
    assertEquals(true, nodeService.exists(file1Link));
    nodeService.deleteNode(file1Link);
    assertEquals(false, nodeService.hasAspect(testFile1, ApplicationModel.ASPECT_LINKED));
    // Create link to testFolder2 in same folder (testFolder1)
    req = new Request("POST", CREATE_LINK_API + testFolder2.getStoreRef().getProtocol() + "/" + testFolder2.getStoreRef().getIdentifier() + "/" + testFolder2.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFolder2.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef folder2Link = new NodeRef(nodeRef);
    assertEquals(true, nodeService.hasAspect(testFolder2, ApplicationModel.ASPECT_LINKED));
    assertEquals(true, nodeService.exists(folder2Link));
    // create another link of testFolder2 in siteDocLib
    req = new Request("POST", CREATE_LINK_API + testFolder2.getStoreRef().getProtocol() + "/" + testFolder2.getStoreRef().getIdentifier() + "/" + testFolder2.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, siteDocLib.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFolder2.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef folder2Link2 = new NodeRef(nodeRef);
    // delete folder2Link and check that aspect exists since we have another
    // link for testFolder2
    nodeService.deleteNode(folder2Link);
    assertEquals(true, nodeService.hasAspect(testFolder2, ApplicationModel.ASPECT_LINKED));
    nodeService.deleteNode(folder2Link2);
    assertEquals(false, nodeService.hasAspect(testFolder2, ApplicationModel.ASPECT_LINKED));
    // Create link to testFile1, testFile2 and testFile3 in same testFolder1
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFile1.toString());
    jsonArray.add(testFile2.toString());
    jsonArray.add(testFile3.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(3, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("3", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    NodeRef fileLink = null;
    List<NodeRef> fileLinks = new ArrayList<NodeRef>();
    for (int i = 0; i < jsonLinkNodes.size(); i++) {
        jsonLinkNode = (JSONObject) jsonLinkNodes.get(i);
        nodeRef = (String) jsonLinkNode.get("nodeRef");
        fileLink = new NodeRef(nodeRef);
        fileLinks.add(fileLink);
        assertEquals(true, nodeService.exists(fileLink));
    }
    // try to create another link in the same location - an exception should be thrown
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFile1.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_BAD_REQUEST));
    // delete all 3 files and check that the links are deleted too
    nodeService.deleteNode(testFile1);
    nodeService.deleteNode(testFile2);
    nodeService.deleteNode(testFile3);
    for (NodeRef linkNodeRef : fileLinks) {
        assertEquals(false, nodeService.exists(linkNodeRef));
    }
    // try create a link to a site - shouldn't be possible
    SiteInfo site2 = createSite("Site2TestingNodeCreateLink");
    NodeRef siteNodeRef = site2.getNodeRef();
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(siteNodeRef.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_BAD_REQUEST));
    // create a file and a link to that file inside the documentLibrary
    NodeRef site2DocLib = siteService.getContainer(TEST_SITE.getShortName(), SiteService.DOCUMENT_LIBRARY);
    NodeRef testFileSite2 = createNode(site2DocLib, "testingLinkCreationFileInSite2", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, site2DocLib.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFileSite2.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // links are created with success, try to delete site2 - should succeed
    siteService.deleteSite(site2.getShortName());
    nodeArchiveService.purgeArchivedNode(nodeArchiveService.getArchivedNode(siteNodeRef));
    // Links can be created in Shared Files, My Files and Repository
    NodeRef testFile4 = createNode(testFolder1, "testingLinkCreationFile4", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    req = new Request("POST", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://company/shared");
    jsonArray = new JSONArray();
    jsonArray.add(testFile4.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    req = new Request("POST", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://user/home");
    jsonArray = new JSONArray();
    jsonArray.add(testFile4.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // create link in Repository as Admin
    AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
    req = new Request("POST", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://company/home");
    jsonArray = new JSONArray();
    jsonArray.add(testFile4.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // all 3 links are created with success, delete all links
    req = new Request("DELETE", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId() + "/delete");
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // all links are removed with success marker aspect should be removed too
    assertEquals(false, nodeService.hasAspect(testFile4, ApplicationModel.ASPECT_LINKED));
    AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
    // you should not be able to create links for locked nodes but you can delete links
    final NodeRef testFile5 = createNode(testFolder1, "testingLinkCreationFileWithLock", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    req = new Request("POST", CREATE_LINK_API + testFile5.getStoreRef().getProtocol() + "/" + testFile5.getStoreRef().getIdentifier() + "/" + testFile5.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFile5.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef file5Link = new NodeRef(nodeRef);
    assertEquals(true, nodeService.hasAspect(testFile5, ApplicationModel.ASPECT_LINKED));
    assertEquals(true, nodeService.exists(file5Link));
    // checkout the node testFile5
    final NodeRef workingCopy = retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Exception {
            return checkOutCheckInService.checkout(testFile5);
        }
    });
    assertNotNull(workingCopy);
    // try to create link for working copy
    req = new Request("POST", CREATE_LINK_API + workingCopy.getStoreRef().getProtocol() + "/" + workingCopy.getStoreRef().getIdentifier() + "/" + workingCopy.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(workingCopy.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    // should fail since source node is working copy
    json = asJSON(sendRequest(req, Status.STATUS_BAD_REQUEST));
    // delete the link when source node is locked
    nodeService.deleteNode(file5Link);
    assertEquals(false, nodeService.hasAspect(testFile5, ApplicationModel.ASPECT_LINKED));
    // release the lock
    retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            checkOutCheckInService.checkin(workingCopy, new HashMap<String, Serializable>());
            return null;
        }
    });
    assertEquals(false, nodeService.hasAspect(testFile5, ContentModel.ASPECT_LOCKABLE));
    // test that Consumers can create and delete links if they have only read permission
    NodeRef testFile6 = createNode(testFolder1, "testingLinkCreationFile6", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
    assertTrue(permissionService.hasPermission(testFile6, PermissionService.READ) == AccessStatus.ALLOWED);
    assertTrue(permissionService.hasPermission(testFile6, PermissionService.WRITE) == AccessStatus.DENIED);
    // create another link of testFile1 in MyFiles
    req = new Request("POST", CREATE_LINK_API + testFile6.getStoreRef().getProtocol() + "/" + testFile6.getStoreRef().getIdentifier() + "/" + testFile6.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://user/home");
    jsonArray = new JSONArray();
    jsonArray.add(testFile6.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef testFileSite3Link = new NodeRef(nodeRef);
    nodeService.exists(testFileSite3Link);
    assertEquals(true, nodeService.hasAspect(testFile6, ApplicationModel.ASPECT_LINKED));
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) JSONArray(org.json.simple.JSONArray) Request(org.springframework.extensions.webscripts.TestWebScriptServer.Request) ArrayList(java.util.ArrayList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Example 73 with NodeRef

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

the class NodeWebScripTest method testFolderCreation.

@SuppressWarnings("unchecked")
public void testFolderCreation() throws Exception {
    // Create a folder within the DocLib
    NodeRef siteDocLib = siteService.getContainer(TEST_SITE.getShortName(), SiteService.DOCUMENT_LIBRARY);
    String testFolderName = "testing";
    Map<QName, Serializable> testFolderProps = new HashMap<QName, Serializable>();
    testFolderProps.put(ContentModel.PROP_NAME, testFolderName);
    NodeRef testFolder = nodeService.createNode(siteDocLib, ContentModel.ASSOC_CONTAINS, QName.createQName("testing"), ContentModel.TYPE_FOLDER, testFolderProps).getChildRef();
    String testNodeName = "aNEWfolder";
    String testNodeTitle = "aTITLEforAfolder";
    String testNodeDescription = "DESCRIPTIONofAfolder";
    JSONObject jsonReq = null;
    JSONObject json = null;
    NodeRef folder = null;
    // By NodeID
    Request req = new Request("POST", "/api/node/folder/" + testFolder.getStoreRef().getProtocol() + "/" + testFolder.getStoreRef().getIdentifier() + "/" + testFolder.getId());
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_TITLE));
    assertEquals(null, nodeService.getProperty(folder, ContentModel.PROP_DESCRIPTION));
    assertEquals(testFolder, nodeService.getPrimaryParent(folder).getParentRef());
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // In a Site Container
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("description", testNodeDescription);
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_TITLE));
    assertEquals(testNodeDescription, nodeService.getProperty(folder, ContentModel.PROP_DESCRIPTION));
    assertEquals(siteDocLib, nodeService.getPrimaryParent(folder).getParentRef());
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // A Child of a Site Container
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("title", testNodeTitle);
    jsonReq.put("description", testNodeDescription);
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(testNodeTitle, nodeService.getProperty(folder, ContentModel.PROP_TITLE));
    assertEquals(testNodeDescription, nodeService.getProperty(folder, ContentModel.PROP_DESCRIPTION));
    assertEquals(testFolder, nodeService.getPrimaryParent(folder).getParentRef());
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // Type needs to be a subtype of folder
    // explicit cm:folder
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("type", "cm:folder");
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // cm:systemfolder extends from cm:folder
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("type", "cm:systemfolder");
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(ContentModel.TYPE_SYSTEM_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // cm:content isn't allowed
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("type", "cm:content");
    req.setBody(jsonReq.toString().getBytes());
    sendRequest(req, Status.STATUS_BAD_REQUEST);
    // Check permissions - need to be Contributor
    AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
    req = new Request("POST", "/api/node/folder/" + testFolder.getStoreRef().getProtocol() + "/" + testFolder.getStoreRef().getIdentifier() + "/" + testFolder.getId());
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    nodeService.deleteNode(folder);
    AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
    sendRequest(req, Status.STATUS_FORBIDDEN);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Request(org.springframework.extensions.webscripts.TestWebScriptServer.Request)

Example 74 with NodeRef

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

the class NodeLocatorWebScriptTest method testAncestorOfTypeNodeLocator.

public void testAncestorOfTypeNodeLocator() throws Exception {
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    NodeRef folder = makeChildFolderNode(companyHome);
    try {
        NodeRef sysFolder = makeChildSystemFolderNode(folder);
        NodeRef subFolder = makeChildFolderNode(sysFolder);
        NodeRef source = makeChildContentNode(subFolder);
        // No params so should find first parent.
        String ancestorUrl = makeUrl(source, AncestorNodeLocator.NAME);
        checkNodeLocator(ancestorUrl, subFolder);
        // Set type to cm:content. Should not match any node.
        String encodedContentType = URLEncoder.encode(ContentModel.TYPE_CONTENT.toPrefixString(namespaceService));
        String contentAncestorUrl = ancestorUrl + "?type=" + encodedContentType;
        checkNodeLocator(contentAncestorUrl, null);
        // Set type to cm:systemfolder. Should find the sysFolder node.
        String encodedSysFolderType = URLEncoder.encode(ContentModel.TYPE_SYSTEM_FOLDER.toPrefixString(namespaceService));
        String sysFolderAncestorUrl = ancestorUrl + "?type=" + encodedSysFolderType;
        checkNodeLocator(sysFolderAncestorUrl, sysFolder);
        // Set aspect to cm:ownable. Should not match any node.
        String encodedOwnableAspect = URLEncoder.encode(ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService));
        String ownableAncestorUrl = ancestorUrl + "?aspect=" + encodedOwnableAspect;
        checkNodeLocator(ownableAncestorUrl, null);
        // Add ownable aspect to folder node. Now that node should be found.
        nodeService.addAspect(folder, ContentModel.ASPECT_OWNABLE, null);
        checkNodeLocator(ownableAncestorUrl, folder);
        // Set aspect to cm:ownable and type to cm:systemfolder. Should not match any node.
        String ownableSysFolderAncestorUrl = sysFolderAncestorUrl + "&aspect=" + encodedOwnableAspect;
        checkNodeLocator(ownableSysFolderAncestorUrl, null);
        // Set aspect to cm:ownable and type to cm:folder. Should find folder node.
        String encodedFOlderType = URLEncoder.encode(ContentModel.TYPE_FOLDER.toPrefixString(namespaceService));
        String ownableFolderAncestorUrl = ownableAncestorUrl + "&type=" + encodedFOlderType;
        checkNodeLocator(ownableFolderAncestorUrl, folder);
    } finally {
        nodeService.deleteNode(folder);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef)

Example 75 with NodeRef

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

the class NodeLocatorWebScriptTest method testXPathNodeLocator.

public void testXPathNodeLocator() throws Exception {
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    NodeRef first = makeChildFolderNode(companyHome);
    try {
        NodeRef second = makeChildFolderNode(first);
        NodeRef content = makeChildContentNode(second);
        // Check bad path returns null
        String badPath = URLEncoder.encode("cm:foo/cm:bar/cm:foobar");
        String badPathUrl = baseURL + XPathNodeLocator.NAME + "?query=" + badPath;
        checkNodeLocator(badPathUrl, null);
        String path = nodeService.getPath(content).toPrefixString(namespaceService);
        String encodedPath = URLEncoder.encode(path);
        // Check default store ref works.
        String defaultStoreUrl = baseURL + XPathNodeLocator.NAME + "?query=" + encodedPath;
        checkNodeLocator(defaultStoreUrl, content);
        // Check specified store ref works.
        String storeIdUrl = defaultStoreUrl + "&store_type=workspace&store_id=SpacesStore";
        checkNodeLocator(storeIdUrl, content);
        // Check node store ref works.
        String nodePathUrl = makeUrl(companyHome, XPathNodeLocator.NAME) + "?query=" + encodedPath;
        checkNodeLocator(nodePathUrl, content);
    } finally {
        nodeService.deleteNode(first);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef)

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