diff --git a/e2e-test/src/test/java/org/alfresco/test/search/functional/AbstractE2EFunctionalTest.java b/e2e-test/src/test/java/org/alfresco/test/search/functional/AbstractE2EFunctionalTest.java index d65cfac7c..601fee8d7 100644 --- a/e2e-test/src/test/java/org/alfresco/test/search/functional/AbstractE2EFunctionalTest.java +++ b/e2e-test/src/test/java/org/alfresco/test/search/functional/AbstractE2EFunctionalTest.java @@ -22,6 +22,7 @@ import org.alfresco.dataprep.SiteService.Visibility; import org.alfresco.rest.core.RestProperties; import org.alfresco.rest.core.RestWrapper; import org.alfresco.rest.model.RestRequestSpellcheckModel; +import org.alfresco.rest.search.Pagination; import org.alfresco.rest.search.RestRequestHighlightModel; import org.alfresco.rest.search.RestRequestQueryModel; import org.alfresco.rest.search.SearchRequest; @@ -394,10 +395,15 @@ public abstract class AbstractE2EFunctionalTest extends AbstractTestNGSpringCont return restClient.authenticateUser(user).withSearchAPI().search(searchRequest); } - protected SearchResponse queryAsUser(UserModel user, RestRequestQueryModel queryModel) + protected SearchResponse queryAsUser(UserModel user, RestRequestQueryModel queryModel, Pagination paging) { SearchRequest searchRequest = new SearchRequest(); searchRequest.setQuery(queryModel); + + if (ofNullable(paging).isPresent()) + { + searchRequest.setPaging(paging); + } return restClient.authenticateUser(user).withSearchAPI().search(searchRequest); } @@ -437,7 +443,7 @@ public abstract class AbstractE2EFunctionalTest extends AbstractTestNGSpringCont */ protected SearchResponse testSearchQuery(String query, Integer expectedCount, SearchLanguage queryLanguage) { - SearchResponse response = performSearch(testUser, query, queryLanguage); + SearchResponse response = performSearch(testUser, query, queryLanguage, getDefaultPagingOptions()); if (ofNullable(expectedCount).isPresent()) { @@ -456,7 +462,7 @@ public abstract class AbstractE2EFunctionalTest extends AbstractTestNGSpringCont */ protected SearchResponse testSearchQueryOrdered(String query, List expectedNames, SearchLanguage queryLanguage) { - SearchResponse response = performSearch(testUser, query, queryLanguage); + SearchResponse response = performSearch(testUser, query, queryLanguage, getDefaultPagingOptions()); List names = response.getEntries().stream().map(s -> s.getModel().getName()).collect(Collectors.toList()); @@ -475,7 +481,7 @@ public abstract class AbstractE2EFunctionalTest extends AbstractTestNGSpringCont */ protected SearchResponse testSearchQueryUnordered(String query, Set expectedNames, SearchLanguage queryLanguage) { - SearchResponse response = performSearch(testUser, query, queryLanguage); + SearchResponse response = performSearch(testUser, query, queryLanguage, getDefaultPagingOptions()); Set names = response.getEntries().stream().map(s -> s.getModel().getName()).collect(Collectors.toSet()); @@ -484,7 +490,7 @@ public abstract class AbstractE2EFunctionalTest extends AbstractTestNGSpringCont return response; } - private SearchResponse performSearch(UserModel asUser, String query, SearchLanguage queryLanguage) + protected SearchResponse performSearch(UserModel asUser, String query, SearchLanguage queryLanguage, Pagination paging) { RestRequestQueryModel queryModel = new RestRequestQueryModel(); queryModel.setQuery(query); @@ -499,11 +505,49 @@ public abstract class AbstractE2EFunctionalTest extends AbstractTestNGSpringCont queryModel.setLanguage(queryLanguage.toString()); } - SearchResponse response = queryAsUser(asUser, queryModel); + SearchResponse response = queryAsUser(asUser, queryModel, paging); return response; } + /** + * Returns pagination object with alfresco default settings + * Sets skipCount = 0, maxItems = 100 + * + * @return + */ + private Pagination getDefaultPagingOptions() + { + Pagination paging = new Pagination(); + paging.setSkipCount(0); + paging.setMaxItems(100); + + return paging; + } + + /** + * Set the pagination options for the API query + * @param skipCount Integer + * @param maxItems Integer + * @return + */ + protected Pagination setPaging(Integer skipCount, Integer maxItems) + { + Pagination paging = new Pagination(); + + if (ofNullable(skipCount).isPresent()) + { + paging.setSkipCount(skipCount); + } + + if (ofNullable(maxItems).isPresent()) + { + paging.setMaxItems(maxItems); + } + + return paging; + } + /** * Method to create and run a simple spellcheck query * When a spellcheck query is run a user, the query inputed and the user query is inputted diff --git a/e2e-test/src/test/java/org/alfresco/test/search/functional/searchServices/search/SearchQueryPaginationTest.java b/e2e-test/src/test/java/org/alfresco/test/search/functional/searchServices/search/SearchQueryPaginationTest.java new file mode 100644 index 000000000..92ab67fb7 --- /dev/null +++ b/e2e-test/src/test/java/org/alfresco/test/search/functional/searchServices/search/SearchQueryPaginationTest.java @@ -0,0 +1,258 @@ +/* + * Copyright 2019 Alfresco Software, Ltd. All rights reserved. + * License rights for this program may be obtained from Alfresco Software, Ltd. + * pursuant to a written agreement and any use of this program without such an + * agreement is prohibited. + */ + +package org.alfresco.test.search.functional.searchServices.search; + +import org.alfresco.rest.search.SearchResponse; +import org.alfresco.test.search.functional.searchServices.cmis.AbstractCmisE2ETest; +import org.alfresco.utility.model.FileModel; +import org.alfresco.utility.model.FileType; +import org.alfresco.utility.model.FolderModel; +import org.alfresco.utility.model.UserModel; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * The purpose of this test is to test search query pagination using cmis and afts query + * + * @author Meenal Bhave + */ +public class SearchQueryPaginationTest extends AbstractCmisE2ETest +{ + private UserModel testUser2; + private FolderModel testFolder ; + private FileModel testFile; + + @BeforeClass(alwaysRun = true) + public void dataPreparation() throws Exception + { + // Create testUser2: This user does not have access to the testSite + testUser2 = dataUser.createRandomTestUser("testUser2"); + + // Create a new folder and 10 files inside the folder + testFolder = dataContent.usingUser(testUser).usingSite(testSite).createFolder(); + + for (int i = 0; i < 10; i++) + { + testFile = new FileModel(i + "-File.txt", unique_searchString, "", FileType.TEXT_PLAIN); + dataContent.usingUser(testUser).usingSite(testSite).usingResource(testFolder).createContent(testFile); + } + + waitForMetadataIndexing(testFile.getName(), true); + } + + @Test(priority = 1) + public void testCmisSearchWithPagination() + { + // Search for the cmis documents using cmis query + String query = "select * from cmis:document"; + + // Set skipCount = 0, maxItems = 1000 + SearchResponse response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(0, 1000)); + + // Get getTotalItems, Expect hasModeItems = false + Integer totalDocuments = response.getPagination().getTotalItems(); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = totalDocument, maxItems = 1000 + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(totalDocuments, 1000)); + + // Check getTotalItems is the same, hasMoreItems = false + Assert.assertEquals(response.getPagination().getTotalItems(), totalDocuments, "Total Document Count doesn't match"); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 0, maxItems = totalDocuments/2 + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(0, totalDocuments/2)); + + // Check getTotalItems is the same, hasMoreItems = True + Assert.assertEquals(response.getPagination().getTotalItems(), totalDocuments, "Total Document Count doesn't match"); + Assert.assertTrue(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 1, maxItems = totalDocuments-2 + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(1, totalDocuments-2)); + + // Check getTotalItems is the same, hasMoreItems = True + Assert.assertEquals(response.getPagination().getTotalItems(), totalDocuments, "Total Document Count doesn't match"); + Assert.assertTrue(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = totalDocuments, maxItems = totalDocuments/2 + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(totalDocuments, totalDocuments/2)); + + // Check getTotalItems is the same, hasMoreItems = false + Assert.assertEquals(response.getPagination().getTotalItems(), totalDocuments, "Total Document Count doesn't match"); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 0, maxItems = totalDocuments + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(0, totalDocuments)); + + // Check getTotalItems is the same, hasMoreItems = false + Assert.assertEquals(response.getPagination().getTotalItems(), totalDocuments, "Total Document Count doesn't match"); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = totalDocuments/2+1, maxItems = totalDocuments + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(totalDocuments/2+1, totalDocuments)); + + // Check getTotalItems is the same, hasMoreItems = false + Assert.assertEquals(response.getPagination().getTotalItems(), totalDocuments, "Total Document Count doesn't match"); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = totalDocuments, maxItems = totalDocuments + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(totalDocuments, totalDocuments)); + + // Check getTotalItems is the same, hasMoreItems = false + Assert.assertEquals(response.getPagination().getTotalItems(), totalDocuments, "Total Document Count doesn't match"); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 0, maxItems = 0 + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(0, 0)); + Assert.assertTrue(response.isEmpty(), "Empty Response, Error is expected when maxItems is <= 0"); + + // Set skipCount = totalDocuments, maxItems = 0 + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(totalDocuments, 0)); + Assert.assertTrue(response.isEmpty(), "Empty Response, Error is expected when maxItems <= 0"); + + // Set skipCount = -1, maxItems = 1 + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(-1, 1)); + Assert.assertTrue(response.isEmpty(), "Empty Response, Error is expected when skipCount < 0"); + } + + @Test(priority = 2) + public void testPagination() + { + // Search for the files under the testFolder using cmis query + String parentId = testFolder.getNodeRefWithoutVersion(); + String query = "select * from cmis:document where IN_FOLDER('" + parentId + "')"; + + // Set skipCount = 0, maxItems = 100 + SearchResponse response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(0, 100)); + + // Check getTotalItems = 10, Expect hasModeItems = false + testPaginationDetails(response, 10, 0, 100); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 0, maxItems = 10: Expect hasModeItems = false + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(0, 10)); + + testPaginationDetails(response, 10, 0, 10); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 0, maxItems = 5: Expect hasModeItems = true + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(0, 5)); + + testPaginationDetails(response, 10, 0, 5); + Assert.assertTrue(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 2, maxItems = 10: Expect hasModeItems = false + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(2, 10)); + + testPaginationDetails(response, 10, 2, 10); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 2, maxItems = 7: Expect hasModeItems = true + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(2, 7)); + + testPaginationDetails(response, 10, 2, 7); + Assert.assertTrue(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 2, maxItems = 8: Expect hasModeItems = false + response = performSearch(testUser, query, SearchLanguage.CMIS, setPaging(2, 8)); + + testPaginationDetails(response, 10, 2, 8); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + } + + @Test(priority = 3) + public void testPaginationRespectsACLs() + { + // Search for the files under the testFolder using cmis query + String parentId = testFolder.getNodeRefWithoutVersion(); + String query = "select * from cmis:document where IN_FOLDER('" + parentId + "')"; + + // Set skipCount = 0, maxItems = 100 + SearchResponse response = performSearch(testUser2, query, SearchLanguage.CMIS, setPaging(0, 100)); + + // Get getTotalItems, Expect hasModeItems = false + testPaginationDetails(response, 0, 0, 100); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 1, maxItems = 1 + response = performSearch(testUser2, query, SearchLanguage.CMIS, setPaging(1, 1)); + + // Get getTotalItems, Expect hasModeItems = false + testPaginationDetails(response, 0, 1, 1); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + } + + @Test(priority = 4) + public void testSearchApiPagination() + { + // Search for the files with specific title + String query = "cm:title:'" + unique_searchString + "'"; + + // Set skipCount = 0, maxItems = 100 + SearchResponse response = performSearch(testUser, query, SearchLanguage.AFTS, setPaging(0, 100)); + + // Get getTotalItems, Expect hasModeItems = false + testPaginationDetails(response, 10, 0, 100); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 1, maxItems = 1 + response = performSearch(testUser, query, SearchLanguage.AFTS, setPaging(1, 1)); + + // Get getTotalItems, Expect hasModeItems = true + testPaginationDetails(response, 10, 1, 1); + Assert.assertTrue(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 9, maxItems = 10 + response = performSearch(testUser, query, SearchLanguage.AFTS, setPaging(9, 10)); + + // Get getTotalItems, Expect hasModeItems = false + testPaginationDetails(response, 10, 9, 10); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 10, maxItems = 10 + response = performSearch(testUser, query, SearchLanguage.AFTS, setPaging(10, 10)); + + // Get getTotalItems, Expect hasModeItems = false + testPaginationDetails(response, 10, 10, 10); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + } + + @Test(priority = 5) + public void testSearchApiPaginationRespectsACLs() + { + // Search for the files with specific title + String query = "cm:title:'" + unique_searchString + "'"; + + // Set skipCount = 0, maxItems = 100 + SearchResponse response = performSearch(testUser2, query, SearchLanguage.AFTS, setPaging(0, 100)); + + // Get getTotalItems, Expect hasModeItems = false + testPaginationDetails(response, 0, 0, 100); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + + // Set skipCount = 1, maxItems = 1 + response = performSearch(testUser2, query, SearchLanguage.AFTS, setPaging(1, 1)); + + // Get getTotalItems, Expect hasModeItems = false + testPaginationDetails(response, 0, 1, 1); + Assert.assertFalse(response.getPagination().isHasMoreItems(), "Incorrect: hasMoreItems"); + } + + private void testPaginationDetails(SearchResponse response, int expectedTotalCount, int skipCount, int maxItems) + { + Assert.assertEquals(response.getPagination().getTotalItems().intValue(), expectedTotalCount, "Unexpected document count"); + + Assert.assertEquals(response.getPagination().getSkipCount(), skipCount, "Unexpected skip count returned"); + Assert.assertEquals(response.getPagination().getMaxItems(), maxItems, "Unexpected maxItems returned"); + + // count = (total-skipCount) < maxItems ? total-skipCount: maxItems + int expectedCount = (expectedTotalCount < skipCount) ? 0 : (expectedTotalCount-skipCount) < maxItems ? expectedTotalCount-skipCount : maxItems; + Assert.assertEquals(response.getPagination().getCount(), expectedCount, "Unexpected document count"); + } +}