From 68fc7c7cc1d9e00a0e45740c1c440a74a8b620a6 Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Thu, 3 Nov 2016 13:33:06 +0000 Subject: [PATCH] Merged 5.2.N (5.2.1) to HEAD (5.2) 131275 cturlica: REPO-1360: Filter sites by visibility - add automated tests - added new tests for filter by visibility - updated getSites by personId because the value of totalItems wasn't calculated, also updated existing tests that where using the incorrect expected value. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@132229 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/rest/api/impl/SitesImpl.java | 21 +- .../alfresco/rest/api/tests/RepoService.java | 9 +- .../rest/api/tests/TestPersonSites.java | 236 +++++++++++++++++- .../alfresco/rest/api/tests/TestSites.java | 151 +++++++++++ 4 files changed, 394 insertions(+), 23 deletions(-) diff --git a/source/java/org/alfresco/rest/api/impl/SitesImpl.java b/source/java/org/alfresco/rest/api/impl/SitesImpl.java index 31e84ba390..378361b2cc 100644 --- a/source/java/org/alfresco/rest/api/impl/SitesImpl.java +++ b/source/java/org/alfresco/rest/api/impl/SitesImpl.java @@ -562,8 +562,10 @@ public class SitesImpl implements Sites List filterProps = getFilterPropListOfSites(parameters); + int counter; + int totalItems = 0; Iterator it = sortedSiteMembers.iterator(); - for(int counter = 0; counter < pageDetails.getEnd() && it.hasNext();) + for(counter = 0; it.hasNext();) { SiteMembership siteMember = it.next(); @@ -574,22 +576,23 @@ public class SitesImpl implements Sites if(counter < pageDetails.getSkipCount()) { + totalItems++; counter++; continue; } - if(counter > pageDetails.getEnd() - 1) + if (counter <= pageDetails.getEnd() - 1) { - break; + SiteInfo siteInfo = siteMember.getSiteInfo(); + MemberOfSite memberOfSite = new MemberOfSite(siteInfo.getShortName(), siteInfo.getNodeRef(), siteMember.getRole()); + ret.add(memberOfSite); + + counter++; } - SiteInfo siteInfo = siteMember.getSiteInfo(); - MemberOfSite memberOfSite = new MemberOfSite(siteInfo.getShortName(), siteInfo.getNodeRef(), siteMember.getRole()); - ret.add(memberOfSite); - - counter++; + totalItems++; } - return CollectionWithPagingInfo.asPaged(paging, ret, pageDetails.hasMoreItems(), null); + return CollectionWithPagingInfo.asPaged(paging, ret, counter < totalItems, totalItems); } diff --git a/source/test-java/org/alfresco/rest/api/tests/RepoService.java b/source/test-java/org/alfresco/rest/api/tests/RepoService.java index e86c8c9235..ba9bb8a9f2 100644 --- a/source/test-java/org/alfresco/rest/api/tests/RepoService.java +++ b/source/test-java/org/alfresco/rest/api/tests/RepoService.java @@ -1371,13 +1371,18 @@ public class RepoService } } - public TestSite createSite(SiteVisibility siteVisibility) + public TestSite createSite(String siteRootName, SiteVisibility siteVisibility) { - String shortName = "TESTSITE" + GUID.generate(); + String shortName = "TESTSITE" + (siteRootName != null ? siteRootName : "") + GUID.generate(); SiteInformation siteInfo = new SiteInformation(shortName, shortName, shortName, siteVisibility); return createSite(siteInfo); } + public TestSite createSite(SiteVisibility siteVisibility) + { + return createSite(null, siteVisibility); + } + /** * @deprecated replace with AbstractBaseApiTest.createSite (or PublicApiClient.sites.createSite) */ 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 c01fe0a3d8..9aec4fb3ca 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestPersonSites.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestPersonSites.java @@ -64,10 +64,13 @@ public class TestPersonSites extends EnterpriseTestApi { private TestNetwork network1; private TestNetwork network2; + private TestNetwork network4; private TestPerson person11; private TestPerson person12; private TestPerson person21; + private TestPerson person41; + private TestPerson person42; private List sites = new ArrayList<>(10); @@ -87,6 +90,11 @@ public class TestPersonSites extends EnterpriseTestApi private String site3_title = "b_" + GUID.generate(); private SiteRole site3_role = SiteRole.SiteConsumer; + private TestSite site41; + private TestSite site42; + private TestSite site43; + private TestSite site44; + @Override @Before public void setup() throws Exception @@ -227,7 +235,49 @@ public class TestPersonSites extends EnterpriseTestApi } }, person31.getId(), network1.getId()); } - + + private void initializePersonAndNetwork4WithSites() throws Exception + { + if (network4 == null) + { + network4 = getRepoService().createNetwork(this.getClass().getSimpleName().toLowerCase() + "-3-" + GUID.generate(), true); + network4.create(); + + // Create some users + TenantUtil.runAsSystemTenant(new TenantRunAsWork() + { + @Override + public Void doWork() throws Exception + { + person41 = network4.createUser(); + person42 = network4.createUser(); + return null; + } + }, network4.getId()); + + // ...and some sites + TenantUtil.runAsUserTenant(new TenantRunAsWork() + { + @Override + public Void doWork() throws Exception + { + site41 = network4.createSite("A", SiteVisibility.PRIVATE); + site41.inviteToSite(person41.getId(), SiteRole.SiteContributor); + + site42 = network4.createSite("B", SiteVisibility.PUBLIC); + site42.inviteToSite(person41.getId(), SiteRole.SiteContributor); + + site43 = network4.createSite("C", SiteVisibility.PUBLIC); + site43.inviteToSite(person41.getId(), SiteRole.SiteContributor); + + site44 = network4.createSite("D", SiteVisibility.MODERATED); + site44.inviteToSite(person41.getId(), SiteRole.SiteContributor); + return null; + } + }, person42.getId(), network4.getId()); + } + } + @Test public void testPersonSites() throws Exception { @@ -307,7 +357,7 @@ public class TestPersonSites extends EnterpriseTestApi { int skipCount = 0; int maxItems = 2; - Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); + Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size()); 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); @@ -316,7 +366,7 @@ public class TestPersonSites extends EnterpriseTestApi { int skipCount = 2; int maxItems = 8; - Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); + Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size()); 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); @@ -326,7 +376,7 @@ public class TestPersonSites extends EnterpriseTestApi { int skipCount = 0; int maxItems = 2; - Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null); + Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size()); 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); @@ -533,7 +583,7 @@ public class TestPersonSites extends EnterpriseTestApi int skipCount = 1; int maxItems = 2; int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(paging, "title", true); @@ -559,7 +609,7 @@ public class TestPersonSites extends EnterpriseTestApi int skipCount = 1; int maxItems = 2; int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(paging, "title", false); @@ -584,7 +634,7 @@ public class TestPersonSites extends EnterpriseTestApi int skipCount = 1; int maxItems = 2; int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(paging, "role", true); @@ -609,7 +659,7 @@ public class TestPersonSites extends EnterpriseTestApi int skipCount = 1; int maxItems = 2; int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(paging, "role", false); @@ -634,7 +684,7 @@ public class TestPersonSites extends EnterpriseTestApi int skipCount = 1; int maxItems = 2; int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(paging, "id", true); @@ -659,7 +709,7 @@ public class TestPersonSites extends EnterpriseTestApi int skipCount = 1; int maxItems = 2; int totalResults = 3; - Paging paging = getPaging(skipCount, maxItems, totalResults, null); + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(paging, "id", false); @@ -682,7 +732,7 @@ public class TestPersonSites extends EnterpriseTestApi { // paging int totalResults = 3; - Paging paging = getPaging(null, null, totalResults, null); + Paging paging = getPaging(null, null, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(null, null, false); @@ -738,7 +788,7 @@ public class TestPersonSites extends EnterpriseTestApi // paging int totalResults = 4; - Paging paging = getPaging(null, null, totalResults, null); + Paging paging = getPaging(null, null, totalResults, totalResults); // get memberships ListResponse resp = getSiteMembershipsForPerson32(null, null, false); @@ -763,4 +813,166 @@ public class TestPersonSites extends EnterpriseTestApi checkList(expectedList, paging.getExpectedPaging(), resp); } } + + /** + * Retrieves the site memberships associated to a user. + * + * @param paging + * The paging object + * @param params + * Public api parameters map. + * @param person + * The test person. + * @param network + * The test network. + * @return The site memberships associated to the give user. + * @throws Exception + */ + private ListResponse getSiteMembershipsForPersonAndNetwork(final Paging paging, Map params, TestPerson person, TestNetwork network, boolean runAsUserTenant) + throws Exception + { + final Sites sitesProxy = publicApiClient.sites(); + publicApiClient.setRequestContext(new RequestContext(network.getId(), person.getId())); + + ListResponse resp; + if (runAsUserTenant) + { + // get memberships + resp = TenantUtil.runAsUserTenant(new TenantRunAsWork>() + { + @Override + public ListResponse doWork() throws Exception + { + ListResponse resp = sitesProxy.getPersonSites(person.getId(), createParams(paging, params)); + return resp; + } + }, person.getId(), network.getId()); + } + else + { + resp = sitesProxy.getPersonSites(person.getId(), createParams(paging, params)); + } + + return resp; + } + + private List getPersonSites(TestPerson person, TestSite... sites) throws PublicApiException + { + Sites sitesProxy = publicApiClient.sites(); + + List memberOfSiteList = new ArrayList<>(); + for (TestSite site : sites) + { + memberOfSiteList.add(sitesProxy.getPersonSite(person.getId(), site.getSiteId())); + } + + return memberOfSiteList; + } + + private ListResponse getSiteMembershipsForPerson41(final Paging paging, String siteVisibility, boolean runAsUserTenant) throws Exception + { + final Map params = new HashMap<>(); + params.put("orderBy", "title" + " " + "ASC"); + + if (siteVisibility != null) + { + params.put("where", "(visibility=" + siteVisibility + ")"); + } + + return getSiteMembershipsForPersonAndNetwork(paging, params, person41, network4, runAsUserTenant); + } + + private ListResponse getSiteMembershipsForPerson41(final Paging paging, String siteVisibility) throws Exception + { + return getSiteMembershipsForPerson41(paging, siteVisibility, true); + } + + public void testGetSiteMembershipsWhereSiteVisibilityPrivate() throws Exception + { + // paging + int totalResults = 1; + Paging paging = getPaging(null, null, totalResults, totalResults); + + // list sites + ListResponse resp = getSiteMembershipsForPerson41(null, SiteVisibility.PRIVATE.name()); + + // check results + List expectedList = getPersonSites(person41, site41); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testGetSiteMembershipsWhereSiteVisibilityPublic() throws Exception + { + // paging + int totalResults = 2; + Paging paging = getPaging(null, null, totalResults, totalResults); + + // list sites + ListResponse resp = getSiteMembershipsForPerson41(null, SiteVisibility.PUBLIC.name()); + + // check results + List expectedList = getPersonSites(person41, site42, site43); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testGetSiteMembershipsWhereSiteVisibilityPublicAndSkipCount() throws Exception + { + // paging + Integer skipCount = 1; + int maxItems = 2; + int totalResults = 2; + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); + + // list sites + ListResponse resp = getSiteMembershipsForPerson41(paging, SiteVisibility.PUBLIC.name()); + + // check results + List expectedList = getPersonSites(person41, site43); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testGetSiteMembershipsWhereSiteVisibilityModerated() throws Exception + { + // paging + int totalResults = 1; + Paging paging = getPaging(null, null, totalResults, totalResults); + + // list sites + ListResponse resp = getSiteMembershipsForPerson41(null, SiteVisibility.MODERATED.name()); + + // check results + List expectedList = getPersonSites(person41, site44); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testGetSiteMembershipsWhereSiteVisibilityInvalid() throws Exception + { + try + { + getSiteMembershipsForPerson41(null, "invalidVisibility", false); + fail(""); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode()); + } + } + + @Test + public void testGetSiteMembershipsWithWhereClause() throws Exception + { + initializePersonAndNetwork4WithSites(); + publicApiClient.setRequestContext(new RequestContext(network4.getId(), person41.getId())); + + testGetSiteMembershipsWhereSiteVisibilityPrivate(); + testGetSiteMembershipsWhereSiteVisibilityPublic(); + testGetSiteMembershipsWhereSiteVisibilityPublicAndSkipCount(); + testGetSiteMembershipsWhereSiteVisibilityModerated(); + testGetSiteMembershipsWhereSiteVisibilityInvalid(); + } + } diff --git a/source/test-java/org/alfresco/rest/api/tests/TestSites.java b/source/test-java/org/alfresco/rest/api/tests/TestSites.java index 5fb62552fb..79526543f0 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestSites.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestSites.java @@ -47,6 +47,7 @@ import org.alfresco.rest.api.tests.client.RequestContext; import org.alfresco.rest.api.tests.client.data.Site; import org.alfresco.rest.api.tests.client.data.SiteImpl; import org.alfresco.rest.api.tests.client.data.SiteRole; +import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.service.cmr.site.SiteVisibility; import org.alfresco.util.GUID; import org.apache.commons.httpclient.HttpStatus; @@ -82,6 +83,15 @@ public class TestSites extends EnterpriseTestApi private Site site5; private Site site6; + // test network 3 + private TestNetwork network3; + private String person4Id; + + private Site site7; + private Site site8; + private Site site9; + private Site site10; + private String site4_id = "a-" + GUID.generate(); private String site4_title = "c_" + GUID.generate(); private String site4_description = "b_" + GUID.generate(); @@ -152,6 +162,41 @@ public class TestSites extends EnterpriseTestApi } } + private void initializeNetwork3WithSites() throws Exception + { + if (network3 == null) + { + network3 = getRepoService().createNetwork(this.getClass().getName().toLowerCase()+"-2-"+RUNID, true); + network3.create(); + + TenantUtil.runAsSystemTenant(new TenantRunAsWork() + { + @Override + public Void doWork() throws Exception + { + person4Id = network3.createUser().getId(); + return null; + } + }, network3.getId()); + + publicApiClient.setRequestContext(new RequestContext(network3.getId(), person4Id)); + + Sites sitesProxy = publicApiClient.sites(); + + Site site = new SiteImpl("site A" + GUID.generate(), SiteVisibility.PRIVATE.toString()); + site7 = sitesProxy.createSite(site); + + site = new SiteImpl("site B" + GUID.generate(), SiteVisibility.PUBLIC.toString()); + site8 = sitesProxy.createSite(site); + + site = new SiteImpl("site C" + GUID.generate(), SiteVisibility.PUBLIC.toString()); + site9 = sitesProxy.createSite(site); + + site = new SiteImpl("site D" + GUID.generate(), SiteVisibility.MODERATED.toString()); + site10 = sitesProxy.createSite(site); + } + } + @Test public void testGetSiteAndListSites() throws Exception { @@ -931,4 +976,110 @@ public class TestSites extends EnterpriseTestApi return sitesProxy.getSites(createParams(paging, params)); } + + private ListResponse listSitesWithWhere(final Paging paging, String siteVisibility) throws Exception + { + final Sites sitesProxy = publicApiClient.sites(); + + final Map params = new HashMap<>(); + if (siteVisibility != null) + { + params.put("where", "(visibility=" + siteVisibility + ")"); + } + + return sitesProxy.getSites(createParams(paging, params)); + } + + public void testListSitesWhereSiteVisibilityPrivate() throws Exception + { + // paging + int totalResults = 1; + Paging paging = getPaging(null, null, totalResults, totalResults); + + // list sites + ListResponse resp = listSitesWithWhere(null, SiteVisibility.PRIVATE.name()); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add((SiteImpl) site7); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testListSitesWhereSiteVisibilityPublic() throws Exception + { + // paging + int totalResults = 2; + Paging paging = getPaging(null, null, totalResults, totalResults); + + // list sites + ListResponse resp = listSitesWithWhere(null, SiteVisibility.PUBLIC.name()); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add((SiteImpl) site8); + expectedList.add((SiteImpl) site9); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testListSitesWhereSiteVisibilityPublicAndSkipCount() throws Exception + { + // paging + Integer skipCount = 1; + int maxItems = 2; + int totalResults = 2; + Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults); + + // list sites + ListResponse resp = listSitesWithWhere(paging, SiteVisibility.PUBLIC.name()); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add((SiteImpl) site9); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testListSitesWhereSiteVisibilityModerated() throws Exception + { + // paging + int totalResults = 1; + Paging paging = getPaging(null, null, totalResults, totalResults); + + // list sites + ListResponse resp = listSitesWithWhere(null, SiteVisibility.MODERATED.name()); + + // check results + List expectedList = new LinkedList<>(); + expectedList.add((SiteImpl) site10); + + checkList(expectedList, paging.getExpectedPaging(), resp); + } + + public void testListSitesWhereSiteVisibilityInvalid() throws Exception + { + try + { + listSitesWithWhere(null, "invalidVisibility"); + fail(""); + } + catch (PublicApiException e) + { + assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode()); + } + } + + @Test + public void testListSitesWhereExpected() throws Exception + { + initializeNetwork3WithSites(); + publicApiClient.setRequestContext(new RequestContext(network3.getId(), person4Id)); + + testListSitesWhereSiteVisibilityPrivate(); + testListSitesWhereSiteVisibilityPublic(); + testListSitesWhereSiteVisibilityPublicAndSkipCount(); + testListSitesWhereSiteVisibilityModerated(); + testListSitesWhereSiteVisibilityInvalid(); + } }