diff --git a/source/java/org/alfresco/util/test/junitrules/TemporarySites.java b/source/java/org/alfresco/util/test/junitrules/TemporarySites.java index 2b4a66c9c2..961344d302 100644 --- a/source/java/org/alfresco/util/test/junitrules/TemporarySites.java +++ b/source/java/org/alfresco/util/test/junitrules/TemporarySites.java @@ -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 temporarySites = new ArrayList(); + private List temporarySiteUsers = new ArrayList(); /** * 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 st:site) 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 userNames = transactionHelper.doInTransaction(new RetryingTransactionCallback>() + { + public List execute() throws Throwable + { + List users = new ArrayList(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; + } + } } diff --git a/source/java/org/alfresco/util/test/junitrules/TemporarySitesTest.java b/source/java/org/alfresco/util/test/junitrules/TemporarySitesTest.java index 46cfb5fe89..581af6352c 100644 --- a/source/java/org/alfresco/util/test/junitrules/TemporarySitesTest.java +++ b/source/java/org/alfresco/util/test/junitrules/TemporarySitesTest.java @@ -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() + { + 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; + } + }); + } }