mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
RM-5416: REST API Tests for Folder Classification - added tests for classified folders search
This commit is contained in:
@@ -24,13 +24,17 @@
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
package org.alfresco.rest.v0;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.dataprep.AlfrescoHttpClient;
|
||||
import org.alfresco.dataprep.AlfrescoHttpClientFactory;
|
||||
import org.alfresco.rest.core.v0.BaseAPI;
|
||||
import org.apache.http.NameValuePair;
|
||||
@@ -49,8 +53,13 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class SearchAPI extends BaseAPI
|
||||
{
|
||||
|
||||
/** http client factory */
|
||||
@Autowired private AlfrescoHttpClientFactory alfrescoHttpClientFactory;
|
||||
@Autowired
|
||||
private AlfrescoHttpClientFactory alfrescoHttpClientFactory;
|
||||
|
||||
/** search API endpoint */
|
||||
private static final String SEARCH_ENDPOINT = "{0}alfresco/s/slingshot/search?{1}";
|
||||
|
||||
/** faceted search API endpoint */
|
||||
private static final String FACETED_SEARCH_ENDPOINT = "{0}alfresco/s/slingshot/rmsearch/faceted/rmsearch?{1}";
|
||||
@@ -59,28 +68,26 @@ public class SearchAPI extends BaseAPI
|
||||
private static final String RM_SEARCH_ENDPOINT = "{0}alfresco/s/slingshot/rmsearch/{1}?{2}";
|
||||
|
||||
/** RM document search filters */
|
||||
private static final String RM_DEFAULT_RECORD_FILTERS =
|
||||
"records/true,undeclared/true,vital/false,folders/false,categories/false,frozen/false,cutoff/false";
|
||||
private static final String RM_DEFAULT_RECORD_FILTERS = "records/true,undeclared/true,vital/false,folders/false,categories/false,frozen/false,cutoff/false";
|
||||
|
||||
/**
|
||||
* Perform search request on search endpoint as a user.
|
||||
* <p>
|
||||
* This method is applicable only to endpoints that support HTTP GET requests and return JSON body as response.
|
||||
*
|
||||
* @param searchEndpoint
|
||||
* @param searchUser
|
||||
* @param searchPassword
|
||||
* @return search results as a {@link JSONObject}, please refer to API documentation for details
|
||||
*/
|
||||
private JSONObject doSearch(
|
||||
String searchEndpoint,
|
||||
String searchUser,
|
||||
String searchPassword)
|
||||
private JSONObject doSearch(String searchEndpoint, String searchUser, String searchPassword)
|
||||
{
|
||||
return facetedRequest(searchUser, searchPassword, null, searchEndpoint);
|
||||
return facetedRequest(searchUser, searchPassword, null, searchEndpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic rm search.
|
||||
*
|
||||
* @param username
|
||||
* @param password
|
||||
* @param site
|
||||
@@ -88,45 +95,36 @@ public class SearchAPI extends BaseAPI
|
||||
* @param filters
|
||||
* @return search results (see API reference for more details), null for any errors
|
||||
*/
|
||||
public JSONObject rmSearch(
|
||||
String username,
|
||||
String password,
|
||||
String site,
|
||||
String query,
|
||||
String filters)
|
||||
public JSONObject rmSearch(String username, String password, String site, String query, String filters)
|
||||
{
|
||||
List<BasicNameValuePair> searchParameters = new ArrayList<BasicNameValuePair>();
|
||||
searchParameters.add(new BasicNameValuePair("query", query));
|
||||
searchParameters.add(new BasicNameValuePair("filters", filters));
|
||||
|
||||
String requestURL = MessageFormat.format(
|
||||
RM_SEARCH_ENDPOINT,
|
||||
alfrescoHttpClientFactory.getObject().getAlfrescoUrl(),
|
||||
(site != null) ? site : RM_SITE_ID,
|
||||
URLEncodedUtils.format(searchParameters, "UTF-8"));
|
||||
String requestURL = MessageFormat.format(RM_SEARCH_ENDPOINT,
|
||||
alfrescoHttpClientFactory.getObject().getAlfrescoUrl(), (site != null) ? site : RM_SITE_ID,
|
||||
URLEncodedUtils.format(searchParameters, "UTF-8"));
|
||||
|
||||
return doSearch(requestURL, username, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search as a user for records on site "rm" matching query, using SearchAPI.RM_DEFAULT_RECORD_FILTERS
|
||||
* <br>
|
||||
* Search as a user for records on site "rm" matching query, using SearchAPI.RM_DEFAULT_RECORD_FILTERS <br>
|
||||
* If more fine-grained control of search parameters is required, use rmSearch() directly.
|
||||
*
|
||||
* @param username
|
||||
* @param password
|
||||
* @param query
|
||||
* @return list of record names
|
||||
*/
|
||||
public List<String> searchForRecordsAsUser(
|
||||
String username,
|
||||
String password,
|
||||
String query)
|
||||
public List<String> searchForRecordsAsUser(String username, String password, String query)
|
||||
{
|
||||
return getItemNames(rmSearch(username, password, "rm", query, RM_DEFAULT_RECORD_FILTERS));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic faceted search.
|
||||
*
|
||||
* @param username
|
||||
* @param password
|
||||
* @param parameters
|
||||
@@ -139,6 +137,7 @@ public class SearchAPI extends BaseAPI
|
||||
|
||||
/**
|
||||
* Execute faceted search for term.
|
||||
*
|
||||
* @param searchUser
|
||||
* @param searchPassword
|
||||
* @param searchTerm
|
||||
@@ -146,14 +145,12 @@ public class SearchAPI extends BaseAPI
|
||||
*/
|
||||
public JSONObject facetedSearchForTerm(String searchUser, String searchPassword, String searchTerm)
|
||||
{
|
||||
return facetedSearch(
|
||||
searchUser,
|
||||
searchPassword,
|
||||
Arrays.asList(new BasicNameValuePair("term", searchTerm)));
|
||||
return facetedSearch(searchUser, searchPassword, Arrays.asList(new BasicNameValuePair("term", searchTerm)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to search for documents as a user using faceted search.
|
||||
*
|
||||
* @param username to search as
|
||||
* @param password for username
|
||||
* @param term search term
|
||||
@@ -161,11 +158,39 @@ public class SearchAPI extends BaseAPI
|
||||
*/
|
||||
public List<String> searchForDocumentsAsUser(String username, String password, String term)
|
||||
{
|
||||
|
||||
return getItemNames(facetedSearchForTerm(username, password, term));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to search for documents or folders as a user using search.
|
||||
*
|
||||
* @param username to search as
|
||||
* @param password for username
|
||||
* @param term search term
|
||||
* @return list of document names found
|
||||
* @throws IOException
|
||||
*/
|
||||
public List<String> searchForContentAsUser(String username, String password, String... searchParameters)
|
||||
throws Exception
|
||||
{
|
||||
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
|
||||
String requestURL = MessageFormat.format(SEARCH_ENDPOINT, client.getAlfrescoUrl());
|
||||
|
||||
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
|
||||
for (String searchParameter : searchParameters)
|
||||
{
|
||||
nameValuePairs.add(new BasicNameValuePair("term", searchParameter));
|
||||
}
|
||||
requestURL = MessageFormat.format(SEARCH_ENDPOINT, client.getAlfrescoUrl(),
|
||||
URLEncodedUtils.format(nameValuePairs, Charset.forName("UTF-8")));
|
||||
Thread.sleep(20000);
|
||||
return getItemNames(doGetRequest(username, password, requestURL));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to extract list of names from search result.
|
||||
*
|
||||
* @param searchResult
|
||||
* @return list of document or record names in search result
|
||||
* @throws RuntimeException for malformed search response
|
||||
|
Reference in New Issue
Block a user