Search in sources :

Example 1 with Nodes

use of org.alfresco.rest.api.tests.client.PublicApiClient.Nodes 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 2 with Nodes

use of org.alfresco.rest.api.tests.client.PublicApiClient.Nodes in project alfresco-remote-api by Alfresco.

the class TestCMIS method testCMIS.

/**
 * Tests OpenCMIS api.
 */
@SuppressWarnings("unchecked")
@Test
public void testCMIS() throws Exception {
    // Test Case cloud-2353
    // Test Case cloud-2354
    // Test Case cloud-2356
    // Test Case cloud-2378
    // Test Case cloud-2357
    // Test Case cloud-2358
    // Test Case cloud-2360
    final TestNetwork network1 = getTestFixture().getRandomNetwork();
    Iterator<String> personIt = network1.getPersonIds().iterator();
    final String personId = personIt.next();
    assertNotNull(personId);
    Person person = repoService.getPerson(personId);
    assertNotNull(person);
    // Create a site
    final TestSite site = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            String siteName = "site" + System.currentTimeMillis();
            SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
            TestSite site = network1.createSite(siteInfo);
            return site;
        }
    }, personId, network1.getId());
    publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId));
    CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_10, AlfrescoObjectFactoryImpl.class.getName());
    Nodes nodesProxy = publicApiClient.nodes();
    Comments commentsProxy = publicApiClient.comments();
    String expectedContent = "Ipsum and so on";
    Document doc = null;
    Folder documentLibrary = (Folder) cmisSession.getObjectByPath("/Sites/" + site.getSiteId() + "/documentLibrary");
    FolderNode expectedDocumentLibrary = (FolderNode) CMISNode.createNode(documentLibrary);
    Document testDoc = null;
    Folder testFolder = null;
    FolderNode testFolderNode = null;
    // create some sub-folders and documents
    {
        for (int i = 0; i < 3; i++) {
            Map<String, String> properties = new HashMap<String, String>();
            {
                properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
                properties.put(PropertyIds.NAME, "folder-" + i);
            }
            Folder f = documentLibrary.createFolder(properties);
            FolderNode fn = (FolderNode) CMISNode.createNode(f);
            if (testFolder == null) {
                testFolder = f;
                testFolderNode = fn;
            }
            expectedDocumentLibrary.addFolder(fn);
            for (int k = 0; k < 3; k++) {
                properties = new HashMap<String, String>();
                {
                    properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
                    properties.put(PropertyIds.NAME, "folder-" + k);
                }
                Folder f1 = f.createFolder(properties);
                FolderNode childFolder = (FolderNode) CMISNode.createNode(f1);
                fn.addFolder(childFolder);
            }
            for (int j = 0; j < 3; j++) {
                properties = new HashMap<String, String>();
                {
                    properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
                    properties.put(PropertyIds.NAME, "doc-" + j);
                }
                ContentStreamImpl fileContent = new ContentStreamImpl();
                {
                    ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
                    writer.putContent(expectedContent);
                    ContentReader reader = writer.getReader();
                    fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
                    fileContent.setStream(reader.getContentInputStream());
                }
                Document d = f.createDocument(properties, fileContent, VersioningState.MAJOR);
                if (testDoc == null) {
                    testDoc = d;
                }
                CMISNode childDocument = CMISNode.createNode(d);
                fn.addNode(childDocument);
            }
        }
        for (int i = 0; i < 10; i++) {
            Map<String, String> properties = new HashMap<String, String>();
            {
                properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
                properties.put(PropertyIds.NAME, "doc-" + i);
            }
            ContentStreamImpl fileContent = new ContentStreamImpl();
            {
                ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
                writer.putContent(expectedContent);
                ContentReader reader = writer.getReader();
                fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
                fileContent.setStream(reader.getContentInputStream());
            }
            documentLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
        }
    }
    // try to add and remove ratings, comments, tags to folders created by CMIS
    {
        Aggregate aggregate = new Aggregate(1, null);
        NodeRating expectedNodeRating = new NodeRating("likes", true, aggregate);
        Comment expectedComment = new Comment("commenty", "commenty", false, null, person, person);
        Tag expectedTag = new Tag("taggy");
        NodeRating rating = nodesProxy.createNodeRating(testFolder.getId(), expectedNodeRating);
        expectedNodeRating.expected(rating);
        assertNotNull(rating.getId());
        // REPO-2028 - remove lucene tests
        // Tag tag = nodesProxy.createNodeTag(testFolder.getId(), expectedTag);
        // expectedTag.expected(tag);
        // assertNotNull(tag.getId());
        Comment comment = commentsProxy.createNodeComment(testFolder.getId(), expectedComment);
        expectedComment.expected(comment);
        assertNotNull(comment.getId());
    }
    // try to add and remove ratings, comments, tags to documents created by CMIS
    {
        Aggregate aggregate = new Aggregate(1, null);
        NodeRating expectedNodeRating = new NodeRating("likes", true, aggregate);
        Comment expectedComment = new Comment("commenty", "commenty", false, null, person, person);
        Tag expectedTag = new Tag("taggy");
        NodeRating rating = nodesProxy.createNodeRating(testDoc.getId(), expectedNodeRating);
        expectedNodeRating.expected(rating);
        assertNotNull(rating.getId());
        // REPO-2028 - remove lucene tests
        // Tag tag = nodesProxy.createNodeTag(testDoc.getId(), expectedTag);
        // expectedTag.expected(tag);
        // assertNotNull(tag.getId());
        Comment comment = commentsProxy.createNodeComment(testDoc.getId(), expectedComment);
        expectedComment.expected(comment);
        assertNotNull(comment.getId());
    }
    // descendants
    {
        List<Tree<FileableCmisObject>> descendants = documentLibrary.getDescendants(4);
        expectedDocumentLibrary.checkDescendants(descendants);
    }
    // upload/setContent
    {
        Map<String, String> fileProps = new HashMap<String, String>();
        {
            fileProps.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
            fileProps.put(PropertyIds.NAME, "mydoc-" + GUID.generate() + ".txt");
        }
        ContentStreamImpl fileContent = new ContentStreamImpl();
        {
            ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
            writer.putContent(expectedContent);
            ContentReader reader = writer.getReader();
            fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
            fileContent.setStream(reader.getContentInputStream());
        }
        doc = documentLibrary.createDocument(fileProps, fileContent, VersioningState.MAJOR);
        String nodeId = stripCMISSuffix(doc.getId());
        final NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, nodeId);
        ContentReader reader = TenantUtil.runAsUserTenant(new TenantRunAsWork<ContentReader>() {

            @Override
            public ContentReader doWork() throws Exception {
                ContentReader reader = repoService.getContent(nodeRef, ContentModel.PROP_CONTENT);
                return reader;
            }
        }, personId, network1.getId());
        String actualContent = reader.getContentString();
        assertEquals(expectedContent, actualContent);
    }
    // get content
    {
        ContentStream stream = doc.getContentStream();
        StringWriter writer = new StringWriter();
        IOUtils.copy(stream.getStream(), writer, "UTF-8");
        String actualContent = writer.toString();
        assertEquals(expectedContent, actualContent);
    }
    // get children
    {
        Folder folder = (Folder) cmisSession.getObjectByPath("/Sites/" + site.getSiteId() + "/documentLibrary/" + testFolder.getName());
        ItemIterable<CmisObject> children = folder.getChildren();
        testFolderNode.checkChildren(children);
    }
    // REPO-2028 - remove lucene tests
    // query
    // {
    // Folder folder = (Folder)cmisSession.getObjectByPath("/Sites/" + site.getSiteId() + "/documentLibrary/" + testFolder.getName());
    // String folderId = folder.getId();
    // 
    // Set<String> expectedFolderNames = new HashSet<String>();
    // for(CMISNode n : testFolderNode.getFolderNodes().values())
    // {
    // expectedFolderNames.add((String)n.getProperty("cmis:name"));
    // }
    // int expectedNumFolders = expectedFolderNames.size();
    // int numMatchingFoldersFound = 0;
    // List<CMISNode> results = cmisSession.query("SELECT * FROM cmis:folder WHERE IN_TREE('" + folderId + "')", false, 0, Integer.MAX_VALUE);
    // for(CMISNode node : results)
    // {
    // String name = (String)node.getProperties().get("cmis:name");
    // if(expectedFolderNames.contains(name))
    // {
    // numMatchingFoldersFound++;
    // }
    // }
    // assertEquals(expectedNumFolders, numMatchingFoldersFound);
    // 
    // Set<String> expectedDocNames = new HashSet<String>();
    // for(CMISNode n : testFolderNode.getDocumentNodes().values())
    // {
    // expectedDocNames.add((String)n.getProperty("cmis:name"));
    // }
    // int expectedNumDocs = expectedDocNames.size();
    // int numMatchingDocsFound = 0;
    // results = cmisSession.query("SELECT * FROM cmis:document where IN_TREE('" + folderId + "')", false, 0, Integer.MAX_VALUE);
    // for(CMISNode node : results)
    // {
    // String name = (String)node.getProperties().get("cmis:name");
    // if(expectedDocNames.contains(name))
    // {
    // numMatchingDocsFound++;
    // }
    // }
    // assertEquals(expectedNumDocs, numMatchingDocsFound);
    // }
    // versioning
    {
        String nodeId = stripCMISSuffix(doc.getId());
        final NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, nodeId);
        // checkout
        ObjectId pwcId = doc.checkOut();
        Document pwc = (Document) cmisSession.getObject(pwcId.getId());
        Boolean isCheckedOut = TenantUtil.runAsUserTenant(new TenantRunAsWork<Boolean>() {

            @Override
            public Boolean doWork() throws Exception {
                Boolean isCheckedOut = repoService.isCheckedOut(nodeRef);
                return isCheckedOut;
            }
        }, personId, network1.getId());
        assertTrue(isCheckedOut);
        // checkin with new content
        expectedContent = "Big bad wolf";
        ContentStreamImpl fileContent = new ContentStreamImpl();
        {
            ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
            writer.putContent(expectedContent);
            ContentReader reader = writer.getReader();
            fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
            fileContent.setStream(reader.getContentInputStream());
        }
        ObjectId checkinId = pwc.checkIn(true, Collections.EMPTY_MAP, fileContent, "checkin 1");
        doc = (Document) cmisSession.getObject(checkinId.getId());
        isCheckedOut = TenantUtil.runAsUserTenant(new TenantRunAsWork<Boolean>() {

            @Override
            public Boolean doWork() throws Exception {
                Boolean isCheckedOut = repoService.isCheckedOut(nodeRef);
                return isCheckedOut;
            }
        }, personId, network1.getId());
        assertFalse(isCheckedOut);
        // check that the content has been updated
        ContentStream stream = doc.getContentStream();
        StringWriter writer = new StringWriter();
        IOUtils.copy(stream.getStream(), writer, "UTF-8");
        String actualContent = writer.toString();
        assertEquals(expectedContent, actualContent);
        List<Document> allVersions = doc.getAllVersions();
        assertEquals(2, allVersions.size());
        assertEquals("2.0", allVersions.get(0).getVersionLabel());
        assertEquals(CMIS_VERSION_10, allVersions.get(1).getVersionLabel());
    }
    {
        // https://issues.alfresco.com/jira/browse/PUBLICAPI-95
        // Test that documents are created with autoVersion=true
        Map<String, String> fileProps = new HashMap<String, String>();
        {
            fileProps.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
            fileProps.put(PropertyIds.NAME, "mydoc-" + GUID.generate() + ".txt");
        }
        ContentStreamImpl fileContent = new ContentStreamImpl();
        {
            ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
            writer.putContent("Ipsum and so on");
            ContentReader reader = writer.getReader();
            fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
            fileContent.setStream(reader.getContentInputStream());
        }
        {
            // a versioned document
            Document autoVersionedDoc = documentLibrary.createDocument(fileProps, fileContent, VersioningState.MAJOR);
            String objectId = autoVersionedDoc.getId();
            String bareObjectId = stripCMISSuffix(objectId);
            final NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, bareObjectId);
            Boolean autoVersion = TenantUtil.runAsUserTenant(new TenantRunAsWork<Boolean>() {

                @Override
                public Boolean doWork() throws Exception {
                    Boolean autoVersion = (Boolean) repoService.getProperty(nodeRef, ContentModel.PROP_AUTO_VERSION);
                    return autoVersion;
                }
            }, personId, network1.getId());
            assertEquals(Boolean.TRUE, autoVersion);
        }
        // https://issues.alfresco.com/jira/browse/PUBLICAPI-92
        // Test that a get on an objectId without a version suffix returns the current version of the document
        {
            // do a few checkout, checkin cycles to create some versions
            fileProps = new HashMap<String, String>();
            {
                fileProps.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
                fileProps.put(PropertyIds.NAME, "mydoc-" + GUID.generate() + ".txt");
            }
            Document autoVersionedDoc = documentLibrary.createDocument(fileProps, fileContent, VersioningState.MAJOR);
            String objectId = autoVersionedDoc.getId();
            String bareObjectId = stripCMISSuffix(objectId);
            for (int i = 0; i < 3; i++) {
                Document doc1 = (Document) cmisSession.getObject(bareObjectId);
                ObjectId pwcId = doc1.checkOut();
                Document pwc = (Document) cmisSession.getObject(pwcId.getId());
                ContentStreamImpl contentStream = new ContentStreamImpl();
                {
                    ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
                    expectedContent = GUID.generate();
                    writer.putContent(expectedContent);
                    ContentReader reader = writer.getReader();
                    contentStream.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
                    contentStream.setStream(reader.getContentInputStream());
                }
                pwc.checkIn(true, Collections.EMPTY_MAP, contentStream, "checkin " + i);
            }
            // get the object, supplying an objectId without a version suffix
            Document doc1 = (Document) cmisSession.getObject(bareObjectId);
            String versionLabel = doc1.getVersionLabel();
            ContentStream cs = doc1.getContentStream();
            String content = IOUtils.toString(cs.getStream());
            assertEquals("4.0", versionLabel);
            assertEquals(expectedContent, content);
        }
    }
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) CMISNode(org.alfresco.rest.api.tests.client.data.CMISNode) FolderNode(org.alfresco.rest.api.tests.client.data.FolderNode) HashMap(java.util.HashMap) AlfrescoDocument(org.alfresco.cmis.client.AlfrescoDocument) Document(org.apache.chemistry.opencmis.client.api.Document) AlfrescoFolder(org.alfresco.cmis.client.AlfrescoFolder) Folder(org.apache.chemistry.opencmis.client.api.Folder) NodeRating(org.alfresco.rest.api.tests.client.data.NodeRating) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) StringWriter(java.io.StringWriter) SiteInformation(org.alfresco.rest.api.tests.RepoService.SiteInformation) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) FileableCmisObject(org.apache.chemistry.opencmis.client.api.FileableCmisObject) Comment(org.alfresco.rest.api.tests.client.data.Comment) AlfrescoObjectFactoryImpl(org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl) CmisSession(org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession) ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) Comments(org.alfresco.rest.api.tests.client.PublicApiClient.Comments) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) ContentReader(org.alfresco.service.cmr.repository.ContentReader) CmisUpdateConflictException(org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) Nodes(org.alfresco.rest.api.tests.client.PublicApiClient.Nodes) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) ItemIterable(org.apache.chemistry.opencmis.client.api.ItemIterable) Tag(org.alfresco.rest.api.tests.client.data.Tag) Aggregate(org.alfresco.rest.api.tests.client.data.NodeRating.Aggregate) Person(org.alfresco.rest.api.tests.client.data.Person) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) VersionableAspectTest(org.alfresco.repo.version.VersionableAspectTest) Test(org.junit.Test)

Example 3 with Nodes

use of org.alfresco.rest.api.tests.client.PublicApiClient.Nodes in project alfresco-remote-api by Alfresco.

the class TestNodeComments method testNodeComments.

@Test
public // TODO test update comment and modifiedBy in result is a person object
void testNodeComments() throws Exception {
    Comments commentsProxy = publicApiClient.comments();
    Nodes nodesProxy = publicApiClient.nodes();
    People peopleProxy = publicApiClient.people();
    // invalid node id
    try {
        Comment comment = new Comment("Test Comment 4", "Test Comment 4");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.createNodeComment(GUID.generate(), comment);
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // person from the same network - no permission
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        Comment comment = new Comment("Test Comment 4", "Test Comment 4");
        commentsProxy.createNodeComment(nodeRef1.getId(), comment);
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
    }
    // Test Case cloud-2196
    // multi-byte characters, create and update comments
    {
        Comment[] multiByteComments = new Comment[] { new Comment("ڠڡڢ", "ڠڡڢ"), new Comment("\u67e5\u770b\u5168\u90e8", "\u67e5\u770b\u5168\u90e8") };
        Map<String, Comment> createdComments = new HashMap<String, Comment>();
        for (Comment comment : multiByteComments) {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Comment ret = commentsProxy.createNodeComment(nodeRef2.getId(), comment);
            createdComments.put(ret.getId(), ret);
        }
        // test that it is possible to add comment to custom type node
        commentsProxy.createNodeComment(customTypeObject.getId(), new Comment("Custom type node comment", "The Comment"));
        try {
            // test that it is not possible to add comment to cm:object node
            commentsProxy.createNodeComment(cmObjectNodeRef.getId(), new Comment("CM Object node comment", "The Comment"));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        // get comments of the non-folder/non-document nodeRef
        try {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems);
            commentsProxy.getNodeComments(cmObjectNodeRef.getId(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef2.getId(), createParams(paging, null));
        List<Comment> retComments = resp.getList();
        assertEquals(2, retComments.size());
        for (Comment comment : retComments) {
            String commentId = comment.getId();
            Comment expectedComment = createdComments.get(commentId);
            expectedComment.expected(comment);
        }
        Comment[] multiByteCommentUpdates = new Comment[] { new Comment("ӉӋӐӞ", "ӉӋӐӞ"), new Comment("\u4e00\u4e01\u4e02\u4e03", "\u4e00\u4e01\u4e02\u4e03") };
        Map<String, Comment> updatedComments = new HashMap<String, Comment>();
        for (Comment comment : multiByteCommentUpdates) {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Comment ret = commentsProxy.createNodeComment(nodeRef2.getId(), comment);
            updatedComments.put(ret.getId(), ret);
        }
        skipCount = 0;
        maxItems = 2;
        paging = getPaging(skipCount, maxItems);
        resp = commentsProxy.getNodeComments(nodeRef2.getId(), createParams(paging, null));
        retComments = resp.getList();
        assertEquals(2, retComments.size());
        for (Comment comment : retComments) {
            String commentId = comment.getId();
            Comment expectedComment = updatedComments.get(commentId);
            expectedComment.expected(comment);
        }
    }
    {
        // special characters
        Comment comment = new Comment("", "?*^&*(,");
        List<Comment> expectedComments = new ArrayList<Comment>(1);
        expectedComments.add(comment);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.createNodeComment(nodeRef3.getId(), comment);
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef3.getId(), createParams(paging, null));
        checkList(expectedComments.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
    }
    try {
        Comment comment = new Comment("", "");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.createNodeComment(nodeRef2.getId(), comment);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }
    DateFormat format = PublicApiDateFormat.getDateFormat();
    final List<Comment> expectedComments = new ArrayList<Comment>(10);
    final List<Comment> comments = new ArrayList<Comment>(10);
    comments.add(new Comment("Test Comment 4", "Test Comment 4"));
    comments.add(new Comment("Test Comment 1", "Test Comment 1"));
    comments.add(new Comment("Test Comment 3", "Test Comment 3"));
    comments.add(new Comment("Test Comment 2", "Test Comment 2"));
    {
        Date time = new Date();
        for (Comment comment : comments) {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Comment resp = commentsProxy.createNodeComment(nodeRef1.getId(), comment);
            // check response
            assertEquals(comment.getContent(), resp.getContent());
            assertFalse(StringUtils.isEmpty(resp.getCreatedBy().getDisplayName()));
            assertEquals(resp.getCreatedBy().getDisplayName(), person11.getDisplayName());
            assertTrue(format.parse(resp.getCreatedAt()).after(time));
            person11.expected(resp.getCreatedBy());
            assertNotNull(resp.getId());
            expectedComments.add(resp);
        }
        // check activities have been raised
        repoService.generateFeed();
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Paging paging = getPaging(0, Integer.MAX_VALUE);
            ListResponse<Activity> activities = peopleProxy.getActivities(person11.getId(), createParams(paging, null));
            boolean found = false;
            for (Activity activity : activities.getList()) {
                String activityType = activity.getActivityType();
                if (activityType.equals(ActivityType.COMMENT_CREATED)) {
                    Map<String, Object> summary = activity.getSummary();
                    assertNotNull(summary);
                    String objectId = (String) summary.get("objectId");
                    assertNotNull(objectId);
                    if (nodeRef1.getId().equals(objectId)) {
                        found = true;
                        break;
                    }
                }
            }
            assertTrue(found);
        }
    }
    // try to add a comment to a comment
    try {
        Comment comment = comments.get(0);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        Comment newComment = commentsProxy.createNodeComment(nodeRef1.getId(), comment);
        expectedComments.add(newComment);
        commentsProxy.createNodeComment(newComment.getId(), comment);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // try to add a comment to a tag
    try {
        Comment comment = comments.get(0);
        Tag tag = new Tag("taggification");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        Tag addedTag = nodesProxy.createNodeTag(nodeRef1.getId(), tag);
        commentsProxy.createNodeComment(addedTag.getId(), comment);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // add a comment to a folder
    {
        Date time = new Date();
        Comment comment = comments.get(0);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        Comment resp = commentsProxy.createNodeComment(folderNodeRef1.getId(), comment);
        // check response
        assertEquals(comment.getContent(), resp.getContent());
        assertTrue(format.parse(resp.getCreatedAt()).after(time));
        person11.expected(resp.getCreatedBy());
        assertNotNull(resp.getId());
    }
    Collections.sort(expectedComments);
    // Test Case cloud-2205
    // Test Case cloud-2217
    // Test Case cloud-1517
    // pagination
    {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef1.getId(), createParams(paging, null));
        checkList(expectedComments.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
    }
    {
        int skipCount = 2;
        int maxItems = 10;
        Paging paging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef1.getId(), createParams(paging, null));
        checkList(expectedComments.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
    }
    // invalid node id - 404
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging expectedPaging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        commentsProxy.getNodeComments("invalid", createParams(expectedPaging, null));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        commentsProxy.getNodeComments(nodeRef1.getId() + ";pwc", createParams(paging, null));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // suffix the node id with a version number
    {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef1.getId() + ";3.0", createParams(paging, null));
        checkList(expectedComments.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
    }
    // in which the comment resides
    try {
        int skipCount = 0;
        int maxItems = 2;
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        Paging expectedPaging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        commentsProxy.getNodeComments(nodeRef1.getId(), createParams(expectedPaging, null));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
    }
    // document owned by another person in another network, the user is not a member of that network
    try {
        int skipCount = 0;
        int maxItems = 2;
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
        Paging expectedPaging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        commentsProxy.getNodeComments(nodeRef1.getId(), createParams(expectedPaging, null));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }
    // Test Case cloud-1971
    // invalid methods
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        int skipCount = 0;
        int maxItems = 2;
        Paging expectedPaging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef1.getId(), createParams(expectedPaging, null));
        List<Comment> nodeComments = resp.getList();
        assertTrue(nodeComments.size() > 0);
        Comment comment = nodeComments.get(0);
        try {
            commentsProxy.create("nodes", nodeRef1.getId(), "comments", comment.getId(), null, "Unable to POST to a node comment");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            commentsProxy.update("nodes", nodeRef1.getId(), "comments", null, null, "Unable to PUT node comments");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            commentsProxy.getSingle("nodes", nodeRef1.getId(), "comments", comment.getId(), "Unable to GET a node comment");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            commentsProxy.remove("nodes", nodeRef1.getId(), "comments", null, "Unable to DELETE node comments");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
    // Test Case cloud-2184
    // update comments
    {
        Comment[] testComments = new Comment[] { new Comment("ӉӋӐӞ", "ӉӋӐӞ"), new Comment("\u4e00\u4e01\u4e02\u4e03", "\u4e00\u4e01\u4e02\u4e03") };
        List<Comment> mlComments = new ArrayList<Comment>();
        mlComments.add(new Comment("ӉӋӐӞ", "ӉӋӐӞ"));
        mlComments.add(new Comment("\u4e00\u4e01\u4e02\u4e03", "\u4e00\u4e01\u4e02\u4e03"));
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        // create some comments
        Map<String, Comment> createdComments = new HashMap<String, Comment>();
        for (Comment comment : testComments) {
            Comment ret = commentsProxy.createNodeComment(nodeRef4.getId(), comment);
            createdComments.put(ret.getId(), ret);
        }
        // update them with multi-byte content
        int i = 0;
        List<Comment> updatedComments = new ArrayList<Comment>();
        for (Comment comment : createdComments.values()) {
            Comment updateComment = mlComments.get(i);
            Comment ret = commentsProxy.updateNodeComment(nodeRef4.getId(), comment.getId(), updateComment);
            updatedComments.add(ret);
            i++;
        }
        Collections.sort(updatedComments);
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, mlComments.size(), mlComments.size());
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef4.getId(), createParams(paging, null));
        checkList(updatedComments, paging.getExpectedPaging(), resp);
    }
    // invalid node id
    try {
        Comment comment = expectedComments.get(1);
        Comment update = new Comment("Test Comment 4", "Test Comment 4");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.updateNodeComment(GUID.generate(), comment.getId(), update);
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // invalid comment id
    try {
        Comment update = new Comment("Test Comment 4", "Test Comment 4");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.updateNodeComment(nodeRef1.getId(), GUID.generate(), update);
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // person from the same network, not comment creator
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        Comment comment = expectedComments.get(1);
        Comment update = new Comment("Test Comment 4", "Test Comment 4");
        commentsProxy.updateNodeComment(nodeRef1.getId(), comment.getId(), update);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
    }
    // person from a different network
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person22.getId()));
        Comment comment = expectedComments.get(1);
        Comment update = new Comment("Test Comment 4", "Test Comment 4");
        commentsProxy.updateNodeComment(nodeRef1.getId(), comment.getId(), update);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }
    // successful update
    {
        Date time = new Date();
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        Comment comment = expectedComments.get(1);
        Comment update = new Comment("Updated comment", "Updated comment");
        Comment resp = commentsProxy.updateNodeComment(nodeRef1.getId(), comment.getId(), update);
        // simulate a user edit to a comment
        Thread.sleep(100);
        Comment expected = new Comment(comment);
        expected.setTitle("Updated comment");
        expected.setEdited(true);
        expected.setContent("Updated comment");
        expected.setModifiedBy(repoService.getPerson(person11.getId()));
        expected.setModifiedAt(PublicApiDateFormat.getDateFormat().format(time));
        expected.expected(resp);
    }
    // invalid node ref
    try {
        Comment comment = expectedComments.get(1);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.removeNodeComment(GUID.generate(), comment.getId());
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // invalid comment id
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.removeNodeComment(nodeRef1.getId(), GUID.generate());
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // successful delete
    {
        Comment toDelete = expectedComments.get(1);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        commentsProxy.removeNodeComment(nodeRef1.getId(), toDelete.getId());
        // check it's been removed
        int skipCount = 0;
        int maxItems = Integer.MAX_VALUE;
        Paging expectedPaging = getPaging(skipCount, maxItems, expectedComments.size(), expectedComments.size());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        ListResponse<Comment> resp = commentsProxy.getNodeComments(nodeRef1.getId(), createParams(expectedPaging, null));
        List<Comment> actualComments = resp.getList();
        assertFalse(actualComments.contains(toDelete));
    }
    // PUT: test update with null/empty comment
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        Comment comment = new Comment();
        comment.setContent("my comment");
        Comment createdComment = commentsProxy.createNodeComment(nodeRef1.getId(), comment);
        Comment updatedComment = new Comment();
        updatedComment.setContent(null);
        commentsProxy.updateNodeComment(nodeRef1.getId(), createdComment.getId(), updatedComment);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }
    // ACE-5463
    testSkipCountHighValue(expectedComments, commentsProxy);
}
Also used : Comment(org.alfresco.rest.api.tests.client.data.Comment) ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) Comments(org.alfresco.rest.api.tests.client.PublicApiClient.Comments) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) People(org.alfresco.rest.api.tests.client.PublicApiClient.People) ArrayList(java.util.ArrayList) Activity(org.alfresco.rest.api.tests.client.data.Activity) Nodes(org.alfresco.rest.api.tests.client.PublicApiClient.Nodes) Date(java.util.Date) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) DateFormat(java.text.DateFormat) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Tag(org.alfresco.rest.api.tests.client.data.Tag) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with Nodes

use of org.alfresco.rest.api.tests.client.PublicApiClient.Nodes in project alfresco-remote-api by Alfresco.

the class TestNodeRatings method testNodeRatings.

@Test
public void testNodeRatings() 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
    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 person11 = people.get(0);
    final TestPerson person12 = people.get(1);
    final TestPerson person21 = people.get(2);
    PublicApiClient.Sites sitesProxy = publicApiClient.sites();
    Comments commentsProxy = publicApiClient.comments();
    People peopleProxy = publicApiClient.people();
    Nodes nodesProxy = publicApiClient.nodes();
    DateFormat format = PublicApiDateFormat.getDateFormat();
    // Create site and document
    publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
    String siteId = "TESTSITE" + GUID.generate();
    Site site = new SiteImpl(siteId, siteId, siteId, SiteVisibility.PRIVATE.toString());
    site = sitesProxy.createSite(site);
    SiteContainer sc = sitesProxy.getSingleSiteContainer(site.getSiteId(), "documentLibrary");
    final String node1Id = createTextFile(sc.getId(), "Test Doc 1.txt", "Test Content").getId();
    // TEMP - pending remote api to list node ratings
    NodeRef nodeRef1 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, node1Id);
    // try to add a rating to a comment
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        Comment comment = new Comment("Test Comment", "Test Comment");
        Comment newComment = commentsProxy.createNodeComment(node1Id, comment);
        NodeRating rating = new NodeRating("likes", true);
        nodesProxy.createNodeRating(newComment.getId(), rating);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // invalid node id
    try {
        NodeRating rating = new NodeRating("likes", true);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(GUID.generate(), rating);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // try to add a rating to a tag
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        Tag tag = new Tag("testTag");
        Tag newTag = nodesProxy.createNodeTag(node1Id, tag);
        NodeRating rating = new NodeRating("likes", true);
        nodesProxy.createNodeRating(newTag.getId(), rating);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // invalid rating scheme
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(node1Id, new NodeRating("missingRatingScheme", Double.valueOf(1.0f)));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }
    // invalid rating
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(node1Id, new NodeRating("likes", Double.valueOf(2.0f)));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }
    // invalid rating
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(node1Id, new NodeRating("fiveStar", true));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }
    // invalid rating - can't rate own content for fiveStar
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(node1Id, new NodeRating("fiveStar", 5));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }
    // valid ratings
    {
        NodeRating rating = new NodeRating("likes", true);
        Date time = new Date();
        // rate by multiple users in more than 1 network
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        NodeRating ret = nodesProxy.createNodeRating(node1Id, rating);
        assertEquals(rating.getMyRating(), ret.getMyRating());
        assertTrue(format.parse(ret.getRatedAt()).after(time));
        assertEquals(rating.getId(), ret.getId());
        assertEquals(new NodeRating.Aggregate(1, null), ret.getAggregate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        ret = nodesProxy.createNodeRating(node1Id, rating);
        assertEquals(rating.getMyRating(), ret.getMyRating());
        assertTrue(format.parse(ret.getRatedAt()).after(time));
        assertEquals(rating.getId(), ret.getId());
        assertEquals(new NodeRating.Aggregate(2, null), ret.getAggregate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        ret = nodesProxy.createNodeRating(node1Id, rating);
        assertEquals(rating.getMyRating(), ret.getMyRating());
        assertTrue(format.parse(ret.getRatedAt()).after(time));
        assertEquals(rating.getId(), ret.getId());
        assertEquals(new NodeRating.Aggregate(2, null), ret.getAggregate());
        // different network - unauthorized
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
            nodesProxy.createNodeRating(node1Id, rating);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }
        // Test Case cloud-2209
        // Test Case cloud-2220
        // Test Case cloud-1520
        // check that the node ratings are there, test paging
        {
            // person11
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            List<NodeRating> expectedRatings = repoService.getNodeRatings(person11.getId(), network1.getId(), nodeRef1);
            {
                int skipCount = 0;
                int maxItems = 1;
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(node1Id, createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
            }
            {
                int skipCount = 1;
                int maxItems = Integer.MAX_VALUE;
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(node1Id, createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
            }
            {
                int skipCount = 1;
                int maxItems = expectedRatings.size();
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(node1Id, createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
            }
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
            // person12
            List<NodeRating> expectedRatings = repoService.getNodeRatings(person12.getId(), network1.getId(), nodeRef1);
            {
                int skipCount = 0;
                int maxItems = 1;
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(node1Id, createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
            }
            {
                int skipCount = 1;
                int maxItems = Integer.MAX_VALUE;
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(node1Id, createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
            }
        }
        {
            // person21
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
            List<NodeRating> expectedRatings = Collections.emptyList();
            try {
                int skipCount = 0;
                int maxItems = 1;
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                nodesProxy.getNodeRatings(node1Id, createParams(paging, null));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
            }
        }
        // invalid node id
        try {
            int skipCount = 1;
            int maxItems = Integer.MAX_VALUE;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Paging paging = getPaging(skipCount, maxItems);
            nodesProxy.getNodeRatings(GUID.generate(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // check activities have been raised for the created ratings
        repoService.generateFeed();
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Paging paging = getPaging(0, Integer.MAX_VALUE);
            ListResponse<Activity> activities = peopleProxy.getActivities(person11.getId(), createParams(paging, null));
            boolean found = false;
            for (Activity activity : activities.getList()) {
                String activityType = activity.getActivityType();
                if (activityType.equals(ActivityType.FILE_LIKED)) {
                    Map<String, Object> summary = activity.getSummary();
                    assertNotNull(summary);
                    String objectId = (String) summary.get("objectId");
                    assertNotNull(objectId);
                    if (node1Id.equals(objectId)) {
                        found = true;
                        break;
                    }
                }
            }
            assertTrue(found);
        }
    }
    {
        // remove node rating
        NodeRating rating = new NodeRating("likes", null);
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
            nodesProxy.removeNodeRating(node1Id, rating);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
            nodesProxy.removeNodeRating(node1Id, rating);
        }
        // check list
        {
            List<NodeRating> ratings = repoService.getNodeRatings(person11.getId(), network1.getId(), nodeRef1);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Paging paging = getPaging(skipCount, maxItems, ratings.size(), ratings.size());
            ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(node1Id, createParams(paging, null));
            checkList(ratings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
    }
    // get a node rating
    // 1977
    {
        NodeRating rating = new NodeRating("likes", true);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        NodeRating expected = nodesProxy.createNodeRating(node1Id, rating);
        NodeRating actual = nodesProxy.getNodeRating(node1Id, "likes");
        expected.expected(actual);
    }
    {
        // update node rating
        NodeRating rating = new NodeRating("fiveStar", 2);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        // create initial rating
        NodeRating createdRating = nodesProxy.createNodeRating(node1Id, rating);
        NodeRating updateRating = new NodeRating(createdRating.getId(), 5);
        // update - not supported
        try {
            nodesProxy.updateNodeRating(node1Id, updateRating);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
    // Test Case cloud-1977
    // invalid methods
    {
        try {
            // -ve test - cannot create multiple ratings in single POST call (unsupported)
            List<NodeRating> ratings = new ArrayList<>(2);
            ratings.add(new NodeRating("likes", true));
            ratings.add(new NodeRating("likes", false));
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.create("nodes", node1Id, "ratings", null, JSONArray.toJSONString(ratings), "Unable to POST to multiple ratings");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        // get an arbitrary rating
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(node1Id, createParams(getPaging(0, Integer.MAX_VALUE), null));
        List<NodeRating> nodeRatings = resp.getList();
        assertTrue(nodeRatings.size() > 0);
        try {
            NodeRating rating = new NodeRating("likes", true);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.create("nodes", node1Id, "ratings", "likes", rating.toJSON().toString(), "Unable to POST to a node rating");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.update("nodes", node1Id, "ratings", null, null, "Unable to PUT node ratings");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.remove("nodes", node1Id, "ratings", null, "Unable to DELETE node ratings");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            NodeRating rating = nodeRatings.get(0);
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.update("nodes", node1Id, "ratings", rating.getId(), null, "Unable to PUT a node rating");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
    // Test case ACE-5453
    {
        try {
            testSkipCountLargeValue(person11, network1, nodeRef1, nodesProxy);
        } catch (PublicApiException e) {
            fail();
        }
    }
}
Also used : TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) Site(org.alfresco.rest.api.tests.client.data.Site) ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) ArrayList(java.util.ArrayList) Activity(org.alfresco.rest.api.tests.client.data.Activity) NodeRating(org.alfresco.rest.api.tests.client.data.NodeRating) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Comment(org.alfresco.rest.api.tests.client.data.Comment) Comments(org.alfresco.rest.api.tests.client.PublicApiClient.Comments) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) People(org.alfresco.rest.api.tests.client.PublicApiClient.People) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Nodes(org.alfresco.rest.api.tests.client.PublicApiClient.Nodes) Date(java.util.Date) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) SiteImpl(org.alfresco.rest.api.tests.client.data.SiteImpl) DateFormat(java.text.DateFormat) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) Tag(org.alfresco.rest.api.tests.client.data.Tag) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) SiteContainer(org.alfresco.rest.api.tests.client.data.SiteContainer) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 List (java.util.List)4 Comments (org.alfresco.rest.api.tests.client.PublicApiClient.Comments)4 Nodes (org.alfresco.rest.api.tests.client.PublicApiClient.Nodes)4 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)4 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)4 Comment (org.alfresco.rest.api.tests.client.data.Comment)4 Tag (org.alfresco.rest.api.tests.client.data.Tag)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)3 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)3 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)3 ListResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse)3 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 DateFormat (java.text.DateFormat)2 Date (java.util.Date)2 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)2