mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
More JUnit rules fun. Added a new rule to help with the creation and automatic cleanup of temporary test nodes.
Also refactored RatingServiceIntegrationTest to use JUnit Rules properly. This class might be a template for future use of Rules. We'll see... git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@34328 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2005-2011 Alfresco Software Limited.
|
* Copyright (C) 2005-2012 Alfresco Software Limited.
|
||||||
*
|
*
|
||||||
* This file is part of Alfresco
|
* This file is part of Alfresco
|
||||||
*
|
*
|
||||||
@@ -19,6 +19,12 @@
|
|||||||
|
|
||||||
package org.alfresco.repo.rating;
|
package org.alfresco.repo.rating;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -28,40 +34,65 @@ import org.alfresco.model.ContentModel;
|
|||||||
import org.alfresco.repo.jscript.ClasspathScriptLocation;
|
import org.alfresco.repo.jscript.ClasspathScriptLocation;
|
||||||
import org.alfresco.repo.model.Repository;
|
import org.alfresco.repo.model.Repository;
|
||||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||||
|
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||||
|
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||||
import org.alfresco.service.cmr.rating.Rating;
|
import org.alfresco.service.cmr.rating.Rating;
|
||||||
import org.alfresco.service.cmr.rating.RatingScheme;
|
import org.alfresco.service.cmr.rating.RatingScheme;
|
||||||
import org.alfresco.service.cmr.rating.RatingService;
|
import org.alfresco.service.cmr.rating.RatingService;
|
||||||
import org.alfresco.service.cmr.rating.RatingServiceException;
|
import org.alfresco.service.cmr.rating.RatingServiceException;
|
||||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
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.ScriptLocation;
|
import org.alfresco.service.cmr.repository.ScriptLocation;
|
||||||
import org.alfresco.service.cmr.repository.ScriptService;
|
import org.alfresco.service.cmr.repository.ScriptService;
|
||||||
import org.alfresco.service.cmr.security.PersonService;
|
|
||||||
import org.alfresco.service.namespace.QName;
|
import org.alfresco.service.namespace.QName;
|
||||||
import org.alfresco.service.namespace.RegexQNamePattern;
|
import org.alfresco.service.namespace.RegexQNamePattern;
|
||||||
import org.alfresco.util.BaseAlfrescoSpringTest;
|
import org.alfresco.util.test.junitrules.AlfrescoPerson;
|
||||||
import org.alfresco.util.PropertyMap;
|
import org.alfresco.util.test.junitrules.ApplicationContextInit;
|
||||||
|
import org.alfresco.util.test.junitrules.TemporaryNodes;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.RuleChain;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Neil McErlean
|
* @author Neil McErlean
|
||||||
* @since 3.4
|
* @since 3.4
|
||||||
*/
|
*/
|
||||||
public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
public class RatingServiceIntegrationTest
|
||||||
{
|
{
|
||||||
private static final String USER_ONE = "UserOne";
|
// Rule to initialise the default Alfresco spring configuration
|
||||||
private static final String USER_TWO = "UserTwo";
|
public static ApplicationContextInit APP_CONTEXT_INIT = new ApplicationContextInit();
|
||||||
// private CopyService copyService;
|
|
||||||
private PersonService personService;
|
|
||||||
private RatingService ratingService;
|
|
||||||
private Repository repositoryHelper;
|
|
||||||
private ScriptService scriptService;
|
|
||||||
private RatingNamingConventionsUtil ratingNamingConventions;
|
|
||||||
|
|
||||||
private NodeRef companyHome;
|
// Rules to create 2 test users.
|
||||||
|
public static AlfrescoPerson TEST_USER1 = new AlfrescoPerson(APP_CONTEXT_INIT, "UserOne");
|
||||||
|
public static AlfrescoPerson TEST_USER2 = new AlfrescoPerson(APP_CONTEXT_INIT, "UserTwo");
|
||||||
|
|
||||||
|
// A rule to manage test nodes reused across all the test methods
|
||||||
|
public static TemporaryNodes STATIC_TEST_NODES = new TemporaryNodes(APP_CONTEXT_INIT);
|
||||||
|
|
||||||
|
// Tie them together in a static Rule Chain
|
||||||
|
@ClassRule public static RuleChain ruleChain = RuleChain.outerRule(APP_CONTEXT_INIT)
|
||||||
|
.around(TEST_USER1)
|
||||||
|
.around(TEST_USER2)
|
||||||
|
.around(STATIC_TEST_NODES);
|
||||||
|
|
||||||
|
// A rule to manage test nodes use in each test method
|
||||||
|
@Rule public TemporaryNodes testNodes = new TemporaryNodes(APP_CONTEXT_INIT);
|
||||||
|
|
||||||
|
// Various services
|
||||||
|
private static NodeService NODE_SERVICE;
|
||||||
|
private static RatingService RATING_SERVICE;
|
||||||
|
private static RetryingTransactionHelper TRANSACTION_HELPER;
|
||||||
|
private static ScriptService SCRIPT_SERVICE;
|
||||||
|
private static RatingNamingConventionsUtil RATING_NAMING_CONVENTIONS;
|
||||||
|
|
||||||
|
private static NodeRef COMPANY_HOME;
|
||||||
|
|
||||||
// These NodeRefs are used by the test methods.
|
// These NodeRefs are used by the test methods.
|
||||||
private NodeRef testFolder;
|
private static NodeRef TEST_FOLDER;
|
||||||
// private NodeRef testFolderCopyDest;
|
|
||||||
private NodeRef testDoc_Admin;
|
private NodeRef testDoc_Admin;
|
||||||
private NodeRef testDoc_UserOne;
|
private NodeRef testDoc_UserOne;
|
||||||
private NodeRef testDoc_UserTwo;
|
private NodeRef testDoc_UserTwo;
|
||||||
@@ -70,55 +101,37 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
|||||||
private static final String LIKES_SCHEME_NAME = "likesRatingScheme";
|
private static final String LIKES_SCHEME_NAME = "likesRatingScheme";
|
||||||
private static final String FIVE_STAR_SCHEME_NAME = "fiveStarRatingScheme";
|
private static final String FIVE_STAR_SCHEME_NAME = "fiveStarRatingScheme";
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@BeforeClass public static void initStaticData() throws Exception
|
||||||
@Override
|
|
||||||
protected void onSetUpInTransaction() throws Exception
|
|
||||||
{
|
{
|
||||||
super.onSetUpInTransaction();
|
NODE_SERVICE = (NodeService) APP_CONTEXT_INIT.getApplicationContext().getBean("nodeService");
|
||||||
// this.copyService = (CopyService)this.applicationContext.getBean("CopyService");
|
RATING_NAMING_CONVENTIONS = (RatingNamingConventionsUtil) APP_CONTEXT_INIT.getApplicationContext().getBean("rollupNamingConventions");
|
||||||
this.personService = (PersonService)this.applicationContext.getBean("PersonService");
|
RATING_SERVICE = (RatingService) APP_CONTEXT_INIT.getApplicationContext().getBean("ratingService");
|
||||||
this.ratingService = (RatingService) this.applicationContext.getBean("ratingService");
|
SCRIPT_SERVICE = (ScriptService) APP_CONTEXT_INIT.getApplicationContext().getBean("scriptService");
|
||||||
this.repositoryHelper = (Repository) this.applicationContext.getBean("repositoryHelper");
|
TRANSACTION_HELPER = (RetryingTransactionHelper) APP_CONTEXT_INIT.getApplicationContext().getBean("retryingTransactionHelper");
|
||||||
// this.transactionHelper = (RetryingTransactionHelper) this.applicationContext.getBean("retryingTransactionHelper");
|
|
||||||
this.scriptService = (ScriptService) this.applicationContext.getBean("scriptService");
|
Repository repositoryHelper = (Repository) APP_CONTEXT_INIT.getApplicationContext().getBean("repositoryHelper");
|
||||||
this.ratingNamingConventions = (RatingNamingConventionsUtil)this.applicationContext.getBean("rollupNamingConventions");
|
COMPANY_HOME = repositoryHelper.getCompanyHome();
|
||||||
|
|
||||||
|
// Create some static test content
|
||||||
|
TEST_FOLDER = STATIC_TEST_NODES.createNode(COMPANY_HOME, "testFolder", ContentModel.TYPE_FOLDER, AuthenticationUtil.getAdminUserName());
|
||||||
|
|
||||||
// Set the current security context as admin
|
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
|
|
||||||
|
|
||||||
companyHome = this.repositoryHelper.getCompanyHome();
|
|
||||||
|
|
||||||
testFolder = createNode(companyHome, "testFolder", ContentModel.TYPE_FOLDER);
|
|
||||||
// testFolderCopyDest = createNode(companyHome, "testFolderCopyDest", ContentModel.TYPE_FOLDER);
|
|
||||||
testDoc_Admin = createNode(testFolder, "testDocInFolder", ContentModel.TYPE_CONTENT);
|
|
||||||
|
|
||||||
createUser(USER_ONE);
|
|
||||||
createUser(USER_TWO);
|
|
||||||
|
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
|
|
||||||
testDoc_UserOne = createNode(testFolder, "userOnesDoc", ContentModel.TYPE_CONTENT);
|
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
|
|
||||||
testDoc_UserTwo = createNode(testFolder, "userTwosDoc", ContentModel.TYPE_CONTENT);
|
|
||||||
|
|
||||||
// And back to admin
|
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Before public void createTestContent()
|
||||||
protected void onTearDownInTransaction() throws Exception
|
|
||||||
{
|
{
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
|
// Create some test content
|
||||||
deleteUser(USER_TWO);
|
testDoc_Admin = testNodes.createNode(TEST_FOLDER, "testDocInFolder", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
|
||||||
deleteUser(USER_ONE);
|
testDoc_UserOne = testNodes.createNode(TEST_FOLDER, "userOnesDoc", ContentModel.TYPE_CONTENT, TEST_USER1.getUsername());
|
||||||
|
testDoc_UserTwo = testNodes.createNode(TEST_FOLDER, "userTwosDoc", ContentModel.TYPE_CONTENT, TEST_USER2.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method tests that the expected 'out of the box' rating schemes are available
|
* This method tests that the expected 'out of the box' rating schemes are available
|
||||||
* and correctly initialised.
|
* and correctly initialised.
|
||||||
*/
|
*/
|
||||||
public void testOutOfTheBoxRatingSchemes() throws Exception
|
@Test public void outOfTheBoxRatingSchemes() throws Exception
|
||||||
{
|
{
|
||||||
Map<String, RatingScheme> schemes = this.ratingService.getRatingSchemes();
|
Map<String, RatingScheme> schemes = RATING_SERVICE.getRatingSchemes();
|
||||||
|
|
||||||
assertNotNull("rating scheme collection was null.", schemes);
|
assertNotNull("rating scheme collection was null.", schemes);
|
||||||
assertTrue("rating scheme collection was empty.", schemes.isEmpty() == false);
|
assertTrue("rating scheme collection was empty.", schemes.isEmpty() == false);
|
||||||
@@ -126,22 +139,24 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
|||||||
RatingScheme likesRS = schemes.get(LIKES_SCHEME_NAME);
|
RatingScheme likesRS = schemes.get(LIKES_SCHEME_NAME);
|
||||||
assertNotNull("'likes' rating scheme was missing.", likesRS);
|
assertNotNull("'likes' rating scheme was missing.", likesRS);
|
||||||
assertEquals("'likes' rating scheme had wrong name.", LIKES_SCHEME_NAME, likesRS.getName());
|
assertEquals("'likes' rating scheme had wrong name.", LIKES_SCHEME_NAME, likesRS.getName());
|
||||||
assertEquals("'likes' rating scheme had wrong min.", 1.0f, likesRS.getMinRating());
|
assertEquals("'likes' rating scheme had wrong min.", 1, (int)likesRS.getMinRating());
|
||||||
assertEquals("'likes' rating scheme had wrong max.", 1.0f, likesRS.getMaxRating());
|
assertEquals("'likes' rating scheme had wrong max.", 1, (int)likesRS.getMaxRating());
|
||||||
|
|
||||||
RatingScheme fiveStarRS = schemes.get(FIVE_STAR_SCHEME_NAME);
|
RatingScheme fiveStarRS = schemes.get(FIVE_STAR_SCHEME_NAME);
|
||||||
assertNotNull("'5*' rating scheme was missing.", fiveStarRS);
|
assertNotNull("'5*' rating scheme was missing.", fiveStarRS);
|
||||||
assertEquals("'5*' rating scheme had wrong name.", FIVE_STAR_SCHEME_NAME, fiveStarRS.getName());
|
assertEquals("'5*' rating scheme had wrong name.", FIVE_STAR_SCHEME_NAME, fiveStarRS.getName());
|
||||||
assertEquals("'5*' rating scheme had wrong min.", 1.0f, fiveStarRS.getMinRating());
|
assertEquals("'5*' rating scheme had wrong min.", 1, (int)fiveStarRS.getMinRating());
|
||||||
assertEquals("'5*' rating scheme had wrong max.", 5.0f, fiveStarRS.getMaxRating());
|
assertEquals("'5*' rating scheme had wrong max.", 5, (int)fiveStarRS.getMaxRating());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test method ensures that an attempt to apply an out-of-range rating value
|
* This test method ensures that an attempt to apply an out-of-range rating value
|
||||||
* throws the expected exception.
|
* throws the expected exception.
|
||||||
*/
|
*/
|
||||||
public void testApplyIllegalRatings() throws Exception
|
@Test public void applyIllegalRatings() throws Exception
|
||||||
{
|
{
|
||||||
|
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername());
|
||||||
|
|
||||||
// See rating-services-context.xml for definitions of these rating schemes.
|
// See rating-services-context.xml for definitions of these rating schemes.
|
||||||
float[] illegalRatings = new float[]{0.0f, 2.0f};
|
float[] illegalRatings = new float[]{0.0f, 2.0f};
|
||||||
for (float illegalRating : illegalRatings)
|
for (float illegalRating : illegalRatings)
|
||||||
@@ -154,7 +169,7 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ratingService.applyRating(nodeRef, illegalRating, schemeName);
|
RATING_SERVICE.applyRating(nodeRef, illegalRating, schemeName);
|
||||||
}
|
}
|
||||||
catch (RatingServiceException expectedException)
|
catch (RatingServiceException expectedException)
|
||||||
{
|
{
|
||||||
@@ -163,142 +178,151 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
|||||||
fail("Illegal rating " + illegalRating + " should have caused exception.");
|
fail("Illegal rating " + illegalRating + " should have caused exception.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testApplyUpdateDeleteRatings() throws Exception
|
@Test public void applyUpdateDeleteRatings() throws Exception
|
||||||
{
|
{
|
||||||
// We'll do all this as user 'UserOne'.
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
|
{
|
||||||
|
public Void execute() throws Throwable
|
||||||
//Before we start, let's ensure the read behaviour on a pristine node is correct.
|
{
|
||||||
Rating nullRating = ratingService.getRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME);
|
// We'll do all this as user 'UserTwo'.
|
||||||
assertNull("Expected a null rating,", nullRating);
|
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER2.getUsername());
|
||||||
assertNull("Expected a null remove result.", ratingService.removeRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME));
|
|
||||||
|
//Before we start, let's ensure the read behaviour on a pristine node is correct.
|
||||||
final float fiveStarScore = 5;
|
Rating nullRating = RATING_SERVICE.getRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME);
|
||||||
|
assertNull("Expected a null rating,", nullRating);
|
||||||
ratingService.applyRating(testDoc_Admin, fiveStarScore, FIVE_STAR_SCHEME_NAME);
|
assertNull("Expected a null remove result.", RATING_SERVICE.removeRatingByCurrentUser(testDoc_Admin, LIKES_SCHEME_NAME));
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
|
||||||
|
final int fiveStarScore = 5;
|
||||||
// Some basic node structure tests.
|
|
||||||
assertTrue(ContentModel.ASPECT_RATEABLE + " aspect missing.",
|
RATING_SERVICE.applyRating(testDoc_Admin, fiveStarScore, FIVE_STAR_SCHEME_NAME);
|
||||||
nodeService.hasAspect(testDoc_Admin, ContentModel.ASPECT_RATEABLE));
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
|
|
||||||
List<ChildAssociationRef> allChildren = nodeService.getChildAssocs(testDoc_Admin,
|
// Some basic node structure tests.
|
||||||
ContentModel.ASSOC_RATINGS, RegexQNamePattern.MATCH_ALL);
|
assertTrue(ContentModel.ASPECT_RATEABLE + " aspect missing.",
|
||||||
|
NODE_SERVICE.hasAspect(testDoc_Admin, ContentModel.ASPECT_RATEABLE));
|
||||||
// It's one cm:rating node per user
|
|
||||||
assertEquals("Wrong number of ratings nodes.", 1, allChildren.size());
|
List<ChildAssociationRef> allChildren = NODE_SERVICE.getChildAssocs(testDoc_Admin,
|
||||||
// child-assoc of type cm:ratings
|
ContentModel.ASSOC_RATINGS, RegexQNamePattern.MATCH_ALL);
|
||||||
assertEquals("Wrong type qname on ratings assoc", ContentModel.ASSOC_RATINGS, allChildren.get(0).getTypeQName());
|
|
||||||
// child-assoc of name cm:<username__ratingScheme>
|
// It's one cm:rating node per user
|
||||||
QName expectedAssocName = ratingNamingConventions.getRatingAssocNameFor(AuthenticationUtil.getFullyAuthenticatedUser(), FIVE_STAR_SCHEME_NAME);
|
assertEquals("Wrong number of ratings nodes.", 1, allChildren.size());
|
||||||
assertEquals("Wrong qname on ratings assoc", expectedAssocName, allChildren.get(0).getQName());
|
// child-assoc of type cm:ratings
|
||||||
// node structure seems ok.
|
assertEquals("Wrong type qname on ratings assoc", ContentModel.ASSOC_RATINGS, allChildren.get(0).getTypeQName());
|
||||||
|
// child-assoc of name cm:<username__ratingScheme>
|
||||||
|
QName expectedAssocName = RATING_NAMING_CONVENTIONS.getRatingAssocNameFor(AuthenticationUtil.getFullyAuthenticatedUser(), FIVE_STAR_SCHEME_NAME);
|
||||||
// Now to check the persisted ratings data are ok.
|
assertEquals("Wrong qname on ratings assoc", expectedAssocName, allChildren.get(0).getQName());
|
||||||
Rating fiveStarRating = ratingService.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
// node structure seems ok.
|
||||||
|
|
||||||
assertNotNull("'5*' rating was null.", fiveStarRating);
|
|
||||||
assertEquals("Wrong score for rating", fiveStarScore, fiveStarRating.getScore());
|
// Now to check the persisted ratings data are ok.
|
||||||
assertEquals("Wrong user for rating", AuthenticationUtil.getFullyAuthenticatedUser(), fiveStarRating.getAppliedBy());
|
Rating fiveStarRating = RATING_SERVICE.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
final Date fiveStarRatingAppliedAt = fiveStarRating.getAppliedAt();
|
|
||||||
assertDateIsCloseToNow(fiveStarRatingAppliedAt);
|
assertNotNull("'5*' rating was null.", fiveStarRating);
|
||||||
|
assertEquals("Wrong score for rating", fiveStarScore, (int)fiveStarRating.getScore());
|
||||||
// Now we'll update a rating
|
assertEquals("Wrong user for rating", AuthenticationUtil.getFullyAuthenticatedUser(), fiveStarRating.getAppliedBy());
|
||||||
final float updatedFiveStarScore = 3;
|
final Date fiveStarRatingAppliedAt = fiveStarRating.getAppliedAt();
|
||||||
ratingService.applyRating(testDoc_Admin, updatedFiveStarScore, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
// Now we'll update a rating
|
||||||
|
final int updatedFiveStarScore = 3;
|
||||||
// Some basic node structure tests.
|
RATING_SERVICE.applyRating(testDoc_Admin, updatedFiveStarScore, FIVE_STAR_SCHEME_NAME);
|
||||||
allChildren = nodeService.getChildAssocs(testDoc_Admin,
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
ContentModel.ASSOC_RATINGS, RegexQNamePattern.MATCH_ALL);
|
|
||||||
|
// Some basic node structure tests.
|
||||||
// Still one cm:rating node
|
allChildren = NODE_SERVICE.getChildAssocs(testDoc_Admin,
|
||||||
assertEquals("Wrong number of ratings nodes.", 1, allChildren.size());
|
ContentModel.ASSOC_RATINGS, RegexQNamePattern.MATCH_ALL);
|
||||||
// Same assoc names
|
|
||||||
assertEquals("Wrong type qname on ratings assoc", ContentModel.ASSOC_RATINGS, allChildren.get(0).getTypeQName());
|
// Still one cm:rating node
|
||||||
assertEquals("Wrong qname on ratings assoc", expectedAssocName, allChildren.get(0).getQName());
|
assertEquals("Wrong number of ratings nodes.", 1, allChildren.size());
|
||||||
// node structure seems ok.
|
// Same assoc names
|
||||||
|
assertEquals("Wrong type qname on ratings assoc", ContentModel.ASSOC_RATINGS, allChildren.get(0).getTypeQName());
|
||||||
|
assertEquals("Wrong qname on ratings assoc", expectedAssocName, allChildren.get(0).getQName());
|
||||||
// Now to check the updated ratings data are ok.
|
// node structure seems ok.
|
||||||
Rating updatedFiveStarRating = ratingService.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
|
|
||||||
// 'five star' data should be changed - new score, new date
|
// Now to check the updated ratings data are ok.
|
||||||
assertNotNull("'5*' rating was null.", updatedFiveStarRating);
|
Rating updatedFiveStarRating = RATING_SERVICE.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
assertEquals("Wrong score for rating", updatedFiveStarScore, updatedFiveStarRating.getScore());
|
|
||||||
assertEquals("Wrong user for rating", AuthenticationUtil.getFullyAuthenticatedUser(), updatedFiveStarRating.getAppliedBy());
|
// 'five star' data should be changed - new score, new date
|
||||||
assertTrue("five star rating date was unchanged.", fiveStarRatingAppliedAt.equals(updatedFiveStarRating.getAppliedAt()) == false);
|
assertNotNull("'5*' rating was null.", updatedFiveStarRating);
|
||||||
assertDateIsCloseToNow(updatedFiveStarRating.getAppliedAt());
|
assertEquals("Wrong score for rating", updatedFiveStarScore, (int)updatedFiveStarRating.getScore());
|
||||||
|
assertEquals("Wrong user for rating", AuthenticationUtil.getFullyAuthenticatedUser(), updatedFiveStarRating.getAppliedBy());
|
||||||
// And delete the 'five star' rating.
|
assertTrue("five star rating date was unchanged.", fiveStarRatingAppliedAt.equals(updatedFiveStarRating.getAppliedAt()) == false);
|
||||||
Rating deletedStarRating = ratingService.removeRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
// And delete the 'five star' rating.
|
||||||
// 'five star' rating data should be unchanged.
|
Rating deletedStarRating = RATING_SERVICE.removeRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
assertNotNull("'5*' rating was null.", deletedStarRating);
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
assertEquals("Wrong score for rating", updatedFiveStarScore, deletedStarRating.getScore());
|
// 'five star' rating data should be unchanged.
|
||||||
assertEquals("Wrong user for rating", AuthenticationUtil.getFullyAuthenticatedUser(), deletedStarRating.getAppliedBy());
|
assertNotNull("'5*' rating was null.", deletedStarRating);
|
||||||
assertEquals("Wrong date for rating", updatedFiveStarRating.getAppliedAt(), deletedStarRating.getAppliedAt());
|
assertEquals("Wrong score for rating", updatedFiveStarScore, (int)deletedStarRating.getScore());
|
||||||
|
assertEquals("Wrong user for rating", AuthenticationUtil.getFullyAuthenticatedUser(), deletedStarRating.getAppliedBy());
|
||||||
// And the deleted ratings should be gone.
|
assertEquals("Wrong date for rating", updatedFiveStarRating.getAppliedAt(), deletedStarRating.getAppliedAt());
|
||||||
assertNull("5* rating not null.", ratingService.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME));
|
|
||||||
|
// And the deleted ratings should be gone.
|
||||||
|
assertNull("5* rating not null.", RATING_SERVICE.getRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME));
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Test public void oneUserRatesAndRerates() throws Exception
|
||||||
* This test method asserts that the specified date is effectively equal to now.
|
|
||||||
* We can't assert that the two dates are exactly equal but we do assert that
|
|
||||||
* they are equal to within a specified tolerance.
|
|
||||||
* @param d the date to check
|
|
||||||
*/
|
|
||||||
private void assertDateIsCloseToNow(Date d)
|
|
||||||
{
|
{
|
||||||
// assertNotNull("Date was unexpectedly null", d);
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
|
||||||
// Date now = new Date();
|
{
|
||||||
// assertTrue("Date was not before 'now'", now.after(d));
|
public Void execute() throws Throwable
|
||||||
// final long millisTolerance = 5000l; // 5 seconds
|
{
|
||||||
// assertTrue("Date was not within " + millisTolerance + "ms of 'now'.", now.getTime() - d.getTime() < millisTolerance);
|
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername());
|
||||||
}
|
RATING_SERVICE.applyRating(testDoc_Admin, 1.0f, FIVE_STAR_SCHEME_NAME);
|
||||||
|
|
||||||
public void testOneUserRatesAndRerates() throws Exception
|
// A new score in the same rating scheme by the same user should replace the previous score.
|
||||||
{
|
RATING_SERVICE.applyRating(testDoc_Admin, 2.0f, FIVE_STAR_SCHEME_NAME);
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
|
|
||||||
ratingService.applyRating(testDoc_Admin, 1.0f, FIVE_STAR_SCHEME_NAME);
|
float meanRating = RATING_SERVICE.getAverageRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong mean rating.", 2, (int)meanRating);
|
||||||
// A new score in the same rating scheme by the same user should replace the previous score.
|
|
||||||
ratingService.applyRating(testDoc_Admin, 2.0f, FIVE_STAR_SCHEME_NAME);
|
float totalRating = RATING_SERVICE.getTotalRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong total rating.", 2, (int)totalRating);
|
||||||
float meanRating = ratingService.getAverageRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong mean rating.", 2f, meanRating);
|
int ratingsCount = RATING_SERVICE.getRatingsCount(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong ratings count.", 1, ratingsCount);
|
||||||
float totalRating = ratingService.getTotalRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong total rating.", 2.0f, totalRating);
|
// There should only be one rating child node under the rated node.
|
||||||
|
assertEquals("Wrong number of child nodes", 1 , NODE_SERVICE.getChildAssocs(testDoc_Admin).size());
|
||||||
int ratingsCount = ratingService.getRatingsCount(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong ratings count.", 1, ratingsCount);
|
return null;
|
||||||
|
}
|
||||||
// There should only be one rating child node under the rated node.
|
});
|
||||||
assertEquals("Wrong number of child nodes", 1 , nodeService.getChildAssocs(testDoc_Admin).size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test method ensures that if a single user attempts to rate a piece of content in two
|
* This test method ensures that if a single user attempts to rate a piece of content in two
|
||||||
* different rating schemes, then an exception should not be thrown.
|
* different rating schemes, then an exception should not be thrown.
|
||||||
*/
|
*/
|
||||||
public void testOneUserRatesInTwoSchemes() throws Exception
|
@Test public void oneUserRatesInTwoSchemes() throws Exception
|
||||||
{
|
{
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
|
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername());
|
||||||
ratingService.applyRating(testDoc_Admin, 2.0f, FIVE_STAR_SCHEME_NAME);
|
|
||||||
|
|
||||||
// A new score in a different rating scheme by the same user should not fail.
|
|
||||||
ratingService.applyRating(testDoc_Admin, 1.0f, LIKES_SCHEME_NAME);
|
|
||||||
|
|
||||||
// There should be two rating child nodes under the rated node.
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
|
||||||
assertEquals("Wrong number of child nodes", 2 , nodeService.getChildAssocs(testDoc_Admin).size());
|
{
|
||||||
|
public Void execute() throws Throwable
|
||||||
List<Rating> ratings = ratingService.getRatingsByCurrentUser(testDoc_Admin);
|
{
|
||||||
assertEquals(2, ratings.size());
|
|
||||||
assertEquals(FIVE_STAR_SCHEME_NAME, ratings.get(0).getScheme().getName());
|
RATING_SERVICE.applyRating(testDoc_UserTwo, 2.0f, FIVE_STAR_SCHEME_NAME);
|
||||||
assertEquals(LIKES_SCHEME_NAME, ratings.get(1).getScheme().getName());
|
|
||||||
|
// A new score in a different rating scheme by the same user should not fail.
|
||||||
|
RATING_SERVICE.applyRating(testDoc_UserTwo, 1.0f, LIKES_SCHEME_NAME);
|
||||||
|
|
||||||
|
// There should be two rating child nodes under the rated node.
|
||||||
|
assertEquals("Wrong number of child nodes", 2 , NODE_SERVICE.getChildAssocs(testDoc_UserTwo).size());
|
||||||
|
|
||||||
|
List<Rating> ratings = RATING_SERVICE.getRatingsByCurrentUser(testDoc_UserTwo);
|
||||||
|
assertEquals(2, ratings.size());
|
||||||
|
assertEquals(FIVE_STAR_SCHEME_NAME, ratings.get(0).getScheme().getName());
|
||||||
|
assertEquals(LIKES_SCHEME_NAME, ratings.get(1).getScheme().getName());
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -306,43 +330,51 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
|||||||
* It checks that the ratings are applied correctly and that the cm:modifier is not
|
* It checks that the ratings are applied correctly and that the cm:modifier is not
|
||||||
* updated by these changes.
|
* updated by these changes.
|
||||||
*/
|
*/
|
||||||
public void testApplyRating_MultipleUsers() throws Exception
|
@Test public void applyRating_MultipleUsers() throws Exception
|
||||||
{
|
{
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
|
||||||
|
{
|
||||||
// 2 different users rating the same piece of content in the same rating scheme
|
public Void execute() throws Throwable
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
|
{
|
||||||
ratingService.applyRating(testDoc_Admin, 4.0f, FIVE_STAR_SCHEME_NAME);
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
|
||||||
|
// 2 different users rating the same piece of content in the same rating scheme
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
|
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername());
|
||||||
ratingService.applyRating(testDoc_Admin, 2.0f, FIVE_STAR_SCHEME_NAME);
|
RATING_SERVICE.applyRating(testDoc_Admin, 4.0f, FIVE_STAR_SCHEME_NAME);
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
|
|
||||||
float meanRating = ratingService.getAverageRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER2.getUsername());
|
||||||
assertEquals("Document had wrong mean rating.", 3f, meanRating);
|
RATING_SERVICE.applyRating(testDoc_Admin, 2.0f, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
float totalRating = ratingService.getTotalRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong total rating.", 6.0f, totalRating);
|
float meanRating = RATING_SERVICE.getAverageRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong mean rating.", 3, (int)meanRating);
|
||||||
int ratingsCount = ratingService.getRatingsCount(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong ratings count.", 2, ratingsCount);
|
float totalRating = RATING_SERVICE.getTotalRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong total rating.", 6, (int)totalRating);
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
|
||||||
|
int ratingsCount = RATING_SERVICE.getRatingsCount(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
// One user removes their rating.
|
assertEquals("Document had wrong ratings count.", 2, ratingsCount);
|
||||||
ratingService.removeRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
meanRating = ratingService.getAverageRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong mean rating.", 4f, meanRating);
|
// One user removes their rating.
|
||||||
|
RATING_SERVICE.removeRatingByCurrentUser(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
totalRating = ratingService.getTotalRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong total rating.", 4f, totalRating);
|
meanRating = RATING_SERVICE.getAverageRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong mean rating.", 4, (int)meanRating);
|
||||||
ratingsCount = ratingService.getRatingsCount(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
|
||||||
assertEquals("Document had wrong ratings count.", 1, ratingsCount);
|
totalRating = RATING_SERVICE.getTotalRating(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong total rating.", 4, (int)totalRating);
|
||||||
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
|
||||||
|
ratingsCount = RATING_SERVICE.getRatingsCount(testDoc_Admin, FIVE_STAR_SCHEME_NAME);
|
||||||
|
assertEquals("Document had wrong ratings count.", 1, ratingsCount);
|
||||||
|
|
||||||
|
assertModifierIs(testDoc_Admin, AuthenticationUtil.getAdminUserName());
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -353,62 +385,51 @@ public class RatingServiceIntegrationTest extends BaseAlfrescoSpringTest
|
|||||||
*/
|
*/
|
||||||
private void assertModifierIs(NodeRef nodeRef, final String expectedModifier)
|
private void assertModifierIs(NodeRef nodeRef, final String expectedModifier)
|
||||||
{
|
{
|
||||||
String actualModifier = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_MODIFIER);
|
String actualModifier = (String)NODE_SERVICE.getProperty(nodeRef, ContentModel.PROP_MODIFIER);
|
||||||
assertEquals("Incorrect cm:modifier", expectedModifier, actualModifier);
|
assertEquals("Incorrect cm:modifier", expectedModifier, actualModifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testUsersCantRateTheirOwnContent() throws Exception
|
@Test public void usersCantRateTheirOwnContent() throws Exception
|
||||||
{
|
{
|
||||||
// In the likes rating scheme, users can rate their own content.
|
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER2.getUsername());
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
|
|
||||||
ratingService.applyRating(testDoc_UserTwo, 1, LIKES_SCHEME_NAME);
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
// But fiveStar rating scheme disallows rating your own content.
|
public Void execute() throws Throwable
|
||||||
ratingService.applyRating(testDoc_UserTwo, 4, FIVE_STAR_SCHEME_NAME);
|
{
|
||||||
}
|
// In the likes rating scheme, users can rate their own content.
|
||||||
catch (RatingServiceException expected)
|
RATING_SERVICE.applyRating(testDoc_UserTwo, 1, LIKES_SCHEME_NAME);
|
||||||
{
|
|
||||||
return;
|
// But fiveStar rating scheme disallows rating your own content.
|
||||||
}
|
boolean expectedExceptionThrown = false;
|
||||||
fail("Expected exception not thrown");
|
try
|
||||||
|
{
|
||||||
|
RATING_SERVICE.applyRating(testDoc_UserTwo, 4, FIVE_STAR_SCHEME_NAME);
|
||||||
|
} catch (RatingServiceException expected)
|
||||||
|
{
|
||||||
|
expectedExceptionThrown = true;
|
||||||
|
}
|
||||||
|
assertTrue(expectedExceptionThrown);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createUser(String userName)
|
@Test public void javascriptAPI() throws Exception
|
||||||
{
|
{
|
||||||
if (! authenticationService.authenticationExists(userName))
|
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
|
||||||
{
|
{
|
||||||
authenticationService.createAuthentication(userName, "PWD".toCharArray());
|
public Void execute() throws Throwable
|
||||||
}
|
{
|
||||||
|
Map<String, Object> model = new HashMap<String, Object>();
|
||||||
if (! personService.personExists(userName))
|
model.put("testNode", testDoc_UserOne);
|
||||||
{
|
|
||||||
PropertyMap ppOne = new PropertyMap(4);
|
ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/rating/script/test_ratingService.js");
|
||||||
ppOne.put(ContentModel.PROP_USERNAME, userName);
|
SCRIPT_SERVICE.executeScript(location, model);
|
||||||
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
|
|
||||||
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
|
return null;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testJavascriptAPI() throws Exception
|
|
||||||
{
|
|
||||||
Map<String, Object> model = new HashMap<String, Object>();
|
|
||||||
model.put("testNode", this.testDoc_UserOne);
|
|
||||||
|
|
||||||
ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/rating/script/test_ratingService.js");
|
|
||||||
this.scriptService.executeScript(location, model);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2005-2012
|
||||||
|
Alfresco Software Limited.
|
||||||
|
*
|
||||||
|
* This file is part of Alfresco
|
||||||
|
*
|
||||||
|
* Alfresco is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Alfresco is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package org.alfresco.util.test.junitrules;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.alfresco.model.ContentModel;
|
||||||
|
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||||
|
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
|
||||||
|
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||||
|
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||||
|
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||||
|
import org.alfresco.service.cmr.repository.NodeRef;
|
||||||
|
import org.alfresco.service.cmr.repository.NodeService;
|
||||||
|
import org.alfresco.service.namespace.NamespaceService;
|
||||||
|
import org.alfresco.service.namespace.QName;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.junit.rules.ExternalResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A JUnit rule designed to help with the automatic cleanup of temporary test nodes.
|
||||||
|
*
|
||||||
|
* @author Neil Mc Erlean
|
||||||
|
* @since Odin
|
||||||
|
*/
|
||||||
|
public class TemporaryNodes extends ExternalResource
|
||||||
|
{
|
||||||
|
private static final Log log = LogFactory.getLog(TemporaryNodes.class);
|
||||||
|
|
||||||
|
private final ApplicationContextInit appContextRule;
|
||||||
|
private List<NodeRef> temporaryNodeRefs = new ArrayList<NodeRef>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the rule with a reference to a {@link ApplicationContextInit rule} which can be used to retrieve the ApplicationContext.
|
||||||
|
*
|
||||||
|
* @param appContextRule a rule which can be used to retrieve the spring app context.
|
||||||
|
*/
|
||||||
|
public TemporaryNodes(ApplicationContextInit appContextRule)
|
||||||
|
{
|
||||||
|
this.appContextRule = appContextRule;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override protected void before() throws Throwable
|
||||||
|
{
|
||||||
|
// Intentionally empty
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override protected void after()
|
||||||
|
{
|
||||||
|
final RetryingTransactionHelper transactionHelper = (RetryingTransactionHelper) appContextRule.getApplicationContext().getBean("retryingTransactionHelper");
|
||||||
|
final NodeService nodeService = (NodeService) appContextRule.getApplicationContext().getBean("nodeService");
|
||||||
|
|
||||||
|
// Run as admin to ensure all non-system nodes can be deleted irrespecive of which user created them.
|
||||||
|
AuthenticationUtil.runAs(new RunAsWork<Void>()
|
||||||
|
{
|
||||||
|
@Override public Void doWork() throws Exception
|
||||||
|
{
|
||||||
|
transactionHelper.doInTransaction(new RetryingTransactionCallback<Void>()
|
||||||
|
{
|
||||||
|
@Override public Void execute() throws Throwable
|
||||||
|
{
|
||||||
|
// Although we loop through all nodes, this is a cascade-delete and so we may only need to delete the first node.
|
||||||
|
for (NodeRef node : temporaryNodeRefs)
|
||||||
|
{
|
||||||
|
if (nodeService.exists(node))
|
||||||
|
{
|
||||||
|
log.debug("Deleting temporary node " + nodeService.getProperty(node, ContentModel.PROP_NAME));
|
||||||
|
nodeService.deleteNode(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}, AuthenticationUtil.getAdminUserName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a specified NodeRef to the list of NodeRefs to be deleted by this rule.
|
||||||
|
*
|
||||||
|
* @param temporaryNodeRef a NodeRef
|
||||||
|
*/
|
||||||
|
public void addNodeRef(NodeRef temporaryNodeRef)
|
||||||
|
{
|
||||||
|
this.temporaryNodeRefs.add(temporaryNodeRef);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method creates a NodeRef and adds it to the internal list of NodeRefs to be tidied up by the rule.
|
||||||
|
* This method will be run in its own transaction and will be run with the specified user as the fully authenticated user,
|
||||||
|
* thus ensuring the named user is the cm:creator of the new node.
|
||||||
|
*
|
||||||
|
* @param parentNode the parent node
|
||||||
|
* @param nodeCmName the cm:name of the new node
|
||||||
|
* @param nodeType the type of the new node
|
||||||
|
* @param nodeCreator the username of the person who will create the node
|
||||||
|
* @return the newly created NodeRef.
|
||||||
|
*/
|
||||||
|
public NodeRef createNode(final NodeRef parentNode, final String nodeCmName, final QName nodeType, final String nodeCreator)
|
||||||
|
{
|
||||||
|
final RetryingTransactionHelper transactionHelper = (RetryingTransactionHelper) appContextRule.getApplicationContext().getBean("retryingTransactionHelper");
|
||||||
|
|
||||||
|
AuthenticationUtil.pushAuthentication();
|
||||||
|
AuthenticationUtil.setFullyAuthenticatedUser(nodeCreator);
|
||||||
|
|
||||||
|
NodeRef newNodeRef = transactionHelper.doInTransaction(new RetryingTransactionCallback<NodeRef>()
|
||||||
|
{
|
||||||
|
public NodeRef execute() throws Throwable
|
||||||
|
{
|
||||||
|
final NodeService nodeService = (NodeService) appContextRule.getApplicationContext().getBean("nodeService");
|
||||||
|
|
||||||
|
Map<QName, Serializable> props = new HashMap<QName, Serializable>();
|
||||||
|
props.put(ContentModel.PROP_NAME, nodeCmName);
|
||||||
|
QName childName = QName.createQName(NamespaceService.APP_MODEL_1_0_URI, nodeCmName);
|
||||||
|
ChildAssociationRef childAssoc = nodeService.createNode(parentNode,
|
||||||
|
ContentModel.ASSOC_CONTAINS,
|
||||||
|
childName,
|
||||||
|
nodeType,
|
||||||
|
props);
|
||||||
|
return childAssoc.getChildRef();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
AuthenticationUtil.popAuthentication();
|
||||||
|
|
||||||
|
this.temporaryNodeRefs.add(newNodeRef);
|
||||||
|
return newNodeRef;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user