As part of prep to fix ALF-11305, I have enhanced the TemporarySites JUnit rule.

The rule now has a method, createTestSiteWithUserPerRole(...), which will create a Share Site and a user for each of the 4 share roles and ensure that they are automatically deleted after the Rule has run (usually after a test method or test class).


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@39685 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2012-07-24 15:40:17 +00:00
parent 008e4b4570
commit b5b0cd7ab9
2 changed files with 115 additions and 4 deletions

View File

@@ -33,7 +33,7 @@ import org.alfresco.service.cmr.site.SiteVisibility;
import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.rules.ExternalResource;
import org.springframework.extensions.webscripts.GUID;
/**
* A JUnit rule designed to help with the automatic cleanup of temporary st:site nodes.
@@ -41,12 +41,12 @@ import org.junit.rules.ExternalResource;
* @author Neil Mc Erlean
* @since 4.0.3
*/
public class TemporarySites extends ExternalResource
public class TemporarySites extends AbstractPersonRule
{
private static final Log log = LogFactory.getLog(TemporarySites.class);
private final ApplicationContextInit appContextRule;
private List<SiteInfo> temporarySites = new ArrayList<SiteInfo>();
private List<String> temporarySiteUsers = new ArrayList<String>();
/**
* Constructs the rule with a reference to a {@link ApplicationContextInit rule} which can be used to retrieve the ApplicationContext.
@@ -55,7 +55,7 @@ public class TemporarySites extends ExternalResource
*/
public TemporarySites(ApplicationContextInit appContextRule)
{
this.appContextRule = appContextRule;
super(appContextRule);
}
@@ -88,6 +88,12 @@ public class TemporarySites extends ExternalResource
}
}
for (String username : temporarySiteUsers)
{
log.debug("Deleting temporary site user " + username);
deletePerson(username);
}
return null;
}
});
@@ -162,4 +168,82 @@ public class TemporarySites extends ExternalResource
this.temporarySites.add(newSite);
return newSite;
}
/**
* This method creates a test site (of Alfresco type <code>st:site</code>) and one user for each of the Share Site Roles.
* 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 creator of the new site.
* The site and its users will be deleted automatically by the rule.
*
* @param sitePreset the site preset.
* @param visibility the Site visibility.
* @param siteCreator the username of a user who will be used to create the site (user must exist of course).
* @return the {@link SiteInfo} object for the newly created site.
*/
public TestSiteAndMemberInfo createTestSiteWithUserPerRole(final String siteShortName, String sitePreset, SiteVisibility visibility, String siteCreator)
{
// create the site
SiteInfo result = this.createSite(sitePreset, siteShortName, null, null, visibility, siteCreator);
// create the users
final RetryingTransactionHelper transactionHelper = appContextRule.getApplicationContext().getBean("retryingTransactionHelper", RetryingTransactionHelper.class);
final SiteService siteService = appContextRule.getApplicationContext().getBean("siteService", SiteService.class);
AuthenticationUtil.pushAuthentication();
AuthenticationUtil.setFullyAuthenticatedUser(siteCreator);
List<String> userNames = transactionHelper.doInTransaction(new RetryingTransactionCallback<List<String>>()
{
public List<String> execute() throws Throwable
{
List<String> users = new ArrayList<String>(4);
for (String shareRole : SiteModel.STANDARD_PERMISSIONS)
{
final String userName = siteShortName + "_" + shareRole + "_" + GUID.generate();
log.debug("Creating temporary site user " + userName);
createPerson(userName);
siteService.setMembership(siteShortName, userName, shareRole);
users.add(userName);
temporarySiteUsers.add(userName);
}
return users;
}
});
AuthenticationUtil.popAuthentication();
return new TestSiteAndMemberInfo(result, userNames.get(0),
userNames.get(1),
userNames.get(2),
userNames.get(3));
}
/**
* A simple POJO class to store the {@link SiteInfo} for this site and its initial, automatically created members' usernames.
*
* @author Neil Mc Erlean
*/
public static class TestSiteAndMemberInfo
{
public final SiteInfo siteInfo;
public final String siteManager;
public final String siteCollaborator;
public final String siteContributor;
public final String siteConsumer;
public TestSiteAndMemberInfo(SiteInfo siteInfo, String siteManager, String siteCollaborator, String siteContributor, String siteConsumer)
{
this.siteInfo = siteInfo;
this.siteManager = siteManager;
this.siteCollaborator = siteCollaborator;
this.siteContributor = siteContributor;
this.siteConsumer = siteConsumer;
}
}
}

View File

@@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.site.SiteInfo;
@@ -30,6 +31,8 @@ import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.cmr.site.SiteVisibility;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.GUID;
import org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
@@ -70,6 +73,7 @@ public class TemporarySitesTest
// These SiteInfos are used by the test methods.
private SiteInfo testSite1, testSite2;
private TestSiteAndMemberInfo testSiteWithMembers;
@BeforeClass public static void initStaticData() throws Exception
{
@@ -84,6 +88,8 @@ public class TemporarySitesTest
testSite1 = testSites.createSite("sitePreset", "testSite1", "t", "d", SiteVisibility.PUBLIC, AuthenticationUtil.getAdminUserName());
final QName subSiteType = QName.createQName("testsite", "testSubsite", NAMESPACE_SERVICE);
testSite2 = testSites.createSite("sitePreset", "testSite2", "T", "D", SiteVisibility.PUBLIC, subSiteType, AuthenticationUtil.getAdminUserName());
testSiteWithMembers = testSites.createTestSiteWithUserPerRole(GUID.generate(), "sitePreset", SiteVisibility.PUBLIC, AuthenticationUtil.getAdminUserName());
}
@Test public void ensureTestSitesWereCreatedOk() throws Exception
@@ -108,4 +114,25 @@ public class TemporarySitesTest
}
});
}
@Test public void ensureUsersWithShareRolesArePresentAndCorrect() throws Exception
{
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
final String shortName = testSiteWithMembers.siteInfo.getShortName();
final SiteInfo recoveredSite = SITE_SERVICE.getSite(shortName);
assertNotNull("Test site does not exist", recoveredSite);
assertEquals(SiteModel.SITE_MANAGER, SITE_SERVICE.getMembersRole(shortName, testSiteWithMembers.siteManager));
assertEquals(SiteModel.SITE_COLLABORATOR, SITE_SERVICE.getMembersRole(shortName, testSiteWithMembers.siteCollaborator));
assertEquals(SiteModel.SITE_CONTRIBUTOR, SITE_SERVICE.getMembersRole(shortName, testSiteWithMembers.siteContributor));
assertEquals(SiteModel.SITE_CONSUMER, SITE_SERVICE.getMembersRole(shortName, testSiteWithMembers.siteConsumer));
return null;
}
});
}
}