Search-1346: Moved the FacetFields test to its own class, so that the tests could be split meaningfully

This commit is contained in:
Meenal Bhave
2019-01-11 17:02:44 +00:00
parent e443eddce4
commit 68f51416f4
3 changed files with 247 additions and 141 deletions
@@ -0,0 +1,246 @@
/*
* Copyright (C) 2019 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.rest.search;
import java.util.ArrayList;
import java.util.List;
import javax.json.Json;
import javax.json.JsonObject;
import org.alfresco.dataprep.SiteService.Visibility;
import org.alfresco.utility.data.RandomData;
import org.alfresco.utility.model.FileModel;
import org.alfresco.utility.model.FileType;
import org.alfresco.utility.model.FolderModel;
import org.alfresco.utility.model.SiteModel;
import org.alfresco.utility.model.TestGroup;
import org.alfresco.utility.model.UserModel;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Faceted search test with FacetFields
* @author Meenal Bhave
*
*/
public class FacetFieldsSearchTest extends AbstractSearchTest
{
private UserModel userWithNoAccess, userCanAccessTextFile;
private SiteModel testSite;
private FolderModel testFolder;
private FileModel textFile, htmlFile;
private String fname;
@BeforeClass(alwaysRun = true)
public void setupEnvironment() throws Exception
{
serverHealth.assertServerIsOnline();
fname = unique_searchString + "facet";
testSite = new SiteModel(RandomData.getRandomName("SiteSearch"));
testSite.setVisibility(Visibility.PRIVATE);
testSite = dataSite.usingUser(userModel).createSite(testSite);
// Create another user who would not have access to the Private Site created by userModel
userWithNoAccess = dataUser.createRandomTestUser("UserSearch2");
userCanAccessTextFile = dataUser.createRandomTestUser("UserSearch3");
testFolder = dataContent.usingSite(testSite).usingUser(userModel).createFolder();
// Create a folder and a file as test User
FolderModel folder = new FolderModel(fname);
dataContent.usingUser(userModel).usingSite(testSite).createFolder(folder);
textFile = new FileModel(fname + "-1.txt", fname, fname, FileType.TEXT_PLAIN, fname + " file for search ");
dataContent.usingUser(userModel).usingSite(testSite).createContent(textFile);
htmlFile = new FileModel(fname + "-2.html", fname, fname, FileType.HTML, fname + " file 2 for search ");
dataContent.usingUser(userModel).usingSite(testSite).createContent(htmlFile);
// Set Node Permissions to allow access for user3 user for text File
JsonObject userPermission = Json.createObjectBuilder()
.add("permissions", Json.createObjectBuilder().add("isInheritanceEnabled", false)
.add("locallySet",Json.createObjectBuilder().add("authorityId", userCanAccessTextFile.getUsername())
.add("name", "SiteConsumer").add("accessStatus", "ALLOWED")))
.build();
String putBody = userPermission.toString();
restClient.authenticateUser(userModel).withCoreAPI().usingNode(textFile).updateNode(putBody);
// Wait for the file to be indexed
waitForIndexing(htmlFile.getName(), true);
}
@Test(groups = { TestGroup.REST_API, TestGroup.SEARCH, TestGroup.ASS_13 })
public void testSearchFacetFieldsBucketExcludedWhenMinCount2() throws Exception
{
// Create Query with FacetFields: Site and Content MimeType
SearchRequest query = new SearchRequest();
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("name:" + fname);
query.setQuery(queryReq);
RestRequestFacetFieldsModel facetFields = new RestRequestFacetFieldsModel();
List<RestRequestFacetFieldModel> facets = new ArrayList<RestRequestFacetFieldModel>();
facets.add(new RestRequestFacetFieldModel("SITE", "SEARCH.FACET_FIELDS.SITE", 0)); // MinCount = 0
facets.add(new RestRequestFacetFieldModel("cm:content.mimetype", "Mimetype", 2)); // MinCount = 2
facetFields.setFacets(facets);
query.setFacetFields(facetFields);
// Search query using user who created site: Expect Only 1 Facet bucket to be retrieved
SearchResponse response = query(query);
Assert.assertFalse(response.getContext().getFacetsFields().isEmpty());
Assert.assertEquals(response.getContext().getFacetsFields().size(), 1);
RestResultBucketsModel facetFieldList = response.getContext().getFacetsFields().get(0);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELDS.SITE");
Assert.assertEquals(facetFieldList.getBuckets().size(), 1); // MimeType bucket won't be shown as minCount = 2 won't be reached
FacetFieldBucket bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is(testSite.getId());
bucket1.assertThat().field("filterQuery").contains(testSite.getId());
bucket1.assertThat().field("count").is(3); // One folder and 2 files created above
}
@Test(groups = { TestGroup.REST_API, TestGroup.SEARCH, TestGroup.ASS_13 })
public void testSearchWithFacetFieldsMinCountChecks() throws Exception
{
// MinCount 1 or not set: Defaults to 1
SearchRequest query = new SearchRequest();
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("name:" + fname);
query.setQuery(queryReq);
RestRequestFacetFieldsModel facetFields = new RestRequestFacetFieldsModel();
List<RestRequestFacetFieldModel> facets = new ArrayList<RestRequestFacetFieldModel>();
facets.add(new RestRequestFacetFieldModel("SITE", "SEARCH.FACET_FIELD1.SITE", null)); // MinCount Not set
facets.add(new RestRequestFacetFieldModel("cm:content.mimetype", "SEARCH.FACET_FIELD2.Mimetype", 1)); // MinCount = 1
facetFields.setFacets(facets);
query.setFacetFields(facetFields);
// Search query using user who created site: Expect 2 Facet buckets to be retrieved
SearchResponse response = query(query);
List<RestResultBucketsModel> facetFieldBucketsList = response.getContext().getFacetsFields();
Assert.assertEquals(facetFieldBucketsList.size(), 2, "FacetField");
RestResultBucketsModel facetFieldList = facetFieldBucketsList.get(0);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD1.SITE");
FacetFieldBucket bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is(testSite.getId());
bucket1.assertThat().field("filterQuery").contains(testSite.getId());
bucket1.assertThat().field("count").is(3);
facetFieldList = facetFieldBucketsList.get(1);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD2.Mimetype");
Assert.assertEquals(facetFieldList.getBuckets().size(), 2); // MimeType bucket will be shown with 2 buckets
bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is("text/html");
bucket1.assertThat().field("filterQuery").contains("text/html");
bucket1.assertThat().field("count").is(1);
bucket1.assertThat().field("display").is("HTML");
bucket1 = facetFieldList.getBuckets().get(1);
bucket1.assertThat().field("label").is("text/plain");
bucket1.assertThat().field("filterQuery").contains("text/plain");
bucket1.assertThat().field("count").is(1);
bucket1.assertThat().field("display").is("Plain Text");
}
@Test(groups = { TestGroup.REST_API, TestGroup.SEARCH, TestGroup.ASS_13 })
public void testSearchWithFacetFieldsNoFacetsWhenNoAccess() throws Exception
{
SearchRequest query = new SearchRequest();
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("name:" + fname);
query.setQuery(queryReq);
RestRequestFacetFieldsModel facetFields = new RestRequestFacetFieldsModel();
List<RestRequestFacetFieldModel> facets = new ArrayList<RestRequestFacetFieldModel>();
facets.add(new RestRequestFacetFieldModel("SITE", "SEARCH.FACET_FIELD1.SITE", null)); // MinCount Not set
facets.add(new RestRequestFacetFieldModel("cm:content.mimetype", "SEARCH.FACET_FIELD2.Mimetype", 1)); // MinCount = 1
facetFields.setFacets(facets);
query.setFacetFields(facetFields);
// Search query using other user: No access hence no buckets expected
SearchResponse response = restClient.authenticateUser(userWithNoAccess).withSearchAPI().search(query);
Assert.assertNull(response.getContext().getFacetsFields());
}
@Test(groups = { TestGroup.REST_API, TestGroup.SEARCH, TestGroup.ASS_13 })
public void testSearchWithFacetFieldsOnlyFacetsWhereAccess() throws Exception
{
SearchRequest query = new SearchRequest();
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("name:" + fname);
query.setQuery(queryReq);
RestRequestFacetFieldsModel facetFields = new RestRequestFacetFieldsModel();
List<RestRequestFacetFieldModel> facets = new ArrayList<RestRequestFacetFieldModel>();
facets.add(new RestRequestFacetFieldModel("SITE", "SEARCH.FACET_FIELD1.SITE", null)); // MinCount Not set
facets.add(new RestRequestFacetFieldModel("cm:content.mimetype", "SEARCH.FACET_FIELD2.Mimetype", 1)); // MinCount = 1
facetFields.setFacets(facets);
query.setFacetFields(facetFields);
// Search query using user3
SearchResponse response = restClient.authenticateUser(userCanAccessTextFile).withSearchAPI().search(query);
List<RestResultBucketsModel> facetFieldBucketsList = response.getContext().getFacetsFields();
Assert.assertFalse(facetFieldBucketsList.isEmpty());
Assert.assertEquals(facetFieldBucketsList.size(), 2);
// Check FacetField 1
RestResultBucketsModel facetFieldList = facetFieldBucketsList.get(0);
// User3 has granular permissions to content within this private site, so expect the Site bucket
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD1.SITE");
FacetFieldBucket bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is(testSite.getId());
bucket1.assertThat().field("filterQuery").contains(testSite.getId());
bucket1.assertThat().field("count").is(1);
// Check FacetField 2
facetFieldList = facetFieldBucketsList.get(1);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD2.Mimetype");
Assert.assertEquals(facetFieldList.getBuckets().size(), 1); // MimeType bucket will be shown with 1 bucket only
// User3 has access to text file alone, so expect bucket for text/plain and not for html content.
bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is("text/plain");
bucket1.assertThat().field("label").isNot("text/html");
bucket1.assertThat().field("filterQuery").contains("text/plain");
bucket1.assertThat().field("count").is(1);
bucket1.assertThat().field("display").is("Plain Text");
}
}
@@ -315,145 +315,4 @@ public class FacetedSearchTest extends AbstractSearchTest
bucket1.assertThat().field("filterQuery").is("modifier:\"" + userModel.getUsername() + "\"");
bucket1.assertThat().field("metrics").is("[{entry=null, type=count, value={count=1}}]");
}
@Test(groups = { TestGroup.REST_API, TestGroup.SEARCH, TestGroup.ASS_13 })
public void searchWithFactedFieldsWithACLs() throws Exception
{
String fname = unique_searchString + "facet";
// Create another user who would not have access to the Private Site created by userModel
UserModel userWithNoAccess = dataUser.createRandomTestUser("UserSearch2");
UserModel userCanAccessTextFile = dataUser.createRandomTestUser("UserSearch3");
// Create a folder and a file as test User
FolderModel folder = new FolderModel(fname);
dataContent.usingUser(userModel).usingSite(siteModel).createFolder(folder);
FileModel file = new FileModel(fname + "-1.txt", fname, fname, FileType.TEXT_PLAIN, fname + " file for search ");
dataContent.usingUser(userModel).usingSite(siteModel).createContent(file);
FileModel file2 = new FileModel(fname + "-2.html", fname, fname, FileType.HTML, fname + " file 2 for search ");
dataContent.usingUser(userModel).usingSite(siteModel).createContent(file2);
// Set Node Permissions to allow access for user3 user for text File
JsonObject userPermission = Json.createObjectBuilder()
.add("permissions", Json.createObjectBuilder().add("isInheritanceEnabled", false)
.add("locallySet",Json.createObjectBuilder().add("authorityId", userCanAccessTextFile.getUsername())
.add("name", "SiteConsumer").add("accessStatus", "ALLOWED")))
.build();
String putBody = userPermission.toString();
restClient.authenticateUser(userModel).withCoreAPI().usingNode(file).updateNode(putBody);
waitForIndexing(file.getName(), true);
// Create Query with FacetFields: Site and Content MimeType
SearchRequest query = new SearchRequest();
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("name:" + fname);
query.setQuery(queryReq);
RestRequestFacetFieldsModel facetFields = new RestRequestFacetFieldsModel();
List<RestRequestFacetFieldModel> facets = new ArrayList<RestRequestFacetFieldModel>();
facets.add(new RestRequestFacetFieldModel("SITE", "SEARCH.FACET_FIELDS.SITE", 0)); // MinCount = 0
facets.add(new RestRequestFacetFieldModel("cm:content.mimetype", "Mimetype", 2)); // MinCount = 2
facetFields.setFacets(facets);
query.setFacetFields(facetFields);
// Search query using user who created site: Expect Only 1 Facet bucket to be retrieved
SearchResponse response = query(query);
Assert.assertFalse(response.getContext().getFacetsFields().isEmpty());
Assert.assertEquals(response.getContext().getFacetsFields().size(), 1);
RestResultBucketsModel facetFieldList = response.getContext().getFacetsFields().get(0);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELDS.SITE");
Assert.assertEquals(facetFieldList.getBuckets().size(), 1); // MimeType bucket won't be shown as minCount = 2 won't be reached
FacetFieldBucket bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is(siteModel.getId());
bucket1.assertThat().field("filterQuery").contains(siteModel.getId());
bucket1.assertThat().field("count").is(3); // One folder and 2 files created above
// MinCount 1 or not set: Defaults to 1
query = new SearchRequest();
queryReq = new RestRequestQueryModel();
queryReq.setQuery("name:" + fname);
query.setQuery(queryReq);
facetFields = new RestRequestFacetFieldsModel();
facets = new ArrayList<RestRequestFacetFieldModel>();
facets.add(new RestRequestFacetFieldModel("SITE", "SEARCH.FACET_FIELD1.SITE", null)); // MinCount Not set
facets.add(new RestRequestFacetFieldModel("cm:content.mimetype", "SEARCH.FACET_FIELD2.Mimetype", 1)); // MinCount = 1
facetFields.setFacets(facets);
query.setFacetFields(facetFields);
// Search query using user who created site: Expect 2 Facet buckets to be retrieved
response = query(query);
List<RestResultBucketsModel> facetFieldBucketsList = response.getContext().getFacetsFields();
Assert.assertEquals(facetFieldBucketsList.size(), 2, "FacetField");
facetFieldList = facetFieldBucketsList.get(0);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD1.SITE");
bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is(siteModel.getId());
bucket1.assertThat().field("filterQuery").contains(siteModel.getId());
bucket1.assertThat().field("count").is(3);
facetFieldList = facetFieldBucketsList.get(1);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD2.Mimetype");
Assert.assertEquals(facetFieldList.getBuckets().size(), 2); // MimeType bucket will be shown with 2 buckets
bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is("text/html");
bucket1.assertThat().field("filterQuery").contains("text/html");
bucket1.assertThat().field("count").is(1);
bucket1.assertThat().field("display").is("HTML");
bucket1 = facetFieldList.getBuckets().get(1);
bucket1.assertThat().field("label").is("text/plain");
bucket1.assertThat().field("filterQuery").contains("text/plain");
bucket1.assertThat().field("count").is(1);
bucket1.assertThat().field("display").is("Plain Text");
// Search query using other user: No access hence no buckets expected
response = restClient.authenticateUser(userWithNoAccess).withSearchAPI().search(query);
Assert.assertNull(response.getContext().getFacetsFields());
// Search query using user3
response = restClient.authenticateUser(userCanAccessTextFile).withSearchAPI().search(query);
facetFieldBucketsList = response.getContext().getFacetsFields();
Assert.assertEquals(facetFieldBucketsList.size(), 2);
// Check FacetField 1
facetFieldList = facetFieldBucketsList.get(0);
// User3 has granular permissions to content within this private site, so expect the Site bucket
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD1.SITE");
bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is(siteModel.getId());
bucket1.assertThat().field("filterQuery").contains(siteModel.getId());
bucket1.assertThat().field("count").is(1);
// Check FacetField 2
facetFieldList = facetFieldBucketsList.get(1);
Assert.assertEquals(facetFieldList.getLabel(), "SEARCH.FACET_FIELD2.Mimetype");
Assert.assertEquals(facetFieldList.getBuckets().size(), 1); // MimeType bucket will be shown with 1 bucket only
// User3 has access to text file alone, so expect bucket for text/plain and not for html content.
bucket1 = facetFieldList.getBuckets().get(0);
bucket1.assertThat().field("label").is("text/plain");
bucket1.assertThat().field("label").isNot("text/html");
bucket1.assertThat().field("filterQuery").contains("text/plain");
bucket1.assertThat().field("count").is(1);
bucket1.assertThat().field("display").is("Plain Text");
}
}
+1
View File
@@ -13,6 +13,7 @@
<class name="org.alfresco.rest.search.FacetRangeSearchTest"/>
<class name="org.alfresco.rest.search.PivotFacetedSearchTest"/>
<class name="org.alfresco.rest.search.FacetedSearchTest"/>
<class name="org.alfresco.rest.search.FacetFieldsSearchTest"/>
</classes>
</test>