Search in sources :

Example 16 with ListResponse

use of org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse in project alfresco-remote-api by Alfresco.

the class TestFavouriteSites method testFavouriteSites.

@Test
public void testFavouriteSites() throws Exception {
    Iterator<TestNetwork> networksIt = getTestFixture().getNetworksIt();
    assertTrue(networksIt.hasNext());
    final TestNetwork network1 = networksIt.next();
    assertTrue(networksIt.hasNext());
    final TestNetwork network2 = networksIt.next();
    // Create some users and sites
    final List<TestPerson> people = new ArrayList<TestPerson>();
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestPerson person = network1.createUser();
            people.add(person);
            person = network1.createUser();
            people.add(person);
            person = network1.createUser();
            people.add(person);
            return null;
        }
    }, network1.getId());
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestPerson person = network2.createUser();
            people.add(person);
            person = network2.createUser();
            people.add(person);
            return null;
        }
    }, network2.getId());
    final TestPerson person1 = people.get(0);
    final TestPerson person2 = people.get(1);
    final TestPerson person3 = people.get(2);
    final TestPerson person4 = people.get(3);
    final TestPerson person5 = people.get(3);
    TestSite testSite = transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<TestSite>() {

        @SuppressWarnings("synthetic-access")
        public TestSite execute() throws Throwable {
            return TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

                public TestSite doWork() throws Exception {
                    SiteInformation siteInfo = new SiteInformation(GUID.generate(), "", "", SiteVisibility.PUBLIC);
                    return network1.createSite(siteInfo);
                }
            }, person1.getId(), network1.getId());
        }
    }, false, true);
    TestSite testSite1 = transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<TestSite>() {

        @SuppressWarnings("synthetic-access")
        public TestSite execute() throws Throwable {
            return TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

                public TestSite doWork() throws Exception {
                    SiteInformation siteInfo = new SiteInformation(GUID.generate(), "", "", SiteVisibility.PUBLIC);
                    return network1.createSite(siteInfo);
                }
            }, person1.getId(), network1.getId());
        }
    }, false, true);
    TestSite testSite3 = transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<TestSite>() {

        @SuppressWarnings("synthetic-access")
        public TestSite execute() throws Throwable {
            return TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

                public TestSite doWork() throws Exception {
                    SiteInformation siteInfo = new SiteInformation(GUID.generate(), "", "", SiteVisibility.PUBLIC);
                    return network1.createSite(siteInfo);
                }
            }, person1.getId(), network1.getId());
        }
    }, false, true);
    TestSite testSite4 = transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<TestSite>() {

        @SuppressWarnings("synthetic-access")
        public TestSite execute() throws Throwable {
            return TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

                public TestSite doWork() throws Exception {
                    SiteInformation siteInfo = new SiteInformation(GUID.generate(), "", "", SiteVisibility.PUBLIC);
                    return network1.createSite(siteInfo);
                }
            }, person5.getId(), network2.getId());
        }
    }, false, true);
    Sites sitesProxy = publicApiClient.sites();
    // invalid methods
    try {
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        sitesProxy.create("people", person1.getId(), "favorite-sites", testSite.getSiteId(), fs.toJSON().toString(), "Unable to POST to a favorite-site");
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        sitesProxy.update("people", person1.getId(), "favorite-sites", null, fs.toJSON().toString(), "Unable to PUT favorite-sites");
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        sitesProxy.update("people", person1.getId(), "favorite-sites", testSite.getSiteId(), fs.toJSON().toString(), "Unable to PUT a favorite-site");
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        sitesProxy.remove("people", person1.getId(), "favorite-sites", null, "Unable to DELETE favorite-sites");
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // unknown user - 404
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        sitesProxy.createFavouriteSite("invalid.user", fs);
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // user from another network - 401 (not able to auth against tenant)
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person4.getId()));
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        sitesProxy.createFavouriteSite(person1.getId(), fs);
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }
    // another user from the same network
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        sitesProxy.createFavouriteSite(person2.getId(), fs);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
    }
    // a member of this site
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        FavouriteSite resp = sitesProxy.createFavouriteSite(person1.getId(), fs);
        fs.expected(resp);
    }
    // add same favourite site
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        sitesProxy.createFavouriteSite(person1.getId(), new FavouriteSite(testSite.getSiteId()));
        fail();
    } catch (PublicApiException e) {
        assertEquals(409, e.getHttpResponse().getStatusCode());
    }
    // "-me" user
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
        FavouriteSite fs = new FavouriteSite(testSite.getSiteId());
        FavouriteSite resp = sitesProxy.createFavouriteSite(org.alfresco.rest.api.People.DEFAULT_USER, fs);
        fs.expected(resp);
        final List<FavouriteSite> expectedFavouriteSites = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<FavouriteSite>>() {

            @Override
            public List<FavouriteSite> doWork() throws Exception {
                return repoService.getFavouriteSites(person2);
            }
        }, person2.getId(), network1.getId());
        // check it's there
        int skipCount = 0;
        int maxItems = Integer.MAX_VALUE;
        Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
        sitesProxy.getFavouriteSites(person2.getId(), createParams(paging, null));
    }
    // not a member of this site
    {
        FavouriteSite fs = new FavouriteSite(testSite1.getSiteId());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        FavouriteSite ret = sitesProxy.createFavouriteSite(person1.getId(), fs);
        fs.expected(ret);
    }
    // GET favourite sites
    {
        final List<FavouriteSite> expectedFavouriteSites = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<FavouriteSite>>() {

            @Override
            public List<FavouriteSite> doWork() throws Exception {
                return repoService.getFavouriteSites(person1);
            }
        }, person1.getId(), network1.getId());
        // unknown user
        try {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            sitesProxy.getFavouriteSites(GUID.generate(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // authentication: unknown user
        try {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), GUID.generate(), "password"));
            sitesProxy.getFavouriteSites(person1.getId(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }
        // another user from the same network - 403
        try {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            ListResponse<FavouriteSite> response = sitesProxy.getFavouriteSites(person1.getId(), createParams(paging, null));
            checkList(expectedFavouriteSites, paging.getExpectedPaging(), response);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
        }
        // another user from another network - 401
        try {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person4.getId()));
            sitesProxy.getFavouriteSites(person1.getId(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }
        // successful GET
        {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<FavouriteSite> response = sitesProxy.getFavouriteSites(person1.getId(), createParams(paging, null));
            checkList(expectedFavouriteSites, paging.getExpectedPaging(), response);
        }
        // skipCount is greater than the number of favourite sites
        {
            int skipCount = expectedFavouriteSites.size() + 100;
            Paging paging = getPaging(skipCount, null, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            List<FavouriteSite> expected = Collections.emptyList();
            ListResponse<FavouriteSite> response = sitesProxy.getFavouriteSites(person1.getId(), createParams(paging, null));
            checkList(expected, paging.getExpectedPaging(), response);
        }
        // "-me-" user
        {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<FavouriteSite> response = sitesProxy.getFavouriteSites(org.alfresco.rest.api.People.DEFAULT_USER, createParams(paging, null));
            checkList(expectedFavouriteSites, paging.getExpectedPaging(), response);
        }
    }
    // user is a member of the site which he has favourited
    {
        publicApiClient.setRequestContext(new RequestContext(network2.getId(), person5.getId()));
        List<FavouriteSite> expectedFavouriteSites = new ArrayList<FavouriteSite>(1);
        FavouriteSite fs = new FavouriteSite(testSite4.getSiteId());
        expectedFavouriteSites.add(fs);
        FavouriteSite ret = sitesProxy.createFavouriteSite(person5.getId(), fs);
        fs.expected(ret);
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
        ListResponse<FavouriteSite> response = sitesProxy.getFavouriteSites(person5.getId(), createParams(paging, null));
        checkList(expectedFavouriteSites, paging.getExpectedPaging(), response);
    }
    // remove
    {
        // create some favourite sites
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person3.getId()));
            FavouriteSite fs = new FavouriteSite(testSite);
            sitesProxy.createFavouriteSite(person3.getId(), fs);
            fs = new FavouriteSite(testSite1);
            sitesProxy.createFavouriteSite(person3.getId(), fs);
        }
        // known user
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            FavouriteSite fs = new FavouriteSite(testSite);
            sitesProxy.removeFavouriteSite(person1.getId(), fs);
            List<FavouriteSite> expectedFavouriteSites = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<FavouriteSite>>() {

                @Override
                public List<FavouriteSite> doWork() throws Exception {
                    return repoService.getFavouriteSites(person1);
                }
            }, person1.getId(), network1.getId());
            // check removed
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<FavouriteSite> response = sitesProxy.getFavouriteSites(person1.getId(), createParams(paging, null));
            assertFalse(response.getList().contains(fs));
        }
        // unknown user
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            FavouriteSite fs = new FavouriteSite(testSite);
            sitesProxy.removeFavouriteSite(GUID.generate(), fs);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // unknown site
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            FavouriteSite fs = new FavouriteSite(GUID.generate());
            sitesProxy.removeFavouriteSite(person1.getId(), fs);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // try to remove a favourite site that is not a favourite site
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            FavouriteSite fs = new FavouriteSite(testSite3);
            sitesProxy.removeFavouriteSite(person1.getId(), fs);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // "-me-" user
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person3.getId()));
            FavouriteSite fs = new FavouriteSite(testSite1);
            sitesProxy.removeFavouriteSite(org.alfresco.rest.api.People.DEFAULT_USER, fs);
            List<FavouriteSite> expectedFavouriteSites = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<FavouriteSite>>() {

                @Override
                public List<FavouriteSite> doWork() throws Exception {
                    return repoService.getFavouriteSites(person3);
                }
            }, person3.getId(), network1.getId());
            // check removed
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavouriteSites.size(), expectedFavouriteSites.size());
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person3.getId()));
            ListResponse<FavouriteSite> response = sitesProxy.getFavouriteSites(person3.getId(), createParams(paging, null));
            assertFalse(response.getList().contains(fs));
        }
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ArrayList(java.util.ArrayList) SiteInformation(org.alfresco.rest.api.tests.RepoService.SiteInformation) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Sites(org.alfresco.rest.api.tests.client.PublicApiClient.Sites) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) FavouriteSite(org.alfresco.rest.api.tests.client.data.FavouriteSite) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) Test(org.junit.Test)

Example 17 with ListResponse

use of org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse in project alfresco-remote-api by Alfresco.

the class TestActivities method testPersonActivities.

@Test
public void testPersonActivities() throws Exception {
    People peopleProxy = publicApiClient.people();
    // Test with default tenant domain. see ALF-20448
    {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person5.getId(), null, false, true);
                return activities;
            }
        }, person5.getId(), defaultNetwork.getId());
        for (Activity activity : expectedActivities) {
            if (activity.getSiteId() == null) {
                fail("SiteId should present in user-joined activity.");
            }
        }
        {
            int skipCount = 0;
            int maxItems = expectedActivities.size();
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(defaultNetwork.getId(), person5.getId()));
            ListResponse<Activity> response = peopleProxy.getActivities(person5.getId(), params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), response);
        }
    }
    // Test Case cloud-2204
    // Test case cloud-1500
    // Test Case cloud-2216
    // paging
    // Test Case cloud-1500
    {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());
        {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), response);
        }
        {
            int skipCount = 2;
            int maxItems = expectedActivities.size() - 2;
            assertTrue(maxItems > 0);
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), response);
        }
        // "-me-" user
        {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<Activity> response = peopleProxy.getActivities(org.alfresco.rest.api.People.DEFAULT_USER, params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), response);
        }
    }
    // unknown user - 404
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());
        int skipCount = 0;
        int maxItems = 2;
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        peopleProxy.getActivities(GUID.generate(), createParams(paging, null));
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // unknown site - 404
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        Map<String, String> params = createParams(paging, null);
        params.put("siteId", GUID.generate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(GUID.generate(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // user from another network - 404
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person3.getId()));
        peopleProxy.getActivities(person1.getId(), createParams(paging, null));
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }
    // another user from the same network - 403
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
        peopleProxy.getActivities(person1.getId(), createParams(paging, null));
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
    }
    try {
        List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());
        assertTrue(activities.size() > 0);
        Activity activity = activities.get(0);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.remove("people", person1.getId(), "activities", String.valueOf(activity.getId()), "Unable to DELETE a person activity");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // Test Case cloud-1500
    // other user activities
    {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> expectedActivities = repoService.getActivities(person1.getId(), null, true, false);
                return expectedActivities;
            }
        }, person1.getId(), network1.getId());
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        Map<String, String> params = createParams(paging, null);
        params.put("who", String.valueOf(ActivityWho.others));
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
        checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), response);
    }
    // all activities with siteId exclusion
    {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> expectedActivities = repoService.getActivities(person1.getId(), testSite.getSiteId(), false, false);
                return expectedActivities;
            }
        }, person1.getId(), network1.getId());
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        Map<String, String> params = createParams(paging, null);
        params.put("siteId", testSite.getSiteId());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
        checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), response);
    }
    // all activities with siteId exclusion, unknown site id
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);
        Map<String, String> params = createParams(paging, null);
        params.put("siteId", GUID.generate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(person1.getId(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // unknown person id
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);
        Map<String, String> params = createParams(paging, null);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(GUID.generate(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // invalid who parameter
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);
        Map<String, String> params = createParams(paging, null);
        params.put("who", GUID.generate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(person1.getId(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }
    // Test Case cloud-1970
    // Not allowed methods
    // try
    // {
    // publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    // peopleProxy.create("people", person1.getId(), "activities", null, null, "Unable to POST to person activities");
    // fail("");
    // }
    // catch(PublicApiException e)
    // {
    // assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    // }
    // 
    // try
    // {
    // publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    // peopleProxy.update("people", person1.getId(), "activities", null, null, "Unable to PUT person activities");
    // fail("");
    // }
    // catch(PublicApiException e)
    // {
    // assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    // }
    // 
    // try
    // {
    // publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    // peopleProxy.remove("people", person1.getId(), "activities", null, "Unable to DELETE person activities");
    // fail("");
    // }
    // catch(PublicApiException e)
    // {
    // assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    // }
    // 
    // try
    // {
    // List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>()
    // {
    // @Override
    // public List<Activity> doWork() throws Exception
    // {
    // List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
    // return activities;
    // }
    // }, person1.getId(), network1.getId());
    // assertTrue(activities.size() > 0);
    // Activity activity = activities.get(0);
    // 
    // publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    // peopleProxy.create("people", person1.getId(), "activities", String.valueOf(activity.getId()), null, "Unable to POST to a person activity");
    // fail("");
    // }
    // catch(PublicApiException e)
    // {
    // assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    // }
    // 
    // try
    // {
    // List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>()
    // {
    // @Override
    // public List<Activity> doWork() throws Exception
    // {
    // List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
    // return activities;
    // }
    // }, person1.getId(), network1.getId());
    // assertTrue(activities.size() > 0);
    // Activity activity = activities.get(0);
    // 
    // publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    // peopleProxy.update("people", person1.getId(), "activities", String.valueOf(activity.getId()), null, "Unable to PUT a person activity");
    // fail("");
    // }
    // catch(PublicApiException e)
    // {
    // assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    // }
    // Test Case cloud-1970
    // not allowed methods
    {
        List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {

            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, false);
                return activities;
            }
        }, person1.getId(), network1.getId());
        assertTrue(activities.size() > 0);
        Activity activity = activities.get(0);
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.create("people", person1.getId(), "activities", null, null, "Unable to POST to activities");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.create("people", person1.getId(), "activities", String.valueOf(activity.getId()), null, "Unable to POST to an activity");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.update("people", person1.getId(), "activities", null, null, "Unable to PUT activities");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.update("people", person1.getId(), "activities", String.valueOf(activity.getId()), null, "Unable to PUT an activity");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.remove("people", person1.getId(), "activities", null, "Unable to DELETE activities");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.remove("people", person1.getId(), "activities", String.valueOf(activity.getId()), "Unable to DELETE an activity");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) People(org.alfresco.rest.api.tests.client.PublicApiClient.People) Activity(org.alfresco.rest.api.tests.client.data.Activity) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Map(java.util.Map) Test(org.junit.Test)

Example 18 with ListResponse

use of org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse in project alfresco-remote-api by Alfresco.

the class GroupsTest method canGetGroupsForUserId.

private void canGetGroupsForUserId() throws Exception {
    Person personAlice;
    {
        publicApiClient.setRequestContext(new RequestContext(networkOne.getId(), networkAdmin, "admin"));
        personAlice = new Person();
        String aliceId = "alice-" + UUID.randomUUID() + "@" + networkOne.getId();
        personAlice.setUserName(aliceId);
        personAlice.setId(aliceId);
        personAlice.setFirstName("Alice");
        personAlice.setEmail("alison.smith@example.com");
        personAlice.setPassword("password");
        personAlice.setEnabled(true);
        PublicApiClient.People people = publicApiClient.people();
        people.create(personAlice);
    }
    Groups groupsProxy = publicApiClient.groups();
    // As admin
    setRequestContext(networkOne.getId(), networkAdmin, "admin");
    // New user has only the one default group.
    {
        ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(personAlice.getId(), null, "Couldn't get user's groups", 200);
        assertEquals(1L, (long) groups.getPaging().getTotalItems());
        Iterator<Group> it = groups.getList().iterator();
        assertEquals(GROUP_EVERYONE, it.next().getId());
    }
    // Add the user to a couple more groups and list them.
    {
        AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
        authorityService.addAuthority(groupA.getId(), personAlice.getId());
        authorityService.addAuthority(groupB.getId(), personAlice.getId());
        ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(personAlice.getId(), null, "Couldn't get user's groups", 200);
        assertEquals(4L, (long) groups.getPaging().getTotalItems());
        Iterator<Group> it = groups.getList().iterator();
        assertEquals(groupA, it.next());
        assertEquals(groupB, it.next());
        assertEquals(GROUP_EVERYONE, it.next().getId());
        assertEquals(rootGroup, it.next());
    }
    {
        Group aardvark = new Group();
        try {
            aardvark.setId("GROUP_AARDVARK");
            aardvark.setDisplayName("Aardvark");
            authorityService.createAuthority(AuthorityType.GROUP, aardvark.getId());
            authorityService.setAuthorityDisplayName(aardvark.getId(), aardvark.getDisplayName());
            Person personBob;
            personBob = new Person();
            String bobId = "bob-" + UUID.randomUUID() + "@" + networkOne.getId();
            personBob.setUserName(bobId);
            personBob.setId(bobId);
            personBob.setFirstName("Bob");
            personBob.setEmail("bob.cratchet@example.com");
            personBob.setPassword("password");
            personBob.setEnabled(true);
            PublicApiClient.People people = publicApiClient.people();
            people.create(personBob);
            authorityService.addAuthority(aardvark.getId(), personBob.getId());
            // Check sorting by ID when groups have no display name (REPO-1844)
            // We don't want the GROUP_EVERYONE group to be sorted as an empty string, for example,
            // which is what the comparator was doing.
            Map<String, String> params = new HashMap<>();
            addOrderBy(params, org.alfresco.rest.api.Groups.PARAM_ID, true);
            ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(personBob.getId(), params, "Couldn't get user's groups", 200);
            assertEquals(2L, (long) groups.getPaging().getTotalItems());
            Iterator<Group> it = groups.getList().iterator();
            assertEquals(aardvark.getId(), it.next().getId());
            assertEquals("GROUP_EVERYONE", it.next().getId());
        } finally {
            authorityService.deleteAuthority(aardvark.getId());
        }
    }
    // Get network admin's groups by explicit ID.
    {
        ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(networkAdmin, null, "Couldn't get user's groups", 200);
        assertEquals(6L, (long) groups.getPaging().getTotalItems());
    }
    // test -me- alias (as network admin)
    {
        ListResponse<Group> groups = groupsProxy.getGroupsByPersonId("-me-", null, "Couldn't get user's groups", 200);
        assertEquals(6L, (long) groups.getPaging().getCount());
        Iterator<Group> it = groups.getList().iterator();
        assertEquals("GROUP_ALFRESCO_ADMINISTRATORS", it.next().getId());
    }
    // Filter by isRoot
    {
        Map<String, String> params = new HashMap<>();
        params.put("where", "(isRoot=true)");
        ListResponse<Group> response = groupsProxy.getGroupsByPersonId("-me-", params, "Couldn't get user's groups", 200);
        List<Group> groups = response.getList();
        assertFalse(groups.isEmpty());
        // All groups should be root groups.
        groups.forEach(group -> assertTrue(group.getIsRoot()));
        params.put("where", "(isRoot=false)");
        response = groupsProxy.getGroupsByPersonId("-me-", params, "Couldn't get user's groups", 200);
        groups = response.getList();
        assertFalse(groups.isEmpty());
        // All groups should be non-root groups.
        groups.forEach(group -> assertFalse(group.getIsRoot()));
    }
    // -ve test: attempt to get groups for non-existent person
    {
        groupsProxy.getGroupsByPersonId("i-do-not-exist", null, "Incorrect response", 404);
    }
    // As Alice
    setRequestContext(networkOne.getId(), personAlice.getId(), "password");
    // test -me- alias as Alice
    {
        ListResponse<Group> groups = groupsProxy.getGroupsByPersonId("-me-", null, "Couldn't get user's groups", 200);
        assertEquals(4L, (long) groups.getPaging().getCount());
        Iterator<Group> it = groups.getList().iterator();
        assertEquals(groupA, it.next());
        assertEquals(groupB, it.next());
        assertEquals(GROUP_EVERYONE, it.next().getId());
        assertEquals(rootGroup, it.next());
    }
    // +ve: check skip count.
    {
        // Sort params
        Map<String, String> otherParams = new HashMap<>();
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_DISPLAY_NAME, false);
        // Paging and list groups
        int skipCount = 0;
        int maxItems = 4;
        Paging paging = getPaging(skipCount, maxItems);
        ListResponse<Group> resp = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        // Paging and list groups with skip count.
        skipCount = 2;
        maxItems = 2;
        paging = getPaging(skipCount, maxItems);
        ListResponse<Group> sublistResponse = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        List<Group> expectedSublist = sublist(resp.getList(), skipCount, maxItems);
        checkList(expectedSublist, sublistResponse.getPaging(), sublistResponse);
    }
    // -ve: check skip count.
    {
        getGroupsByPersonId(personAlice.getId(), getPaging(-1, null), null, HttpServletResponse.SC_BAD_REQUEST);
    }
    // orderBy=sortColumn should be the same to orderBy=sortColumn ASC
    {
        // paging
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        Map<String, String> otherParams = new HashMap<>();
        // Default order.
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_DISPLAY_NAME, null);
        ListResponse<Group> resp = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        List<Group> groups = resp.getList();
        assertTrue("groups order not valid", groups.indexOf(groupA) < groups.indexOf(groupB));
        // Ascending order.
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_DISPLAY_NAME, true);
        ListResponse<Group> respOrderAsc = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        checkList(respOrderAsc.getList(), resp.getPaging(), resp);
    }
    // Sorting should be the same regardless of implementation (canned query
    // or postprocessing).
    {
        // paging
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        Map<String, String> otherParams = new HashMap<>();
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_DISPLAY_NAME, null);
        // Get and sort groups using canned query.
        ListResponse<Group> respCannedQuery = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        // Get and sort groups using postprocessing.
        otherParams.put("where", "(isRoot=true)");
        ListResponse<Group> respPostProcess = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        List<Group> expected = respCannedQuery.getList();
        expected.retainAll(respPostProcess.getList());
        // If this assertion fails, then the tests aren't providing any value - change them!
        assertTrue("List doesn't contain enough items for test to be conclusive.", expected.size() > 0);
        checkList(expected, respPostProcess.getPaging(), respPostProcess);
    }
    {
        // paging
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        Map<String, String> otherParams = new HashMap<>();
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_ID, null);
        // Get and sort groups using canned query.
        ListResponse<Group> respCannedQuery = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        // Get and sort groups using postprocessing.
        otherParams.put("where", "(isRoot=true)");
        ListResponse<Group> respPostProcess = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        List<Group> expected = respCannedQuery.getList();
        expected.retainAll(respPostProcess.getList());
        // If this assertion fails, then the tests aren't providing any value - change them!
        assertTrue("List doesn't contain enough items for test to be conclusive.", expected.size() > 0);
        checkList(expected, respPostProcess.getPaging(), respPostProcess);
    }
    // Sort by id.
    {
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        Map<String, String> otherParams = new HashMap<>();
        Group groupEveryone = new Group();
        groupEveryone.setId(PermissionService.ALL_AUTHORITIES);
        // Sort by ID ascending
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_ID, true);
        ListResponse<Group> resp = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        assertEquals(4, resp.getList().size());
        Iterator<Group> it = resp.getList().iterator();
        // GROUP_EVERYONE
        assertEquals(groupEveryone, it.next());
        // GROUP_Group_ROOT<UUID>
        assertEquals(rootGroup, it.next());
        // GROUP_Test_GroupA<UUID>
        assertEquals(groupA, it.next());
        // GROUP_Test_GroupB<UUID>
        assertEquals(groupB, it.next());
        // Sort by ID descending
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_ID, false);
        resp = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        assertEquals(4, resp.getList().size());
        it = resp.getList().iterator();
        // GROUP_Test_GroupB<UUID>
        assertEquals(groupB, it.next());
        // GROUP_Test_GroupA<UUID>
        assertEquals(groupA, it.next());
        // GROUP_Group_ROOT<UUID>
        assertEquals(rootGroup, it.next());
        // GROUP_EVERYONE
        assertEquals(groupEveryone, it.next());
    }
    // Multiple sort fields not allowed.
    {
        // paging
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        Map<String, String> otherParams = new HashMap<>();
        otherParams.put("orderBy", org.alfresco.rest.api.Groups.PARAM_ID + " ASC," + org.alfresco.rest.api.Groups.PARAM_DISPLAY_NAME + " ASC");
        getGroupsByPersonId(personAlice.getId(), paging, otherParams, HttpServletResponse.SC_BAD_REQUEST);
    }
    // Check include parent ids.
    {
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        Map<String, String> otherParams = new HashMap<>();
        otherParams.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_PARENT_IDS);
        ListResponse<Group> resp = getGroupsByPersonId(personAlice.getId(), paging, otherParams);
        assertEquals(4, resp.getList().size());
        Iterator<Group> it = resp.getList().iterator();
        Group group = it.next();
        assertEquals(groupA.getId(), group.getId());
        assertEquals(1, group.getParentIds().size());
        assertTrue(group.getParentIds().contains(rootGroup.getId()));
        group = it.next();
        assertEquals(groupB.getId(), group.getId());
        assertEquals(1, group.getParentIds().size());
        assertTrue(group.getParentIds().contains(rootGroup.getId()));
        group = it.next();
        assertEquals(PermissionService.ALL_AUTHORITIES, group.getId());
        assertEquals(0, group.getParentIds().size());
        group = it.next();
        assertEquals(rootGroup.getId(), group.getId());
        assertEquals(0, group.getParentIds().size());
    }
    // Filter by zone, use the -me- alias.
    {
        Map<String, String> params = new HashMap<>();
        params.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_ZONES);
        params.put("where", "(zones in ('APP.DEFAULT'))");
        // Use the -me- alias
        ListResponse<Group> response = groupsProxy.getGroupsByPersonId("-me-", params, "Couldn't get user's groups", 200);
        List<Group> groups = response.getList();
        assertFalse(groups.isEmpty());
        // All groups should contain the selected zone.
        groups.forEach(group -> assertTrue(group.getZones().contains("APP.DEFAULT")));
    }
    // Filter by zone, use the -me- alias.
    {
        Map<String, String> params = new HashMap<>();
        params.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_ZONES);
        params.put("where", "(zones in ('APITEST.MYZONE'))");
        // Use the -me- alias
        ListResponse<Group> response = groupsProxy.getGroupsByPersonId("-me-", params, "Couldn't get user's groups", 200);
        List<Group> groups = response.getList();
        assertEquals(3, groups.size());
        // All groups should contain the selected zone.
        groups.forEach(group -> assertTrue(group.getZones().contains("APITEST.MYZONE")));
    }
    // Filter by zone - use the person's ID, without "include"-ing zones
    {
        Map<String, String> params = new HashMap<>();
        params.put("where", "(zones in ('APITEST.ANOTHER'))");
        ListResponse<Group> response = groupsProxy.getGroupsByPersonId(personAlice.getId(), params, "Couldn't get user's groups", 200);
        List<Group> groups = response.getList();
        assertEquals(1, groups.size());
        // We haven't included the zone info
        groups.forEach(group -> assertNull(group.getZones()));
    }
    // Filter zones while using where isRoot=true
    // (this causes a different query path to be used)
    {
        Map<String, String> otherParams = new HashMap<>();
        // Ensure predictable result ordering
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_DISPLAY_NAME, true);
        otherParams.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_ZONES);
        otherParams.put("where", "(isRoot=true AND zones in ('APITEST.MYZONE'))");
        ListResponse<Group> response = groupsProxy.getGroupsByPersonId("-me-", otherParams, "Unexpected response", 200);
        List<Group> groups = response.getList();
        assertEquals(1, groups.size());
        assertEquals(rootGroup, groups.get(0));
        assertTrue(groups.get(0).getZones().contains("APITEST.MYZONE"));
        // Zone that doesn't exist.
        otherParams.put("where", "(isRoot=true AND zones in ('I.DO.NOT.EXIST'))");
        response = groupsProxy.getGroupsByPersonId("-me-", otherParams, "Unexpected response", 200);
        groups = response.getList();
        assertTrue(groups.isEmpty());
    }
    // Filter zones while using where isRoot=false
    {
        Map<String, String> otherParams = new HashMap<>();
        // Ensure predictable result ordering
        addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_DISPLAY_NAME, true);
        otherParams.put("where", "(isRoot=false AND zones in ('APITEST.MYZONE'))");
        ListResponse<Group> response = groupsProxy.getGroupsByPersonId("-me-", otherParams, "Unexpected response", 200);
        List<Group> groups = response.getList();
        assertEquals(2, groups.size());
        assertEquals(groupA, groups.get(0));
        assertEquals(groupB, groups.get(1));
        // We haven't included the zones info.
        groups.forEach(group -> assertNull(group.getZones()));
        // Zone that doesn't exist.
        otherParams.put("where", "(isRoot=false AND zones in ('I.DO.NOT.EXIST'))");
        response = groupsProxy.getGroupsByPersonId("-me-", otherParams, "Unexpected response", 200);
        groups = response.getList();
        assertTrue(groups.isEmpty());
    }
    // -ve test: invalid zones clause
    {
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        Map<String, String> otherParams = new HashMap<>();
        otherParams.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_ZONES);
        // Empty zone list
        otherParams.put("where", "(zones in ())");
        groupsProxy.getGroupsByPersonId(personAlice.getId(), otherParams, "Incorrect response", 400);
        // Empty zone name
        otherParams.put("where", "(zones in (''))");
        groupsProxy.getGroupsByPersonId(personAlice.getId(), otherParams, "Incorrect response", 400);
        // Too many zones
        otherParams.put("where", "(zones in ('APP.DEFAULT', 'APITEST.MYZONE'))");
        groupsProxy.getGroupsByPersonId(personAlice.getId(), otherParams, "Incorrect response", 400);
        // "A series of unfortunate errors"
        otherParams.put("where", "(zones in ('', 'APP.DEFAULT', '', 'APITEST.MYZONE'))");
        groupsProxy.getGroupsByPersonId(personAlice.getId(), otherParams, "Incorrect response", 400);
        // OR operator not currently supported
        otherParams.put("where", "(isRoot=true OR zones in ('APP.DEFAULT'))");
        groupsProxy.getGroupsByPersonId(personAlice.getId(), otherParams, "Incorrect response", 400);
    }
}
Also used : AuthorityDAOImpl(org.alfresco.repo.security.authority.AuthorityDAOImpl) java.util(java.util) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) GroupMember(org.alfresco.rest.api.tests.client.data.GroupMember) AuthorityService(org.alfresco.service.cmr.security.AuthorityService) Mock(org.mockito.Mock) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) Groups(org.alfresco.rest.api.tests.client.PublicApiClient.Groups) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) PermissionService(org.alfresco.service.cmr.security.PermissionService) GUID(org.alfresco.util.GUID) After(org.junit.After) ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) AuthorityType(org.alfresco.service.cmr.security.AuthorityType) LuceneTests(org.alfresco.util.testing.category.LuceneTests) Before(org.junit.Before) SortColumn(org.alfresco.rest.framework.resource.parameters.SortColumn) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) Category(org.junit.experimental.categories.Category) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest) Group(org.alfresco.rest.api.tests.client.data.Group) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) Person(org.alfresco.rest.api.tests.client.data.Person) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) Assert(org.junit.Assert) Group(org.alfresco.rest.api.tests.client.data.Group) ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) Groups(org.alfresco.rest.api.tests.client.PublicApiClient.Groups) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Person(org.alfresco.rest.api.tests.client.data.Person)

Example 19 with ListResponse

use of org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse in project alfresco-remote-api by Alfresco.

the class ListParser method parseList.

public ListResponse<T> parseList(JSONObject jsonResponse) {
    List<T> entries = new ArrayList<T>();
    JSONObject jsonList = (JSONObject) jsonResponse.get("list");
    assertNotNull(jsonList);
    JSONArray jsonEntries = (JSONArray) jsonList.get("entries");
    assertNotNull(jsonEntries);
    for (int i = 0; i < jsonEntries.size(); i++) {
        JSONObject jsonEntry = (JSONObject) jsonEntries.get(i);
        JSONObject entry = (JSONObject) jsonEntry.get("entry");
        entries.add(parseEntry(entry));
    }
    ExpectedPaging paging = ExpectedPaging.parsePagination(jsonList);
    ListResponse<T> resp = new ListResponse<T>(paging, entries);
    return resp;
}
Also used : JSONObject(org.json.simple.JSONObject) ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging)

Example 20 with ListResponse

use of org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse in project alfresco-remote-api by Alfresco.

the class TestActivities method testPUBLICAPI23.

/*
	 * Test that empty roles are not returned
	 */
@Test
public void testPUBLICAPI23() throws Exception {
    // Add and then remove personId as a member of the public site
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            testSite.updateMember(person2.getId(), SiteRole.SiteConsumer);
            testSite.removeMember(person2.getId());
            return null;
        }
    }, person1.getId(), network1.getId());
    // make sure activities have been generated
    repoService.generateFeed();
    // check that (empty) role is not in the response
    People peopleProxy = publicApiClient.people();
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
        int skipCount = 0;
        int maxItems = 10;
        Paging paging = getPaging(skipCount, maxItems);
        ListResponse<Activity> activities = peopleProxy.getActivities(person2.getId(), createParams(paging, null));
        for (Activity activity : activities.getList()) {
            String activityType = activity.getActivityType();
            if (activityType.equals("org.alfresco.site.user-left")) {
                String role = (String) activity.getSummary().get("role");
                String feedPersonId = activity.getFeedPersonId();
                if (feedPersonId.equals(person2.getId())) {
                    assertTrue(role == null);
                    break;
                }
            }
        }
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) People(org.alfresco.rest.api.tests.client.PublicApiClient.People) Activity(org.alfresco.rest.api.tests.client.data.Activity) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Test(org.junit.Test)

Aggregations

ListResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse)41 ArrayList (java.util.ArrayList)30 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)22 JSONObject (org.json.simple.JSONObject)21 Test (org.junit.Test)21 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)20 ExpectedPaging (org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging)19 JSONArray (org.json.simple.JSONArray)19 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)18 List (java.util.List)13 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)10 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)8 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)8 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Sites (org.alfresco.rest.api.tests.client.PublicApiClient.Sites)6 People (org.alfresco.rest.api.tests.client.PublicApiClient.People)5 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 ParseException (java.text.ParseException)4