mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
ALF-9367. Added new/improved Unit Tests in area of tagged blogposts and sorted result sets.
Also fixed a bug-ette whereby for 'get all' blogposts, the drafts at the end where sorted the wrong way. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29071 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -21,8 +21,8 @@ package org.alfresco.repo.blog;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@@ -46,7 +46,6 @@ import org.alfresco.service.cmr.blog.BlogService.RangedDateProperty;
|
|||||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||||
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
||||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
|
||||||
import org.alfresco.service.cmr.repository.NodeRef;
|
import org.alfresco.service.cmr.repository.NodeRef;
|
||||||
import org.alfresco.service.cmr.repository.NodeService;
|
import org.alfresco.service.cmr.repository.NodeService;
|
||||||
import org.alfresco.service.cmr.security.MutableAuthenticationService;
|
import org.alfresco.service.cmr.security.MutableAuthenticationService;
|
||||||
@@ -300,19 +299,27 @@ public class BlogServiceImplTest
|
|||||||
|
|
||||||
@Test public void createTaggedDraftBlogPost() throws Exception
|
@Test public void createTaggedDraftBlogPost() throws Exception
|
||||||
{
|
{
|
||||||
final List<String> tags = Arrays.asList(new String[]{"foo", "bar"});
|
final List<String> tags = Arrays.asList(new String[]{"alpha", "beta", "gamma"});
|
||||||
|
|
||||||
final NodeRef blogPost = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>()
|
// Create a list of Blog Posts, all drafts, each with one of the tags above.
|
||||||
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<List<NodeRef>>()
|
||||||
{
|
{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NodeRef execute() throws Throwable
|
public List<NodeRef> execute() throws Throwable
|
||||||
{
|
{
|
||||||
BlogPostInfo newBlogPost = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "draftWithTag", "Hello world", true);
|
List<NodeRef> results = new ArrayList<NodeRef>();
|
||||||
TAGGING_SERVICE.addTags(newBlogPost.getNodeRef(), tags);
|
|
||||||
testNodesToTidy.add(newBlogPost.getNodeRef());
|
|
||||||
|
|
||||||
return newBlogPost.getNodeRef();
|
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;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -321,16 +328,23 @@ public class BlogServiceImplTest
|
|||||||
@Override
|
@Override
|
||||||
public Void execute() throws Throwable
|
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);
|
PagingRequest pagingReq = new PagingRequest(0, 10, null);
|
||||||
|
|
||||||
PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.getDrafts(BLOG_CONTAINER_NODE, ADMIN_USER, pagingReq);
|
PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.getDrafts(BLOG_CONTAINER_NODE, ADMIN_USER, pagingReq);
|
||||||
assertEquals("Expected one blog post", 1, pagedResults.getPage().size());
|
assertEquals("Wrong number of blog posts", tags.size(), pagedResults.getPage().size());
|
||||||
|
|
||||||
NodeRef blogNode = pagedResults.getPage().get(0).getNodeRef();
|
for (BlogPostInfo bpi : pagedResults.getPage())
|
||||||
assertEquals("Incorrect NodeRef.", blogNode, blogPost);
|
{
|
||||||
|
NodeRef blogNode = bpi.getNodeRef();
|
||||||
List<String> recoveredTags = TAGGING_SERVICE.getTags(blogNode);
|
List<String> recoveredTags = TAGGING_SERVICE.getTags(blogNode);
|
||||||
assertEquals("Incorrect tags.", tags, recoveredTags);
|
assertEquals("Wrong number of tags", 1, recoveredTags.size());
|
||||||
|
assertTrue("Missing expected tag", expectedTags.remove(recoveredTags.get(0)));
|
||||||
|
}
|
||||||
|
assertTrue("Not all tags were recovered from a blogpost", expectedTags.isEmpty());
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -452,6 +466,119 @@ public class BlogServiceImplTest
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void ensureBlogPostsAreCorrectlySorted() throws Exception
|
||||||
|
{
|
||||||
|
final int testBlogCount = 3;
|
||||||
|
|
||||||
|
// Set up some test data to check sorting. We don't need to retain references to these posts.
|
||||||
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Void execute() throws Throwable
|
||||||
|
{
|
||||||
|
// Create some blog posts. They'll all be published 'now' but the slight delay between each should ensure they
|
||||||
|
// are given distinct creation dates
|
||||||
|
final long slightDelay = 50;
|
||||||
|
|
||||||
|
for (int i = 0; i < testBlogCount; i++)
|
||||||
|
{
|
||||||
|
BlogPostInfo newDraft = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "draftPost_ensureBlogPostsAreCorrectlySorted" + i, "x", true);
|
||||||
|
|
||||||
|
Thread.sleep(slightDelay);
|
||||||
|
|
||||||
|
// And the same for some published posts...
|
||||||
|
BlogPostInfo newPublished = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "publishedPost_ensureBlogPostsAreCorrectlySorted" + i, "x", false);
|
||||||
|
|
||||||
|
Thread.sleep(slightDelay);
|
||||||
|
|
||||||
|
testNodesToTidy.add(newDraft.getNodeRef());
|
||||||
|
testNodesToTidy.add(newPublished.getNodeRef());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
final PagingRequest pagingReq = new PagingRequest(100);
|
||||||
|
|
||||||
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
|
||||||
|
{
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@Override
|
||||||
|
public Void execute() throws Throwable
|
||||||
|
{
|
||||||
|
String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
|
||||||
|
|
||||||
|
// get DRAFTS
|
||||||
|
PagingResults<BlogPostInfo> resultsPage = BLOG_SERVICE.getDrafts(BLOG_CONTAINER_NODE, currentUser, pagingReq);
|
||||||
|
List<BlogPostInfo> blogPosts = resultsPage.getPage();
|
||||||
|
assertTrue("Expected more draft blog posts than " + blogPosts.size(),
|
||||||
|
blogPosts.size() >= testBlogCount);
|
||||||
|
|
||||||
|
assertSortingIsCorrect(blogPosts);
|
||||||
|
|
||||||
|
// And the published ones
|
||||||
|
resultsPage = BLOG_SERVICE.getPublished(BLOG_CONTAINER_NODE, null, null, currentUser, pagingReq); // Date filtering tested elsewhere.
|
||||||
|
blogPosts = resultsPage.getPage();
|
||||||
|
assertTrue("Expected more published blog posts than " + blogPosts.size(),
|
||||||
|
blogPosts.size() >= testBlogCount);
|
||||||
|
|
||||||
|
assertSortingIsCorrect(blogPosts);
|
||||||
|
|
||||||
|
|
||||||
|
// And the combination. This should be ordered:
|
||||||
|
// published posts, most recent cm:published first - followed by
|
||||||
|
// draft posts, most recent cm:created first
|
||||||
|
System.out.println(" getMyDraftsAndAllPublished");
|
||||||
|
|
||||||
|
resultsPage = BLOG_SERVICE.getMyDraftsAndAllPublished(BLOG_CONTAINER_NODE, null, null, pagingReq);
|
||||||
|
blogPosts = resultsPage.getPage();
|
||||||
|
|
||||||
|
assertSortingIsCorrect(blogPosts);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertSortingIsCorrect(List<BlogPostInfo> blogPosts)
|
||||||
|
{
|
||||||
|
// Sometimes you just have to see the data...
|
||||||
|
for (BlogPostInfo bpi : blogPosts)
|
||||||
|
{
|
||||||
|
System.out.println(" -----");
|
||||||
|
Date published = (Date) NODE_SERVICE.getProperty(bpi.getNodeRef(), ContentModel.PROP_PUBLISHED);
|
||||||
|
Date created = (Date) NODE_SERVICE.getProperty(bpi.getNodeRef(), ContentModel.PROP_CREATED);
|
||||||
|
System.out.print(" published: " + (published == null ? " " : published.getTime()));
|
||||||
|
System.out.println(" created : " + created.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < blogPosts.size() - 1; i++) // We only want to iterate to the second-last item
|
||||||
|
{
|
||||||
|
BlogPostInfo nextBPI = blogPosts.get(i);
|
||||||
|
BlogPostInfo followingBPI = blogPosts.get(i + 1);
|
||||||
|
|
||||||
|
Date nextPublishedDate = (Date) NODE_SERVICE.getProperty(nextBPI.getNodeRef(), ContentModel.PROP_PUBLISHED);
|
||||||
|
Date followingPublishedDate = (Date) NODE_SERVICE.getProperty(followingBPI.getNodeRef(), ContentModel.PROP_PUBLISHED);
|
||||||
|
Date nextCreatedDate = (Date) NODE_SERVICE.getProperty(nextBPI.getNodeRef(), ContentModel.PROP_CREATED);
|
||||||
|
Date followingCreatedDate = (Date) NODE_SERVICE.getProperty(followingBPI.getNodeRef(), ContentModel.PROP_CREATED);
|
||||||
|
|
||||||
|
// published must precede draft
|
||||||
|
if ( nextPublishedDate == null && followingPublishedDate != null)
|
||||||
|
{
|
||||||
|
fail("Published posts must precede draft posts");
|
||||||
|
}
|
||||||
|
else if (nextPublishedDate != null && followingPublishedDate != null)
|
||||||
|
{
|
||||||
|
assertTrue("Error in BlogPostInfo sorting. Published dates in wrong order.", !nextPublishedDate.before(followingPublishedDate));
|
||||||
|
}
|
||||||
|
else if (nextPublishedDate == null && followingPublishedDate == null)
|
||||||
|
{
|
||||||
|
assertTrue("Error in BlogPostInfo sorting. Created dates in wrong order.", !nextCreatedDate.before(followingCreatedDate));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test uses two different users to create draft and internally published blog posts.
|
* This test uses two different users to create draft and internally published blog posts.
|
||||||
* Then it ensures that each user sees the correct posts when they retrieve them from the service.
|
* Then it ensures that each user sees the correct posts when they retrieve them from the service.
|
||||||
|
@@ -85,10 +85,15 @@ public abstract class AbstractBlogPostsCannedQueryFactory extends AbstractCanned
|
|||||||
|
|
||||||
protected CannedQuerySortDetails createCQSortDetails(QName sortProp, SortOrder sortOrder)
|
protected CannedQuerySortDetails createCQSortDetails(QName sortProp, SortOrder sortOrder)
|
||||||
{
|
{
|
||||||
CannedQuerySortDetails cqsd = null;
|
List<Pair<? extends Object, SortOrder>> singlePair = new ArrayList<Pair<? extends Object, SortOrder>>(1);
|
||||||
List<Pair<? extends Object, SortOrder>> sortPairs = new ArrayList<Pair<? extends Object, SortOrder>>();
|
singlePair.add(new Pair<QName, SortOrder>(sortProp, sortOrder));
|
||||||
sortPairs.add(new Pair<QName, SortOrder>(sortProp, sortOrder));
|
|
||||||
cqsd = new CannedQuerySortDetails(sortPairs);
|
return this.createCQSortDetails(singlePair);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected CannedQuerySortDetails createCQSortDetails(List<Pair<? extends Object, SortOrder>> sortPairs)
|
||||||
|
{
|
||||||
|
CannedQuerySortDetails cqsd = new CannedQuerySortDetails(sortPairs);
|
||||||
return cqsd;
|
return cqsd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -137,17 +137,17 @@ public class DraftsAndPublishedBlogPostsCannedQuery extends AbstractCannedQueryP
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<Pair<? extends Object, SortOrder>> sortPairs = parameters.getSortDetails().getSortPairs();
|
List<Pair<? extends Object, SortOrder>> sortPairs = parameters.getSortDetails().getSortPairs();
|
||||||
// For now, the BlogService only sorts by a single property.
|
|
||||||
if (sortPairs != null && !sortPairs.isEmpty())
|
if (sortPairs != null && !sortPairs.isEmpty())
|
||||||
{
|
{
|
||||||
Pair<? extends Object, SortOrder> sortPair = sortPairs.get(0);
|
for (Pair<? extends Object, SortOrder> sortPair : sortPairs)
|
||||||
|
|
||||||
QName sortProperty = (QName) sortPair.getFirst();
|
|
||||||
final PropertyBasedComparator comparator = new PropertyBasedComparator(sortProperty);
|
|
||||||
|
|
||||||
if (sortPair.getSecond() == SortOrder.DESCENDING)
|
|
||||||
{
|
{
|
||||||
Collections.sort(filtered, Collections.reverseOrder(comparator));
|
QName sortProperty = (QName) sortPair.getFirst();
|
||||||
|
final PropertyBasedComparator comparator = new PropertyBasedComparator(sortProperty);
|
||||||
|
|
||||||
|
if (sortPair.getSecond() == SortOrder.DESCENDING)
|
||||||
|
{
|
||||||
|
Collections.sort(filtered, Collections.reverseOrder(comparator));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,7 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.alfresco.repo.blog.cannedqueries;
|
package org.alfresco.repo.blog.cannedqueries;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.alfresco.model.ContentModel;
|
import org.alfresco.model.ContentModel;
|
||||||
import org.alfresco.query.CannedQuery;
|
import org.alfresco.query.CannedQuery;
|
||||||
@@ -30,6 +32,8 @@ import org.alfresco.query.PagingRequest;
|
|||||||
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
|
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
|
||||||
import org.alfresco.service.cmr.blog.BlogService.BlogPostInfo;
|
import org.alfresco.service.cmr.blog.BlogService.BlogPostInfo;
|
||||||
import org.alfresco.service.cmr.repository.NodeRef;
|
import org.alfresco.service.cmr.repository.NodeRef;
|
||||||
|
import org.alfresco.service.namespace.QName;
|
||||||
|
import org.alfresco.util.Pair;
|
||||||
import org.alfresco.util.ParameterCheck;
|
import org.alfresco.util.ParameterCheck;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,7 +71,15 @@ public class DraftsAndPublishedBlogPostsCannedQueryFactory extends AbstractBlogP
|
|||||||
fromDate, toDate);
|
fromDate, toDate);
|
||||||
|
|
||||||
CannedQueryPageDetails cqpd = createCQPageDetails(pagingReq);
|
CannedQueryPageDetails cqpd = createCQPageDetails(pagingReq);
|
||||||
CannedQuerySortDetails cqsd = createCQSortDetails(ContentModel.PROP_PUBLISHED, SortOrder.DESCENDING);
|
|
||||||
|
List<Pair<? extends Object, SortOrder>> sortPairs = new ArrayList<Pair<? extends Object, SortOrder>>(2);
|
||||||
|
|
||||||
|
// Sort by created then published. We want a list of all published (most recently published first),
|
||||||
|
// followed by all unpublished (most recently created first)
|
||||||
|
sortPairs.add(new Pair<QName, SortOrder>(ContentModel.PROP_CREATED, SortOrder.DESCENDING));
|
||||||
|
sortPairs.add(new Pair<QName, SortOrder>(ContentModel.PROP_PUBLISHED, SortOrder.DESCENDING));
|
||||||
|
|
||||||
|
CannedQuerySortDetails cqsd = createCQSortDetails(sortPairs);
|
||||||
|
|
||||||
// create query params holder
|
// create query params holder
|
||||||
CannedQueryParameters params = new CannedQueryParameters(paramBean, cqpd, cqsd, requestTotalCountMax, pagingReq.getQueryExecutionId());
|
CannedQueryParameters params = new CannedQueryParameters(paramBean, cqpd, cqsd, requestTotalCountMax, pagingReq.getQueryExecutionId());
|
||||||
|
Reference in New Issue
Block a user