mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Rating Service Part 2.
Users can't rate their own content any more. Added a RunAs(System) so that users can rate content they don't own, which is all they can rate! First stab at adding rating totals, means and counts - to be tidied tomorrow. Miscellaneous improvements & doc'ing. Tests for the above. Refactored existing tests to run as different users (all were running as admin previously). git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21013 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -34,10 +34,12 @@ import org.alfresco.service.cmr.rating.RatingService;
|
||||
import org.alfresco.service.cmr.rating.RatingServiceException;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.service.namespace.RegexQNamePattern;
|
||||
import org.alfresco.util.BaseAlfrescoSpringTest;
|
||||
import org.alfresco.util.PropertyMap;
|
||||
|
||||
/**
|
||||
* @author Neil McErlean
|
||||
@@ -45,15 +47,18 @@ import org.alfresco.util.BaseAlfrescoSpringTest;
|
||||
*/
|
||||
public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
{
|
||||
private static final String USER_ERNIE = "Ernie";
|
||||
private static final String USER_ERIC = "Eric";
|
||||
private PersonService personService;
|
||||
private RatingService ratingService;
|
||||
private Repository repositoryHelper;
|
||||
private NodeRef companyHome;
|
||||
|
||||
// These NodeRefs are used by the test methods.
|
||||
private NodeRef testFolder;
|
||||
private NodeRef testSubFolder;
|
||||
private NodeRef testDocInFolder;
|
||||
private NodeRef testDocInSubFolder;
|
||||
private NodeRef testDoc_Admin;
|
||||
private NodeRef testDoc_Eric;
|
||||
private NodeRef testDoc_Ernie;
|
||||
|
||||
// The out of the box scheme names.
|
||||
private static final String LIKES_SCHEME_NAME = "likesRatingScheme";
|
||||
@@ -63,6 +68,7 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
protected void onSetUpInTransaction() throws Exception
|
||||
{
|
||||
super.onSetUpInTransaction();
|
||||
this.personService = (PersonService)this.applicationContext.getBean("PersonService");
|
||||
this.ratingService = (RatingService) this.applicationContext.getBean("ratingService");
|
||||
this.repositoryHelper = (Repository) this.applicationContext.getBean("repositoryHelper");
|
||||
|
||||
@@ -73,10 +79,26 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
|
||||
//TODO These could be created @BeforeClass
|
||||
testFolder = createNode(companyHome, "testFolder", ContentModel.TYPE_FOLDER);
|
||||
testSubFolder = createNode(testFolder, "testSubFolder", ContentModel.TYPE_FOLDER);
|
||||
|
||||
testDocInFolder = createNode(testFolder, "testDocInFolder", ContentModel.TYPE_CONTENT);
|
||||
testDocInSubFolder = createNode(testSubFolder, "testDocInSubFolder", ContentModel.TYPE_CONTENT);
|
||||
testDoc_Admin = createNode(testFolder, "testDocInFolder", ContentModel.TYPE_CONTENT);
|
||||
|
||||
createUser(USER_ERIC);
|
||||
createUser(USER_ERNIE);
|
||||
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ERIC);
|
||||
testDoc_Eric = createNode(testFolder, "ericsDoc", ContentModel.TYPE_CONTENT);
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ERNIE);
|
||||
testDoc_Ernie = createNode(testFolder, "erniesDoc", ContentModel.TYPE_CONTENT);
|
||||
|
||||
// And back to admin
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onTearDownInTransaction() throws Exception
|
||||
{
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
|
||||
deleteUser(USER_ERIC);
|
||||
deleteUser(USER_ERNIE);
|
||||
}
|
||||
|
||||
private NodeRef createNode(NodeRef parentNode, String name, QName type)
|
||||
@@ -127,7 +149,7 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
int[] illegalRatings = new int[]{0, 2};
|
||||
for (int illegalRating : illegalRatings)
|
||||
{
|
||||
applyIllegalRating(testDocInFolder, illegalRating, LIKES_SCHEME_NAME);
|
||||
applyIllegalRating(testDoc_Admin, illegalRating, LIKES_SCHEME_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,24 +167,25 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
|
||||
public void testApplyUpdateDeleteRatings_SingleUserMultipleSchemes() throws Exception
|
||||
{
|
||||
// We'll do all this as user 'eric'.
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ERIC);
|
||||
|
||||
//Before we start, let's ensure the read behaviour on a pristine node is correct.
|
||||
final RatingScheme likesRatingScheme = ratingService.getRatingScheme(LIKES_SCHEME_NAME);
|
||||
Rating nullRating = ratingService.getRatingByCurrentUser(testDocInFolder, likesRatingScheme);
|
||||
Rating nullRating = ratingService.getRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME);
|
||||
assertNull("Expected a null rating,", nullRating);
|
||||
assertNull("Expected a null remove result.", ratingService.removeRatingByCurrentUser(testDocInFolder, likesRatingScheme));
|
||||
assertNull("Expected a null remove result.", ratingService.removeRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME));
|
||||
|
||||
final int likesScore = 1;
|
||||
final int fiveStarScore = 5;
|
||||
|
||||
// Both of these ratings will be applied by the same user: the 'current' user.
|
||||
ratingService.applyRating(testDocInFolder, likesScore, LIKES_SCHEME_NAME);
|
||||
ratingService.applyRating(testDocInFolder, fiveStarScore, FIVE_STAR_SCHEME_NAME);
|
||||
ratingService.applyRating(testDoc_Admin, likesScore, LIKES_SCHEME_NAME);
|
||||
ratingService.applyRating(testDoc_Admin, fiveStarScore, FIVE_STAR_SCHEME_NAME);
|
||||
|
||||
// Some basic node structure tests.
|
||||
assertTrue(ContentModel.ASPECT_RATEABLE + " aspect missing.",
|
||||
nodeService.hasAspect(testDocInFolder, ContentModel.ASPECT_RATEABLE));
|
||||
nodeService.hasAspect(testDoc_Admin, ContentModel.ASPECT_RATEABLE));
|
||||
|
||||
List<ChildAssociationRef> allChildren = nodeService.getChildAssocs(testDocInFolder,
|
||||
List<ChildAssociationRef> allChildren = nodeService.getChildAssocs(testDoc_Admin,
|
||||
ContentModel.ASSOC_RATINGS, RegexQNamePattern.MATCH_ALL);
|
||||
|
||||
// It's one cm:rating node per user
|
||||
@@ -176,10 +199,9 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
|
||||
|
||||
// Now to check the persisted ratings data are ok.
|
||||
Rating likeRating = ratingService.getRatingByCurrentUser(testDocInFolder, likesRatingScheme);
|
||||
Rating likeRating = ratingService.getRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME);
|
||||
|
||||
final RatingScheme fiveStarRatingScheme = ratingService.getRatingScheme(FIVE_STAR_SCHEME_NAME);
|
||||
Rating fiveStarRating = ratingService.getRatingByCurrentUser(testDocInFolder, fiveStarRatingScheme);
|
||||
Rating fiveStarRating = ratingService.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||
|
||||
assertNotNull("'like' rating was null.", likeRating);
|
||||
assertEquals("Wrong score for rating", likesScore, likeRating.getScore());
|
||||
@@ -195,10 +217,10 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
|
||||
// Now we'll update a rating
|
||||
final int updatedFiveStarScore = 3;
|
||||
ratingService.applyRating(testDocInFolder, updatedFiveStarScore, FIVE_STAR_SCHEME_NAME);
|
||||
ratingService.applyRating(testDoc_Admin, updatedFiveStarScore, FIVE_STAR_SCHEME_NAME);
|
||||
|
||||
// Some basic node structure tests.
|
||||
allChildren = nodeService.getChildAssocs(testDocInFolder,
|
||||
allChildren = nodeService.getChildAssocs(testDoc_Admin,
|
||||
ContentModel.ASSOC_RATINGS, RegexQNamePattern.MATCH_ALL);
|
||||
|
||||
// Still one cm:rating node
|
||||
@@ -210,7 +232,7 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
|
||||
|
||||
// Now to check the updated ratings data are ok.
|
||||
Rating updatedFiveStarRating = ratingService.getRatingByCurrentUser(testDocInFolder, fiveStarRatingScheme);
|
||||
Rating updatedFiveStarRating = ratingService.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||
|
||||
// 'like' rating data should be unchanged.
|
||||
assertNotNull("'like' rating was null.", likeRating);
|
||||
@@ -226,7 +248,7 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
assertDateIsCloseToNow(updatedFiveStarRating.getAppliedAt());
|
||||
|
||||
// Now we'll delete the 'likes' rating.
|
||||
Rating deletedLikesRating = ratingService.removeRatingByCurrentUser(testDocInFolder, likesRatingScheme);
|
||||
Rating deletedLikesRating = ratingService.removeRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME);
|
||||
// 'like' rating data should be unchanged.
|
||||
assertNotNull("'like' rating was null.", deletedLikesRating);
|
||||
assertEquals("Wrong score for rating", likesScore, deletedLikesRating.getScore());
|
||||
@@ -234,7 +256,7 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
assertEquals("Wrong date for rating", likeRatingAppliedAt, deletedLikesRating.getAppliedAt());
|
||||
|
||||
// And delete the 'five star' rating.
|
||||
Rating deletedStarRating = ratingService.removeRatingByCurrentUser(testDocInFolder, fiveStarRatingScheme);
|
||||
Rating deletedStarRating = ratingService.removeRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||
// 'five star' rating data should be unchanged.
|
||||
assertNotNull("'5*' rating was null.", deletedStarRating);
|
||||
assertEquals("Wrong score for rating", updatedFiveStarScore, deletedStarRating.getScore());
|
||||
@@ -256,6 +278,65 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
||||
final long millisTolerance = 5000l; // 5 seconds
|
||||
assertTrue("Date was not within " + millisTolerance + "ms of 'now'.", now.getTime() - d.getTime() < millisTolerance);
|
||||
}
|
||||
|
||||
public void testApplyRating_MultipleUsers() throws Exception
|
||||
{
|
||||
// 2 different users rating the same piece of content in the same rating scheme
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ERNIE);
|
||||
ratingService.applyRating(testDoc_Admin, 4, FIVE_STAR_SCHEME_NAME);
|
||||
|
||||
//TODO Multiple users applying ratings to a doc.
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ERIC);
|
||||
ratingService.applyRating(testDoc_Admin, 2, FIVE_STAR_SCHEME_NAME);
|
||||
|
||||
float meanRating = ratingService.getAverageRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||
assertEquals("Document had wrong mean rating.", 3f, meanRating);
|
||||
|
||||
int totalRating = ratingService.getTotalRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||
assertEquals("Document had wrong total rating.", 6, totalRating);
|
||||
|
||||
int ratingsCount = ratingService.getRatingsCount(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||
assertEquals("Document had wrong ratings count.", 2, ratingsCount);
|
||||
}
|
||||
|
||||
public void testUsersCantRateTheirOwnContent() throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ERNIE);
|
||||
ratingService.applyRating(testDoc_Ernie, 4, FIVE_STAR_SCHEME_NAME);
|
||||
} catch (RatingServiceException expected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
fail("Expected exception not thrown");
|
||||
}
|
||||
|
||||
private void createUser(String userName)
|
||||
{
|
||||
if (! authenticationService.authenticationExists(userName))
|
||||
{
|
||||
authenticationService.createAuthentication(userName, "PWD".toCharArray());
|
||||
}
|
||||
|
||||
if (! personService.personExists(userName))
|
||||
{
|
||||
PropertyMap ppOne = new PropertyMap(4);
|
||||
ppOne.put(ContentModel.PROP_USERNAME, userName);
|
||||
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
|
||||
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
|
||||
ppOne.put(ContentModel.PROP_EMAIL, "email@email.com");
|
||||
ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
|
||||
|
||||
personService.createPerson(ppOne);
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteUser(String userName)
|
||||
{
|
||||
if (personService.personExists(userName))
|
||||
{
|
||||
personService.deletePerson(userName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user