Search in sources :

Example 41 with NodeRef

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

the class TestTags method testTags.

@Test
@Category({ LuceneTests.class, RedundantTests.class })
public void testTags() throws Exception {
    Iterator<TestNetwork> networksIt = getTestFixture().getNetworksIt();
    assertTrue(networksIt.hasNext());
    final TestNetwork network1 = networksIt.next();
    assertTrue(networksIt.hasNext());
    final TestNetwork network2 = networksIt.next();
    final List<TestPerson> people = new ArrayList<TestPerson>(3);
    // create users and some preferences
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestPerson person = network1.createUser();
            people.add(person);
            person = network1.createUser();
            people.add(person);
            return null;
        }
    }, network1.getId());
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestPerson person = network2.createUser();
            people.add(person);
            return null;
        }
    }, network2.getId());
    final TestPerson person1 = people.get(0);
    final TestPerson person2 = people.get(1);
    final TestPerson person3 = people.get(2);
    final List<NodeRef> nodes = new ArrayList<NodeRef>();
    final List<TestSite> sites = new ArrayList<TestSite>();
    // Create site
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestSite site = network1.createSite(SiteVisibility.PRIVATE);
            sites.add(site);
            NodeRef nodeRef = repoService.createDocument(site.getContainerNodeRef("documentLibrary"), "Test Doc", "Test Content");
            nodes.add(nodeRef);
            nodeRef = repoService.createDocument(site.getContainerNodeRef("documentLibrary"), "Test Doc 1", "Test Content 1");
            nodes.add(nodeRef);
            return null;
        }
    }, person1.getId(), network1.getId());
    final NodeRef nodeRef1 = nodes.get(0);
    final NodeRef nodeRef2 = nodes.get(1);
    Nodes nodesProxy = publicApiClient.nodes();
    Comments commentsProxy = publicApiClient.comments();
    Tags tagsProxy = publicApiClient.tags();
    final List<Tag> tags = new ArrayList<Tag>();
    tags.add(new Tag("tag 1"));
    tags.add(new Tag("tag 9"));
    tags.add(new Tag("other tag 3"));
    tags.add(new Tag("my tag 1"));
    tags.add(new Tag("tag 5"));
    // try to add a tag to a comment
    try {
        Comment comment = new Comment("Test Comment", "Test Comment");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        Comment newComment = commentsProxy.createNodeComment(nodeRef1.getId(), comment);
        Tag tag = new Tag("testTag");
        nodesProxy.createNodeTag(newComment.getId(), tag);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // try to add a tag to a tag
    try {
        Tag tag = new Tag("testTag");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        Tag newTag = nodesProxy.createNodeTag(nodeRef1.getId(), tag);
        nodesProxy.createNodeTag(newTag.getId(), tag);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // Test Case cloud-2221
    // Test Case cloud-2222
    // multi-byte characters, special characters, create and update tags
    {
        Tag[] multiByteTags = new Tag[] { new Tag("\u67e5\u770b\u5168\u90e8"), new Tag("\u67e5\u770b\u5168\u91e8"), new Tag("%^&%&$^£@") };
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        // first, create tags
        Map<String, Tag> createdTags = new HashMap<String, Tag>();
        for (Tag tag : multiByteTags) {
            Tag ret = nodesProxy.createNodeTag(nodeRef2.getId(), tag);
            createdTags.put(ret.getId(), ret);
        }
        int skipCount = 0;
        int maxItems = Integer.MAX_VALUE;
        Paging paging = getPaging(skipCount, maxItems);
        ListResponse<Tag> resp = nodesProxy.getNodeTags(nodeRef2.getId(), createParams(paging, null));
        List<Tag> retTags = resp.getList();
        assertEquals(createdTags.size(), retTags.size());
        for (Tag tag : retTags) {
            String tagId = tag.getId();
            Tag expectedTag = createdTags.get(tagId);
            expectedTag.expected(tag);
        }
        try {
            // update with an empty tag i.e. "" -> bad request
            Tag tag = new Tag("");
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.createNodeTag(nodeRef2.getId(), tag);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }
        List<Tag> tagUpdates = new ArrayList<Tag>(createdTags.values());
        tagUpdates.get(0).setTag("\u4e00\u4e01\u4e02\u4e10");
        tagUpdates.get(1).setTag("\u4e00\u4e01\u4e12\u4e11");
        tagUpdates.get(2).setTag("\u4e00\u4e01\u4e12\u4e12");
        Map<String, Tag> updatedTags = new HashMap<String, Tag>();
        for (Tag tag : tagUpdates) {
            Tag ret = tagsProxy.update(tag);
            assertNotNull(ret.getId());
            assertNotNull(ret.getTag());
            // tag.expected(ret); disabled because tag id changes
            updatedTags.put(ret.getId(), ret);
        }
        // get updated tags
        List<Tag> expectedNodeTags = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Tag>>() {

            @Override
            public List<Tag> doWork() throws Exception {
                List<Tag> tags = repoService.getTags(nodeRef2);
                return tags;
            }
        }, person1.getId(), network1.getId());
        skipCount = 0;
        maxItems = tagUpdates.size();
        paging = getPaging(skipCount, maxItems, tagUpdates.size(), tagUpdates.size());
        resp = nodesProxy.getNodeTags(nodeRef2.getId(), createParams(paging, null));
        checkList(expectedNodeTags.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
    }
    {
        List<Tag> createdTags = new ArrayList<Tag>();
        // Test Case cloud-1975
        for (Tag tag : tags) {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            Tag ret = nodesProxy.createNodeTag(nodeRef1.getId(), tag);
            assertEquals(tag.getTag(), ret.getTag());
            assertNotNull(ret.getId());
            createdTags.add(ret);
        }
        // update tag, empty string
        try {
            Tag tag = new Tag(createdTags.get(0).getId(), "");
            tagsProxy.update(tag);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }
        // invalid node id
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.createNodeTag(GUID.generate(), tags.get(0));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // Test Case cloud-1973
        // Test Case cloud-2208
        // Test Case cloud-2219
        // check that the tags are there in the node tags, test paging
        List<Tag> expectedNodeTags = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Tag>>() {

            @Override
            public List<Tag> doWork() throws Exception {
                List<Tag> tags = repoService.getTags(nodeRef1);
                return tags;
            }
        }, person1.getId(), network1.getId());
        {
            int skipCount = 0;
            int maxItems = 2;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            Paging paging = getPaging(skipCount, maxItems, expectedNodeTags.size(), expectedNodeTags.size());
            ListResponse<Tag> resp = nodesProxy.getNodeTags(nodeRef1.getId(), createParams(paging, null));
            checkList(expectedNodeTags.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        {
            int skipCount = 2;
            int maxItems = Integer.MAX_VALUE;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            Paging paging = getPaging(skipCount, maxItems, expectedNodeTags.size(), expectedNodeTags.size());
            ListResponse<Tag> resp = nodesProxy.getNodeTags(nodeRef1.getId(), createParams(paging, null));
            checkList(expectedNodeTags.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // invalid node
        try {
            int skipCount = 0;
            int maxItems = 2;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            Paging paging = getPaging(skipCount, maxItems, expectedNodeTags.size(), expectedNodeTags.size());
            ListResponse<Tag> allTags = nodesProxy.getNodeTags("invalidNode", createParams(paging, null));
            checkList(expectedNodeTags.subList(skipCount, paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), allTags);
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // user from another account - 403
        try {
            int skipCount = 0;
            int maxItems = 2;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person3.getId()));
            Paging expectedPaging = getPaging(skipCount, maxItems, expectedNodeTags.size(), expectedNodeTags.size());
            nodesProxy.getNodeTags(nodeRef1.getId(), createParams(expectedPaging, null));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }
        // another user from the same account
        try {
            int skipCount = 0;
            int maxItems = 2;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            Paging paging = getPaging(skipCount, maxItems, expectedNodeTags.size(), expectedNodeTags.size());
            ListResponse<Tag> resp = nodesProxy.getNodeTags(nodeRef1.getId(), createParams(paging, null));
            checkList(expectedNodeTags.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
        }
        // Test Case cloud-1519
        // Test Case cloud-2206
        // Test Case cloud-2218
        // check that the tags are there in the network tags, test paging
        // TODO for user from another network who is invited to this network
        List<Tag> expectedNetworkTags = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Tag>>() {

            @Override
            public List<Tag> doWork() throws Exception {
                List<Tag> tags = repoService.getTags();
                return tags;
            }
        }, person1.getId(), network1.getId());
        {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedNetworkTags.size(), null);
            ListResponse<Tag> allTags = tagsProxy.getTags(createParams(paging, null));
            checkList(expectedNetworkTags.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), allTags);
        }
        {
            int skipCount = 2;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedNetworkTags.size(), null);
            ListResponse<Tag> allTags = tagsProxy.getTags(createParams(paging, null));
            checkList(expectedNetworkTags.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), allTags);
        }
    }
    {
        // Try a create with the same tag value
        Tag tag = tags.get(0);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        nodesProxy.createNodeTag(nodeRef1.getId(), tag);
    }
    try {
        // Invalid node id
        Tag tag = tags.get(0);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        nodesProxy.createNodeTag(GUID.generate(), tag);
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // Test Case cloud-2183
    // update tags
    {
        // get a network tag
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        ListResponse<Tag> allTags = tagsProxy.getTags(createParams(paging, null));
        assertTrue(allTags.getList().size() > 0);
        // and update it
        Tag tag = allTags.getList().get(0);
        String newTagValue = GUID.generate();
        Tag newTag = new Tag(tag.getId(), newTagValue);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        Tag ret = tagsProxy.update(newTag);
        assertEquals(newTagValue, ret.getTag());
    // assertNotEquals(tag.getId, ret.getId()); // disabled due to CLOUD-628
    }
    // update invalid/unknown tag id
    try {
        Tag unknownTag = new Tag(GUID.generate(), GUID.generate());
        tagsProxy.update(unknownTag);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // Test Case cloud-1972
    // Test Case cloud-1974
    // not allowed methods
    {
        List<Tag> networkTags = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Tag>>() {

            @Override
            public List<Tag> doWork() throws Exception {
                List<Tag> tags = repoService.getTags();
                return tags;
            }
        }, person1.getId(), network1.getId());
        assertTrue(networkTags.size() > 0);
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.update("nodes", nodeRef1.getId(), "tags", null, null, "Unable to PUT node tags");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.remove("nodes", nodeRef1.getId(), "tags", null, "Unable to DELETE node tags");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            Tag tag = networkTags.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.update("nodes", nodeRef1.getId(), "tags", tag.getId(), null, "Unable to PUT node tag");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            Tag tag = networkTags.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.create("tags", null, null, null, tag.toJSON().toString(), "Unable to POST to tags");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            Tag tag = networkTags.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.update("tags", null, null, null, tag.toJSON().toString(), "Unable to PUT tags");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.remove("tags", null, null, null, "Unable to DELETE tags");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            Tag tag = networkTags.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.create("tags", tag.getId(), null, null, tag.toJSON().toString(), "Unable to POST to a tag");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            Tag tag = networkTags.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.remove("tags", tag.getId(), null, null, "Unable to DELETE a tag");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        // delete node tag
        {
            Tag tag = networkTags.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.removeNodeTag(nodeRef1.getId(), tag.getId());
            // check that the tag is gone
            ListResponse<Tag> resp = nodesProxy.getNodeTags(nodeRef1.getId(), createParams(getPaging(0, Integer.MAX_VALUE), null));
            List<Tag> nodeTags = resp.getList();
            assertTrue(!nodeTags.contains(tag));
        }
        try {
            Tag tag = networkTags.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            nodesProxy.getSingle("nodes", nodeRef1.getId(), "tags", tag.getId(), "Unable to GET node tag");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) ArrayList(java.util.ArrayList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Tags(org.alfresco.rest.api.tests.client.PublicApiClient.Tags) Comment(org.alfresco.rest.api.tests.client.data.Comment) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) Comments(org.alfresco.rest.api.tests.client.PublicApiClient.Comments) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Nodes(org.alfresco.rest.api.tests.client.PublicApiClient.Nodes) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) Tag(org.alfresco.rest.api.tests.client.data.Tag) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) HashMap(java.util.HashMap) Map(java.util.Map) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 42 with NodeRef

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

the class RepoService method createDocument.

public NodeRef createDocument(final NodeRef parentNodeRef, final String name, final String title, final String description, final String content) {
    NodeRef nodeRef = fileFolderService.create(parentNodeRef, name, ContentModel.TYPE_CONTENT).getNodeRef();
    ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
    writer.putContent(content);
    nodeService.setProperty(nodeRef, ContentModel.PROP_TITLE, title);
    nodeService.setProperty(nodeRef, ContentModel.PROP_DESCRIPTION, description);
    return nodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter)

Example 43 with NodeRef

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

the class TaskWorkflowApiTest method testDeleteTaskItem.

@Test
@SuppressWarnings("unchecked")
public void testDeleteTaskItem() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    String otherPerson = getOtherPersonInNetwork(requestContext.getRunAsUser(), requestContext.getNetworkId()).getId();
    RequestContext otherContext = new RequestContext(requestContext.getNetworkId(), otherPerson);
    String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
    RequestContext adminContext = new RequestContext(requestContext.getNetworkId(), tenantAdmin);
    // Create test-document and add to package
    NodeRef[] docNodeRefs = createTestDocuments(requestContext);
    ProcessInfo processInfo = startAdhocProcess(requestContext, docNodeRefs);
    final Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInfo.getId()).singleResult();
    assertNotNull(task);
    activitiProcessEngine.getTaskService().setAssignee(task.getId(), null);
    try {
        TasksClient tasksClient = publicApiClient.tasksClient();
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item as admin
        JSONObject createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        JSONObject result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        JSONObject itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        publicApiClient.setRequestContext(adminContext);
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item with candidate user
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        activitiProcessEngine.getTaskService().addCandidateUser(task.getId(), otherPerson);
        publicApiClient.setRequestContext(otherContext);
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item with not involved user
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        activitiProcessEngine.getTaskService().deleteCandidateUser(task.getId(), otherPerson);
        publicApiClient.setRequestContext(otherContext);
        try {
            tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(403, e.getHttpResponse().getStatusCode());
        }
        // delete item with user from candidate group with no assignee
        List<MemberOfSite> memberships = getTestFixture().getNetwork(otherContext.getNetworkId()).getSiteMemberships(otherContext.getRunAsUser());
        assertTrue(memberships.size() > 0);
        MemberOfSite memberOfSite = memberships.get(0);
        String group = "GROUP_site_" + memberOfSite.getSiteId() + "_" + memberOfSite.getRole().name();
        activitiProcessEngine.getTaskService().deleteCandidateUser(task.getId(), otherContext.getRunAsUser());
        activitiProcessEngine.getTaskService().addCandidateGroup(task.getId(), group);
        publicApiClient.setRequestContext(otherContext);
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item with user from candidate group with assignee
        activitiProcessEngine.getTaskService().setAssignee(task.getId(), requestContext.getRunAsUser());
        publicApiClient.setRequestContext(requestContext);
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        publicApiClient.setRequestContext(otherContext);
        try {
            tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(403, e.getHttpResponse().getStatusCode());
        }
        publicApiClient.setRequestContext(requestContext);
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // invalid task id
        publicApiClient.setRequestContext(requestContext);
        try {
            tasksClient.deleteTaskItem("fakeid", docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // invalid item id
        try {
            tasksClient.deleteTaskItem(task.getId(), "fakeid");
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item from completed task
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                activitiProcessEngine.getTaskService().complete(task.getId());
                return null;
            }
        }, requestContext.getRunAsUser(), requestContext.getNetworkId());
        try {
            tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
    } finally {
        cleanupProcessInstance(processInfo.getId());
    }
}
Also used : Task(org.activiti.engine.task.Task) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) MemberOfSite(org.alfresco.rest.api.tests.client.data.MemberOfSite) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 44 with NodeRef

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

the class TaskWorkflowApiTest method testGetTaskVariablesRawVariableTypes.

@Test
public void testGetTaskVariablesRawVariableTypes() throws Exception {
    RequestContext requestContext = initApiClientWithTestUser();
    ProcessInstance processInstance = startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null);
    try {
        Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        assertNotNull(task);
        Calendar dateCal = Calendar.getInstance();
        // Set all supported variables on the task to check the resulting types
        Map<String, Object> variablesToSet = new HashMap<String, Object>();
        variablesToSet.put("testVarString", "string");
        variablesToSet.put("testVarInteger", 1234);
        variablesToSet.put("testVarLong", 123456789L);
        variablesToSet.put("testVarDouble", 1234.5678D);
        variablesToSet.put("testVarFloat", 1234.0F);
        variablesToSet.put("testVarBoolean", Boolean.TRUE);
        variablesToSet.put("testVarDate", dateCal.getTime());
        variablesToSet.put("testVarQName", ContentModel.TYPE_AUTHORITY);
        variablesToSet.put("testVarNodeRef", new ActivitiScriptNode(new NodeRef("workspace:///testNode"), serviceRegistry));
        variablesToSet.put("testVarRawNodeRef", new NodeRef("workspace:///testNode"));
        activitiProcessEngine.getTaskService().setVariablesLocal(task.getId(), variablesToSet);
        Map<String, Object> actualLocalVariables = activitiProcessEngine.getTaskService().getVariablesLocal(task.getId());
        TasksClient tasksClient = publicApiClient.tasksClient();
        // Get all local variables
        JSONObject variables = tasksClient.findTaskVariables(task.getId(), Collections.singletonMap("where", "(scope = local)"));
        assertNotNull(variables);
        JSONObject list = (JSONObject) variables.get("list");
        assertNotNull(list);
        JSONArray entries = (JSONArray) list.get("entries");
        assertNotNull(entries);
        // Check pagination object for size
        JSONObject pagination = (JSONObject) list.get("pagination");
        assertNotNull(pagination);
        assertEquals(actualLocalVariables.size(), ((Long) pagination.get("count")).intValue());
        assertEquals(actualLocalVariables.size(), ((Long) pagination.get("totalItems")).intValue());
        assertEquals(0L, pagination.get("skipCount"));
        assertFalse((Boolean) pagination.get("hasMoreItems"));
        assertEquals(actualLocalVariables.size(), entries.size());
        // Add JSON entries to map for easy access when asserting values
        Map<String, JSONObject> entriesByName = new HashMap<String, JSONObject>();
        for (int i = 0; i < entries.size(); i++) {
            JSONObject var = (JSONObject) entries.get(i);
            entriesByName.put((String) ((JSONObject) var.get("entry")).get("name"), (JSONObject) var.get("entry"));
        }
        // Check all values and types
        JSONObject var = entriesByName.get("testVarString");
        assertNotNull(var);
        assertEquals("d:text", var.get("type"));
        assertEquals("string", var.get("value"));
        var = entriesByName.get("testVarInteger");
        assertNotNull(var);
        assertEquals("d:int", var.get("type"));
        assertEquals(1234L, var.get("value"));
        var = entriesByName.get("testVarLong");
        assertNotNull(var);
        assertEquals("d:long", var.get("type"));
        assertEquals(123456789L, var.get("value"));
        var = entriesByName.get("testVarDouble");
        assertNotNull(var);
        assertEquals("d:double", var.get("type"));
        assertEquals(1234.5678D, var.get("value"));
        var = entriesByName.get("testVarFloat");
        assertNotNull(var);
        assertEquals("d:float", var.get("type"));
        assertEquals(1234.0D, var.get("value"));
        var = entriesByName.get("testVarBoolean");
        assertNotNull(var);
        assertEquals("d:boolean", var.get("type"));
        assertEquals(Boolean.TRUE, var.get("value"));
        var = entriesByName.get("testVarDate");
        assertNotNull(var);
        assertEquals("d:datetime", var.get("type"));
        assertEquals(dateCal.getTime(), parseDate(var, "value"));
        var = entriesByName.get("testVarQName");
        assertNotNull(var);
        assertEquals("d:qname", var.get("type"));
        assertEquals("cm:authority", var.get("value"));
        var = entriesByName.get("testVarRawNodeRef");
        assertNotNull(var);
        assertEquals("d:noderef", var.get("type"));
        assertEquals("workspace:///testNode", var.get("value"));
        var = entriesByName.get("testVarNodeRef");
        assertNotNull(var);
        assertEquals("d:noderef", var.get("type"));
        assertEquals("workspace:///testNode", var.get("value"));
    } finally {
        cleanupProcessInstance(processInstance);
    }
}
Also used : Task(org.activiti.engine.task.Task) HashMap(java.util.HashMap) Calendar(java.util.Calendar) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) JSONArray(org.json.simple.JSONArray) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) JSONObject(org.json.simple.JSONObject) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 45 with NodeRef

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

the class SurfConfigCleaner method beforeDeleteNode.

@Override
public void beforeDeleteNode(NodeRef nodeRef) {
    final String userName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_USERNAME);
    final NodeRef componentsRef = getGlobalComponentsNodeRef();
    final NodeRef usersFolderRef = getGlobalUserFolderNodeRef();
    // ^^^^^ encoded username
    if (usersFolderRef != null) {
        NodeRef userFolderNodeRef = nodeService.getChildByName(usersFolderRef, ContentModel.ASSOC_CONTAINS, encodePath(userName));
        if (userFolderNodeRef != null) {
            // CLOUD-2053: Need to set as temporary to delete node instead of archiving.
            nodeService.addAspect(userFolderNodeRef, ContentModel.ASPECT_TEMPORARY, null);
            nodeService.deleteNode(userFolderNodeRef);
        }
    }
    // ^^^^^ encoded username
    if (componentsRef != null) {
        List<FileInfo> configNodes = getFileNodes(fileFolderService.getFileInfo(componentsRef), buildUserConfigSearchPattern(userName), true).getPage();
        for (FileInfo fileInfo : configNodes) {
            // CLOUD-2053: Need to set as temporary to delete node instead of archiving.
            nodeService.addAspect(fileInfo.getNodeRef(), ContentModel.ASPECT_TEMPORARY, null);
            nodeService.deleteNode(fileInfo.getNodeRef());
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo)

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