Merged 5.2.N (5.2.2) to HEAD (5.2)

134418 cturlica: REPO-1303: Update a group
      - added update group capabilities.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@137346 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Andrei Rebegea
2017-06-14 16:57:46 +00:00
parent b74efb3846
commit c708379824
7 changed files with 170 additions and 10 deletions

View File

@@ -58,6 +58,21 @@ public interface Groups
*/
Group create(Group group, Parameters parameters);
/**
* Update the given group. Not all fields are used, only those as defined in
* the Open API spec.
*
* @param groupId
* the group ID
* @param group
* details to use for the update
* @param parameters
* the {@link Parameters} object to get the parameters passed
* into the request including: - include param (parentIds, zones)
* @return Updated group
*/
Group update(String groupId, Group group, Parameters parameters);
/**
* Get a group by it's id.
*

View File

@@ -47,7 +47,8 @@ import java.util.List;
* @author cturlica
*/
@EntityResource(name = "groups", title = "Groups")
public class GroupsEntityResource implements EntityResourceAction.Read<Group>, EntityResourceAction.ReadById<Group>, EntityResourceAction.Create<Group>, InitializingBean
public class GroupsEntityResource
implements EntityResourceAction.Read<Group>, EntityResourceAction.ReadById<Group>, EntityResourceAction.Create<Group>, EntityResourceAction.Update<Group>, InitializingBean
{
private Groups groups;
@@ -86,4 +87,11 @@ public class GroupsEntityResource implements EntityResourceAction.Read<Group>, E
result.add(groups.create(entity.get(0), parameters));
return result;
}
@Override
@WebApiDescription(title = "Update group", description = "Update group")
public Group update(String groupId, Group group, Parameters parameters)
{
return groups.update(groupId, group, parameters);
}
}

View File

@@ -114,7 +114,7 @@ public class GroupsImpl implements Groups
public Group create(Group group, Parameters parameters)
{
validateGroup(group);
validateGroup(group, false);
// Create authority with default zones.
final Set<String> authorityZones = authorityService.getDefaultZones();
@@ -136,6 +136,16 @@ public class GroupsImpl implements Groups
return getGroup(authority, parameters);
}
public Group update(String groupId, Group group, Parameters parameters)
{
validateGroupId(groupId);
validateGroup(group, true);
authorityService.setAuthorityDisplayName(groupId, group.getDisplayName());
return getGroup(groupId, parameters);
}
public Group getGroup(String groupId, Parameters parameters) throws EntityNotFoundException
{
AuthorityInfo authorityInfo = getAuthorityInfo(groupId);
@@ -649,13 +659,15 @@ public class GroupsImpl implements Groups
}
}
private void validateGroup(Group group)
private void validateGroup(Group group, boolean isUpdate)
{
if (group == null)
{
throw new InvalidArgumentException("group is null");
}
if (!isUpdate)
{
if (group.getId() == null || group.getId().isEmpty())
{
throw new InvalidArgumentException("groupId is null or empty");
@@ -666,6 +678,29 @@ public class GroupsImpl implements Groups
throw new ConstraintViolatedException("Group '" + group.getId() + "' already exists.");
}
}
else
{
if (group.wasSet(Group.ID))
{
throw new InvalidArgumentException("Group update does not support field: id");
}
if (group.wasSet(Group.IS_ROOT))
{
throw new InvalidArgumentException("Group update does not support field: isRoot");
}
if (group.wasSet(Group.PARENT_IDS))
{
throw new InvalidArgumentException("Group update does not support field: parentIds");
}
if (group.wasSet(Group.ZONES))
{
throw new InvalidArgumentException("Group update does not support field: zones");
}
}
}
private boolean groupAuthorityExists(String shortName)
{

View File

@@ -25,6 +25,8 @@
*/
package org.alfresco.rest.api.model;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.alfresco.rest.framework.resource.UniqueId;
@@ -44,6 +46,14 @@ public class Group implements Comparable<Group>
protected Set<String> parentIds;
protected Set<String> zones;
private Map<String, Boolean> setFields = new HashMap<>(7);
public static final String ID = "id";
public static final String DISPLAY_NAME = "displayName";
public static final String IS_ROOT = "isRoot";
public static final String PARENT_IDS = "parentIds";
public static final String ZONES = "zones";
public Group()
{
}
@@ -57,6 +67,7 @@ public class Group implements Comparable<Group>
public void setId(String id)
{
this.id = id;
setFields.put(ID, true);
}
public String getDisplayName()
@@ -67,6 +78,7 @@ public class Group implements Comparable<Group>
public void setDisplayName(String displayName)
{
this.displayName = displayName;
setFields.put(DISPLAY_NAME, true);
}
public Boolean getIsRoot()
@@ -77,6 +89,7 @@ public class Group implements Comparable<Group>
public void setIsRoot(Boolean isRoot)
{
this.isRoot = isRoot;
setFields.put(IS_ROOT, true);
}
public Set<String> getParentIds()
@@ -87,6 +100,7 @@ public class Group implements Comparable<Group>
public void setParentIds(Set<String> parentIds)
{
this.parentIds = parentIds;
setFields.put(PARENT_IDS, true);
}
public Set<String> getZones()
@@ -97,6 +111,7 @@ public class Group implements Comparable<Group>
public void setZones(Set<String> zones)
{
this.zones = zones;
setFields.put(ZONES, true);
}
@Override
@@ -141,4 +156,10 @@ public class Group implements Comparable<Group>
{
return "Group [id=" + id + ", displayName=" + displayName + ", isRoot=" + isRoot + "]";
}
public boolean wasSet(String fieldName)
{
Boolean b = setFields.get(fieldName);
return (b != null ? b : false);
}
}

View File

@@ -892,6 +892,73 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
}
}
@Test
public void testUpdateGroup() throws Exception
{
final Groups groupsProxy = publicApiClient.groups();
Map<String, String> otherParams = new HashMap<>();
otherParams.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_PARENT_IDS);
setRequestContext(networkOne.getId(), networkAdmin, DEFAULT_ADMIN_PWD);
Group group = groupsProxy.createGroup(generateGroup(), null, HttpServletResponse.SC_CREATED);
Set<String> subGroupParents = new HashSet<>();
subGroupParents.add(group.getId());
Group generatedSubGroup = generateGroup();
generatedSubGroup.setParentIds(subGroupParents);
Group subGroup = groupsProxy.createGroup(generatedSubGroup, otherParams, HttpServletResponse.SC_CREATED);
// User without admin rights can't update a group.
{
setRequestContext(user1);
groupsProxy.updateGroup(group.getId(), new Group(), null, HttpServletResponse.SC_FORBIDDEN);
}
// Invalid auth.
{
setRequestContext(networkOne.getId(), GUID.generate(), "password");
groupsProxy.updateGroup(group.getId(), new Group(), null, HttpServletResponse.SC_UNAUTHORIZED);
}
// Update group and subgroup.
{
setRequestContext(networkOne.getId(), networkAdmin, DEFAULT_ADMIN_PWD);
String displayName = "newDisplayName";
Group mySubGroup = new Group();
mySubGroup.setDisplayName(displayName);
Group updateGroup = groupsProxy.updateGroup(subGroup.getId(), mySubGroup, otherParams, HttpServletResponse.SC_OK);
// Validate default response and additional information (parentIds).
assertNotNull(updateGroup);
assertNotNull(updateGroup.getId());
assertFalse(updateGroup.getIsRoot());
assertNotNull(updateGroup.getParentIds());
// Check that only display name changed.
assertEquals(displayName, updateGroup.getDisplayName());
// Check that nothing else changed.
assertEquals(subGroup.getId(), updateGroup.getId());
assertEquals(subGroup.getIsRoot(), updateGroup.getIsRoot());
assertEquals(subGroup.getParentIds(), updateGroup.getParentIds());
}
// Group id doesn't exist.
{
setRequestContext(networkOne.getId(), networkAdmin, DEFAULT_ADMIN_PWD);
groupsProxy.updateGroup("invalidId", group, null, HttpServletResponse.SC_NOT_FOUND);
}
}
private Group generateGroup()
{
Group group = new Group();

View File

@@ -2289,6 +2289,12 @@ public class PublicApiClient
return parseGroupEntity(response);
}
public Group updateGroup(String groupId, Group group, Map<String, String> params, int expectedStatus) throws PublicApiException
{
HttpResponse response = update("groups", groupId, null, null, group.toJSON().toString(), params, "Failed to update group " + group.getId(), expectedStatus);
return parseGroupEntity(response);
}
public Group getGroup(String groupId) throws PublicApiException
{
return getGroup(groupId, HttpServletResponse.SC_OK);

View File

@@ -66,9 +66,17 @@ public class Group extends org.alfresco.rest.api.model.Group implements Serializ
public JSONObject toJSON()
{
JSONObject groupJson = new JSONObject();
if (getId() != null)
{
groupJson.put("id", getId());
}
groupJson.put("displayName", getDisplayName());
if (getIsRoot() != null)
{
groupJson.put("isRoot", getIsRoot());
}
if (getParentIds() != null)
{