();
             }
             permsValue.add(ap.getPermission());
             perms.put(ap.getAuthority(), permsValue);
          }
       }
       return perms;
    }
    
    /**
     * ALF-10343 - When the default public group for sites isn't EVERYBODY,
     * check that creating and altering sites results in the correct permissions 
     */
    public void testNonDefaultPublicGroupPermissions() throws Exception
    {
       // Sanity check the current permissions
       assertEquals(PermissionService.ALL_AUTHORITIES, sysAdminParams.getSitePublicGroup());
       
       // Change the public site group
       SysAdminParamsImpl sp = new SysAdminParamsImpl();
       sp.setSitePublicGroup(groupFour);
       siteServiceImpl.setSysAdminParams(sp);
       
       // Create sites of the three types
       SiteInfo s1 = this.siteService.createSite(TEST_SITE_PRESET, "SiteTest_priv", "priv", "priv", SiteVisibility.PRIVATE);
       SiteInfo s2 = this.siteService.createSite(TEST_SITE_PRESET, "SiteTest_mod", "mod", "mod", SiteVisibility.MODERATED);
       SiteInfo s3 = this.siteService.createSite(TEST_SITE_PRESET, "SiteTest_pub", "pub", "pub", SiteVisibility.PUBLIC);
       
       
       // Check the permissions on them
       // Everyone has read permissions only, not Consumer
       assertTrue(getAllowedPermissionsMap(s1).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       assertTrue(getAllowedPermissionsMap(s2).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       assertTrue(getAllowedPermissionsMap(s3).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       
       // On the public + moderated sites, the special group will be a Consumer
       assertEquals(null,                    getAllowedPermissionsMap(s1).get(groupFour));
       assertTrue(getAllowedPermissionsMap(s2).get(groupFour).contains(SiteModel.SITE_CONSUMER));
       assertTrue(getAllowedPermissionsMap(s3).get(groupFour).contains(SiteModel.SITE_CONSUMER));
       
       // Our current user will be Manager
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s1.getShortName(), USER_ONE));
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s2.getShortName(), USER_ONE));
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s3.getShortName(), USER_ONE));
       
       
       // Swap the visibilites around, private+moderated -> public, public -> private
       s1.setVisibility(SiteVisibility.PUBLIC);
       s2.setVisibility(SiteVisibility.PUBLIC);
       s3.setVisibility(SiteVisibility.PRIVATE);
       siteService.updateSite(s1);
       siteService.updateSite(s2);
       siteService.updateSite(s3);
       
       // Check the permissions now
       // Everyone still has read permissions everywhere, but nothing more
       assertTrue(getAllowedPermissionsMap(s1).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       assertTrue(getAllowedPermissionsMap(s2).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       assertTrue(getAllowedPermissionsMap(s3).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       
       // The site public group has consumer permissions on mod+public
       assertTrue(getAllowedPermissionsMap(s1).get(groupFour).contains(SiteModel.SITE_CONSUMER));
       assertTrue(getAllowedPermissionsMap(s2).get(groupFour).contains(SiteModel.SITE_CONSUMER));
       assertEquals(null,                    getAllowedPermissionsMap(s3).get(groupFour));
       
       // Our user is still the manager
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s1.getShortName(), USER_ONE));
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s2.getShortName(), USER_ONE));
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s3.getShortName(), USER_ONE));
       
       
       // Swap them back again
       s1.setVisibility(SiteVisibility.PRIVATE);
       s2.setVisibility(SiteVisibility.MODERATED);
       s3.setVisibility(SiteVisibility.PUBLIC);
       siteService.updateSite(s1);
       siteService.updateSite(s2);
       siteService.updateSite(s3);
       
       // Check the permissions have restored
       // Everyone only has read permissions
       assertTrue(getAllowedPermissionsMap(s1).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       assertTrue(getAllowedPermissionsMap(s2).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       assertTrue(getAllowedPermissionsMap(s3).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
       
       // The site public group has consumer permissions on mod+public
       assertEquals(null,                    getAllowedPermissionsMap(s1).get(groupFour));
       assertTrue(getAllowedPermissionsMap(s2).get(groupFour).contains(SiteModel.SITE_CONSUMER));
       assertTrue(getAllowedPermissionsMap(s3).get(groupFour).contains(SiteModel.SITE_CONSUMER));
       
       // Our user is still the manager
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s1.getShortName(), USER_ONE));
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s2.getShortName(), USER_ONE));
       assertEquals(SiteModel.SITE_MANAGER, siteService.getMembersRole(s3.getShortName(), USER_ONE));
    }
    
    private SiteInfo createTestSiteWithContent(String siteShortName, String componentId, SiteVisibility visibility)
    {
        return this.createTestSiteWithContent(siteShortName, componentId, visibility, "");
    }
    
    /**
     * Creates a site with a simple content tree within it.
     * The content looks like
     * 
     * [site] {siteShortName}
     *    |
     *    --- [siteContainer] {componentId}
     *          |
     *          --- [cm:content] fileFolderPrefix + "file.txt"
     *          |
     *          |-- [folder] fileFolderPrefix + "folder"
     *                  |
     *                  |-- [cm:content] fileFolderPrefix + "fileInFolder.txt"
     *                  |
     *                  |-- [folder] fileFolderPrefix + "subfolder"
     *                         |
     *                         |-- [cm:content] fileFolderPrefix + "fileInSubfolder.txt"
     *  
     * 
     * @param siteShortName short name for the site
     * @param componentId the component id for the container
     * @param visibility visibility for the site.
     * @param fileFolderPrefix a prefix String to put on all folders/files created.
     */
    private SiteInfo createTestSiteWithContent(String siteShortName, String componentId, SiteVisibility visibility, String fileFolderPrefix)
    {
             // Create a public site
             SiteInfo siteInfo = this.siteService.createSite(TEST_SITE_PRESET, 
                                                        siteShortName, 
                                                             TEST_TITLE, 
                                                             TEST_DESCRIPTION, 
                                                             visibility);
        
        NodeRef siteContainer = this.siteService.createContainer(siteShortName, componentId, ContentModel.TYPE_FOLDER, null);
        FileInfo fileInfo = this.fileFolderService.create(siteContainer, fileFolderPrefix + "file.txt", ContentModel.TYPE_CONTENT);
             ContentWriter writer = this.fileFolderService.getWriter(fileInfo.getNodeRef());
             writer.putContent("Just some old content that doesn't mean anything");
             
        FileInfo folder1Info = this.fileFolderService.create(siteContainer, fileFolderPrefix + "folder", ContentModel.TYPE_FOLDER);
        FileInfo fileInfo2 = this.fileFolderService.create(folder1Info.getNodeRef(), fileFolderPrefix + "fileInFolder.txt", ContentModel.TYPE_CONTENT);
        ContentWriter writer2 = this.fileFolderService.getWriter(fileInfo2.getNodeRef());
        writer2.putContent("Just some old content that doesn't mean anything");
        
        FileInfo folder2Info = this.fileFolderService.create(folder1Info.getNodeRef(), fileFolderPrefix + "subfolder", ContentModel.TYPE_FOLDER);
        FileInfo fileInfo3 = this.fileFolderService.create(folder2Info.getNodeRef(), fileFolderPrefix + "fileInSubfolder.txt", ContentModel.TYPE_CONTENT);
        ContentWriter writer3 = this.fileFolderService.getWriter(fileInfo3.getNodeRef());
        writer3.putContent("Just some old content that doesn't mean anything");
        
         return siteInfo;
    }
    
    private void testVisibilityPermissions(String message, String userName, SiteInfo siteInfo, boolean listSite, boolean readSite)
    {
        String holdUser = this.authenticationComponent.getCurrentUserName();
        this.authenticationComponent.setCurrentUser(userName);
        try
        {
            // Can the site be seen in the list sites by the user?
            List sites = this.siteService.listSites(null, null);
            boolean siteInList = sites.contains(siteInfo);
            if (listSite == true && siteInList == false)
            {
                fail(message + ":  The site '" + siteInfo.getShortName() + "' was expected in the list of sites for user '" + userName + "'");
            }
            else if (listSite == false && siteInList == true)
            {
                fail(message + ":  The site '" + siteInfo.getShortName() + "' was NOT expected in the list of sites for user '" + userName + "'");
            }
            
            if (siteInList == true)
            {
                try
                {
                    // Can site content be read by the user?
                    NodeRef folder = this.siteService.getContainer(siteInfo.getShortName(), "testComp");
                    @SuppressWarnings("unused")
                    List files = this.fileFolderService.listFiles(folder);
                    if (readSite == false)
                    {
                        fail(message + ":  Content of the site '" + siteInfo.getShortName() + "' was NOT expected to be read by user '" + userName + "'");
                    }
                }
                catch (Exception exception)
                {
                    if (readSite == true)
                    {
                        fail(message + ":  Content of the site '" + siteInfo.getShortName() + "' was expected to be read by user '" + userName + "'");
                    }
                }
            }
        }
        finally
        {
            this.authenticationComponent.setCurrentUser(holdUser);
        }
    }
    
    /**
     * Create a site with a USER manager.
     * Add Group manager membership.
     * 
     * Lower User membership - should be O.K. because of Group Membership
     * Lower Group membership - should be prevented (last manager)
     * 
     * Reset User membership to Manager
     * 
     * Lower Group membership - should be O.K. because of User Membership
     * Lower User membership - should be prevented (last manager)
     * 
     */
    public void testALFCOM_3109()
    {
        // USER_ONE - SiteManager
        // GROUP_TWO - Manager
        
        String siteName = "testALFCOM_3019";
        
        // Create a site as user one
        this.siteService.createSite(TEST_SITE_PRESET, siteName, TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.MODERATED);
        
        Map members = this.siteService.listMembers(siteName, null, null, 0);
        String managerName = members.keySet().iterator().next();
        
         /**
         *  Add a group (GROUP_TWO) with role Manager
         */
        this.siteService.setMembership(siteName, this.groupTwo, SiteModel.SITE_MANAGER);  
        
        // Should be allowed
        this.siteService.setMembership(siteName, managerName, SiteModel.SITE_CONTRIBUTOR); 
        
        /**
         * Should not be allowed to delete last group
         */
        try
        {
            this.siteService.setMembership(siteName, this.groupTwo, SiteModel.SITE_CONTRIBUTOR); 
            fail();
        }
        catch (Exception e)
        {
            // Should go here   
        }
        
        this.siteService.setMembership(siteName, managerName, SiteModel.SITE_MANAGER); 
        
        this.siteService.setMembership(siteName, this.groupTwo, SiteModel.SITE_CONTRIBUTOR); 
        
        /**
         * Should not be allowed to delete last user
         */
        try
        {
            this.siteService.setMembership(siteName, managerName, SiteModel.SITE_CONTRIBUTOR); 
            fail();
        }
        catch (Exception e)
        {
            // Should go here
        }  
    }
    
    /**
     * Create a site with a USER manager.
     * Add Group manager membership.
     * 
     * Remove User membership - should be O.K. because of Group Membership
     * Remove Group membership - should be prevented (last manager)
     * 
     * Add User membership to Manager
     * 
     * Remove Group membership - should be O.K. because of User Membership
     * Remove User membership - should be prevented (last manager)
     * 
     */
    public void testALFCOM_3111()
    {
        // USER_ONE - SiteManager
        // GROUP_TWO - Manager
        
        String siteName = "testALFCOM_3019";
        
        // Create a site as user one
        this.siteService.createSite(TEST_SITE_PRESET, siteName, TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.MODERATED);
        
        Map members = this.siteService.listMembers(siteName, null, null, 0);
        String managerName = members.keySet().iterator().next();
        
         /**
         *  Add a group (GROUP_TWO) with role Manager
         */
        this.siteService.setMembership(siteName, this.groupTwo, SiteModel.SITE_MANAGER);  
        
        // Should be allowed
        this.siteService.removeMembership(siteName, managerName); 
        
        /**
         * Should not be allowed to delete last group
         */
        try
        {
            this.siteService.removeMembership(siteName, this.groupTwo); 
            fail();
        }
        catch (Exception e)
        {
            // Should go here   
        }
        
        this.siteService.setMembership(siteName, managerName, SiteModel.SITE_MANAGER); 
        
        this.siteService.removeMembership(siteName, this.groupTwo); 
        
        /**
         * Should not be allowed to delete last user
         */
        try
        {
            this.siteService.removeMembership(siteName, managerName); 
            fail();
        }
        catch (Exception e)
        {
            // Should go here
        }  
    }
    /**
     * Create a private site.
     *
     * Attempt to access a private site by someone that is not a consumer of that site.
     * 
     */
    public void testETHREEOH_1268()
    {
        RetryingTransactionCallback work = new RetryingTransactionCallback()
        {
            @Override
            public Object execute() throws Throwable
            {
                // USER_ONE - SiteManager
                // GROUP_TWO - Manager
                String siteName = "testALFCOM_XXXX";
                // Create a site as user one
                siteService.createSite(TEST_SITE_PRESET, siteName, TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.PRIVATE);
                SiteInfo si = siteService.getSite(siteName);
                assertNotNull("site info is null", si);
                authenticationComponent.setCurrentUser(USER_TWO);
                si = siteService.getSite(siteName);
                assertNull("site info is not null", si);
                authenticationComponent.setSystemUserAsCurrentUser();
                siteService.deleteSite(siteName);
                
                return null;
            }
        };
        endTransaction();
        transactionService.getRetryingTransactionHelper().doInTransaction(work);
        startNewTransaction();
    }
    /**
     * ALF-3200
     * You shouldn't be able to rename a Site using the normal node service
     *  type operations, because the relationship between a site and its
     *  authorities is based on a pattern that uses the site name.
     * However, you are free to change a site's display name. 
     */
    public void testALF_3200() throws Exception
    {
       // Create the site
       String siteName = "testALF_3200";
       SiteInfo siteInfo = this.siteService.createSite(
             TEST_SITE_PRESET, siteName, TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.MODERATED);
       
       // Grab the details
       NodeRef siteNodeRef = siteInfo.getNodeRef();
       
       // Try to rename it
       try 
       {
          fileFolderService.rename(siteNodeRef, "RenamedName");
          fail("Shouldn't be able to rename a site but did");
       } 
       catch(SiteServiceException e) 
       {
           // expected
       }
       
       // Now just try to change the display name (title) via the node service
       assertEquals(TEST_TITLE, nodeService.getProperty(siteNodeRef, ContentModel.PROP_TITLE));
       
       String newName = "ChangedTitleName";
       String newName2 = "Changed2Title2Name";
       nodeService.setProperty(siteNodeRef, ContentModel.PROP_TITLE, newName);
       assertEquals(newName, nodeService.getProperty(siteNodeRef, ContentModel.PROP_TITLE));
       
       // And also via the site info
       siteInfo = this.siteService.getSite(siteNodeRef);
       assertEquals(newName, siteInfo.getTitle());
       siteInfo.setTitle(newName2);
       siteService.updateSite(siteInfo);
       
       assertEquals(newName2, siteInfo.getTitle());
       assertEquals(newName2, nodeService.getProperty(siteNodeRef, ContentModel.PROP_TITLE));
    }
    
    public void testALF_5556() throws Exception
    {
        String siteName = "testALF_5556";
        SiteInfo siteInfo = this.siteService.createSite(
              TEST_SITE_PRESET, siteName, TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.MODERATED);
        
        // create a container for the site
        NodeRef container = this.siteService.createContainer(siteInfo.getShortName(), "folder.component", null, null);
        
        // Try to rename the container
        try 
        {
           fileFolderService.rename(container, "RenamedContainer");
           fail("Shouldn't be able to rename a container but was able to");
        } 
        catch (SiteServiceException e) 
        {
            // expected
        }
    }
    public void testALF8036_PermissionsAfterCopyingFolderBetweenSites() throws Exception
    {
        alf8036Impl(true);
    }
    private void alf8036Impl(boolean copyNotMove)
    {
        // Create two test sites
        SiteInfo fromSite = this.createTestSiteWithContent("fromSite", "doclib", SiteVisibility.PUBLIC, "FROM");
        SiteInfo toSite = this.createTestSiteWithContent("toSite", "doclib", SiteVisibility.PUBLIC, "TO");
        
        // Find the folder to be copied/moved.
        NodeRef fromDoclibContainer = nodeService.getChildByName(fromSite.getNodeRef(), ContentModel.ASSOC_CONTAINS, "doclib");
        assertNotNull(fromDoclibContainer);
        NodeRef fromFolder = nodeService.getChildByName(fromDoclibContainer, ContentModel.ASSOC_CONTAINS, "FROMfolder");
        assertNotNull(fromFolder);
        NodeRef fromSubFolder = nodeService.getChildByName(fromFolder, ContentModel.ASSOC_CONTAINS, "FROMsubfolder");
        assertNotNull(fromSubFolder);
        
        // The bug is only observed if we set some specific permissions on the folder.
        // We'll demote contributors to consumer-level permissions.
        permissionService.setPermission(fromFolder, siteServiceImpl.getSiteRoleGroup(fromSite.getShortName(), SiteModel.SITE_CONTRIBUTOR, true), SiteModel.SITE_CONSUMER, false);
        
        // And we'll change permissions on a subfolder too
        permissionService.setPermission(fromSubFolder, siteServiceImpl.getSiteRoleGroup(fromSite.getShortName(), SiteModel.SITE_COLLABORATOR, true), SiteModel.SITE_CONSUMER, false);
               
        // Find the folder to copy/move it to.
        NodeRef toDoclibContainer = nodeService.getChildByName(toSite.getNodeRef(), ContentModel.ASSOC_CONTAINS, "doclib");
        assertNotNull(toDoclibContainer);
        NodeRef toFolder = nodeService.getChildByName(toDoclibContainer, ContentModel.ASSOC_CONTAINS, "TOfolder");
        assertNotNull(toFolder);
        // Copy/move it
        NodeRef relocatedNode;
        if (copyNotMove)
        {
            relocatedNode = copyService.copy(fromFolder, toFolder, ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS, true);
        }
        else
        {
            relocatedNode = nodeService.moveNode(fromFolder, toDoclibContainer, ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS).getChildRef();
        }
        siteService.cleanSitePermissions(relocatedNode, null);
        
        // Ensure the permissions on the copied/moved node are those of the target site and not those of the source site.
        Map expectedPermissions = new HashMap();
        expectedPermissions.put(siteService.getSiteRoleGroup(toSite.getShortName(), SiteModel.SITE_MANAGER), SiteModel.SITE_MANAGER);
        expectedPermissions.put(siteService.getSiteRoleGroup(toSite.getShortName(), SiteModel.SITE_COLLABORATOR), SiteModel.SITE_COLLABORATOR);
        expectedPermissions.put(siteService.getSiteRoleGroup(toSite.getShortName(), SiteModel.SITE_CONTRIBUTOR), SiteModel.SITE_CONTRIBUTOR);
        expectedPermissions.put(siteService.getSiteRoleGroup(toSite.getShortName(), SiteModel.SITE_CONSUMER), SiteModel.SITE_CONSUMER);
        validatePermissionsOnRelocatedNode(fromSite, toSite, relocatedNode, expectedPermissions);
        
        // Get the subfolder and check its permissions too.
        NodeRef copyOfSubFolder = nodeService.getChildByName(relocatedNode, ContentModel.ASSOC_CONTAINS, "FROMsubfolder");
        assertNotNull(copyOfSubFolder);
        validatePermissionsOnRelocatedNode(fromSite, toSite, copyOfSubFolder, expectedPermissions);
    }
    
    /**
     * ALF-1017 - Non sites in the Sites Space container shouldn't
     *  break the listing methods
     */
    public void testALF_1017_nonSitesInSitesSpace() throws Exception
    {
       // Initially listing is fine
       List sites = this.siteService.listSites(null, null);
       assertNotNull("sites list was null.", sites);
       final int preexistingSitesCount = sites.size();
       
       // Create some sites
       SiteInfo site1 = this.siteService.createSite(TEST_SITE_PRESET, "mySiteOne", TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.PUBLIC);
       SiteInfo site2 = this.siteService.createSite(TEST_SITE_PRESET, "mySiteTwo", TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.PRIVATE);
       // Listing is still ok
       sites = this.siteService.listSites(null, null);
       assertNotNull("sites list was null.", sites);
       assertEquals(preexistingSitesCount+2, sites.size());
       
       // Now add a random folder, and a random document to the sites root
       final NodeRef sitesSpace = this.nodeService.getPrimaryParent(site1.getNodeRef()).getParentRef();
       final NodeRef folder = AuthenticationUtil.runAsSystem(new RunAsWork() {
           @Override
           public NodeRef doWork() throws Exception {
              return nodeService.createNode(
                    sitesSpace, ContentModel.ASSOC_CONTAINS,
                    QName.createQName("Folder"), ContentModel.TYPE_FOLDER
              ).getChildRef();
           }
       });
       final NodeRef document = AuthenticationUtil.runAsSystem(new RunAsWork() {
           @Override
           public NodeRef doWork() throws Exception {
              return nodeService.createNode(
                    sitesSpace, ContentModel.ASSOC_CONTAINS,
                    QName.createQName("Document"), ContentModel.TYPE_CONTENT
              ).getChildRef();
           }
       });
       
       // Listing should still be fine, and count won't have increased
       sites = this.siteService.listSites(null, null);
       assertNotNull("sites list was null.", sites);
       assertEquals(preexistingSitesCount+2, sites.size());
       
       // Delete one site, listing still ok
       this.siteService.deleteSite(site2.getShortName());
       sites = this.siteService.listSites(null, null);
       assertNotNull("sites list was null.", sites);
       assertEquals(preexistingSitesCount+1, sites.size());
       
       // Tidy up the random nodes, listing still fine
       this.nodeService.deleteNode(folder);
       this.nodeService.deleteNode(document);
       
       sites = this.siteService.listSites(null, null);
       assertNotNull("sites list was null.", sites);
       assertEquals(preexistingSitesCount+1, sites.size());
    }
    
    private SiteInfo createSite(String siteShortName, String componentId, SiteVisibility visibility)
    {
        // Create a public site
        SiteInfo siteInfo = this.siteService.createSite(TEST_SITE_PRESET, 
                siteShortName, 
                TEST_TITLE, 
                TEST_DESCRIPTION, 
                visibility);
        this.siteService.createContainer(siteShortName, componentId, ContentModel.TYPE_FOLDER, null);
        return siteInfo;
    }
    public void testRenameSite()
    {
        // test that changing the name of a site generates an appropriate exception
        try
        {
            String siteName = GUID.generate();
            SiteInfo siteInfo = createSite(siteName, "doclib", SiteVisibility.PUBLIC);
            NodeRef childRef = siteInfo.getNodeRef();
            Map props = new HashMap(); 
            props.put(ContentModel.PROP_NAME, siteName + "Renamed"); 
    
            nodeService.addProperties(childRef, props);
            fail("Should have caught rename");
        }
        catch(SiteServiceException e)
        {
            assertTrue(e.getMessage().contains("can not be renamed"));
        }
    }
    
    private void validatePermissionsOnRelocatedNode(SiteInfo fromSite,
            SiteInfo toSite, NodeRef relocatedNode, Map expectedPermissions)
    {
        Set permissions = permissionService.getAllSetPermissions(relocatedNode);
        
        // None of the 'from' site permissions should be there.
        for (String sitePermission : SiteModel.STANDARD_PERMISSIONS)
        {
            String siteRoleGroup = siteServiceImpl.getSiteRoleGroup(fromSite.getShortName(), sitePermission, true);
            AccessPermission ap = getPermission(permissions, siteRoleGroup);
            assertNull("Permission " + siteRoleGroup + " was unexpectedly present", ap);
        }
        // All of the 'to' site permissions should be there.
        for (String authority : expectedPermissions.keySet())
        {
            AccessPermission ap = getPermission(permissions, authority);
            assertNotNull("Permission " + authority + " missing", ap);
            
            assertEquals(authority, ap.getAuthority());
            assertEquals("Wrong permission for " + authority, expectedPermissions.get(authority), ap.getPermission());
            assertTrue(ap.isInherited());
        }
    }
    
    private AccessPermission getPermission(Set permissions, String expectedAuthority)
    {
        AccessPermission result = null;
        for (AccessPermission ap : permissions)
        {
            if (expectedAuthority.equals(ap.getAuthority()))
            {
                result = ap;
            }
        }
        return result;
    }
    public void testPermissionsAfterMovingFolderBetweenSites() throws Exception
    {
        alf8036Impl(false);
    }
    // == Test the JavaScript API ==
    
    public void testJSAPI() throws Exception
    {
        // Create a site with a custom property
        SiteInfo siteInfo = this.siteService.createSite(TEST_SITE_PRESET, "mySiteWithCustomProperty", TEST_TITLE, TEST_DESCRIPTION, SiteVisibility.PUBLIC);
        NodeRef siteNodeRef = siteInfo.getNodeRef();
        Map properties = new HashMap(1);
        properties.put(QName.createQName(SiteModel.SITE_CUSTOM_PROPERTY_URL, "additionalInformation"), "information");
        this.nodeService.addAspect(siteNodeRef, QName.createQName(SiteModel.SITE_MODEL_URL, "customSiteProperties"), properties);
        
        // Create a model to pass to the unit test scripts
        Map model = new HashMap();
        model.put("customSiteName", "mySiteWithCustomProperty");
        model.put("preexistingSiteCount", siteService.listSites(null, null).size());
        
        // Execute the unit test script
        ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/site/script/test_siteService.js");
        this.scriptService.executeScript(location, model);
    }
    public void testListMembersInfo()
    {
        String siteShortName = "testMemberInfo";
        // Create a site as user one
        this.siteService.createSite(TEST_SITE_PRESET, siteShortName, TEST_TITLE, TEST_DESCRIPTION,
                    SiteVisibility.PRIVATE);
        // Get the members of the site and check that user one is a manager
        List members = this.siteService.listMembersInfo(siteShortName, null, null, 0, false);
        assertNotNull(members);
        assertEquals(1, members.size());
        SiteMemberInfo user = members.get(0);
        assertNotNull(user);
        assertTrue(user.getMemberName().equals(USER_ONE));
        assertEquals(SiteModel.SITE_MANAGER, user.getMemberRole());
        assertFalse("USER_ONE is NOT member of any group", user.isMemberOfGroup());
        // GROUP_TWO - USER_TWO, USER_THREE
        this.siteService.setMembership(siteShortName, this.groupTwo, SiteModel.SITE_COLLABORATOR);
        this.siteService.setMembership(siteShortName, USER_FOUR, SiteModel.SITE_CONSUMER);
        // Get the members of the site in expanded list
        members = this.siteService.listMembersInfo(siteShortName, null, null, 0, true);
        assertNotNull(members);
        assertEquals(4, members.size());
        // Get USER_TWO who is a member of group two
        user = lookupMemberInfoByUserName(members, USER_TWO);
        assertNotNull(user);
        assertEquals(SiteModel.SITE_COLLABORATOR, user.getMemberRole());
        assertTrue("USER_TWO is member of group two", user.isMemberOfGroup());
        // Get USER_THREE who is a member of group two
        user = lookupMemberInfoByUserName(members, USER_THREE);
        assertNotNull(user);
        assertEquals(SiteModel.SITE_COLLABORATOR, user.getMemberRole());
        assertTrue("USER_THREE is member of group two", user.isMemberOfGroup());
        // Get USER_FOUR
        user = lookupMemberInfoByUserName(members, USER_FOUR);
        assertNotNull(user);
        assertEquals(SiteModel.SITE_CONSUMER, user.getMemberRole());
        assertFalse("USER_FOUR is NOT member of any group", user.isMemberOfGroup());
    }
    private SiteMemberInfo lookupMemberInfoByUserName(List members, String name)
    {
        for (SiteMemberInfo info : members)
        {
            if (name.equals(info.getMemberName())) 
            { 
                return info; 
            }
        }
        return null;
    }
    
    /**
     * From CLOUD-957, insure that GROUP_EVERYONE does not have read access to private sites' containers.
     */
    public void testPrivateSite() throws Exception
    {
        String siteName = GUID.generate();
        SiteInfo siteInfo = createSite(siteName, "doclib", SiteVisibility.PRIVATE);
        NodeRef container = this.siteService.getContainer(siteInfo.getShortName(), "doclib");
        
        assertNull("GROUP_EVERYONE shouldn't have any permissions on a private site's containers", getAllowedPermissionsMap(container).get(PermissionService.ALL_AUTHORITIES));
        
    }
    /**
     * From CLOUD-957, insure that GROUP_EVERYONE does not have read access to moderated sites' containers.
     */
    public void testModeratedSite() throws Exception
    {
        String siteName = GUID.generate();
        SiteInfo siteInfo = createSite(siteName, "doclib", SiteVisibility.MODERATED);
        NodeRef container = this.siteService.getContainer(siteInfo.getShortName(), "doclib");
        
        assertNull("GROUP_EVERYONE shouldn't have any permissions on a moderated site's containers", getAllowedPermissionsMap(container).get(PermissionService.ALL_AUTHORITIES));
        
    }
    
    /**
     *  From MNT-14452, insure that GROUP_EVERYONE have read access to public sites' containers.
     */
    public void testChangeSiteVisibility()
    {
        String siteName = GUID.generate();
        
        //Check Private->public
        SiteInfo siteInfo = createSite(siteName, "doclib", SiteVisibility.PRIVATE);
        NodeRef container = this.siteService.getContainer(siteInfo.getShortName(), "doclib");
        siteInfo.setVisibility(SiteVisibility.PUBLIC);
        siteService.updateSite(siteInfo);
        assertTrue(getAllowedPermissionsMap(container).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
        //Check public->moderated
        siteInfo.setVisibility(SiteVisibility.MODERATED);
        siteService.updateSite(siteInfo);
        assertNull("GROUP_EVERYONE shouldn't have any permissions on a moderated site's containers", getAllowedPermissionsMap(container).get(PermissionService.ALL_AUTHORITIES));
        //Check moderated->public
        siteInfo.setVisibility(SiteVisibility.PUBLIC);
        siteService.updateSite(siteInfo);
        assertTrue(getAllowedPermissionsMap(container).get(PermissionService.ALL_AUTHORITIES).contains("ReadPermissions"));
        
        //Check public->private
        siteInfo.setVisibility(SiteVisibility.PRIVATE);
        siteService.updateSite(siteInfo);
        assertNull("GROUP_EVERYONE shouldn't have any permissions on a moderated site's containers", getAllowedPermissionsMap(container).get(PermissionService.ALL_AUTHORITIES));        
    }
}