Merge branch '5.2.N-SEARCH'

# Conflicts:
#	pom.xml
This commit is contained in:
Paul Brodner
2017-02-27 15:51:14 +00:00
6 changed files with 356 additions and 2 deletions
@@ -0,0 +1,112 @@
/*
* Copyright (C) 2017 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 org.alfresco.rest.RestTest;
import org.alfresco.rest.model.builder.NodesBuilder;
import org.alfresco.utility.model.ContentModel;
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.UserModel;
import org.testng.annotations.BeforeClass;
/**
* Abstract Search test class that contains useful methods
* such as:
* <ul>
* <li>Preparing the data to index.
* <li>Preparing search requests.
* @author Michael Suzuki
*
*/
public class AbstractSearchTest extends RestTest
{
protected static final String SEARCH_DATA_SAMPLE_FOLDER = "folder";
UserModel userModel, adminUserModel;
SiteModel siteModel;
UserModel searchedUser;
NodesBuilder nodesBuilder;
protected FileModel file,file2;
@BeforeClass(alwaysRun = true)
public void dataPreparation() throws Exception
{
adminUserModel = dataUser.getAdminUser();
userModel = dataUser.createRandomTestUser();
siteModel = dataSite.usingUser(userModel).createPublicRandomSite();
/*
* Create the following file structure for preconditions :
* |- folder
* |-- pangram.txt
* |-- cars.pdf
*/
nodesBuilder = restClient.authenticateUser(userModel).withCoreAPI().usingNode(ContentModel.my()).defineNodes();
FolderModel folder = new FolderModel(SEARCH_DATA_SAMPLE_FOLDER);
dataContent.usingSite(siteModel).createFolder(folder);
//Create files
file = new FileModel("pangram.txt", FileType.TEXT_PLAIN, "The quick brown fox jumps over the lazy dog");
file2 = new FileModel("cars.txt", FileType.TEXT_PLAIN, "The landrover discovery is not a sports car");
ContentModel cm = new ContentModel();
cm.setCmisLocation(folder.getCmisLocation());
cm.setName(folder.getName());
dataContent.usingSite(siteModel).usingResource(cm).createContent(file);
dataContent.usingSite(siteModel).usingResource(cm).createContent(file2);
}
/**
* Helper method which create an http post request to Search API end point.
* @param term String search term
* @return {@link SearchResponse} response.
* @throws Exception if error
*
*/
protected SearchResponse query(String term) throws Exception
{
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setLanguage("afts");
queryReq.setQuery(term);
SearchRequest query = new SearchRequest(queryReq);
return restClient.authenticateUser(dataUser.getAdminUser()).withSearchAPI().search(query);
}
/**
* Helper method which create an http post request to Search API end point.
* @param term String search term
* @return {@link SearchResponse} response.
* @throws Exception if error
*
*/
protected SearchResponse query(RestRequestQueryModel queryReq,RestRequestHighlightModel highlight) throws Exception
{
SearchRequest query = new SearchRequest(queryReq);
query.setHighlight(highlight);
return restClient.authenticateUser(dataUser.getAdminUser()).withSearchAPI().search(query);
}
/**
* Helper method which create an http post request to Search API end point.
* @param term String search term
* @return {@link SearchResponse} response.
* @throws Exception if error
*
*/
protected SearchResponse query(SearchRequest query) throws Exception
{
return restClient.authenticateUser(dataUser.getAdminUser()).withSearchAPI().search(query);
}
}
@@ -0,0 +1,112 @@
/*
* Copyright (C) 2017 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 org.alfresco.rest.model.RestRequestFacetFieldsModel;
import org.alfresco.utility.model.TestGroup;
import org.testng.annotations.Test;
/**
* Faceted search test.
* @author Michael Suzuki
*
*/
public class FacetedSearchTest extends AbstractSearchTest
{
@Test(groups={TestGroup.SEARCH, TestGroup.REST_API})
/**
* Perform the below facet query.
* {
* "query": {
* "query": "cars",
* "language": "afts"
* },
* "facetQueries": [
* {"query": "content.size:[o TO 102400]", "label": "small"},
* {"query": "content.size:[102400 TO 1048576]", "label": "medium"},
* {"query": "content.size:[1048576 TO 16777216]", "label": "large"}
* ],
* "facetFields": {"facets": [{"field": "'content.size'"}]}
* }
*
* Expected response
* {"list": {
* "entries": [... All the results],
* "pagination": {
* "maxItems": 100,
* "hasMoreItems": false,
* "totalItems": 61,
* "count": 61,
* "skipCount": 0
* },
* "context": {
* "consistency": {"lastTxId": 512},
* "facetQueries": [
* {
* "count": 61,
* "label": "small"
* },
* {
* "count": 0,
* "label": "large"
* },
* {
* "count": 0,
* "label": "medium"
* }
* ]
* }
* }}
* @throws Exception
*/
public void searchWithFaceting() throws Exception
{
SearchRequest query = new SearchRequest();
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("cars");
query.setQuery(queryReq);
List<FacetQuery> facets = new ArrayList<FacetQuery>();
facets.add(new FacetQuery("content.size:[0 TO 102400]", "small"));
facets.add(new FacetQuery("content.size:[102400 TO 1048576]", "medium"));
facets.add(new FacetQuery("content.size:[1048576 TO 16777216]", "large"));
query.setFacetQueries(facets);
RestRequestFacetFieldsModel facetFields = new RestRequestFacetFieldsModel();
List<Object> list = new ArrayList<Object>();
list.add(new FacetFieldQuery("'content.size'"));
facetFields.setFacets(list);
query.setFacetFields(facetFields);
SearchResponse response = query(query);
response.assertThat().entriesListIsNotEmpty();
response.getContext().assertThat().field("facetQueries").isNotEmpty();
FacetFieldResponse facet = response.getContext().getFacetQueries().get(0);
facet.assertThat().field("label").contains("small").and().field("count").isGreaterThan(0);
response.getContext().getFacetQueries().get(1).assertThat().field("label").contains("large").and().field("count").isLessThan(1);
response.getContext().getFacetQueries().get(2).assertThat().field("label").contains("medium").and().field("count").isLessThan(1);
}
}
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2017 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 org.alfresco.utility.model.TestGroup;
import org.testng.annotations.Test;
/**
* Search high lighting test.
* @author Michael Suzuki
*
*/
public class SearchHighLightTest extends AbstractSearchTest
{
@Test(groups={TestGroup.SEARCH,TestGroup.REST_API})
public void searchWithHighLight() throws Exception
{
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("cm:content:cars");
queryReq.setUserQuery("cars");
RestRequestHighlightModel highlight = new RestRequestHighlightModel();
highlight.setPrefix("¿");
highlight.setPostfix("?");
highlight.setMergeContiguous(true);
List<RestRequestFieldsModel> fields = new ArrayList<RestRequestFieldsModel>();
fields.add(new RestRequestFieldsModel("cm:content"));
highlight.setFields(fields);
SearchResponse nodes = query(queryReq, highlight);
nodes.assertThat().entriesListIsNotEmpty();
ResponseHighLightModel hl = nodes.getEntryByIndex(0).getSearch().getHighlight().get(0);
hl.assertThat().field("snippets").contains( "The landrover discovery is not a sports ¿car?");
}
@Test(groups={TestGroup.SEARCH,TestGroup.REST_API})
public void searchNonIndexedData() throws Exception
{
RestRequestQueryModel queryReq = new RestRequestQueryModel();
queryReq.setQuery("cm:title");
queryReq.setUserQuery("zoro");
RestRequestHighlightModel highlight = new RestRequestHighlightModel();
highlight.setPrefix("¿");
highlight.setPostfix("?");
highlight.setMergeContiguous(true);
List<RestRequestFieldsModel> fields = new ArrayList<RestRequestFieldsModel>();
fields.add(new RestRequestFieldsModel("cm:title"));
highlight.setFields(fields);
SearchResponse nodes = query(queryReq, highlight);
nodes.assertThat().entriesListDoesNotContain("highlight");
}
}
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2017 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 org.alfresco.utility.model.TestGroup;
import org.springframework.http.HttpStatus;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* Search end point Public API test.
* @author Michael Suzuki
*
*/
public class SearchTest extends AbstractSearchTest
{
@Test(groups={TestGroup.SEARCH, TestGroup.REST_API})
public void searchCreatedData() throws Exception
{
SearchResponse nodes = query("fox");
restClient.assertStatusCodeIs(HttpStatus.OK);
nodes.assertThat().entriesListIsNotEmpty();
SearchNodeModel entity = nodes.getEntryByIndex(0);
Assert.assertEquals(entity.getSearch().getScore(), 1);
entity.assertThat().field("name").contains("pangram.txt");
nodes = query("car");
restClient.assertStatusCodeIs(HttpStatus.OK);
entity = nodes.getEntryByIndex(0);
nodes.assertThat().entriesListIsNotEmpty();
Assert.assertEquals(entity.getSearch().getScore(), 1);
entity.assertThat().field("search").contains("score");
entity.assertThat().field("name").contains("cars.txt");
}
@Test(groups={TestGroup.SEARCH,TestGroup.REST_API})
public void searchNonIndexedData() throws Exception
{
SearchResponse nodes = query("yeti");
restClient.assertStatusCodeIs(HttpStatus.OK);
nodes.assertThat().entriesListIsEmpty();
}
}
@@ -5,7 +5,6 @@ import org.alfresco.dataprep.CMISUtil.Priority;
import org.alfresco.rest.RestTest;
import org.alfresco.rest.model.RestErrorModel;
import org.alfresco.rest.model.RestProcessModel;
import org.alfresco.rest.model.RestProcessModelsCollection;
import org.alfresco.rest.model.RestProcessVariableModel;
import org.alfresco.utility.model.FileModel;
import org.alfresco.utility.model.SiteModel;
@@ -4,7 +4,6 @@ import org.alfresco.dataprep.CMISUtil;
import org.alfresco.dataprep.CMISUtil.DocumentType;
import org.alfresco.rest.RestTest;
import org.alfresco.rest.core.RestRequest;
import org.alfresco.rest.exception.EmptyJsonResponseException;
import org.alfresco.rest.model.RestErrorModel;
import org.alfresco.rest.model.RestProcessModel;
import org.alfresco.rest.model.RestTaskModel;