diff --git a/source/java/org/alfresco/rest/api/impl/SitesImpl.java b/source/java/org/alfresco/rest/api/impl/SitesImpl.java index abcdf97759..4a1fa3e7e5 100644 --- a/source/java/org/alfresco/rest/api/impl/SitesImpl.java +++ b/source/java/org/alfresco/rest/api/impl/SitesImpl.java @@ -22,6 +22,7 @@ import java.io.Serializable; import java.text.Collator; import java.util.AbstractList; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; @@ -84,243 +85,243 @@ public class SitesImpl implements Sites private static final String FAVOURITE_SITES_PREFIX = "org.alfresco.share.sites.favourites."; private static final int FAVOURITE_SITES_PREFIX_LENGTH = FAVOURITE_SITES_PREFIX.length(); - protected Nodes nodes; - protected People people; - protected NodeService nodeService; - protected DictionaryService dictionaryService; + protected Nodes nodes; + protected People people; + protected NodeService nodeService; + protected DictionaryService dictionaryService; protected SiteService siteService; protected FavouritesService favouritesService; protected PreferenceService preferenceService; - public void setPreferenceService(PreferenceService preferenceService) - { - this.preferenceService = preferenceService; - } + public void setPreferenceService(PreferenceService preferenceService) + { + this.preferenceService = preferenceService; + } - public void setDictionaryService(DictionaryService dictionaryService) - { - this.dictionaryService = dictionaryService; - } + public void setDictionaryService(DictionaryService dictionaryService) + { + this.dictionaryService = dictionaryService; + } - public void setNodes(Nodes nodes) - { - this.nodes = nodes; - } + public void setNodes(Nodes nodes) + { + this.nodes = nodes; + } - public void setFavouritesService(FavouritesService favouritesService) - { - this.favouritesService = favouritesService; - } + public void setFavouritesService(FavouritesService favouritesService) + { + this.favouritesService = favouritesService; + } - public void setPeople(People people) - { - this.people = people; - } + public void setPeople(People people) + { + this.people = people; + } - public void setNodeService(NodeService nodeService) + public void setNodeService(NodeService nodeService) { this.nodeService = nodeService; } - - public void setSiteService(SiteService siteService) + + public void setSiteService(SiteService siteService) { this.siteService = siteService; } - - public SiteInfo validateSite(NodeRef guid) - { - SiteInfo siteInfo = null; - if(guid == null) - { - throw new InvalidArgumentException("guid is null"); - } - nodes.validateNode(guid); - QName type = nodeService.getType(guid); - boolean isSiteNodeRef = dictionaryService.isSubClass(type, SiteModel.TYPE_SITE); - if(isSiteNodeRef) - { - siteInfo = siteService.getSite(guid); - if(siteInfo == null) - { - // not a site - throw new InvalidArgumentException(guid.getId() + " is not a site"); - } - } - else - { - // site does not exist - throw new EntityNotFoundException(guid.getId()); - } + public SiteInfo validateSite(NodeRef guid) + { + SiteInfo siteInfo = null; - return siteInfo; - } + if(guid == null) + { + throw new InvalidArgumentException("guid is null"); + } + nodes.validateNode(guid); + QName type = nodeService.getType(guid); + boolean isSiteNodeRef = dictionaryService.isSubClass(type, SiteModel.TYPE_SITE); + if(isSiteNodeRef) + { + siteInfo = siteService.getSite(guid); + if(siteInfo == null) + { + // not a site + throw new InvalidArgumentException(guid.getId() + " is not a site"); + } + } + else + { + // site does not exist + throw new EntityNotFoundException(guid.getId()); + } - public SiteInfo validateSite(String siteId) - { - if(siteId == null) - { - throw new InvalidArgumentException("siteId is null"); - } - SiteInfo siteInfo = siteService.getSite(siteId); - return siteInfo; - } + return siteInfo; + } + + public SiteInfo validateSite(String siteId) + { + if(siteId == null) + { + throw new InvalidArgumentException("siteId is null"); + } + SiteInfo siteInfo = siteService.getSite(siteId); + return siteInfo; + } public CollectionWithPagingInfo getSiteMembers(String siteId, Parameters parameters) { - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new EntityNotFoundException(siteId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); - - Paging paging = parameters.getPaging(); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new EntityNotFoundException(siteId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); - PagingRequest pagingRequest = Util.getPagingRequest(paging); + Paging paging = parameters.getPaging(); + + PagingRequest pagingRequest = Util.getPagingRequest(paging); final List> sort = new ArrayList>(); sort.add(new Pair(SiteService.SortFields.LastName, Boolean.TRUE)); sort.add(new Pair(SiteService.SortFields.FirstName, Boolean.TRUE)); sort.add(new Pair(SiteService.SortFields.Role, Boolean.TRUE)); sort.add(new Pair(SiteService.SortFields.Username, Boolean.TRUE)); - PagingResults pagedResults = siteService.listMembersPaged(siteId, true, sort, pagingRequest); + PagingResults pagedResults = siteService.listMembersPaged(siteId, true, sort, pagingRequest); List siteMembers = pagedResults.getPage(); - List ret = new ArrayList(siteMembers.size()); - for(SiteMembership siteMembership : siteMembers) - { - SiteMember siteMember = new SiteMember(siteMembership.getPersonId(), siteMembership.getRole()); - ret.add(siteMember); - } + List ret = new ArrayList(siteMembers.size()); + for(SiteMembership siteMembership : siteMembers) + { + SiteMember siteMember = new SiteMember(siteMembership.getPersonId(), siteMembership.getRole()); + ret.add(siteMember); + } - return CollectionWithPagingInfo.asPaged(paging, ret, pagedResults.hasMoreItems(), null); + return CollectionWithPagingInfo.asPaged(paging, ret, pagedResults.hasMoreItems(), null); } public String getSiteRole(String siteId) { - String personId = AuthenticationUtil.getFullyAuthenticatedUser(); - return getSiteRole(siteId, personId); + String personId = AuthenticationUtil.getFullyAuthenticatedUser(); + return getSiteRole(siteId, personId); } public String getSiteRole(String siteId, String personId) { - return siteService.getMembersRole(siteId, personId); + return siteService.getMembersRole(siteId, personId); } public Site getSite(String siteId) { - return getSite(siteId, true); + return getSite(siteId, true); } public Site getSite(String siteId, boolean includeRole) { - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new EntityNotFoundException(siteId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); - String role = null; - if(includeRole) - { - role = getSiteRole(siteId); - } - return new SiteImpl(siteInfo, role); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new EntityNotFoundException(siteId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); + String role = null; + if(includeRole) + { + role = getSiteRole(siteId); + } + return new SiteImpl(siteInfo, role); } - /** - * people//sites/ - * - * @param siteId String - * @param personId String - * @return MemberOfSite - */ - public MemberOfSite getMemberOfSite(String personId, String siteId) - { - MemberOfSite siteMember = null; + /** + * people//sites/ + * + * @param siteId String + * @param personId String + * @return MemberOfSite + */ + public MemberOfSite getMemberOfSite(String personId, String siteId) + { + MemberOfSite siteMember = null; - personId = people.validatePerson(personId); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new RelationshipResourceNotFoundException(personId, siteId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); + personId = people.validatePerson(personId); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new RelationshipResourceNotFoundException(personId, siteId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); - String roleStr = siteService.getMembersRole(siteInfo.getShortName(), personId); - if(roleStr != null) - { - SiteImpl site = new SiteImpl(siteInfo, roleStr); - siteMember = new MemberOfSite(site.getId(), siteInfo.getNodeRef(), roleStr); - } - else - { - throw new RelationshipResourceNotFoundException(personId, siteId); - } + String roleStr = siteService.getMembersRole(siteInfo.getShortName(), personId); + if(roleStr != null) + { + SiteImpl site = new SiteImpl(siteInfo, roleStr); + siteMember = new MemberOfSite(site.getId(), siteInfo.getNodeRef(), roleStr); + } + else + { + throw new RelationshipResourceNotFoundException(personId, siteId); + } - return siteMember; - } - - public SiteMember getSiteMember(String personId, String siteId) - { - SiteMember siteMember = null; + return siteMember; + } - personId = people.validatePerson(personId); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new RelationshipResourceNotFoundException(personId, siteId); - } - siteId = siteInfo.getShortName(); + public SiteMember getSiteMember(String personId, String siteId) + { + SiteMember siteMember = null; - String role = siteService.getMembersRole(siteId, personId); - if(role != null) - { - siteMember = new SiteMember(personId, role); - } - else - { - throw new RelationshipResourceNotFoundException(personId, siteId); - } + personId = people.validatePerson(personId); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new RelationshipResourceNotFoundException(personId, siteId); + } + siteId = siteInfo.getShortName(); - return siteMember; - } + String role = siteService.getMembersRole(siteId, personId); + if(role != null) + { + siteMember = new SiteMember(personId, role); + } + else + { + throw new RelationshipResourceNotFoundException(personId, siteId); + } - public SiteMember addSiteMember(String siteId, SiteMember siteMember) - { - String personId = people.validatePerson(siteMember.getPersonId()); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new EntityNotFoundException(siteId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); - - String role = siteMember.getRole(); - if(role == null) - { - throw new InvalidArgumentException("Must provide a role"); - } - - if(siteService.isMember(siteId, personId)) - { - throw new ConstraintViolatedException(personId + " is already a member of site " + siteId); - } + return siteMember; + } - if(!siteService.canAddMember(siteId, personId, role)) - { - throw new PermissionDeniedException(); - } + public SiteMember addSiteMember(String siteId, SiteMember siteMember) + { + String personId = people.validatePerson(siteMember.getPersonId()); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new EntityNotFoundException(siteId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); + + String role = siteMember.getRole(); + if(role == null) + { + throw new InvalidArgumentException("Must provide a role"); + } + + if(siteService.isMember(siteId, personId)) + { + throw new ConstraintViolatedException(personId + " is already a member of site " + siteId); + } + + if(!siteService.canAddMember(siteId, personId, role)) + { + throw new PermissionDeniedException(); + } try { @@ -330,75 +331,75 @@ public class SitesImpl implements Sites { throw new InvalidArgumentException("Unknown role '" + role + "'"); } - return siteMember; - } - - public void removeSiteMember(String personId, String siteId) - { - personId = people.validatePerson(personId); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new RelationshipResourceNotFoundException(personId, siteId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); + return siteMember; + } - boolean isMember = siteService.isMember(siteId, personId); - if(!isMember) - { - throw new InvalidArgumentException(); - } - String role = siteService.getMembersRole(siteId, personId); - if(role != null) - { - if(role.equals(SiteModel.SITE_MANAGER)) - { - int numAuthorities = siteService.countAuthoritiesWithRole(siteId, SiteModel.SITE_MANAGER); - if(numAuthorities <= 1) - { - throw new InvalidArgumentException("Can't remove last manager of site " + siteId); - } - siteService.removeMembership(siteId, personId); - } - else - { - siteService.removeMembership(siteId, personId); - } - } - else - { - throw new AlfrescoRuntimeException("Unable to determine role of site member"); - } - } + public void removeSiteMember(String personId, String siteId) + { + personId = people.validatePerson(personId); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new RelationshipResourceNotFoundException(personId, siteId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); - public SiteMember updateSiteMember(String siteId, SiteMember siteMember) - { - String siteMemberId = siteMember.getPersonId(); - if(siteMemberId == null) - { - throw new InvalidArgumentException("Member id is null"); - } - siteMemberId = people.validatePerson(siteMemberId); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new EntityNotFoundException(siteId); - } - siteId = siteInfo.getShortName(); - String siteRole = siteMember.getRole(); - if(siteRole == null) - { - throw new InvalidArgumentException("Must provide a role"); - } + boolean isMember = siteService.isMember(siteId, personId); + if(!isMember) + { + throw new InvalidArgumentException(); + } + String role = siteService.getMembersRole(siteId, personId); + if(role != null) + { + if(role.equals(SiteModel.SITE_MANAGER)) + { + int numAuthorities = siteService.countAuthoritiesWithRole(siteId, SiteModel.SITE_MANAGER); + if(numAuthorities <= 1) + { + throw new InvalidArgumentException("Can't remove last manager of site " + siteId); + } + siteService.removeMembership(siteId, personId); + } + else + { + siteService.removeMembership(siteId, personId); + } + } + else + { + throw new AlfrescoRuntimeException("Unable to determine role of site member"); + } + } - /* MNT-10551 : fix */ - if (!siteService.isMember(siteId, siteMember.getPersonId())) - { - throw new InvalidArgumentException("User is not a member of the site"); - } + public SiteMember updateSiteMember(String siteId, SiteMember siteMember) + { + String siteMemberId = siteMember.getPersonId(); + if(siteMemberId == null) + { + throw new InvalidArgumentException("Member id is null"); + } + siteMemberId = people.validatePerson(siteMemberId); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new EntityNotFoundException(siteId); + } + siteId = siteInfo.getShortName(); + String siteRole = siteMember.getRole(); + if(siteRole == null) + { + throw new InvalidArgumentException("Must provide a role"); + } + + /* MNT-10551 : fix */ + if (!siteService.isMember(siteId, siteMember.getPersonId())) + { + throw new InvalidArgumentException("User is not a member of the site"); + } try { @@ -408,17 +409,17 @@ public class SitesImpl implements Sites { throw new InvalidArgumentException("Unknown role '" + siteRole + "'"); } - return siteMember; - } - - public CollectionWithPagingInfo getSites(String personId, Parameters parameters) - { - Paging paging = parameters.getPaging(); + return siteMember; + } - personId = people.validatePerson(personId); + public CollectionWithPagingInfo getSites(String personId, Parameters parameters) + { + Paging paging = parameters.getPaging(); + + personId = people.validatePerson(personId); + + PagingRequest pagingRequest = Util.getPagingRequest(paging); - PagingRequest pagingRequest = Util.getPagingRequest(paging); - // get the sorting options List> sortPairs = new ArrayList>(parameters.getSorting().size()); for (SortColumn sortColumn : parameters.getSorting()) @@ -443,23 +444,22 @@ public class SitesImpl implements Sites if(sortPairs.size() == 0) { sortPairs.add(new Pair( - SiteService.SortFields.SiteTitle, + SiteService.SortFields.SiteTitle, SortOrder.ASCENDING )); } - - final Set sortedSiteMembers = new TreeSet( - new SiteMembershipComparator( - sortPairs, - SiteMembershipComparator.Type.SITES)); - + // get the unsorted list of site memberships List siteMembers = siteService.listSiteMemberships (personId, 0); - + // sort the list of site memberships int totalSize = siteMembers.size(); - sortedSiteMembers.addAll(siteMembers); + final List sortedSiteMembers = new ArrayList<>(siteMembers); + Collections.sort(sortedSiteMembers, new SiteMembershipComparator( + sortPairs, + SiteMembershipComparator.Type.SITES)); + PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize); - List ret = new ArrayList(totalSize); + List ret = new ArrayList<>(totalSize); Iterator it = sortedSiteMembers.iterator(); for(int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) @@ -482,315 +482,315 @@ public class SitesImpl implements Sites } return CollectionWithPagingInfo.asPaged(paging, ret, pageDetails.hasMoreItems(), null); - } - - public SiteContainer getSiteContainer(String siteId, String containerId) - { - // check site and container node validity - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new RelationshipResourceNotFoundException(siteId, containerId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); - - NodeRef containerNodeRef = siteService.getContainer(siteId, containerId); - if(containerNodeRef == null) - { - throw new RelationshipResourceNotFoundException(siteId, containerId); - } + } - // check that the containerId is actually a container for the specified site - SiteInfo testSiteInfo = siteService.getSite(containerNodeRef); - if(testSiteInfo == null) - { - throw new RelationshipResourceNotFoundException(siteId, containerId); - } - else - { - if(!testSiteInfo.getShortName().equals(siteId)) - { - throw new RelationshipResourceNotFoundException(siteId, containerId); - } - } + public SiteContainer getSiteContainer(String siteId, String containerId) + { + // check site and container node validity + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new RelationshipResourceNotFoundException(siteId, containerId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); - String folderId = (String)nodeService.getProperty(containerNodeRef, SiteModel.PROP_COMPONENT_ID); + NodeRef containerNodeRef = siteService.getContainer(siteId, containerId); + if(containerNodeRef == null) + { + throw new RelationshipResourceNotFoundException(siteId, containerId); + } - SiteContainer siteContainer = new SiteContainer(folderId, containerNodeRef); - return siteContainer; - } - - public PagingResults getSiteContainers(String siteId, Paging paging) - { - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new EntityNotFoundException(siteId); - } + // check that the containerId is actually a container for the specified site + SiteInfo testSiteInfo = siteService.getSite(containerNodeRef); + if(testSiteInfo == null) + { + throw new RelationshipResourceNotFoundException(siteId, containerId); + } + else + { + if(!testSiteInfo.getShortName().equals(siteId)) + { + throw new RelationshipResourceNotFoundException(siteId, containerId); + } + } - final PagingResults pagingResults = siteService.listContainers(siteInfo.getShortName(), Util.getPagingRequest(paging)); - List containerFileInfos = pagingResults.getPage(); - final List siteContainers = new ArrayList(containerFileInfos.size()); - for(FileInfo containerFileInfo : containerFileInfos) - { - NodeRef nodeRef = containerFileInfo.getNodeRef(); - String containerId = (String)nodeService.getProperty(nodeRef, SiteModel.PROP_COMPONENT_ID); - SiteContainer siteContainer = new SiteContainer(containerId, nodeRef); - siteContainers.add(siteContainer); - } + String folderId = (String)nodeService.getProperty(containerNodeRef, SiteModel.PROP_COMPONENT_ID); - return new PagingResults() - { - @Override - public List getPage() - { - return siteContainers; - } + SiteContainer siteContainer = new SiteContainer(folderId, containerNodeRef); + return siteContainer; + } - @Override - public boolean hasMoreItems() - { - return pagingResults.hasMoreItems(); - } + public PagingResults getSiteContainers(String siteId, Paging paging) + { + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new EntityNotFoundException(siteId); + } - @Override - public Pair getTotalResultCount() - { - return pagingResults.getTotalResultCount(); - } + final PagingResults pagingResults = siteService.listContainers(siteInfo.getShortName(), Util.getPagingRequest(paging)); + List containerFileInfos = pagingResults.getPage(); + final List siteContainers = new ArrayList(containerFileInfos.size()); + for(FileInfo containerFileInfo : containerFileInfos) + { + NodeRef nodeRef = containerFileInfo.getNodeRef(); + String containerId = (String)nodeService.getProperty(nodeRef, SiteModel.PROP_COMPONENT_ID); + SiteContainer siteContainer = new SiteContainer(containerId, nodeRef); + siteContainers.add(siteContainer); + } - @Override - public String getQueryExecutionId() - { - return null; - } - }; - } + return new PagingResults() + { + @Override + public List getPage() + { + return siteContainers; + } - public CollectionWithPagingInfo getSites(final Parameters parameters) - { - final BeanPropertiesFilter filter = parameters.getFilter(); + @Override + public boolean hasMoreItems() + { + return pagingResults.hasMoreItems(); + } - Paging paging = parameters.getPaging(); - PagingRequest pagingRequest = Util.getPagingRequest(paging); + @Override + public Pair getTotalResultCount() + { + return pagingResults.getTotalResultCount(); + } + + @Override + public String getQueryExecutionId() + { + return null; + } + }; + } + + public CollectionWithPagingInfo getSites(final Parameters parameters) + { + final BeanPropertiesFilter filter = parameters.getFilter(); + + Paging paging = parameters.getPaging(); + PagingRequest pagingRequest = Util.getPagingRequest(paging); // pagingRequest.setRequestTotalCountMax(requestTotalCountMax) - List> sortProps = new ArrayList>(); - sortProps.add(new Pair(ContentModel.PROP_NAME, Boolean.TRUE)); + List> sortProps = new ArrayList>(); + sortProps.add(new Pair(ContentModel.PROP_NAME, Boolean.TRUE)); final PagingResults pagingResult = siteService.listSites(null, sortProps, pagingRequest); final List sites = pagingResult.getPage(); int totalItems = pagingResult.getTotalResultCount().getFirst(); final String personId = AuthenticationUtil.getFullyAuthenticatedUser(); - List page = new AbstractList() - { - @Override - public SiteImpl get(int index) - { - SiteInfo siteInfo = sites.get(index); + List page = new AbstractList() + { + @Override + public SiteImpl get(int index) + { + SiteInfo siteInfo = sites.get(index); - String role = null; - if(filter.isAllowed(Site.ROLE)) - { - role = siteService.getMembersRole(siteInfo.getShortName(), personId); - } - return new SiteImpl(siteInfo, role); - } + String role = null; + if(filter.isAllowed(Site.ROLE)) + { + role = siteService.getMembersRole(siteInfo.getShortName(), personId); + } + return new SiteImpl(siteInfo, role); + } - @Override - public int size() - { - return sites.size(); - } - }; + @Override + public int size() + { + return sites.size(); + } + }; return CollectionWithPagingInfo.asPaged(paging, page, pagingResult.hasMoreItems(), totalItems); - } - + } + public FavouriteSite getFavouriteSite(String personId, String siteId) { - personId = people.validatePerson(personId); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new RelationshipResourceNotFoundException(personId, siteId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); - NodeRef nodeRef = siteInfo.getNodeRef(); + personId = people.validatePerson(personId); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new RelationshipResourceNotFoundException(personId, siteId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); + NodeRef nodeRef = siteInfo.getNodeRef(); - if(favouritesService.isFavourite(personId, nodeRef)) - { - String role = getSiteRole(siteId, personId); - return new FavouriteSite(siteInfo, role); - } - else - { - throw new RelationshipResourceNotFoundException(personId, siteId); - } + if(favouritesService.isFavourite(personId, nodeRef)) + { + String role = getSiteRole(siteId, personId); + return new FavouriteSite(siteInfo, role); + } + else + { + throw new RelationshipResourceNotFoundException(personId, siteId); + } } public void addFavouriteSite(String personId, FavouriteSite favouriteSite) { - personId = people.validatePerson(personId); - String siteId = favouriteSite.getId(); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new EntityNotFoundException(siteId); - } - // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) - siteId = siteInfo.getShortName(); - - StringBuilder prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX); - prefKey.append(siteId); - String value = (String)preferenceService.getPreference(personId, prefKey.toString()); - boolean isFavouriteSite = (value == null ? false : value.equalsIgnoreCase("true")); + personId = people.validatePerson(personId); + String siteId = favouriteSite.getId(); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new EntityNotFoundException(siteId); + } + // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url) + siteId = siteInfo.getShortName(); - if(isFavouriteSite) - { - throw new ConstraintViolatedException("Site " + siteId + " is already a favourite site"); - } + StringBuilder prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX); + prefKey.append(siteId); + String value = (String)preferenceService.getPreference(personId, prefKey.toString()); + boolean isFavouriteSite = (value == null ? false : value.equalsIgnoreCase("true")); - prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX); - prefKey.append(siteId); + if(isFavouriteSite) + { + throw new ConstraintViolatedException("Site " + siteId + " is already a favourite site"); + } - Map preferences = new HashMap(1); - preferences.put(prefKey.toString(), Boolean.TRUE); - preferenceService.setPreferences(personId, preferences); + prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX); + prefKey.append(siteId); + + Map preferences = new HashMap(1); + preferences.put(prefKey.toString(), Boolean.TRUE); + preferenceService.setPreferences(personId, preferences); } public void removeFavouriteSite(String personId, String siteId) { - personId = people.validatePerson(personId); - SiteInfo siteInfo = validateSite(siteId); - if(siteInfo == null) - { - // site does not exist - throw new RelationshipResourceNotFoundException(personId, siteId); - } - siteId = siteInfo.getShortName(); + personId = people.validatePerson(personId); + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new RelationshipResourceNotFoundException(personId, siteId); + } + siteId = siteInfo.getShortName(); - StringBuilder prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX); - prefKey.append(siteId); - String value = (String)preferenceService.getPreference(personId, prefKey.toString()); - boolean isFavouriteSite = (value == null ? false : value.equalsIgnoreCase("true")); - - if(!isFavouriteSite) - { - throw new NotFoundException("Site " + siteId + " is not a favourite site"); - } + StringBuilder prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX); + prefKey.append(siteId); + String value = (String)preferenceService.getPreference(personId, prefKey.toString()); + boolean isFavouriteSite = (value == null ? false : value.equalsIgnoreCase("true")); - preferenceService.clearPreferences(personId, prefKey.toString()); + if(!isFavouriteSite) + { + throw new NotFoundException("Site " + siteId + " is not a favourite site"); + } + + preferenceService.clearPreferences(personId, prefKey.toString()); } private PagingResults getFavouriteSites(String userName, PagingRequest pagingRequest) { - final Collator collator = Collator.getInstance(); + final Collator collator = Collator.getInstance(); final Set sortedFavouriteSites = new TreeSet(new Comparator() { - @Override - public int compare(SiteInfo o1, SiteInfo o2) - { - return collator.compare(o1.getTitle(), o2.getTitle()); - } - }); + @Override + public int compare(SiteInfo o1, SiteInfo o2) + { + return collator.compare(o1.getTitle(), o2.getTitle()); + } + }); Map prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX); for(Entry entry : prefs.entrySet()) { - boolean isFavourite = false; - Serializable s = entry.getValue(); - if(s instanceof Boolean) - { - isFavourite = (Boolean)s; - } - if(isFavourite) - { - String siteShortName = entry.getKey().substring(FAVOURITE_SITES_PREFIX_LENGTH).replace(".favourited", ""); - SiteInfo siteInfo = siteService.getSite(siteShortName); - if(siteInfo != null) - { - sortedFavouriteSites.add(siteInfo); - } - } + boolean isFavourite = false; + Serializable s = entry.getValue(); + if(s instanceof Boolean) + { + isFavourite = (Boolean)s; + } + if(isFavourite) + { + String siteShortName = entry.getKey().substring(FAVOURITE_SITES_PREFIX_LENGTH).replace(".favourited", ""); + SiteInfo siteInfo = siteService.getSite(siteShortName); + if(siteInfo != null) + { + sortedFavouriteSites.add(siteInfo); + } + } } int totalSize = sortedFavouriteSites.size(); final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize); - final List page = new ArrayList(pageDetails.getPageSize()); - Iterator it = sortedFavouriteSites.iterator(); + final List page = new ArrayList(pageDetails.getPageSize()); + Iterator it = sortedFavouriteSites.iterator(); for(int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) { - SiteInfo favouriteSite = it.next(); + SiteInfo favouriteSite = it.next(); - if(counter < pageDetails.getSkipCount()) - { - continue; - } - - if(counter > pageDetails.getEnd() - 1) - { - break; - } + if(counter < pageDetails.getSkipCount()) + { + continue; + } - page.add(favouriteSite); + if(counter > pageDetails.getEnd() - 1) + { + break; + } + + page.add(favouriteSite); } return new PagingResults() { - @Override - public List getPage() - { - return page; - } + @Override + public List getPage() + { + return page; + } - @Override - public boolean hasMoreItems() - { - return pageDetails.hasMoreItems(); - } + @Override + public boolean hasMoreItems() + { + return pageDetails.hasMoreItems(); + } - @Override - public Pair getTotalResultCount() - { - Integer total = Integer.valueOf(sortedFavouriteSites.size()); - return new Pair(total, total); - } + @Override + public Pair getTotalResultCount() + { + Integer total = Integer.valueOf(sortedFavouriteSites.size()); + return new Pair(total, total); + } - @Override - public String getQueryExecutionId() - { - return null; - } + @Override + public String getQueryExecutionId() + { + return null; + } }; } public CollectionWithPagingInfo getFavouriteSites(String personId, Parameters parameters) { - personId = people.validatePerson(personId); + personId = people.validatePerson(personId); - Paging paging = parameters.getPaging(); - BeanPropertiesFilter filter = parameters.getFilter(); + Paging paging = parameters.getPaging(); + BeanPropertiesFilter filter = parameters.getFilter(); - PagingResults favouriteSites = getFavouriteSites(personId, Util.getPagingRequest(paging)); - List favourites = new ArrayList(favouriteSites.getPage().size()); - for(SiteInfo favouriteSite : favouriteSites.getPage()) - { - String role = null; - if(filter.isAllowed(Site.ROLE)) - { - role = getSiteRole(favouriteSite.getShortName(), personId); - } - FavouriteSite favourite = new FavouriteSite(favouriteSite, role); - favourites.add(favourite); - } + PagingResults favouriteSites = getFavouriteSites(personId, Util.getPagingRequest(paging)); + List favourites = new ArrayList(favouriteSites.getPage().size()); + for(SiteInfo favouriteSite : favouriteSites.getPage()) + { + String role = null; + if(filter.isAllowed(Site.ROLE)) + { + role = getSiteRole(favouriteSite.getShortName(), personId); + } + FavouriteSite favourite = new FavouriteSite(favouriteSite, role); + favourites.add(favourite); + } - return CollectionWithPagingInfo.asPaged(paging, favourites, favouriteSites.hasMoreItems(), favouriteSites.getTotalResultCount().getFirst()); + return CollectionWithPagingInfo.asPaged(paging, favourites, favouriteSites.hasMoreItems(), favouriteSites.getTotalResultCount().getFirst()); } } diff --git a/source/test-java/org/alfresco/rest/api/tests/TestPersonSites.java b/source/test-java/org/alfresco/rest/api/tests/TestPersonSites.java index 5a6156f83c..7779b25d15 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestPersonSites.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestPersonSites.java @@ -1,10 +1,31 @@ +/* + * Copyright (C) 2005-2015 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 . + */ + package org.alfresco.rest.api.tests; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -34,650 +55,700 @@ import org.junit.Test; public class TestPersonSites extends EnterpriseTestApi { - private TestNetwork network1; - private TestNetwork network2; - - private TestPerson person11; - private TestPerson person12; - private TestPerson person21; - - private List sites = new ArrayList(10); - - /* Sites and users used to test the site sorting */ - private TestPerson person31; - private TestPerson person32; - private TestSite site1; - private TestSite site2; - private TestSite site3; - private static String site1_name = "a_" + GUID.generate();; - private static String site1_title = "c_" + GUID.generate(); - private static SiteRole site1_role = SiteRole.SiteContributor; - private static String site2_name = "b_" + GUID.generate();; - private static String site2_title = "a_" + GUID.generate(); - private static SiteRole site2_role = SiteRole.SiteManager; - private static String site3_name = "c_" + GUID.generate();; - private static String site3_title = "b_" + GUID.generate(); - private static SiteRole site3_role = SiteRole.SiteConsumer; - + private TestNetwork network1; + private TestNetwork network2; - public void initializeSites() throws Exception - { - - - /* - * Create data for testing the site sorting. We create the sites as - * person31 and assign roles to person32. The list requests will be - * performed as person32. - */ - TenantUtil.runAsSystemTenant(new TenantRunAsWork() - { - @Override - public Void doWork() throws Exception - { - person31 = network1.createUser(); - person32 = network1.createUser(); - return null; - } - }, network1.getId()); - - this.site1 = TenantUtil.runAsUserTenant(new TenantRunAsWork() - { - @Override - public TestSite doWork() throws Exception - { - SiteInformation siteInfo = new SiteInformation(site1_name, site1_title, site1_title, SiteVisibility.PRIVATE); - TestSite site = network1.createSite(siteInfo); - site.inviteToSite(person32.getId(), site1_role); - return site; - } - }, person31.getId(), network1.getId()); - - this.site2 = TenantUtil.runAsUserTenant(new TenantRunAsWork() - { - @Override - public TestSite doWork() throws Exception - { - SiteInformation siteInfo = new SiteInformation(site2_name, site2_title, site2_title, SiteVisibility.PRIVATE); - TestSite site = network1.createSite(siteInfo); - site.inviteToSite(person32.getId(), site2_role); - return site; - } - }, person31.getId(), network1.getId()); - - this.site3 = TenantUtil.runAsUserTenant(new TenantRunAsWork() - { - @Override - public TestSite doWork() throws Exception - { - SiteInformation siteInfo = new SiteInformation(site3_name, site3_title, site3_title, SiteVisibility.PRIVATE); - TestSite site = network1.createSite(siteInfo); - site.inviteToSite(person32.getId(), site3_role); - return site; - } - }, person31.getId(), network1.getId()); - } - - @Before - public void setup() throws Exception - { - Iterator networksIt = getTestFixture().getNetworksIt(); + private TestPerson person11; + private TestPerson person12; + private TestPerson person21; - assertTrue(networksIt.hasNext()); - this.network1 = networksIt.next(); - - assertTrue(networksIt.hasNext()); - this.network2 = networksIt.next(); + private List sites = new ArrayList<>(10); - // create a user + /* Sites and users used to test the site sorting */ + private TestPerson person31; + private TestPerson person32; + private TestSite site1; + private TestSite site2; + private TestSite site3; + private String site1_name = "a_" + GUID.generate(); + private String site1_title = "c_" + GUID.generate(); + private SiteRole site1_role = SiteRole.SiteContributor; + private String site2_name = "b_" + GUID.generate(); + private String site2_title = "a_" + GUID.generate(); + private SiteRole site2_role = SiteRole.SiteManager; + private String site3_name = "c_" + GUID.generate(); + private String site3_title = "b_" + GUID.generate(); + private SiteRole site3_role = SiteRole.SiteConsumer; - final List people = new ArrayList(1); - // Create some users - TenantUtil.runAsSystemTenant(new TenantRunAsWork() - { - @Override - public Void doWork() throws Exception - { - TestPerson person = network1.createUser(); - people.add(person); - person = network1.createUser(); - people.add(person); - - return null; - } - }, network1.getId()); - - TenantUtil.runAsSystemTenant(new TenantRunAsWork() - { - @Override - public Void doWork() throws Exception - { - TestPerson person = network2.createUser(); - people.add(person); - - return null; - } - }, network2.getId()); - - this.person11 = people.get(0); - this.person12 = people.get(1); - this.person21 = people.get(2); - - // ...and some sites - TenantUtil.runAsUserTenant(new TenantRunAsWork() - { - @Override - public Void doWork() throws Exception - { - TestSite site = network1.createSite(SiteVisibility.PUBLIC); - site.inviteToSite(person11.getId(), SiteRole.SiteContributor); - sites.add(site); - - site = network1.createSite(SiteVisibility.MODERATED); - site.inviteToSite(person11.getId(), SiteRole.SiteContributor); - sites.add(site); - - site = network1.createSite(SiteVisibility.PRIVATE); - site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); - sites.add(site); - - site = network1.createSite(SiteVisibility.PUBLIC); - site.inviteToSite(person11.getId(), SiteRole.SiteManager); - sites.add(site); - - site = network1.createSite(SiteVisibility.PRIVATE); - site.inviteToSite(person11.getId(), SiteRole.SiteCollaborator); - sites.add(site); - - //Special site for person removal - site = network1.createSite(SiteVisibility.PRIVATE); - site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); - sites.add(site); - return null; - } - }, person12.getId(), network1.getId()); - } - - @Test - public void testPersonSites() throws Exception - { - Set personSites = new TreeSet(); - - //Get last site for use with personRemoveSite - TestSite personRemoveSite = sites.get(sites.size()-1); - sites.remove(sites.size()-1); - - personSites.addAll(network1.getSiteMemberships(person11.getId())); - - // Create some sites - personSites.addAll(TenantUtil.runAsUserTenant(new TenantRunAsWork>() - { - @Override - public List doWork() throws Exception - { - List expectedSites = new ArrayList(); - - TestSite site = network1.createSite(SiteVisibility.PRIVATE); - expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager)); - - site = network1.createSite(SiteVisibility.PUBLIC); - expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager)); - - site = network1.createSite(SiteVisibility.MODERATED); - expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager)); - - return expectedSites; - } - }, person11.getId(), network1.getId())); - - personSites.addAll(TenantUtil.runAsUserTenant(new TenantRunAsWork>() - { - @Override - public List doWork() throws Exception - { - List expectedSites = new ArrayList(); - - TestSite site = network1.createSite(SiteVisibility.PRIVATE); - site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); - expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer)); - - site = network1.createSite(SiteVisibility.PUBLIC); - site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); - expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer)); - - site = network1.createSite(SiteVisibility.MODERATED); - site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); - expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer)); - - return expectedSites; - } - }, person12.getId(), network1.getId())); - - final List expectedSites = new ArrayList(personSites); - Sites sitesProxy = publicApiClient.sites(); - - // Test Case cloud-1487 - - // unknown user - try - { - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - - sitesProxy.getPersonSites(GUID.generate(), null); - fail(""); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); - } - - // Test Case cloud-2200 - // Test Case cloud-2213 - // user should be able to list their sites - { - int skipCount = 0; - int maxItems = 2; - Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - ListResponse resp = sitesProxy.getPersonSites(person11.getId(), createParams(paging, null)); - checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp); - } - - { - int skipCount = 2; - int maxItems = 8; - Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - ListResponse resp = sitesProxy.getPersonSites(person11.getId(), createParams(paging, null)); - checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp); - } - - // "-me-" user - { - int skipCount = 0; - int maxItems = 2; - Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - ListResponse resp = sitesProxy.getPersonSites(org.alfresco.rest.api.People.DEFAULT_USER, createParams(paging, null)); - checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp); - } - - // a user in another tenant should not be able to list a user's sites - try - { - int skipCount = 0; - int maxItems = 2; - Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId())); - sitesProxy.getPersonSites(person11.getId(), createParams(paging, null)); - fail(""); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode()); - } - - // Test case cloud-1488 - { - MemberOfSite memberOfSite = expectedSites.get(0); - - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - MemberOfSite ret = sitesProxy.getPersonSite(person11.getId(), memberOfSite.getSiteId()); - memberOfSite.expected(ret); - } - - try - { - MemberOfSite memberOfSite = expectedSites.get(0); - - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.getPersonSite(GUID.generate(), memberOfSite.getSiteId()); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); - } - - try - { - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.getPersonSite(person11.getId(), GUID.generate()); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); - } - - // Test Case cloud-1487 - // unknown person id - try - { - MemberOfSite memberOfSite = expectedSites.get(0); - - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.getPersonSite(GUID.generate(), memberOfSite.getSiteId()); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); - } - - try - { - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.getPersonSite(person11.getId(), GUID.generate()); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); - } - - { - //Tests removing a person from the site - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.remove("people", person11.getId(), "sites", personRemoveSite.getSiteId(), "Unable to DELETE a person site"); - - try - { - sitesProxy.getPersonSite(person11.getId(), personRemoveSite.getSiteId()); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); - } - } - - // TODO - // person from external network listing user sites - - // Test Case cloud-1966 - // Not allowed methods - try - { - MemberOfSite memberOfSite = expectedSites.get(0); - - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.create("people", person11.getId(), "sites", memberOfSite.getSiteId(), null, "Unable to POST to a person site"); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); - } - - try - { - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.create("people", person11.getId(), "sites", null, null, "Unable to POST to person sites"); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); - } - - try - { - MemberOfSite memberOfSite = expectedSites.get(0); - - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.update("people", person11.getId(), "sites", memberOfSite.getSiteId(), null, "Unable to PUT a person site"); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); - } - - try - { - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.update("people", person11.getId(), "sites", null, null, "Unable to PUT person sites"); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); - } - - try - { - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); - sitesProxy.remove("people", person11.getId(), "sites", null, "Unable to DELETE person sites"); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); - } - } - - /** - * Retrieves the site memberships associated to a user - * - * @param skipCount - * @param maxItems - * @param sortColumn - * @param asc - * @return - * @throws Exception + public void initializeSites() throws Exception + { + /* + * Create data for testing the site sorting. We create the sites as + * person31 and assign roles to person32. The list requests will be + * performed as person32. */ - private ListResponse getSiteMembershipsForPerson32(final Paging paging, String sortColumn, boolean asc) throws Exception + TenantUtil.runAsSystemTenant(new TenantRunAsWork() { - final Sites sitesProxy = publicApiClient.sites(); - publicApiClient.setRequestContext(new RequestContext(network1.getId(), person32.getId())); - - // sort params - final Map params = new HashMap(); - if(sortColumn != null) + @Override + public Void doWork() throws Exception { - params.put("orderBy", sortColumn + " " + (asc?"ASC":"DESC")); + person31 = network1.createUser(); + person32 = network1.createUser(); + return null; } - - // get memberships - ListResponse resp = TenantUtil.runAsUserTenant(new TenantRunAsWork>() + }, network1.getId()); + + this.site1 = TenantUtil.runAsUserTenant(new TenantRunAsWork() + { + @Override + public TestSite doWork() throws Exception { - @Override - public ListResponse doWork() throws Exception - { - ListResponse resp = sitesProxy.getPersonSites(person32.getId(), createParams(paging, params)); + SiteInformation siteInfo = new SiteInformation(site1_name, site1_title, site1_title, SiteVisibility.PRIVATE); + TestSite site = network1.createSite(siteInfo); + site.inviteToSite(person32.getId(), site1_role); + return site; + } + }, person31.getId(), network1.getId()); - return resp; - } - }, person32.getId(), network1.getId()); - - return resp; - } - - /** - * Tests the capability to sort and paginate the site memberships associated - * to a user order = Title ASC skip = 1, count = 2 - * - * @throws Exception - */ - public void testSortingAndPagingByTitleAsc() throws Exception + this.site2 = TenantUtil.runAsUserTenant(new TenantRunAsWork() { - // paging - int skipCount = 1; - int maxItems = 2; - int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); - - // get memberships - ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteTitle", true); - - // check results - List expectedList = new LinkedList(); - expectedList.add(new MemberOfSite(site3, site3_role)); - expectedList.add(new MemberOfSite(site1, site1_role)); - - checkList(expectedList, paging.getExpectedPaging(), resp); - } - - /** - * Tests the capability to sort and paginate the site memberships associated - * to a user order = Title DESC skip = 1, count = 2 - * - * @throws Exception - */ - public void testSortingAndPagingByTitleDesc() throws Exception - { - // paging - int skipCount = 1; - int maxItems = 2; - int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); - - // get memberships - ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteTitle", false); - - // check results - List expectedList = new LinkedList(); - expectedList.add(new MemberOfSite(site3, site3_role)); - expectedList.add(new MemberOfSite(site2, site2_role)); - - checkList(expectedList, paging.getExpectedPaging(), resp); - } - - /** - * Tests the capability to sort and paginate the site memberships associated - * to a user order = Role ASC skip = 1, count = 2 - * - * @throws Exception - */ - public void testSortingAndPagingByRoleAsc() throws Exception - { - // paging - int skipCount = 1; - int maxItems = 2; - int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); - - // get memberships - ListResponse resp = getSiteMembershipsForPerson32(paging, "Role", true); - - // check results - List expectedList = new LinkedList(); - expectedList.add(new MemberOfSite(site1, site1_role)); - expectedList.add(new MemberOfSite(site2, site2_role)); - - checkList(expectedList, paging.getExpectedPaging(), resp); - } - - /** - * Tests the capability to sort and paginate the site memberships associated - * to a user order = Role DESC skip = 1, count = 2 - * - * @throws Exception - */ - public void testSortingAndPagingByRoleDesc() throws Exception - { - // paging - int skipCount = 1; - int maxItems = 2; - int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); - - // get memberships - ListResponse resp = getSiteMembershipsForPerson32(paging, "Role", false); - - // check results - List expectedList = new LinkedList(); - expectedList.add(new MemberOfSite(site1, site1_role)); - expectedList.add(new MemberOfSite(site3, site3_role)); - - checkList(expectedList, paging.getExpectedPaging(), resp); - } - - /** - * Tests the capability to sort and paginate the site memberships associated - * to a user order = Site Name ASC skip = 1, count = 2 - * - * @throws Exception - */ - public void testSortingAndPagingBySiteNameAsc() throws Exception - { - // paging - int skipCount = 1; - int maxItems = 2; - int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); - - // get memberships - ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteShortName", true); - - // check results - List expectedList = new LinkedList(); - expectedList.add(new MemberOfSite(site2, site2_role)); - expectedList.add(new MemberOfSite(site3, site3_role)); - - checkList(expectedList, paging.getExpectedPaging(), resp); - } - - /** - * Tests the capability to sort and paginate the site memberships associated - * to a user order = Site Name DESC skip = 1, count = 2 - * - * @throws Exception - */ - public void testSortingAndPagingBySiteNameDesc() throws Exception - { - // paging - int skipCount = 1; - int maxItems = 2; - int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); - - // get memberships - ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteShortName", false); - - // check results - List expectedList = new LinkedList(); - expectedList.add(new MemberOfSite(site2, site2_role)); - expectedList.add(new MemberOfSite(site1, site1_role)); - - checkList(expectedList, paging.getExpectedPaging(), resp); - } - - /** - * Tests the capability to sort and paginate the site memberships associated - * default sorting, all results - * - * @throws Exception - */ - public void testSortingAndPagingDefault() throws Exception - { - // paging - int totalResults = 3; - Paging paging = getPaging(null, null, totalResults, null); - - // get memberships - ListResponse resp = getSiteMembershipsForPerson32(null, null, false); - - // check results - List expectedList = new LinkedList(); - expectedList.add(new MemberOfSite(site2, site2_role)); - expectedList.add(new MemberOfSite(site3, site3_role)); - expectedList.add(new MemberOfSite(site1, site1_role)); - - checkList(expectedList, paging.getExpectedPaging(), resp); - } - - @Test - public void testSortingAndPaging() throws Exception - { - initializeSites(); + @Override + public TestSite doWork() throws Exception + { + SiteInformation siteInfo = new SiteInformation(site2_name, site2_title, site2_title, SiteVisibility.PRIVATE); + TestSite site = network1.createSite(siteInfo); + site.inviteToSite(person32.getId(), site2_role); + return site; + } + }, person31.getId(), network1.getId()); - testSortingAndPagingByTitleAsc(); - testSortingAndPagingByTitleDesc(); - testSortingAndPagingByRoleAsc(); - testSortingAndPagingByRoleDesc(); - testSortingAndPagingBySiteNameAsc(); - testSortingAndPagingBySiteNameDesc(); - testSortingAndPagingDefault(); + this.site3 = TenantUtil.runAsUserTenant(new TenantRunAsWork() + { + @Override + public TestSite doWork() throws Exception + { + SiteInformation siteInfo = new SiteInformation(site3_name, site3_title, site3_title, SiteVisibility.PRIVATE); + TestSite site = network1.createSite(siteInfo); + site.inviteToSite(person32.getId(), site3_role); + return site; + } + }, person31.getId(), network1.getId()); + } + + @Before + public void setup() throws Exception + { + Iterator networksIt = getTestFixture().getNetworksIt(); + + assertTrue(networksIt.hasNext()); + this.network1 = networksIt.next(); + + assertTrue(networksIt.hasNext()); + this.network2 = networksIt.next(); + + // create a user + + final List people = new ArrayList(1); + + // Create some users + TenantUtil.runAsSystemTenant(new TenantRunAsWork() + { + @Override + public Void doWork() throws Exception + { + TestPerson person = network1.createUser(); + people.add(person); + person = network1.createUser(); + people.add(person); + + return null; + } + }, network1.getId()); + + TenantUtil.runAsSystemTenant(new TenantRunAsWork() + { + @Override + public Void doWork() throws Exception + { + TestPerson person = network2.createUser(); + people.add(person); + + return null; + } + }, network2.getId()); + + this.person11 = people.get(0); + this.person12 = people.get(1); + this.person21 = people.get(2); + + // ...and some sites + TenantUtil.runAsUserTenant(new TenantRunAsWork() + { + @Override + public Void doWork() throws Exception + { + TestSite site = network1.createSite(SiteVisibility.PUBLIC); + site.inviteToSite(person11.getId(), SiteRole.SiteContributor); + sites.add(site); + + site = network1.createSite(SiteVisibility.MODERATED); + site.inviteToSite(person11.getId(), SiteRole.SiteContributor); + sites.add(site); + + site = network1.createSite(SiteVisibility.PRIVATE); + site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); + sites.add(site); + + site = network1.createSite(SiteVisibility.PUBLIC); + site.inviteToSite(person11.getId(), SiteRole.SiteManager); + sites.add(site); + + site = network1.createSite(SiteVisibility.PRIVATE); + site.inviteToSite(person11.getId(), SiteRole.SiteCollaborator); + sites.add(site); + + //Special site for person removal + site = network1.createSite(SiteVisibility.PRIVATE); + site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); + sites.add(site); + return null; + } + }, person12.getId(), network1.getId()); + } + + @Test + public void testPersonSites() throws Exception + { + Set personSites = new TreeSet(); + + //Get last site for use with personRemoveSite + TestSite personRemoveSite = sites.get(sites.size() - 1); + sites.remove(sites.size() - 1); + + personSites.addAll(network1.getSiteMemberships(person11.getId())); + + // Create some sites + personSites.addAll(TenantUtil.runAsUserTenant(new TenantRunAsWork>() + { + @Override + public List doWork() throws Exception + { + List expectedSites = new ArrayList(); + + TestSite site = network1.createSite(SiteVisibility.PRIVATE); + expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager)); + + site = network1.createSite(SiteVisibility.PUBLIC); + expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager)); + + site = network1.createSite(SiteVisibility.MODERATED); + expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager)); + + return expectedSites; + } + }, person11.getId(), network1.getId())); + + personSites.addAll(TenantUtil.runAsUserTenant(new TenantRunAsWork>() + { + @Override + public List doWork() throws Exception + { + List expectedSites = new ArrayList(); + + TestSite site = network1.createSite(SiteVisibility.PRIVATE); + site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); + expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer)); + + site = network1.createSite(SiteVisibility.PUBLIC); + site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); + expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer)); + + site = network1.createSite(SiteVisibility.MODERATED); + site.inviteToSite(person11.getId(), SiteRole.SiteConsumer); + expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer)); + + return expectedSites; + } + }, person12.getId(), network1.getId())); + + final List expectedSites = new ArrayList(personSites); + Sites sitesProxy = publicApiClient.sites(); + + // Test Case cloud-1487 + + // unknown user + try + { + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + + sitesProxy.getPersonSites(GUID.generate(), null); + fail(""); } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); + } + + // Test Case cloud-2200 + // Test Case cloud-2213 + // user should be able to list their sites + { + int skipCount = 0; + int maxItems = 2; + Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + ListResponse resp = sitesProxy.getPersonSites(person11.getId(), createParams(paging, null)); + checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp); + } + + { + int skipCount = 2; + int maxItems = 8; + Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + ListResponse resp = sitesProxy.getPersonSites(person11.getId(), createParams(paging, null)); + checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp); + } + + // "-me-" user + { + int skipCount = 0; + int maxItems = 2; + Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + ListResponse resp = sitesProxy.getPersonSites(org.alfresco.rest.api.People.DEFAULT_USER, createParams(paging, null)); + checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp); + } + + // a user in another tenant should not be able to list a user's sites + try + { + int skipCount = 0; + int maxItems = 2; + Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId())); + sitesProxy.getPersonSites(person11.getId(), createParams(paging, null)); + fail(""); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode()); + } + + // Test case cloud-1488 + { + MemberOfSite memberOfSite = expectedSites.get(0); + + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + MemberOfSite ret = sitesProxy.getPersonSite(person11.getId(), memberOfSite.getSiteId()); + memberOfSite.expected(ret); + } + + try + { + MemberOfSite memberOfSite = expectedSites.get(0); + + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.getPersonSite(GUID.generate(), memberOfSite.getSiteId()); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); + } + + try + { + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.getPersonSite(person11.getId(), GUID.generate()); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); + } + + // Test Case cloud-1487 + // unknown person id + try + { + MemberOfSite memberOfSite = expectedSites.get(0); + + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.getPersonSite(GUID.generate(), memberOfSite.getSiteId()); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); + } + + try + { + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.getPersonSite(person11.getId(), GUID.generate()); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); + } + + { + //Tests removing a person from the site + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.remove("people", person11.getId(), "sites", personRemoveSite.getSiteId(), "Unable to DELETE a person site"); + + try + { + sitesProxy.getPersonSite(person11.getId(), personRemoveSite.getSiteId()); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); + } + } + + // TODO + // person from external network listing user sites + + // Test Case cloud-1966 + // Not allowed methods + try + { + MemberOfSite memberOfSite = expectedSites.get(0); + + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.create("people", person11.getId(), "sites", memberOfSite.getSiteId(), null, "Unable to POST to a person site"); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); + } + + try + { + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.create("people", person11.getId(), "sites", null, null, "Unable to POST to person sites"); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); + } + + try + { + MemberOfSite memberOfSite = expectedSites.get(0); + + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.update("people", person11.getId(), "sites", memberOfSite.getSiteId(), null, "Unable to PUT a person site"); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); + } + + try + { + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.update("people", person11.getId(), "sites", null, null, "Unable to PUT person sites"); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); + } + + try + { + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); + sitesProxy.remove("people", person11.getId(), "sites", null, "Unable to DELETE person sites"); + fail(); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); + } + } + + /** + * Retrieves the site memberships associated to a user + * + * @param sortColumn + * @param asc + * @return + * @throws Exception + */ + private ListResponse getSiteMembershipsForPerson32(final Paging paging, String sortColumn, boolean asc) throws Exception + { + final Sites sitesProxy = publicApiClient.sites(); + publicApiClient.setRequestContext(new RequestContext(network1.getId(), person32.getId())); + + // sort params + final Map params = new HashMap(); + if (sortColumn != null) + { + params.put("orderBy", sortColumn + " " + (asc ? "ASC" : "DESC")); + } + + // get memberships + ListResponse resp = TenantUtil.runAsUserTenant(new TenantRunAsWork>() + { + @Override + public ListResponse doWork() throws Exception + { + ListResponse resp = sitesProxy.getPersonSites(person32.getId(), createParams(paging, params)); + + return resp; + } + }, person32.getId(), network1.getId()); + + return resp; + } + + /** + * Tests the capability to sort and paginate the site memberships associated + * to a user order = Title ASC skip = 1, count = 2 + * + * @throws Exception + */ + public void testSortingAndPagingByTitleAsc() throws Exception + { + // paging + int skipCount = 1; + int maxItems = 2; + int totalResults = 3; + Paging paging = getPaging(skipCount, maxItems, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteTitle", true); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site3, site3_role)); + expectedList.add(new MemberOfSite(site1, site1_role)); + + checkList(expectedList, paging.getExpectedPaging(), resp); + + } + + /** + * Tests the capability to sort and paginate the site memberships associated + * to a user order = Title DESC skip = 1, count = 2 + * + * @throws Exception + */ + public void testSortingAndPagingByTitleDesc() throws Exception + { + // paging + int skipCount = 1; + int maxItems = 2; + int totalResults = 3; + Paging paging = getPaging(skipCount, maxItems, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteTitle", false); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site3, site3_role)); + expectedList.add(new MemberOfSite(site2, site2_role)); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + /** + * Tests the capability to sort and paginate the site memberships associated + * to a user order = Role ASC skip = 1, count = 2 + * + * @throws Exception + */ + public void testSortingAndPagingByRoleAsc() throws Exception + { + // paging + int skipCount = 1; + int maxItems = 2; + int totalResults = 3; + Paging paging = getPaging(skipCount, maxItems, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(paging, "Role", true); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site1, site1_role)); + expectedList.add(new MemberOfSite(site2, site2_role)); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + /** + * Tests the capability to sort and paginate the site memberships associated + * to a user order = Role DESC skip = 1, count = 2 + * + * @throws Exception + */ + public void testSortingAndPagingByRoleDesc() throws Exception + { + // paging + int skipCount = 1; + int maxItems = 2; + int totalResults = 3; + Paging paging = getPaging(skipCount, maxItems, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(paging, "Role", false); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site1, site1_role)); + expectedList.add(new MemberOfSite(site3, site3_role)); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + /** + * Tests the capability to sort and paginate the site memberships associated + * to a user order = Site Name ASC skip = 1, count = 2 + * + * @throws Exception + */ + public void testSortingAndPagingBySiteNameAsc() throws Exception + { + // paging + int skipCount = 1; + int maxItems = 2; + int totalResults = 3; + Paging paging = getPaging(skipCount, maxItems, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteShortName", true); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site2, site2_role)); + expectedList.add(new MemberOfSite(site3, site3_role)); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + /** + * Tests the capability to sort and paginate the site memberships associated + * to a user order = Site Name DESC skip = 1, count = 2 + * + * @throws Exception + */ + public void testSortingAndPagingBySiteNameDesc() throws Exception + { + // paging + int skipCount = 1; + int maxItems = 2; + int totalResults = 3; + Paging paging = getPaging(skipCount, maxItems, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(paging, "SiteShortName", false); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site2, site2_role)); + expectedList.add(new MemberOfSite(site1, site1_role)); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + /** + * Tests the capability to sort and paginate the site memberships associated + * default sorting, all results + * + * @throws Exception + */ + public void testSortingAndPagingDefault() throws Exception + { + // paging + int totalResults = 3; + Paging paging = getPaging(null, null, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(null, null, false); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site2, site2_role)); + expectedList.add(new MemberOfSite(site3, site3_role)); + expectedList.add(new MemberOfSite(site1, site1_role)); + + checkList(expectedList, paging.getExpectedPaging(), resp); + + } + + @Test + public void testSortingAndPaging() throws Exception + { + initializeSites(); + + testSortingAndPagingByTitleAsc(); + testSortingAndPagingByTitleDesc(); + testSortingAndPagingByRoleAsc(); + testSortingAndPagingByRoleDesc(); + testSortingAndPagingBySiteNameAsc(); + testSortingAndPagingBySiteNameDesc(); + testSortingAndPagingDefault(); + } + + // ACE-4823 + @Test + public void testSitesWithSameTitles() throws Exception + { + // Creates 3 sites + initializeSites(); + + final String site4_name = "d_" + GUID.generate(); + final String site4_title = site3_title; // Same title as site3 + final SiteRole site4_role = SiteRole.SiteCollaborator; + + TestSite site4 = TenantUtil.runAsUserTenant(new TenantRunAsWork() + { + @Override + public TestSite doWork() throws Exception + { + SiteInformation siteInfo = new SiteInformation(site4_name, site4_title, site4_title, SiteVisibility.PRIVATE); + TestSite site = network1.createSite(siteInfo); + site.inviteToSite(person32.getId(), site4_role); + return site; + } + }, person31.getId(), network1.getId()); + assertNotNull(site4); + + // paging + int totalResults = 4; + Paging paging = getPaging(null, null, totalResults, null); + + // get memberships + ListResponse resp = getSiteMembershipsForPerson32(null, null, false); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add(new MemberOfSite(site2, site2_role)); + expectedList.add(new MemberOfSite(site3, site3_role)); + expectedList.add(new MemberOfSite(site4, site4_role)); + expectedList.add(new MemberOfSite(site1, site1_role)); + + try + { + checkList(expectedList, paging.getExpectedPaging(), resp); + } + catch (AssertionError error) + { + // Site3 and Site4 have a same title, and as we are sorting on titles (default sorting), + // we can't guarantee the order in which the sites will + // return, hence swap the sites and compare again. + Collections.swap(expectedList, 1, 2); + checkList(expectedList, paging.getExpectedPaging(), resp); + } + } }