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