RM-5416: API Tests for REST API for Folder Classification

This commit is contained in:
jcule
2017-09-09 11:44:01 +01:00
parent b123971187
commit b3e8d59f50

View File

@@ -24,6 +24,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.core.v0;
import java.io.IOException;
@@ -83,12 +84,12 @@ public abstract class BaseAPI
@Autowired
private AlfrescoHttpClientFactory alfrescoHttpClientFactory;
@Autowired
@Autowired
private ContentService contentService;
private static final String NODE_REF_WORKSPACE_SPACES_STORE = "workspace://SpacesStore/";
private static final String FILE_PLAN_PATH = "Sites/rm/documentLibrary";
/**
* Helper method to extract list of properties values from result.
*
@@ -117,13 +118,15 @@ public abstract class BaseAPI
}
/**
* Helper method to extract property values from request result and put them in map as a list that corresponds to a unique property value.
* Helper method to extract property values from request result and put them in map as a list that corresponds to a
* unique property value.
*
* @param requestResult the request response
* @return a map containing information about multiple properties values that correspond to a unique one
* @throws RuntimeException for malformed response
*/
protected Map<String, List<String>> getPropertyValuesByUniquePropertyValue(JSONObject requestResult, String uniqueProperty, List<String> otherProperties)
protected Map<String, List<String>> getPropertyValuesByUniquePropertyValue(JSONObject requestResult,
String uniqueProperty, List<String> otherProperties)
{
Map<String, List<String>> valuesByUniqueProperty = new HashMap<>();
try
@@ -154,9 +157,8 @@ public abstract class BaseAPI
*
* @param username the username
* @param password the password
* @param path the path to the container eg. in case of a category it would be the category name,
* in case of a folder it would be /categoryName/folderName
* when trying to get File Plan, the path would be ""
* @param path the path to the container eg. in case of a category it would be the category name, in case of a
* folder it would be /categoryName/folderName when trying to get File Plan, the path would be ""
* @return the container nodeRef
*/
public String getItemNodeRef(String username, String password, String path)
@@ -169,7 +171,7 @@ public abstract class BaseAPI
*
* @param username the user's username
* @param password its password
* @param path the object path
* @param path the object path
* @return the object in case it exists, null if its does not exist
*/
protected CmisObject getObjectByPath(String username, String password, String path)
@@ -178,7 +180,8 @@ public abstract class BaseAPI
try
{
object = contentService.getCMISSession(username, password).getObjectByPath(path);
} catch (CmisObjectNotFoundException notFoundError)
}
catch (CmisObjectNotFoundException notFoundError)
{
return null;
}
@@ -193,23 +196,20 @@ public abstract class BaseAPI
* @param parameters if the request has parameters
* @return result object (see API reference for more details), null for any errors
*/
protected JSONObject facetedRequest(String username, String password, List<NameValuePair> parameters, String requestURI)
protected JSONObject facetedRequest(String username, String password, List<NameValuePair> parameters,
String requestURI)
{
String requestURL;
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
if (parameters == null || parameters.isEmpty())
{
requestURL = MessageFormat.format(
requestURI,
client.getAlfrescoUrl());
requestURL = MessageFormat.format(requestURI, client.getAlfrescoUrl());
}
else
{
requestURL = MessageFormat.format(
requestURI,
client.getAlfrescoUrl(),
URLEncodedUtils.format(parameters, "UTF-8"));
requestURL = MessageFormat.format(requestURI, client.getAlfrescoUrl(),
URLEncodedUtils.format(parameters, "UTF-8"));
}
client.close();
return doGetRequest(username, password, requestURL);
@@ -217,21 +217,17 @@ public abstract class BaseAPI
/**
* Helper method for GET requests
*
* @param adminUser user with administrative privileges
* @param adminPassword password for adminUser
* @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/
protected JSONObject doGetRequest(String adminUser,
String adminPassword,
String urlTemplate,
String ... urlTemplateParams)
protected JSONObject doGetRequest(String adminUser, String adminPassword, String urlTemplate,
String... urlTemplateParams)
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams);
client.close();
try
@@ -246,21 +242,17 @@ public abstract class BaseAPI
/**
* Helper method for Delete requests
*
* @param adminUser user with administrative privileges
* @param adminPassword password for adminUser
* @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/
protected JSONObject doDeleteRequest(String adminUser,
String adminPassword,
String urlTemplate,
String ... urlTemplateParams)
protected JSONObject doDeleteRequest(String adminUser, String adminPassword, String urlTemplate,
String... urlTemplateParams)
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams);
client.close();
try
@@ -275,23 +267,18 @@ public abstract class BaseAPI
/**
* Helper method for PUT requests
*
* @param adminUser user with administrative privileges
* @param adminPassword password for adminUser
* @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/
protected JSONObject doPutRequest(String adminUser,
String adminPassword,
JSONObject requestParams,
String urlTemplate,
String ... urlTemplateParams)
protected JSONObject doPutRequest(String adminUser, String adminPassword, JSONObject requestParams,
String urlTemplate, String... urlTemplateParams)
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams);
client.close();
try
@@ -306,27 +293,20 @@ public abstract class BaseAPI
/**
* Helper method for POST requests
*
* @param adminUser user with administrative privileges
* @param adminPassword password for adminUser
* @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/
protected JSONObject doPostRequest(String adminUser,
String adminPassword,
JSONObject requestParams,
String urlTemplate,
String ... urlTemplateParams)
protected JSONObject doPostRequest(String adminUser, String adminPassword, JSONObject requestParams,
String urlTemplate, String... urlTemplateParams)
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams);
client.close();
LOGGER.debug("REQUEST ***** ", requestUrl);
System.out.println("REQUEST ***** " + requestUrl);
System.out.println("REQUEST PARAMS ***** " + requestParams);
try
{
return doRequest(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams);
@@ -340,23 +320,17 @@ public abstract class BaseAPI
/**
* Helper method for POST requests
*
* @param adminUser user with administrative privileges
* @param adminPassword password for adminUser
* @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template
* @param adminUser user with administrative privileges
* @param adminPassword password for adminUser
* @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/
protected boolean doPostJsonRequest(String adminUser,
String adminPassword,
JSONObject requestParams,
String urlTemplate,
String... urlTemplateParams)
protected boolean doPostJsonRequest(String adminUser, String adminPassword, JSONObject requestParams,
String urlTemplate, String... urlTemplateParams)
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams);
client.close();
try
@@ -371,6 +345,7 @@ public abstract class BaseAPI
/**
* Helper method for handling generic HTTP requests
*
* @param requestType request type (a subclass of {@link HttpRequestBase})
* @param requestUrl URL the request is to be sent to
* @param adminUser user with administrative privileges
@@ -380,12 +355,8 @@ public abstract class BaseAPI
* @throws IllegalAccessException for invalid <i>requestType</i>
* @throws InstantiationException for invalid <i>requestType</i>
*/
private <T extends HttpRequestBase> JSONObject doRequest(
Class<T> requestType,
String requestUrl,
String adminUser,
String adminPassword,
JSONObject requestParams) throws InstantiationException, IllegalAccessException
private <T extends HttpRequestBase> JSONObject doRequest(Class<T> requestType, String requestUrl, String adminUser,
String adminPassword, JSONObject requestParams) throws InstantiationException, IllegalAccessException
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
T request = requestType.newInstance();
@@ -426,7 +397,7 @@ public abstract class BaseAPI
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
case HttpStatus.SC_BAD_REQUEST:
if (responseBody != null && responseBody.has(EXCEPTION_KEY))
if (responseBody != null && responseBody.has(EXCEPTION_KEY))
{
LOGGER.error("Request failed: " + responseBody.getString(EXCEPTION_KEY));
returnValues = responseBody;
@@ -458,12 +429,8 @@ public abstract class BaseAPI
return returnValues;
}
private <T extends HttpRequestBase> boolean doRequestJson(
Class<T> requestType,
String requestUrl,
String adminUser,
String adminPassword,
JSONObject requestParams) throws InstantiationException, IllegalAccessException
private <T extends HttpRequestBase> boolean doRequestJson(Class<T> requestType, String requestUrl, String adminUser,
String adminPassword, JSONObject requestParams) throws InstantiationException, IllegalAccessException
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
T request = requestType.newInstance();
@@ -478,7 +445,8 @@ public abstract class BaseAPI
((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(requestParams.toString()));
}
return client.execute(adminUser, adminPassword, request).getStatusLine().getStatusCode() == HttpStatus.SC_OK;
return client.execute(adminUser, adminPassword, request).getStatusLine()
.getStatusCode() == HttpStatus.SC_OK;
}
catch (UnsupportedEncodingException | URISyntaxException error1)
{
@@ -497,36 +465,16 @@ public abstract class BaseAPI
}
/**
* Used to set RM items properties
* including records, categories and folders
* Used to set RM items properties including records, categories and folders
*/
public enum RMProperty
{
NAME,
TITLE,
CONTENT,
DESCRIPTION,
AUTHOR,
PHYSICAL_SIZE,
NUMBER_OF_COPIES,
STORAGE_LOCATION,
SHELF,
BOX,
FILE,
NAME, TITLE, CONTENT, DESCRIPTION, AUTHOR, PHYSICAL_SIZE, NUMBER_OF_COPIES, STORAGE_LOCATION, SHELF, BOX, FILE,
}
public enum RETENTION_SCHEDULE
{
NAME,
DESCRIPTION,
RETENTION_AUTHORITY,
RETENTION_INSTRUCTIONS,
RETENTION_PERIOD,
RETENTION_LOCATION,
RETENTION_PERIOD_PROPERTY,
RETENTION_GHOST,
RETENTION_ELIGIBLE_FIRST_EVENT,
RETENTION_EVENTS,
NAME, DESCRIPTION, RETENTION_AUTHORITY, RETENTION_INSTRUCTIONS, RETENTION_PERIOD, RETENTION_LOCATION, RETENTION_PERIOD_PROPERTY, RETENTION_GHOST, RETENTION_ELIGIBLE_FIRST_EVENT, RETENTION_EVENTS,
}
@@ -535,11 +483,8 @@ public abstract class BaseAPI
*/
public enum RM_ACTIONS
{
EDIT_DISPOSITION_DATE("editDispositionActionAsOfDate"),
CUT_OFF("cutoff"),
UNDO_CUT_OFF("undoCutoff"),
TRANSFER("transfer"),
DESTROY("destroy");
EDIT_DISPOSITION_DATE("editDispositionActionAsOfDate"), CUT_OFF("cutoff"), UNDO_CUT_OFF("undoCutoff"), TRANSFER(
"transfer"), DESTROY("destroy");
String action;
private RM_ACTIONS(String action)
@@ -555,39 +500,34 @@ public abstract class BaseAPI
public enum PermissionType
{
SET_READ,
REMOVE_READ,
SET_READ_AND_FILE,
REMOVE_READ_AND_FILE,
SET_READ, REMOVE_READ, SET_READ_AND_FILE, REMOVE_READ_AND_FILE,
}
/**
* Util to return the property value from a map
*
* @param properties the map containing properties
* @param property to get value for
* @param property to get value for
* @return the property value
*/
public <K extends Enum<?>> String getPropertyValue(Map<K, String> properties, Enum<?> property)
{
String value = properties.get(property);
if (value == null)
{
return "";
}
if (value == null) { return ""; }
return value;
}
/**
* Retrieves the property value and decides if that gets to be added to the request
*
* @param requestParams the request parameters
* @param requestParams the request parameters
* @param propertyRequestValue the property name in the request, eg. "prop_cm_name"
* @param itemProperties map of item's properties values
* @param property the property in the property map to check value for
* @param itemProperties map of item's properties values
* @param property the property in the property map to check value for
* @return the json object used in request with the property with its value added if that is not null or empty
*/
protected <K extends Enum<?>> JSONObject addPropertyToRequest(JSONObject requestParams, String propertyRequestValue, Map<K, String> itemProperties, Enum<?> property) throws JSONException
protected <K extends Enum<?>> JSONObject addPropertyToRequest(JSONObject requestParams, String propertyRequestValue,
Map<K, String> itemProperties, Enum<?> property) throws JSONException
{
String propertyValue = getPropertyValue(itemProperties, property);
@@ -603,9 +543,9 @@ public abstract class BaseAPI
*
* @param username the username with whom the delete is performed
* @param password the user's password
* @param itemPath the path to the item eg. in case of a category it would be the "/" + category name,
* in case of a folder or subCategory it would be /categoryName/folderName or /categoryName/subCategoryName/
* in case of a record /categoryName/folderName/recordName
* @param itemPath the path to the item eg. in case of a category it would be the "/" + category name, in case of a
* folder or subCategory it would be /categoryName/folderName or /categoryName/subCategoryName/ in case
* of a record /categoryName/folderName/recordName
* @return true if the deletion has been successful
*/
protected boolean deleteItem(String username, String password, String itemPath)
@@ -618,7 +558,7 @@ public abstract class BaseAPI
return getObjectByPath(username, password, itemPath) == null;
}
/**
/**
* Retrieve the node ref spaces store value
*
* @return node ref spaces store