mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
RM-6129 Add API tests for creating and editing RM roles.
This commit is contained in:
@@ -310,6 +310,78 @@ public abstract class BaseAPI
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for PUT requests
|
||||
*
|
||||
* @param adminUser user with administrative privileges
|
||||
* @param adminPassword password for adminUser
|
||||
* @param expectedStatusCode The expected return status code.
|
||||
* @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 HttpResponse doPutJsonRequest(String adminUser,
|
||||
String adminPassword,
|
||||
int expectedStatusCode,
|
||||
JSONObject requestParams,
|
||||
String urlTemplate,
|
||||
String... urlTemplateParams)
|
||||
{
|
||||
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
|
||||
return doPutJsonRequest(adminUser, adminPassword, expectedStatusCode, client.getApiUrl(), requestParams, urlTemplate, urlTemplateParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for PUT requests
|
||||
*
|
||||
* @param adminUser user with administrative privileges
|
||||
* @param adminPassword password for adminUser
|
||||
* @param expectedStatusCode The expected return status code.
|
||||
* @param urlStart the start of the URL (for example "alfresco/s/slingshot").
|
||||
* @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>
|
||||
* @throws AssertionError if the returned status code is not as expected.
|
||||
*/
|
||||
private HttpResponse doPutJsonRequest(String adminUser,
|
||||
String adminPassword,
|
||||
int expectedStatusCode,
|
||||
String urlStart,
|
||||
JSONObject requestParams,
|
||||
String urlTemplate,
|
||||
String... urlTemplateParams)
|
||||
{
|
||||
String requestUrl = formatRequestUrl(urlStart, urlTemplate, urlTemplateParams);
|
||||
try
|
||||
{
|
||||
HttpResponse httpResponse = doRequestJson(HttpPut.class, requestUrl, adminUser, adminPassword, requestParams);
|
||||
assertEquals("PUT request to " + requestUrl + " was not successful.", httpResponse.getStatusLine().getStatusCode(), expectedStatusCode);
|
||||
return httpResponse;
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException error)
|
||||
{
|
||||
throw new IllegalArgumentException("doPutRequest failed", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill in the parameters for a URL template.
|
||||
*
|
||||
* @param urlStart The start of the URL.
|
||||
* @param urlTemplate The template.
|
||||
* @param urlTemplateParams Any parameters that need to be filled into the URL template.
|
||||
* @return The resultant URL.
|
||||
*/
|
||||
private String formatRequestUrl(String urlStart, String urlTemplate, String[] urlTemplateParams)
|
||||
{
|
||||
if (urlTemplateParams.length == 1)
|
||||
{
|
||||
// The format method needs some help to know not to use the whole array object.
|
||||
return MessageFormat.format(urlTemplate, urlStart, urlTemplateParams[0]);
|
||||
}
|
||||
return MessageFormat.format(urlTemplate, urlStart, urlTemplateParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for POST requests
|
||||
* @param adminUser user with administrative privileges
|
||||
@@ -403,11 +475,8 @@ public abstract class BaseAPI
|
||||
String urlTemplate,
|
||||
String... urlTemplateParams)
|
||||
{
|
||||
// Ensure the host is part of the request URL.
|
||||
String requestUrl = MessageFormat.format(
|
||||
urlTemplate,
|
||||
urlStart,
|
||||
urlTemplateParams);
|
||||
String requestUrl;
|
||||
requestUrl = formatRequestUrl(urlStart, urlTemplate, urlTemplateParams);
|
||||
try
|
||||
{
|
||||
HttpResponse httpResponse = doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams);
|
||||
|
@@ -71,8 +71,10 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class RMRolesAndActionsAPI extends BaseAPI
|
||||
{
|
||||
/** The URI to view the configured roles and capabilities. Using is=true includes the in-place readers and writers. */
|
||||
private static final String RM_ROLES = "{0}rma/admin/rmroles?is=true";
|
||||
/** The URI to view the configured roles and capabilities. */
|
||||
private static final String RM_ROLES = "{0}rma/admin/rmroles";
|
||||
/** The URI for REST requests about a particular configured role. */
|
||||
private static final String RM_ROLES_ROLE = RM_ROLES + "/{1}";
|
||||
private static final String RM_ROLES_AUTHORITIES = "{0}rm/roles/{1}/authorities/{2}?alf_ticket={3}";
|
||||
|
||||
// logger
|
||||
@@ -100,7 +102,8 @@ public class RMRolesAndActionsAPI extends BaseAPI
|
||||
*/
|
||||
public Set<String> getConfiguredRoles(String adminUser, String adminPassword)
|
||||
{
|
||||
JSONObject jsonObject = doGetRequest(adminUser, adminPassword, RM_ROLES).getJSONObject("data");
|
||||
// Using "is=true" includes the in-place readers and writers.
|
||||
JSONObject jsonObject = doGetRequest(adminUser, adminPassword, RM_ROLES + "?is=true").getJSONObject("data");
|
||||
return jsonObject.toMap().keySet();
|
||||
}
|
||||
|
||||
@@ -119,6 +122,45 @@ public class RMRolesAndActionsAPI extends BaseAPI
|
||||
return jsonObject.getJSONObject(role).getJSONObject("capabilities").keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RM role.
|
||||
*
|
||||
* @param adminUser The username of the admin user.
|
||||
* @param adminPassword The password for the admin user.
|
||||
* @param roleName The name of the new role.
|
||||
* @param roleDisplayLabel A human-readable label for the role.
|
||||
* @param capabilities A list of capabilities for the role.
|
||||
*/
|
||||
public void createRole(String adminUser, String adminPassword, String roleName, String roleDisplayLabel, Set<String> capabilities)
|
||||
{
|
||||
JSONObject requestBody = new JSONObject();
|
||||
requestBody.put("name", roleName);
|
||||
requestBody.put("displayLabel", roleDisplayLabel);
|
||||
JSONArray capabilitiesArray = new JSONArray();
|
||||
capabilities.forEach(capabilitiesArray::put);
|
||||
requestBody.put("capabilities", capabilitiesArray);
|
||||
doPostJsonRequest(adminUser, adminPassword, HttpStatus.SC_OK, requestBody, RM_ROLES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing RM role.
|
||||
*
|
||||
* @param adminUser The username of the admin user.
|
||||
* @param adminPassword The password for the admin user.
|
||||
* @param roleName The name of the new role.
|
||||
* @param roleDisplayLabel A human-readable label for the role.
|
||||
* @param capabilities A list of capabilities for the role.
|
||||
*/
|
||||
public void updateRole(String adminUser, String adminPassword, String roleName, String roleDisplayLabel, Set<String> capabilities)
|
||||
{
|
||||
JSONObject requestBody = new JSONObject();
|
||||
requestBody.put("name", roleName);
|
||||
requestBody.put("displayLabel", roleDisplayLabel);
|
||||
JSONArray capabilitiesArray = new JSONArray();
|
||||
capabilities.forEach(capabilitiesArray::put);
|
||||
requestBody.put("capabilities", capabilitiesArray);
|
||||
doPutJsonRequest(adminUser, adminPassword, HttpStatus.SC_OK, requestBody, RM_ROLES_ROLE, roleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* create user and assign to records management role
|
||||
|
Reference in New Issue
Block a user