Search in sources :

Example 1 with RangedDateProperty

use of org.alfresco.service.cmr.blog.BlogService.RangedDateProperty in project alfresco-repository by Alfresco.

the class BlogServiceImplTest method createTaggedDraftBlogPost.

@Category(RedundantTests.class)
@Test
public void createTaggedDraftBlogPost() throws Exception {
    // Our tags, which are a mixture of English, Accented European and Chinese
    final List<String> tags = Arrays.asList(new String[] { "alpha", "beta", "gamma", "fran\u00e7ais", "chinese_\u535a\u5ba2" });
    // Create a list of Blog Posts, all drafts, each with one of the tags above.
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<List<NodeRef>>() {

        @Override
        public List<NodeRef> execute() throws Throwable {
            List<NodeRef> results = new ArrayList<NodeRef>();
            for (String tag : tags) {
                final String blogTitle = "draftWithTag" + tag;
                BlogPostInfo newBlogPost = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, blogTitle, "Hello world", true);
                TAGGING_SERVICE.addTags(newBlogPost.getNodeRef(), Arrays.asList(new String[] { tag }));
                testNodesToTidy.add(newBlogPost.getNodeRef());
                results.add(newBlogPost.getNodeRef());
            }
            return results;
        }
    });
    // Check we get the correct tags back
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now we'll recover these blogposts & we should expect to find the same tags.
            Set<String> expectedTags = new HashSet<String>();
            expectedTags.addAll(tags);
            PagingRequest pagingReq = new PagingRequest(0, 10, null);
            PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.getDrafts(BLOG_CONTAINER_NODE, ADMIN_USER, pagingReq);
            assertEquals("Wrong number of blog posts", tags.size(), pagedResults.getPage().size());
            for (BlogPostInfo bpi : pagedResults.getPage()) {
                NodeRef blogNode = bpi.getNodeRef();
                List<String> recoveredTags = TAGGING_SERVICE.getTags(blogNode);
                assertEquals("Wrong number of tags", 1, recoveredTags.size());
                String tag = recoveredTags.get(0);
                assertTrue("Tag found on node but not expected: " + tag, expectedTags.remove(tag));
            }
            assertTrue("Not all tags were recovered from a blogpost", expectedTags.isEmpty());
            return null;
        }
    });
    // Check we can find the posts by their tags
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            PagingRequest pagingReq = new PagingRequest(0, 10, null);
            RangedDateProperty dates = new RangedDateProperty(null, null, ContentModel.PROP_CREATED);
            for (String tag : tags) {
                PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, dates, tag, pagingReq);
                // Check we found our post
                assertEquals("Wrong number of blog posts for " + tag, 1, pagedResults.getPage().size());
            }
            return null;
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) PagingResults(org.alfresco.query.PagingResults) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RangedDateProperty(org.alfresco.service.cmr.blog.BlogService.RangedDateProperty) BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo) PagingRequest(org.alfresco.query.PagingRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) Category(org.junit.experimental.categories.Category) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 2 with RangedDateProperty

use of org.alfresco.service.cmr.blog.BlogService.RangedDateProperty in project alfresco-repository by Alfresco.

the class BlogServiceImplTest method testGetBlogPostsByTagPaging.

/**
 * Test that correct paging info is returned when searching for tagged blog posts.
 */
@Test
@Category(RedundantTests.class)
public void testGetBlogPostsByTagPaging() throws Exception {
    final String tagToSearchBy = "testtag";
    final int numberOfBlogPostsTagged = 2;
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<List<NodeRef>>() {

        @Override
        public List<NodeRef> execute() throws Throwable {
            List<NodeRef> results = new ArrayList<NodeRef>();
            do {
                final String blogTitle = "blogTitle" + GUID.generate();
                BlogPostInfo newBlogPost = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, blogTitle, "Hello world", false);
                TAGGING_SERVICE.addTags(newBlogPost.getNodeRef(), Arrays.asList(new String[] { tagToSearchBy }));
                testNodesToTidy.add(newBlogPost.getNodeRef());
                results.add(newBlogPost.getNodeRef());
            } while (results.size() < numberOfBlogPostsTagged);
            return results;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            PagingRequest pagingReq = new PagingRequest(0, 1, null);
            RangedDateProperty dates = new RangedDateProperty(null, null, ContentModel.PROP_CREATED);
            PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, dates, tagToSearchBy, pagingReq);
            assertEquals("Wrong number of blog posts on page 1 for " + tagToSearchBy, 1, pagedResults.getPage().size());
            assertEquals("Wrong total number of blog posts for " + tagToSearchBy, new Pair<Integer, Integer>(2, 2), pagedResults.getTotalResultCount());
            assertEquals("There should still be blog posts available to be retrieved for " + tagToSearchBy, true, pagedResults.hasMoreItems());
            pagingReq = new PagingRequest(1, 1, null);
            pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, dates, tagToSearchBy, pagingReq);
            assertEquals("Wrong number of blog posts on page 2 for " + tagToSearchBy, 1, pagedResults.getPage().size());
            assertEquals("Wrong total number of blog posts for " + tagToSearchBy, new Pair<Integer, Integer>(2, 2), pagedResults.getTotalResultCount());
            assertEquals("All blog posts should have been retrieved by now for " + tagToSearchBy, false, pagedResults.hasMoreItems());
            return null;
        }
    });
}
Also used : PagingResults(org.alfresco.query.PagingResults) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RangedDateProperty(org.alfresco.service.cmr.blog.BlogService.RangedDateProperty) BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo) PagingRequest(org.alfresco.query.PagingRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair) Category(org.junit.experimental.categories.Category) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 3 with RangedDateProperty

use of org.alfresco.service.cmr.blog.BlogService.RangedDateProperty in project alfresco-repository by Alfresco.

the class BlogServiceImplTest method findBlogPostsByPublishedDate.

/**
 * This test method uses the eventually consistent find*() method and so may fail if Lucene is disabled.
 */
@Category(RedundantTests.class)
@Test
public void findBlogPostsByPublishedDate() throws Exception {
    final List<String> tags = Arrays.asList(new String[] { "hello", "goodbye" });
    // Going to set some specific published dates on these blog posts & query by date.
    final Calendar cal = Calendar.getInstance();
    cal.set(1971, 6, 15);
    final Date _1971 = cal.getTime();
    cal.set(1975, 0, 1);
    final Date _1975 = cal.getTime();
    cal.set(1980, 0, 1);
    final Date _1980 = cal.getTime();
    cal.set(1981, 0, 1);
    final Date _1981 = cal.getTime();
    cal.set(1985, 6, 15);
    final Date _1985 = cal.getTime();
    cal.set(1991, 6, 15);
    final Date _1991 = cal.getTime();
    final Map<Integer, NodeRef> blogPosts = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Map<Integer, NodeRef>>() {

        @Override
        public Map<Integer, NodeRef> execute() throws Throwable {
            Map<Integer, NodeRef> result = new HashMap<Integer, NodeRef>();
            // Create some blog posts. They'll all be published 'now' of course...
            final BlogPostInfo blogPost1971 = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "publishedPostWithTags1971", "Hello world", true);
            final BlogPostInfo blogPost1981 = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "publishedPostWithTags1981", "Hello world", true);
            final BlogPostInfo blogPost1991 = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "publishedPostWithTags1991", "Hello world", true);
            TAGGING_SERVICE.addTags(blogPost1971.getNodeRef(), tags);
            TAGGING_SERVICE.addTags(blogPost1981.getNodeRef(), tags);
            TAGGING_SERVICE.addTags(blogPost1991.getNodeRef(), tags);
            testNodesToTidy.add(blogPost1971.getNodeRef());
            testNodesToTidy.add(blogPost1981.getNodeRef());
            testNodesToTidy.add(blogPost1991.getNodeRef());
            // We need to 'cheat' and set the nodes' cm:published dates to specific values.
            NODE_SERVICE.setProperty(blogPost1971.getNodeRef(), ContentModel.PROP_PUBLISHED, _1971);
            NODE_SERVICE.setProperty(blogPost1981.getNodeRef(), ContentModel.PROP_PUBLISHED, _1981);
            NODE_SERVICE.setProperty(blogPost1991.getNodeRef(), ContentModel.PROP_PUBLISHED, _1991);
            result.put(1971, blogPost1971.getNodeRef());
            result.put(1981, blogPost1981.getNodeRef());
            result.put(1991, blogPost1991.getNodeRef());
            return result;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @SuppressWarnings("deprecation")
        @Override
        public Void execute() throws Throwable {
            // Quick sanity check: Did our cheating with the cm:created dates work?
            assertEquals("Incorrect published date", 71, ((Date) NODE_SERVICE.getProperty(blogPosts.get(1971), ContentModel.PROP_PUBLISHED)).getYear());
            PagingRequest pagingReq = new PagingRequest(0, 10, null);
            final RangedDateProperty publishedBefore1980 = new RangedDateProperty(null, _1980, ContentModel.PROP_PUBLISHED);
            final RangedDateProperty publishedAfter1980 = new RangedDateProperty(_1980, null, ContentModel.PROP_PUBLISHED);
            final RangedDateProperty publishedBetween1975And1985 = new RangedDateProperty(_1975, _1985, ContentModel.PROP_PUBLISHED);
            // Find all
            PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, null, null, pagingReq);
            assertEquals("Wrong number of blog posts", 3, pagedResults.getPage().size());
            Set<NodeRef> recoveredBlogNodes = new HashSet<NodeRef>();
            for (BlogPostInfo bpi : pagedResults.getPage()) {
                recoveredBlogNodes.add(bpi.getNodeRef());
            }
            assertTrue("Missing expected BlogPost NodeRef 71", recoveredBlogNodes.contains(blogPosts.get(1971)));
            assertTrue("Missing expected BlogPost NodeRef 81", recoveredBlogNodes.contains(blogPosts.get(1981)));
            assertTrue("Missing expected BlogPost NodeRef 91", recoveredBlogNodes.contains(blogPosts.get(1991)));
            // Find posts before date
            pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, publishedBefore1980, null, pagingReq);
            assertEquals("Wrong blog post count", 1, pagedResults.getPage().size());
            NodeRef blogNode = pagedResults.getPage().get(0).getNodeRef();
            assertEquals("Incorrect NodeRef.", blogNode, blogPosts.get(1971));
            List<String> recoveredTags = TAGGING_SERVICE.getTags(blogNode);
            assertEquals("Incorrect tags.", tags, recoveredTags);
            // Find posts after date
            pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, publishedAfter1980, "hello", pagingReq);
            assertEquals("Wrong blog post count", 2, pagedResults.getPage().size());
            blogNode = pagedResults.getPage().get(0).getNodeRef();
            assertEquals("Incorrect NodeRef.", blogNode, blogPosts.get(1981));
            // Find posts between dates
            pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, publishedBetween1975And1985, "hello", pagingReq);
            assertEquals("Wrong blog post count", 1, pagedResults.getPage().size());
            blogNode = pagedResults.getPage().get(0).getNodeRef();
            assertEquals("Incorrect NodeRef.", blogNode, blogPosts.get(1981));
            return null;
        }
    });
}
Also used : PagingResults(org.alfresco.query.PagingResults) HashSet(java.util.HashSet) Set(java.util.Set) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) Calendar(java.util.Calendar) RangedDateProperty(org.alfresco.service.cmr.blog.BlogService.RangedDateProperty) BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo) Date(java.util.Date) PagingRequest(org.alfresco.query.PagingRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) PropertyMap(org.alfresco.util.PropertyMap) Category(org.junit.experimental.categories.Category) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 PagingRequest (org.alfresco.query.PagingRequest)3 PagingResults (org.alfresco.query.PagingResults)3 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)3 BlogPostInfo (org.alfresco.service.cmr.blog.BlogPostInfo)3 RangedDateProperty (org.alfresco.service.cmr.blog.BlogService.RangedDateProperty)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 BaseSpringTest (org.alfresco.util.BaseSpringTest)3 Test (org.junit.Test)3 Category (org.junit.experimental.categories.Category)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Pair (org.alfresco.util.Pair)1 PropertyMap (org.alfresco.util.PropertyMap)1