diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java index 4feb2dac87..1b71e49e80 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java @@ -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 urlTemplate + */ + 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 urlTemplate + * @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); diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java index af7f204e97..5a389e0656 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java @@ -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 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 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 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 diff --git a/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/rmroles/RMRolesTests.java b/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/rmroles/RMRolesTests.java index d064db90ee..f7773f95ee 100644 --- a/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/rmroles/RMRolesTests.java +++ b/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/rmroles/RMRolesTests.java @@ -27,8 +27,11 @@ package org.alfresco.rest.rm.community.rmroles; +import static java.util.Collections.singleton; + import static com.google.common.collect.Sets.newHashSet; +import static org.alfresco.rest.rm.community.util.CommonTestUtils.generateTestPrefix; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -49,6 +52,12 @@ public class RMRolesTests extends BaseRMRestTest { /** The name of the RM user role. */ private static final String RM_USER = "User"; + /** The id of the view records capability. */ + public static final String VIEW_RECORDS_CAP = "ViewRecords"; + /** The id of the declare records capability. */ + public static final String DECLARE_RECORDS_CAP = "DeclareRecords"; + /** A list of capabilities. */ + private static final java.util.HashSet CAPABILITIES = newHashSet(VIEW_RECORDS_CAP, DECLARE_RECORDS_CAP); /** The names of the expected default RM roles. */ private static final Set ROLES = newHashSet("Administrator", "RecordsManager", "PowerUser", "SecurityOfficer", RM_USER); @@ -71,7 +80,38 @@ public class RMRolesTests extends BaseRMRestTest { Set capabilities = rmRolesAndActionsAPI .getCapabilitiesForRole(getAdminUser().getUsername(), getAdminUser().getPassword(), RM_USER); - assertEquals("Unexpected capabilities found for RM User.", capabilities, - newHashSet("ViewRecords", "DeclareRecords")); + assertEquals("Unexpected capabilities found for RM User.", capabilities, CAPABILITIES); + } + + /** Check that a new role can be created and retrieved. */ + @Test(description = "Create a new role.") + public void createNewRole() + { + String roleName = generateTestPrefix(RMRolesTests.class) + "newName"; + + // Call the endpoint under test. + rmRolesAndActionsAPI.createRole(getAdminUser().getUsername(), getAdminUser().getPassword(), roleName, + "New Role Label", CAPABILITIES); + + Set actualCapabilities = rmRolesAndActionsAPI + .getCapabilitiesForRole(getAdminUser().getUsername(), getAdminUser().getPassword(), roleName); + assertEquals("Unexpected capabilities found for RM User.", actualCapabilities, CAPABILITIES); + } + + /** Check that a role can be edited. */ + @Test(description = "Update a role.") + public void updateRole() + { + String roleName = generateTestPrefix(RMRolesTests.class) + "Name"; + rmRolesAndActionsAPI.createRole(getAdminUser().getUsername(), getAdminUser().getPassword(), roleName, "Label", + singleton(VIEW_RECORDS_CAP)); + + // Call the endpoint under test. + rmRolesAndActionsAPI.updateRole(getAdminUser().getUsername(), getAdminUser().getPassword(), roleName, + "Updated Label", singleton(DECLARE_RECORDS_CAP)); + + Set actualCapabilities = rmRolesAndActionsAPI + .getCapabilitiesForRole(getAdminUser().getUsername(), getAdminUser().getPassword(), roleName); + assertEquals("Unexpected capabilities for edited RM User.", actualCapabilities, singleton(DECLARE_RECORDS_CAP)); } }