Merged searchapi (5.2.1) to 5.2.N (5.2.1)

129778 gjames: SEARCH-113: Initial implementation of the Search public API


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130169 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-09-06 14:15:02 +00:00
parent f60bdb5914
commit b18f433fca
14 changed files with 971 additions and 11 deletions

View File

@@ -37,6 +37,7 @@ import org.alfresco.repo.webdav.LockInfoImplTest;
import org.alfresco.repo.webdav.RenameShuffleDetectionTest;
import org.alfresco.repo.webdav.WebDAVHelperTest;
import org.alfresco.repo.webdav.WebDAVLockServiceImplTest;
import org.alfresco.rest.api.search.AllSearchApiTests;
import org.alfresco.rest.api.tests.ModulePackageTest;
import org.alfresco.rest.framework.tests.core.AllRestFrameworkTest;
import org.alfresco.rest.framework.tests.metadata.WriterTests;
@@ -66,6 +67,7 @@ public class AllUnitTestsSuite extends TestSuite
static void publicApiTests(TestSuite suite)
{
suite.addTest(new JUnit4TestAdapter(AllRestFrameworkTest.class));
suite.addTest(new JUnit4TestAdapter(AllSearchApiTests.class));
suite.addTest(new JUnit4TestAdapter(WriterTests.class));
suite.addTest(new JUnit4TestAdapter(ModulePackageTest.class));
}

View File

@@ -0,0 +1,41 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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/>.
* #L%
*/
package org.alfresco.rest.api.search;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* The suite of tests for the Search Api
*
* @author Gethin James
*/
@RunWith(Suite.class)
@SuiteClasses({ SearchMapperTests.class, ResultMapperTests.class,SearchQuerySerializerTests.class})
public class AllSearchApiTests
{
}

View File

@@ -0,0 +1,79 @@
package org.alfresco.rest.api.search;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.alfresco.rest.AbstractSingleNetworkSiteTest;
import org.alfresco.rest.api.Queries;
import org.alfresco.rest.api.tests.client.HttpResponse;
import org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging;
import org.alfresco.rest.api.tests.client.PublicApiClient.Paging;
import org.alfresco.rest.api.tests.client.data.FolderNode;
import org.alfresco.rest.api.tests.client.data.Node;
import org.alfresco.rest.api.tests.util.RestApiUtil;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Basic test to exercise the Search API endpoint
*
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/search/versions/1/search}
*
* @author Gethin James
*/
public class BasicSearchApiIntegrationTest extends AbstractSingleNetworkSiteTest
{
private static final String URL_SEARCH = "search";
private static final String SEARCH_API_NAME = "search";
private static final String json = "{ \"query\": {\"query\": \"cm:name:king\",\"userQuery\": \"great\",\"language\": \"afts\"}}";
private static final String bad_json = "{ \"query\": {\"qu\": \"cm:some nonsense \",\"userQuery\": \"great\",\"language\": \"afts\"}}";
/**
* Tests basic api for search
*/
@Test
public void testQuery() throws Exception
{
setRequestContext(user1);
String f1Id = null;
try
{
// As user 1 ...
// Try to get nodes with search term 'king*' - assume clean repo (ie. none to start with)
HttpResponse response = post(URL_SEARCH, json, null, null, SEARCH_API_NAME, 200);
List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
String myFolderNodeId = getMyNodeId();
f1Id = createFolder(myFolderNodeId, "king").getId();
response = post(URL_SEARCH, json, null, null, SEARCH_API_NAME, 200);
ExpectedPaging paging = RestApiUtil.parsePaging(response.getJsonResponse());
assertEquals(1, paging.getTotalItems().intValue());
assertFalse(paging.getHasMoreItems());
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
}
finally
{
// some cleanup
if (f1Id != null)
{
deleteNode(f1Id, true, 204);
}
}
}
@Test
public void testBadQuery() throws Exception
{
setRequestContext(user1);
//Bad request
HttpResponse response = post(URL_SEARCH, bad_json, null, null, SEARCH_API_NAME, 400);
}
}

View File

@@ -0,0 +1,150 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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/>.
* #L%
*/
package org.alfresco.rest.api.search;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.alfresco.repo.search.EmptyResultSet;
import org.alfresco.repo.search.impl.lucene.SolrJSONResultSet;
import org.alfresco.repo.search.results.ChildAssocRefResultSet;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.impl.NodesImpl;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.api.search.impl.ResultMapper;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.tests.core.JsonJacksonTests;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.LimitBy;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.GUID;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.Match;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.http.HttpMethod;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Tests the ResultMapper class
*
* @author Gethin James
*/
public class ResultMapperTests
{
static ResultMapper mapper;
public static final String JSON_REPONSE = "{\"responseHeader\":{\"status\":0,\"QTime\":9},\"_original_parameters_\":\"org.apache.solr.common.params.DefaultSolrParams:{params(df=TEXT&alternativeDic=DEFAULT_DICTIONARY&fl=DBID,score&start=0&fq={!afts}AUTHORITY_FILTER_FROM_JSON&fq={!afts}TENANT_FILTER_FROM_JSON&rows=1000&locale=en_US&wt=json),defaults(carrot.url=id&spellcheck.collateExtendedResults=true&carrot.produceSummary=true&spellcheck.maxCollations=3&spellcheck.maxCollationTries=5&spellcheck.alternativeTermCount=2&spellcheck.extendedResults=false&defType=afts&spellcheck.maxResultsForSuggest=5&spellcheck=false&carrot.outputSubClusters=false&spellcheck.count=5&carrot.title=mltext@m___t@{http://www.alfresco.org/model/content/1.0}title&carrot.snippet=content@s___t@{http://www.alfresco.org/model/content/1.0}content&spellcheck.collate=true)}\",\"_field_mappings_\":{},\"_date_mappings_\":{},\"_range_mappings_\":{},\"_pivot_mappings_\":{},\"_interval_mappings_\":{},\"_stats_field_mappings_\":{},\"_stats_facet_mappings_\":{},\"_facet_function_mappings_\":{},\"response\":{\"numFound\":6,\"start\":0,\"maxScore\":0.7849362,\"docs\":[{\"DBID\":565,\"score\":0.7849362},{\"DBID\":566,\"score\":0.7849362},{\"DBID\":521,\"score\":0.3540957},{\"DBID\":514,\"score\":0.33025497},{\"DBID\":420,\"score\":0.32440513},{\"DBID\":415,\"score\":0.2780319}]},\"processedDenies\":true}";
@BeforeClass
public static void setupTests() throws Exception
{
Map<String, UserInfo> mapUserInfo = new HashMap<>();
mapUserInfo.put(AuthenticationUtil.getSystemUserName(), new UserInfo(AuthenticationUtil.getSystemUserName(), "sys", "sys"));
Map<QName, Serializable> nodeProps = new HashMap<>();
NodesImpl nodes = mock(NodesImpl.class);
ServiceRegistry sr = mock(ServiceRegistry.class);
nodes.setServiceRegistry(sr);
when(nodes.getFolderOrDocument(notNull(NodeRef.class), any(), any(), notNull(List.class), any())).thenAnswer(new Answer<Node>() {
@Override
public Node answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return new Node((NodeRef)args[0], (NodeRef)args[1], nodeProps, mapUserInfo, sr);
}
});
mapper = new ResultMapper(nodes);
}
@Test
public void testNoResults() throws Exception
{
CollectionWithPagingInfo<Node> collection = mapper.toCollectionWithPagingInfo(mockParams(),new EmptyResultSet());
assertNotNull(collection);
assertFalse(collection.hasMoreItems());
assertTrue(collection.getTotalItems() < 1);
}
@Test
public void testToCollectionWithPagingInfo() throws Exception
{
ResultSet results = mockResultset();
CollectionWithPagingInfo<Node> collection = mapper.toCollectionWithPagingInfo(mockParams(),results);
assertNotNull(collection);
Long found = results.getNumberFound();
assertEquals(found.intValue(), collection.getTotalItems().intValue());
}
private ResultSet mockResultset() throws JSONException
{
NodeService nodeService = mock(NodeService.class);
when(nodeService.getNodeRef(any())).thenReturn(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, GUID.generate()));
SearchParameters sp = new SearchParameters();
sp.setBulkFetchEnabled(false);
JSONObject json = new JSONObject(new JSONTokener(JSON_REPONSE));
ResultSet results = new SolrJSONResultSet(json,sp,nodeService, null, LimitBy.FINAL_SIZE, 10);
return results;
}
private Params mockParams()
{
Params params = mock(Params.class);
when(params.getInclude()).thenReturn(new ArrayList<String>());
return params;
}
}

View File

@@ -0,0 +1,139 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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/>.
* #L%
*/
package org.alfresco.rest.api.search;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_CMIS_ALFRESCO;
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_FTS_ALFRESCO;
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_LUCENE;
import org.alfresco.rest.api.search.impl.SearchMapper;
import org.alfresco.rest.api.search.model.Query;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.SearchParameters;
import org.junit.Test;
/**
* Tests the SearchMapper class
*
* @author Gethin James
*/
public class SearchMapperTests
{
static SearchMapper searchMapper = new SearchMapper();
@Test(expected = IllegalArgumentException.class)
public void testMandatory() throws Exception
{
Params params = Params.valueOf(null, null, new SearchQuery(), null);
SearchParameters searchParameters = searchMapper.toSearchParameters(params);
}
@Test
public void toSearchParameters() throws Exception
{
Params params = Params.valueOf(null, null, minimalQuery(), null);
SearchParameters searchParameters = searchMapper.toSearchParameters(params);
assertNotNull(searchParameters);
//Test defaults
assertEquals("There should be only 1 default store", 1,searchParameters.getStores().size());
assertEquals("workspaces store is the default", StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, searchParameters.getStores().get(0));
}
@Test
public void fromQuery() throws Exception
{
SearchParameters searchParameters = new SearchParameters();
try
{
searchMapper.fromQuery(searchParameters, new Query());
fail();
} catch (IllegalArgumentException iae)
{
assertTrue(iae.getLocalizedMessage().contains("query is a mandatory parameter"));
}
Query q = new Query();
q.setQuery("hello");
try
{
searchMapper.fromQuery(searchParameters, q);
fail();
} catch (IllegalArgumentException iae)
{
assertTrue(iae.getLocalizedMessage().contains("language is a mandatory parameter"));
}
q.setLanguage("world");
try
{
searchMapper.fromQuery(searchParameters, q);
fail();
} catch (InvalidArgumentException iae)
{
assertNotNull(iae);
//world is not a valid language type
}
q.setLanguage("afts");
searchMapper.fromQuery(searchParameters, q);
assertEquals(LANGUAGE_FTS_ALFRESCO, searchParameters.getLanguage());
q.setLanguage("cMiS");
searchMapper.fromQuery(searchParameters, q);
assertEquals(LANGUAGE_CMIS_ALFRESCO, searchParameters.getLanguage());
q.setLanguage("LuCENE");
searchMapper.fromQuery(searchParameters, q);
assertEquals(LANGUAGE_LUCENE, searchParameters.getLanguage());
assertEquals("hello", searchParameters.getQuery());
q.setUserQuery("Heload");
searchMapper.fromQuery(searchParameters, q);
assertEquals("Heload", searchParameters.getSearchTerm());
}
private SearchQuery minimalQuery()
{
SearchQuery sq = new SearchQuery();
Query query = new Query();
sq.setQuery(query);
query.setQuery("foo");
query.setLanguage("cmis");
return sq;
}
}

View File

@@ -0,0 +1,83 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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/>.
* #L%
*/
package org.alfresco.rest.api.search;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.alfresco.rest.api.model.Comment;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
import org.alfresco.rest.framework.jacksonextensions.RestJsonModule;
import org.alfresco.rest.framework.tools.ApiAssistant;
import org.alfresco.rest.framework.tools.RequestReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.WebScriptRequest;
import java.io.IOException;
import java.io.StringReader;
/**
* Tests json -> SearchQuery deserialization
*
* @author Gethin James
*/
public class SearchQuerySerializerTests implements RequestReader
{
static JacksonHelper jsonHelper = null;
@BeforeClass
public static void setupTests() throws Exception
{
jsonHelper = new JacksonHelper();
RestJsonModule module = new RestJsonModule();
jsonHelper.setModule(module);
jsonHelper.afterPropertiesSet();
}
@Test
public void testDeserializeQuery() throws IOException
{
String json = "{ \"query\": {\"query\": \"g*\",\"userQuery\": \"great\",\"language\": \"bob\"}}";
SearchQuery searchQuery = extractFromJson(json);
assertEquals(SearchQuery.class, searchQuery.getClass());
assertEquals("bob", searchQuery.getQuery().getLanguage());
assertEquals("g*", searchQuery.getQuery().getQuery());
assertEquals("great", searchQuery.getQuery().getUserQuery());
}
private SearchQuery extractFromJson(String json) throws IOException
{
Content content = mock(Content.class);
when(content.getReader()).thenReturn(new StringReader(json));
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getContent()).thenReturn(content);
return extractJsonContent(request, jsonHelper, SearchQuery.class);
}
}

View File

@@ -25,7 +25,8 @@
*/
package org.alfresco.rest.api.tests;
import org.alfresco.rest.DeletedNodesTest;
import org.alfresco.rest.DeletedNodesTest;
import org.alfresco.rest.api.search.BasicSearchApiIntegrationTest;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@@ -34,25 +35,26 @@ import org.junit.runners.Suite;
* Public API tests.
*
* @author steveglover
* @author janv
* @author Jamal Kaabi-Mofrad
* @author Gethin James
* @author janv
* @author Jamal Kaabi-Mofrad
* @author Gethin James
*
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
NodeApiTest.class,
NodeAssociationsApiTest.class,
NodeApiTest.class,
NodeAssociationsApiTest.class,
NodeVersionsApiTest.class,
BasicSearchApiIntegrationTest.class,
QueriesNodesApiTest.class,
QueriesPeopleApiTest.class,
QueriesSitesApiTest.class,
RenditionsTest.class,
SharedLinkApiTest.class,
ActivitiesPostingTest.class,
DeletedNodesTest.class,
AuthenticationsTest.class,
RenditionsTest.class,
SharedLinkApiTest.class,
ActivitiesPostingTest.class,
DeletedNodesTest.class,
AuthenticationsTest.class,
ModulePackagesApiTest.class,
TestSites.class,
TestNodeComments.class,