From 2cca787161bffd9c1ce1d25049cb6ed450194930 Mon Sep 17 00:00:00 2001 From: "meenal.bhave@alfresco.com" Date: Mon, 11 Feb 2019 09:34:41 +0000 Subject: [PATCH] Search-1359: Tests added to cover Search Range tests for text fields --- .../search/AbstractSearchServiceE2E.java | 38 +++- .../search/SearchWithCustomModelTest.java | 211 ++++++++++++++++++ e2e-test/src/test/resources/SearchSuite.xml | 2 + 3 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 e2e-test/src/test/java/org/alfresco/service/search/SearchWithCustomModelTest.java diff --git a/e2e-test/src/test/java/org/alfresco/service/search/AbstractSearchServiceE2E.java b/e2e-test/src/test/java/org/alfresco/service/search/AbstractSearchServiceE2E.java index 9e1193614..147bceefd 100644 --- a/e2e-test/src/test/java/org/alfresco/service/search/AbstractSearchServiceE2E.java +++ b/e2e-test/src/test/java/org/alfresco/service/search/AbstractSearchServiceE2E.java @@ -9,6 +9,7 @@ package org.alfresco.service.search; import org.alfresco.cmis.CmisWrapper; import org.alfresco.dataprep.ContentService; +import org.alfresco.dataprep.SiteService.Visibility; import org.alfresco.rest.core.RestProperties; import org.alfresco.rest.core.RestWrapper; import org.alfresco.rest.search.RestRequestQueryModel; @@ -20,7 +21,10 @@ import org.alfresco.utility.Utility; import org.alfresco.utility.data.DataContent; import org.alfresco.utility.data.DataSite; import org.alfresco.utility.data.DataUser; +import org.alfresco.utility.data.RandomData; import org.alfresco.utility.model.FileModel; +import org.alfresco.utility.model.SiteModel; +import org.alfresco.utility.model.UserModel; import org.alfresco.utility.network.ServerHealth; import org.apache.chemistry.opencmis.client.api.CmisObject; import org.apache.chemistry.opencmis.client.api.Session; @@ -71,6 +75,12 @@ public abstract class AbstractSearchServiceE2E extends AbstractTestNGSpringConte @Autowired @Getter(value = PROTECTED) private ContentService contentService; + + protected UserModel testUser, adminUserModel; + protected SiteModel testSite; + protected UserModel searchedUser; + + protected static String unique_searchString; public static final String NODE_PREFIX = "workspace/SpacesStore/"; @@ -88,6 +98,16 @@ public abstract class AbstractSearchServiceE2E extends AbstractTestNGSpringConte { LOG.warn("Error Loading Custom Model", e); } + + adminUserModel = dataUser.getAdminUser(); + testUser = dataUser.createRandomTestUser("UserSearch"); + + testSite = new SiteModel(RandomData.getRandomName("SiteSearch")); + testSite.setVisibility(Visibility.PRIVATE); + + testSite = dataSite.usingUser(testUser).createSite(testSite); + + unique_searchString = testSite.getTitle().replace("SiteSearch", "Unique"); } public boolean deployCustomModel(String path) @@ -257,17 +277,31 @@ public abstract class AbstractSearchServiceE2E extends AbstractTestNGSpringConte } /** - * Run a search and return the response + * Run a search as admin user and return the response + * * @param queryString: string to search for, unique search string will guarantee accurate results * @return the search response from the API * @throws Exception */ public SearchResponse query(String queryString) throws Exception + { + return queryWithUser(dataUser.getAdminUser(), queryString); + } + + /** + * Run a search as given user and return the response + * + * @param user: UserModel for the user you wish to run the query as + * @param queryString: string to search for, unique search string will guarantee accurate results + * @return the search response from the API + * @throws Exception + */ + public SearchResponse queryWithUser(UserModel user, String queryString) throws Exception { SearchRequest searchRequest = new SearchRequest(); RestRequestQueryModel queryModel = new RestRequestQueryModel(); queryModel.setQuery(queryString); searchRequest.setQuery(queryModel); - return restClient.authenticateUser(dataUser.getAdminUser()).withSearchAPI().search(searchRequest); + return restClient.authenticateUser(user).withSearchAPI().search(searchRequest); } } diff --git a/e2e-test/src/test/java/org/alfresco/service/search/SearchWithCustomModelTest.java b/e2e-test/src/test/java/org/alfresco/service/search/SearchWithCustomModelTest.java new file mode 100644 index 000000000..49ca6c158 --- /dev/null +++ b/e2e-test/src/test/java/org/alfresco/service/search/SearchWithCustomModelTest.java @@ -0,0 +1,211 @@ +/* + * 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.service.search; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.rest.search.SearchResponse; +import org.alfresco.service.search.AbstractSearchServiceE2E; +import org.alfresco.utility.constants.UserRole; +import org.alfresco.utility.data.DataContent; +import org.alfresco.utility.data.DataSite; +import org.alfresco.utility.model.FileModel; +import org.alfresco.utility.model.FileType; +import org.alfresco.utility.model.FolderModel; +import org.alfresco.utility.model.TestGroup; +import org.apache.chemistry.opencmis.commons.PropertyIds; +import org.apache.chemistry.opencmis.commons.enums.VersioningState; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * Purpose of this TestClass is to test that the search range query tests work as expected with CustomModels + * Tests added for Search-1359 + * + * @author meenal bhave + */ + +public class SearchWithCustomModelTest extends AbstractSearchServiceE2E +{ + @Autowired + protected DataSite dataSite; + + @Autowired + protected DataContent dataContent; + + private FolderModel testFolder; + + private FileModel expenseLondon, expenseParis, expenseNoLocation; + + @BeforeClass(alwaysRun = true) + public void setupEnvironment() throws Exception + { + serverHealth.assertServerIsOnline(); + + dataUser.addUserToSite(testUser, testSite, UserRole.SiteContributor); + + testFolder = dataContent.usingSite(testSite).usingUser(testUser).createFolder(); + + Long uniqueRef = System.currentTimeMillis(); + + expenseLondon = FileModel.getRandomFileModel(FileType.TEXT_PLAIN, "Expense"); + expenseLondon.setName("fin1-" + expenseLondon.getName()); + + Map properties = new HashMap(); + properties.put(PropertyIds.OBJECT_TYPE_ID, "D:finance:Expense"); + properties.put(PropertyIds.NAME, expenseLondon.getName()); + properties.put("finance:No", uniqueRef); + properties.put("finance:amount", 300); + + cmisApi.authenticateUser(testUser).usingSite(testSite).usingResource(testFolder).createFile(expenseLondon, properties, VersioningState.MAJOR) + .assertThat().existsInRepo(); + + // Location value is set to London + cmisApi.authenticateUser(testUser).usingResource(expenseLondon).addSecondaryTypes("P:finance:ParkEx").assertThat() + .secondaryTypeIsAvailable("P:finance:ParkEx"); + cmisApi.authenticateUser(testUser).usingResource(expenseLondon).updateProperty("finance:Location", "London"); + + expenseParis = FileModel.getRandomFileModel(FileType.TEXT_PLAIN, "Expense"); + expenseParis.setName("fin2-" + expenseParis.getName()); + + properties = new HashMap(); + properties.put(PropertyIds.OBJECT_TYPE_ID, "D:finance:Expense"); + properties.put(PropertyIds.NAME, expenseParis.getName()); + properties.put("finance:No", uniqueRef + 1); + properties.put("finance:amount", 100); + properties.put("finance:Title", "Airport Taxi Outgoing"); + properties.put("finance:Emp", "David A"); + + cmisApi.authenticateUser(testUser).usingSite(testSite).usingResource(testFolder).createFile(expenseParis, properties, VersioningState.MAJOR) + .assertThat().existsInRepo(); + + // Location value is set to Paris + cmisApi.authenticateUser(testUser).usingResource(expenseParis).addSecondaryTypes("P:finance:ParkEx").assertThat() + .secondaryTypeIsAvailable("P:finance:ParkEx"); + cmisApi.authenticateUser(testUser).usingResource(expenseParis).updateProperty("finance:Location", "Paris"); + + expenseNoLocation = FileModel.getRandomFileModel(FileType.TEXT_PLAIN, "receipt"); + expenseNoLocation.setName("fin3-" + expenseNoLocation.getName()); + + properties = new HashMap(); + properties.put(PropertyIds.OBJECT_TYPE_ID, "D:finance:Expense"); + properties.put(PropertyIds.NAME, expenseNoLocation.getName()); + properties.put("finance:No", uniqueRef + 2); + properties.put("finance:amount", 0); + properties.put("finance:Title", "Hotel Stay"); + properties.put("finance:Emp", "Daniel S"); + + cmisApi.authenticateUser(testUser).usingSite(testSite).usingResource(testFolder).createFile(expenseNoLocation, properties, VersioningState.MAJOR) + .assertThat().existsInRepo(); + + // Location value is set to null + cmisApi.authenticateUser(testUser).usingResource(expenseNoLocation).addSecondaryTypes("P:finance:ParkEx").assertThat() + .secondaryTypeIsAvailable("P:finance:ParkEx"); + + // Wait for the file to be indexed + waitForIndexing(expenseNoLocation.getName(), true); + } + + // Search-1359: Search AFTS Query with Range + @Test(priority = 1, groups = { TestGroup.ASS_14 }) + public void testRangeQueryTextField() throws Exception + { + // Search Range Query + SearchResponse response = queryWithUser(testUser, "finance_Location:[* TO London]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + + // Content where Location = London is returned, If property is not set, its ignored. + Assert.assertEquals(response.getPagination().getCount(), 1); + + response = queryWithUser(testUser, "finance_Location:[London TO *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + // Content where Location = London, Paris is returned, If property is not set, its ignored. + Assert.assertEquals(response.getPagination().getCount(), 2); + + response = queryWithUser(testUser, "finance_Location:[London To Paris]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + + response = queryWithUser(testUser, "finance_Location:[* To *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + } + + @Test(priority = 2, groups = { TestGroup.ASS_14 }) + public void testRangeQueryTextFieldWhiteSpace() throws Exception + { + SearchResponse response = queryWithUser(testUser, "finance:Emp:[* TO Daniel]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + // TODO: Uncomment when fixed + // Assert.assertEquals(response.getPagination().getCount(), 1); //2 + + response = queryWithUser(testUser, "finance:Emp:[Dan TO *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + + response = queryWithUser(testUser, "finance:Emp:[Dan To David]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + + response = queryWithUser(testUser, "finance:Emp:[* To *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + } + + @Test(priority = 3, groups = { TestGroup.ASS_14 }) + public void testRangeQueryTextFieldNonFacetable() throws Exception + { + // Search Range Query + SearchResponse response = queryWithUser(testUser, "finance:Title:[* TO H]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 1); + + response = queryWithUser(testUser, "finance:Title:[Hotel TO *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + // TODO: Uncomment when fixed + // Assert.assertEquals(response.getPagination().getCount(), 1); //2 + + response = queryWithUser(testUser, "finance:Title:[B To H]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + // TODO: Uncomment when fixed + // Assert.assertEquals(response.getPagination().getCount(), 1); // H, H* return 0 + + response = queryWithUser(testUser, "finance:Title:[B To I]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 1); + + response = queryWithUser(testUser, "finance:Title:[* To *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + } + + @Test(priority = 4, groups = { TestGroup.ASS_14 }) + public void testRangeQueryDoubleField() throws Exception + { + // Search Range Query + SearchResponse response = queryWithUser(testUser, "finance:amount:[* TO 100]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + + response = queryWithUser(testUser, "finance_amount:[100 TO *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + + response = queryWithUser(testUser, "finance_amount:[100 To 300]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 2); + + response = queryWithUser(testUser, "finance_amount:[* To *]"); + restClient.assertStatusCodeIs(HttpStatus.OK); + Assert.assertEquals(response.getPagination().getCount(), 3); + } +} diff --git a/e2e-test/src/test/resources/SearchSuite.xml b/e2e-test/src/test/resources/SearchSuite.xml index 70393bfca..29aecd851 100644 --- a/e2e-test/src/test/resources/SearchSuite.xml +++ b/e2e-test/src/test/resources/SearchSuite.xml @@ -23,7 +23,9 @@ + +