Search in sources :

Example 1 with TenantRunAsWork

use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork 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 2 with TenantRunAsWork

use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.

the class TestFavourites method testValidRequests.

@Test
public void testValidRequests() throws Exception {
    List<Favourite> expectedFavourites = new ArrayList<Favourite>();
    {
        // add some favourites
        // 2467
        log("cloud-2467");
        Favourite siteFavourite1 = makeSiteFavourite(person1PublicSites.get(0));
        FavouriteDocument document = repoService.getDocument(network1.getId(), person1PublicDocs.get(0));
        Favourite fileFavourite1 = makeFileFavourite(document.getGuid());
        FavouriteFolder folder = repoService.getFolder(network1.getId(), person1PublicFolders.get(0));
        Favourite folderFavourite1 = makeFolderFavourite(folder.getGuid());
        Favourite siteFavourite2 = makeSiteFavourite(person1PublicSites.get(1));
        document = repoService.getDocument(network1.getId(), person1PublicDocs.get(1));
        Favourite fileFavourite2 = makeFileFavourite(document.getGuid());
        folder = repoService.getFolder(network1.getId(), person1PublicFolders.get(1));
        Favourite folderFavourite2 = makeFolderFavourite(folder.getGuid());
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Favourite ret = favouritesProxy.createFavourite(person10Id, siteFavourite1);
            expectedFavourites.add(ret);
            siteFavourite1.expected(ret);
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Favourite ret = favouritesProxy.createFavourite(person10Id, fileFavourite1);
            expectedFavourites.add(ret);
            fileFavourite1.expected(ret);
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Favourite ret = favouritesProxy.createFavourite(person10Id, folderFavourite1);
            expectedFavourites.add(ret);
            folderFavourite1.expected(ret);
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Favourite ret = favouritesProxy.createFavourite(person10Id, siteFavourite2);
            expectedFavourites.add(ret);
            siteFavourite2.expected(ret);
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Favourite ret = favouritesProxy.createFavourite(person10Id, fileFavourite2);
            expectedFavourites.add(ret);
            fileFavourite2.expected(ret);
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Favourite ret = favouritesProxy.createFavourite(person10Id, folderFavourite2);
            expectedFavourites.add(ret);
            folderFavourite2.expected(ret);
        }
        // already a favourite - 201
        {
            log("cloud-2472");
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
                Favourite ret = favouritesProxy.createFavourite(person10Id, siteFavourite1);
                siteFavourite1.expected(ret);
            }
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
                Favourite ret = favouritesProxy.createFavourite(person10Id, folderFavourite1);
                folderFavourite1.expected(ret);
            }
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
                Favourite ret = favouritesProxy.createFavourite(person10Id, fileFavourite1);
                fileFavourite1.expected(ret);
            }
        }
        {
            // cloud-2498
            // cloud-2499
            // create and list favourites across networks
            List<Favourite> person21ExpectedFavourites = new ArrayList<Favourite>();
            log("cloud-2498");
            log("cloud-2499");
            {
                // favourite a site in another network
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21Id));
                Favourite favourite = makeSiteFavourite(person1PrivateSites.get(0));
                try {
                    favouritesProxy.createFavourite("-me-", favourite);
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
                int skipCount = 0;
                int maxItems = 10;
                Paging paging = getPaging(skipCount, maxItems, person21ExpectedFavourites.size(), person21ExpectedFavourites.size());
                try {
                    favouritesProxy.getFavourites("-me-", createParams(paging, null));
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
            }
            // favourite a document in another network
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21Id));
                FavouriteDocument document1 = new FavouriteDocument(person1PrivateDocs.get(0).getId());
                Favourite favourite = makeFileFavourite(document1.getGuid());
                try {
                    favouritesProxy.createFavourite("-me-", favourite);
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
                sort(person21ExpectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);
                int skipCount = 0;
                int maxItems = 10;
                Paging paging = getPaging(skipCount, maxItems, person21ExpectedFavourites.size(), person21ExpectedFavourites.size());
                try {
                    favouritesProxy.getFavourites("-me-", createParams(paging, null));
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
            }
            // favourite a folder in another network
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21Id));
                FavouriteFolder folder1 = new FavouriteFolder(person1PrivateFolders.get(0).getId());
                Favourite favourite = makeFolderFavourite(folder1.getGuid());
                try {
                    favouritesProxy.createFavourite("-me-", favourite);
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
                sort(person21ExpectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);
                int skipCount = 0;
                int maxItems = 10;
                Paging paging = getPaging(skipCount, maxItems, person21ExpectedFavourites.size(), person21ExpectedFavourites.size());
                try {
                    favouritesProxy.getFavourites("-me-", createParams(paging, null));
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
            }
        }
    }
    // GET favourites
    // test paging and sorting
    {
        // cloud-2458
        // cloud-2462
        // cloud-2461
        {
            log("cloud-2458");
            log("cloud-2461");
            log("cloud-2462");
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            List<Favourite> expected = new ArrayList<Favourite>(expectedFavourites);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(), expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, null));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            List<Favourite> expected = new ArrayList<Favourite>(expectedFavourites);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 2;
            int maxItems = 4;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(), expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, null));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // 2466
        // GET favourites for "-me-"
        {
            log("cloud-2466");
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            List<Favourite> expected = new ArrayList<Favourite>(expectedFavourites);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(), expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites("-me-", createParams(paging, null));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
    }
    // 2459
    {
        log("cloud-2459");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11Id));
        int skipCount = 0;
        int maxItems = Integer.MAX_VALUE;
        Paging paging = getPaging(skipCount, maxItems, 0, 0);
        ListResponse<Favourite> resp = favouritesProxy.getFavourites(person11Id, createParams(paging, null));
        List<Favourite> empty = Collections.emptyList();
        checkList(empty, paging.getExpectedPaging(), resp);
    }
    // cloud-2460: filtering by target type
    {
        log("cloud-2460");
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/file))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FOLDER));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // target/file
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/file))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // target/folder
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FOLDER));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // target/site
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.SITE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/site))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // target/folder OR target/file.
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FOLDER, Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/file) OR EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // target/site OR target/file.
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.SITE, Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/file) OR EXISTS(target/site))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // target/site OR target/folder.
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.SITE, Type.FOLDER));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/site) OR EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id, createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
    }
    // GET a favourite
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
        favouritesProxy.getFavourite(person10Id, GUID.generate());
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
        Favourite favourite = expectedFavourites.get(0);
        favouritesProxy.getFavourite(GUID.generate(), favourite.getTargetGuid());
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
        Favourite favourite = expectedFavourites.get(0);
        Favourite resp = favouritesProxy.getFavourite(person10Id, favourite.getTargetGuid());
        favourite.expected(resp);
    }
    // cloud-2479, PUT case
    {
        log("cloud-2479.1");
        updateFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.site);
        log("cloud-2479.2");
        updateFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.file);
        log("cloud-2479.3");
        updateFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.folder);
    }
    try {
        // cloud-2474
        // non-existent personId
        log("cloud-2474");
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
        favouritesProxy.removeFavourite(GUID.generate(), GUID.generate());
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // cloud-2475
    // try delete a non-existent favourite for a node that exists
    {
        log("cloud-2475");
        NodeRef doc = TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() throws Exception {
                NodeRef containerNodeRef = person1PublicSites.get(0).getContainerNodeRef("documentLibrary");
                NodeRef doc = repoService.createDocument(containerNodeRef, GUID.generate(), "");
                return doc;
            }
        }, person11Id, network1.getId());
        String favouriteId = doc.getId();
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
        ListResponse<Favourite> before = getFavourites(network1.getId(), person10Id, person10Id, 0, Integer.MAX_VALUE, null, null, null);
        List<Favourite> beforeList = before.getList();
        assertTrue(beforeList.size() > 0);
        try {
            favouritesProxy.removeFavourite(person10Id, favouriteId);
            fail("Should be a 404");
        } catch (PublicApiException e) {
            // expected
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // check no favourites have been removed
        ListResponse<Favourite> after = getFavourites(network1.getId(), person10Id, person10Id, 0, Integer.MAX_VALUE, null, null, null);
        assertEquals(beforeList.size(), after.getList().size());
    }
    // cloud-2473, DELETE case
    {
        log("cloud-2473.1");
        deleteFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.site);
        log("cloud-2473.2");
        deleteFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.file);
        log("cloud-2473.3");
        Favourite favourite = deleteFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.folder);
        // try to delete non-existent favourite
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            favouritesProxy.removeFavourite(person10Id, favourite.getTargetGuid());
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
    }
    // cloud-2476
    // try delete another user's favourite
    {
        log("cloud-2476");
        // make sure there are favourites to delete
        // publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId));
        // SiteFavouriteTarget target = new SiteFavouriteTarget(person1Sites.get(0));
        // Favourite favourite = new Favourite(target);
        // favouritesProxy.createFavourite(personId, favourite);
        ListResponse<Favourite> before = getFavourites(network1.getId(), person10Id, person10Id, 0, Integer.MAX_VALUE, null, null, null);
        assertTrue(before.getList().size() > 0);
        Favourite favourite = before.getList().get(0);
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11Id));
            favouritesProxy.removeFavourite(person10Id, favourite.getTargetGuid());
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        ListResponse<Favourite> after = getFavourites(network1.getId(), person10Id, person10Id, 0, Integer.MAX_VALUE, null, null, null);
        assertEquals(before.getList().size(), after.getList().size());
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ArrayList(java.util.ArrayList) FavouriteFolder(org.alfresco.rest.api.tests.client.data.FavouriteFolder) FavouriteDocument(org.alfresco.rest.api.tests.client.data.FavouriteDocument) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Type(org.alfresco.service.cmr.favourites.FavouritesService.Type) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) List(java.util.List) ArrayList(java.util.ArrayList) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Favourite(org.alfresco.rest.api.tests.client.data.Favourite) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with TenantRunAsWork

use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.

the class TestFavourites method testPUBLICAPI156.

@Test
public void testPUBLICAPI156() throws Exception {
    // person1's public site
    final TestSite publicSite = person1PublicSites.get(0);
    // person1's public site
    final TestSite publicSite1 = person1PublicSites.get(1);
    // person1's folder
    final NodeRef folderNodeRef = person1PublicFolders.get(0);
    // a file in the folder
    final NodeRef nodeRef = person1PublicDocs.get(1);
    final List<Favourite> expectedFavourites = new ArrayList<Favourite>();
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person14Id));
        // invite to another user's public site
        SiteMembershipRequest siteMembershipRequest = new SiteMembershipRequest();
        siteMembershipRequest.setId(publicSite.getSiteId());
        siteMembershipRequest.setMessage("Please can I join your site?");
        siteMembershipRequestsProxy.createSiteMembershipRequest(person14Id, siteMembershipRequest);
        // favourite other users site, folder and file
        Favourite folderFavourite = makeFolderFavourite(folderNodeRef.getId());
        favouritesProxy.createFavourite(person14Id, folderFavourite);
        expectedFavourites.add(folderFavourite);
        Favourite fileFavourite = makeFileFavourite(nodeRef.getId());
        favouritesProxy.createFavourite(person14Id, fileFavourite);
        expectedFavourites.add(fileFavourite);
        final Favourite siteFavourite = makeSiteFavourite(publicSite);
        favouritesProxy.createFavourite(person14Id, siteFavourite);
        expectedFavourites.add(siteFavourite);
        final Favourite siteFavourite1 = makeSiteFavourite(publicSite1);
        favouritesProxy.createFavourite(person14Id, siteFavourite1);
        expectedFavourites.add(siteFavourite1);
        sort(expectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);
        // remove the folder and file
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                repoService.deleteNode(nodeRef);
                repoService.deleteNode(folderNodeRef);
                return null;
            }
        }, person11Id, network1.getId());
        expectedFavourites.remove(folderFavourite);
        expectedFavourites.remove(fileFavourite);
        sort(expectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);
        // GETs should not return the favourites nor error
        {
            try {
                favouritesProxy.getFavourite(person14Id, folderFavourite.getTargetGuid());
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }
            try {
                favouritesProxy.getFavourite(person14Id, fileFavourite.getTargetGuid());
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(), expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person14Id, createParams(paging, null));
            checkList(sublist(expectedFavourites, skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ArrayList(java.util.ArrayList) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) SiteMembershipRequest(org.alfresco.rest.api.tests.client.data.SiteMembershipRequest) Favourite(org.alfresco.rest.api.tests.client.data.Favourite) Test(org.junit.Test)

Example 4 with TenantRunAsWork

use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.

the class TestFavourites method testPUBLICAPI141.

@Test
public void testPUBLICAPI141() throws Exception {
    // person1's public site
    final TestSite publicSite = person1PublicSites.get(0);
    // person1's public site
    final TestSite publicSite1 = person1PublicSites.get(1);
    // person1's private site
    final TestSite privateSite = person1PrivateSites.get(0);
    // person1's folder
    final NodeRef folderNodeRef = person1PublicFolders.get(0);
    // a file in the folder
    final NodeRef nodeRef = person1PublicDocs.get(1);
    final List<Favourite> expectedFavourites = new ArrayList<Favourite>();
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12Id));
        // invite to another user's public site
        SiteMembershipRequest siteMembershipRequest = new SiteMembershipRequest();
        siteMembershipRequest.setId(publicSite.getSiteId());
        siteMembershipRequest.setMessage("Please can I join your site?");
        siteMembershipRequestsProxy.createSiteMembershipRequest(person12Id, siteMembershipRequest);
        // favourite other users site, folder and file
        Favourite folderFavourite = makeFolderFavourite(folderNodeRef.getId());
        favouritesProxy.createFavourite(person12Id, folderFavourite);
        Favourite fileFavourite = makeFileFavourite(nodeRef.getId());
        favouritesProxy.createFavourite(person12Id, fileFavourite);
        final Favourite siteFavourite = makeSiteFavourite(publicSite);
        favouritesProxy.createFavourite(person12Id, siteFavourite);
        expectedFavourites.add(siteFavourite);
        final Favourite siteFavourite1 = makeSiteFavourite(publicSite1);
        favouritesProxy.createFavourite(person12Id, siteFavourite1);
        expectedFavourites.add(siteFavourite1);
        sort(expectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);
        // move the folder and file to person1's private site
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                NodeRef documentLibraryNodeRef = privateSite.getContainerNodeRef("documentLibrary");
                repoService.moveNode(folderNodeRef, documentLibraryNodeRef);
                return null;
            }
        }, person11Id, network1.getId());
        try {
            favouritesProxy.getFavourite(person12Id, folderFavourite.getTargetGuid());
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        try {
            favouritesProxy.getFavourite(person12Id, fileFavourite.getTargetGuid());
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(), expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person12Id, createParams(paging, null));
            checkList(sublist(expectedFavourites, skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
        // make the public sites private
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                publicSite.setSiteVisibility(SiteVisibility.PRIVATE);
                publicSite1.setSiteVisibility(SiteVisibility.PRIVATE);
                return null;
            }
        }, person11Id, network1.getId());
        expectedFavourites.remove(siteFavourite1);
        // Given that person2Id is still a member of 'publicSite', they should still have access and therefore
        // it should show up in their favourites. But person2Id is not a member of 'publicSite1', they should
        // not have access and therefore it should not show up in their favourites.
        {
            Favourite actual = favouritesProxy.getFavourite(person12Id, siteFavourite.getTargetGuid());
            siteFavourite.expected(actual);
            try {
                favouritesProxy.getFavourite(person12Id, siteFavourite1.getTargetGuid());
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(), expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person12Id, createParams(paging, null));
            checkList(sublist(expectedFavourites, skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
        }
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ArrayList(java.util.ArrayList) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) SiteMembershipRequest(org.alfresco.rest.api.tests.client.data.SiteMembershipRequest) Favourite(org.alfresco.rest.api.tests.client.data.Favourite) Test(org.junit.Test)

Example 5 with TenantRunAsWork

use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.

the class TestPersonSites method initializeSites.

// TODO switch to use V1 createSite (instead of RepoService)
private 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<Void>() {

        @Override
        public Void doWork() throws Exception {
            person31 = network1.createUser();
            person32 = network1.createUser();
            return null;
        }
    }, network1.getId());
    this.site1 = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(site1_id, 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<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(site2_id, 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<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(site3_id, site3_title, site3_title, SiteVisibility.PRIVATE);
            TestSite site = network1.createSite(siteInfo);
            site.inviteToSite(person32.getId(), site3_role);
            return site;
        }
    }, person31.getId(), network1.getId());
}
Also used : SiteInformation(org.alfresco.rest.api.tests.RepoService.SiteInformation) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException)

Aggregations

TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)24 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)17 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)17 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)17 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)16 List (java.util.List)12 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)11 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)11 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)11 NodeRef (org.alfresco.service.cmr.repository.NodeRef)10 ListResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse)8 SiteInformation (org.alfresco.rest.api.tests.RepoService.SiteInformation)7 MemberOfSite (org.alfresco.rest.api.tests.client.data.MemberOfSite)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Sites (org.alfresco.rest.api.tests.client.PublicApiClient.Sites)5 SiteMembershipRequest (org.alfresco.rest.api.tests.client.data.SiteMembershipRequest)5 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)4 ParseException (java.text.ParseException)3