mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
REPO-1304: Create a group
- added create group logic; fixed a sorting bug for get groups/group members; updated test framework - post/create action; fix toJSON issue for parentIds and zones. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@134310 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -32,8 +32,10 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@@ -182,6 +184,25 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
|
||||
|
||||
checkList(expected, respPostProcess.getPaging(), respPostProcess);
|
||||
}
|
||||
{
|
||||
// paging
|
||||
Paging paging = getPaging(0, Integer.MAX_VALUE);
|
||||
|
||||
Map<String, String> otherParams = new HashMap<>();
|
||||
addOrderBy(otherParams, org.alfresco.rest.api.Groups.PARAM_ID, null);
|
||||
|
||||
// Get and sort groups using canned query.
|
||||
ListResponse<Group> respCannedQuery = getGroups(paging, otherParams);
|
||||
|
||||
// Get and sort groups using postprocessing.
|
||||
otherParams.put("where", "(isRoot=true)");
|
||||
ListResponse<Group> respPostProcess = getGroups(paging, otherParams);
|
||||
|
||||
List<Group> expected = respCannedQuery.getList();
|
||||
expected.retainAll(respPostProcess.getList());
|
||||
|
||||
checkList(expected, respPostProcess.getPaging(), respPostProcess);
|
||||
}
|
||||
|
||||
// Sort by displayName.
|
||||
{
|
||||
@@ -614,4 +635,80 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
|
||||
clearAuthorityContext();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGroup() throws Exception
|
||||
{
|
||||
final Groups groupsProxy = publicApiClient.groups();
|
||||
|
||||
// User without admin rights can't create a group.
|
||||
{
|
||||
setRequestContext(user1);
|
||||
|
||||
Group group = generateGroup();
|
||||
groupsProxy.createGroup(group, null, HttpServletResponse.SC_FORBIDDEN);
|
||||
}
|
||||
|
||||
// Invalid auth.
|
||||
{
|
||||
setRequestContext(networkOne.getId(), GUID.generate(), "password");
|
||||
groupsProxy.createGroup(generateGroup(), null, HttpServletResponse.SC_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
// Create group and subgroup.
|
||||
{
|
||||
setRequestContext(networkOne.getId(), networkAdmin, DEFAULT_ADMIN_PWD);
|
||||
|
||||
Map<String, String> otherParams = new HashMap<>();
|
||||
otherParams.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_PARENT_IDS);
|
||||
|
||||
Group group = generateGroup();
|
||||
|
||||
Group createdGroup01 = groupsProxy.createGroup(group, null, HttpServletResponse.SC_CREATED);
|
||||
|
||||
assertNotNull(createdGroup01);
|
||||
assertNotNull(createdGroup01.getId());
|
||||
assertTrue(createdGroup01.getIsRoot());
|
||||
assertNull(createdGroup01.getParentIds());
|
||||
|
||||
Set<String> subGroup01Parents = new HashSet<>();
|
||||
subGroup01Parents.add(createdGroup01.getId());
|
||||
|
||||
Group subGroup01 = generateGroup();
|
||||
subGroup01.setParentIds(subGroup01Parents);
|
||||
|
||||
Group createdSubGroup01 = groupsProxy.createGroup(subGroup01, otherParams, HttpServletResponse.SC_CREATED);
|
||||
assertNotNull(createdSubGroup01);
|
||||
assertNotNull(createdSubGroup01.getId());
|
||||
assertFalse(createdSubGroup01.getIsRoot());
|
||||
assertNotNull(createdSubGroup01.getParentIds());
|
||||
assertEquals(subGroup01Parents, createdSubGroup01.getParentIds());
|
||||
}
|
||||
|
||||
// Group id is missing.
|
||||
{
|
||||
setRequestContext(networkOne.getId(), networkAdmin, DEFAULT_ADMIN_PWD);
|
||||
|
||||
Group group = new Group();
|
||||
groupsProxy.createGroup(group, null, HttpServletResponse.SC_BAD_REQUEST);
|
||||
}
|
||||
|
||||
// Id clashes with an existing group.
|
||||
{
|
||||
setRequestContext(networkOne.getId(), networkAdmin, DEFAULT_ADMIN_PWD);
|
||||
|
||||
Group group = generateGroup();
|
||||
|
||||
groupsProxy.createGroup(group, null, HttpServletResponse.SC_CREATED);
|
||||
groupsProxy.createGroup(group, null, HttpServletResponse.SC_CONFLICT);
|
||||
}
|
||||
}
|
||||
|
||||
private Group generateGroup()
|
||||
{
|
||||
Group group = new Group();
|
||||
group.setId("TST" + GUID.generate());
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
@@ -480,12 +480,23 @@ public class PublicApiClient
|
||||
public HttpResponse post(String scope, String entityCollectionName, Object entityId, String relationCollectionName, Object relationshipEntityId, String body) throws IOException
|
||||
{
|
||||
HttpResponse response = client.post(getRequestContext(), scope, entityCollectionName, entityId, relationCollectionName, relationshipEntityId != null ? relationshipEntityId.toString() : null, body);
|
||||
|
||||
|
||||
logger.debug(response.toString());
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponse post(String scope, String entityCollectionName, Object entityId, String relationCollectionName, Object relationshipEntityId, String body,
|
||||
final Map<String, String> params) throws IOException
|
||||
{
|
||||
HttpResponse response = client.post(getRequestContext(), scope, entityCollectionName, entityId, relationCollectionName,
|
||||
relationshipEntityId != null ? relationshipEntityId.toString() : null, body, params);
|
||||
|
||||
logger.debug(response.toString());
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponse post(String scope, String entityCollectionName, Object entityId, String relationCollectionName, Object relationshipEntityId,
|
||||
String body, String contentType) throws IOException
|
||||
{
|
||||
@@ -747,20 +758,27 @@ public class PublicApiClient
|
||||
{
|
||||
return create(entityCollectionName, entityId, relationCollectionName, relationId, body, errorMessage, HttpServletResponse.SC_CREATED);
|
||||
}
|
||||
|
||||
public HttpResponse create(String entityCollectionName, String entityId, String relationCollectionName, String relationId, String body, String errorMessage, int expectedStatus) throws PublicApiException
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponse response = post("public", entityCollectionName, entityId, relationCollectionName, relationId, body);
|
||||
|
||||
public HttpResponse create(String entityCollectionName, String entityId, String relationCollectionName, String relationId, String body, String errorMessage,
|
||||
int expectedStatus) throws PublicApiException
|
||||
{
|
||||
return create(entityCollectionName, entityId, relationCollectionName, relationId, body, errorMessage, expectedStatus, null);
|
||||
}
|
||||
|
||||
public HttpResponse create(String entityCollectionName, String entityId, String relationCollectionName, String relationId, String body, String errorMessage,
|
||||
int expectedStatus, Map<String, String> params) throws PublicApiException
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponse response = post("public", entityCollectionName, entityId, relationCollectionName, relationId, body, params);
|
||||
checkStatus(errorMessage, expectedStatus, response);
|
||||
return response;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new PublicApiException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new PublicApiException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public HttpResponse remove(String entityCollectionName, String entityId, String relationCollectionName, String relationId, String errorMessage) throws PublicApiException
|
||||
{
|
||||
@@ -2265,6 +2283,12 @@ public class PublicApiClient
|
||||
public class Groups extends AbstractProxy
|
||||
{
|
||||
|
||||
public Group createGroup(Group group, Map<String, String> params, int expectedStatus) throws PublicApiException
|
||||
{
|
||||
HttpResponse response = create("groups", null, null, null, group.toJSON().toString(), "Failed to create group " + group.getId(), expectedStatus, params);
|
||||
return parseGroupEntity(response);
|
||||
}
|
||||
|
||||
public Group getGroup(String groupId) throws PublicApiException
|
||||
{
|
||||
return getGroup(groupId, HttpServletResponse.SC_OK);
|
||||
@@ -2278,6 +2302,11 @@ public class PublicApiClient
|
||||
public Group getGroup(String groupId, Map<String, String> params, int expectedStatus) throws PublicApiException
|
||||
{
|
||||
HttpResponse response = getSingle("groups", groupId, null, null, params, "Failed to get group " + groupId, expectedStatus);
|
||||
return parseGroupEntity(response);
|
||||
}
|
||||
|
||||
private Group parseGroupEntity(HttpResponse response)
|
||||
{
|
||||
if ((response != null) && (response.getJsonResponse() != null))
|
||||
{
|
||||
JSONObject jsonEntity = (JSONObject) response.getJsonResponse().get("entry");
|
||||
|
@@ -491,6 +491,12 @@ public class PublicApiHttpClient
|
||||
return post(rq, scope, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, body, "application/json");
|
||||
}
|
||||
|
||||
public HttpResponse post(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId,
|
||||
final String relationCollectionName, final Object relationshipEntityId, final String body, final Map<String, String> params) throws IOException
|
||||
{
|
||||
return post(rq, scope, 1, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, body, "application/json", params);
|
||||
}
|
||||
|
||||
public HttpResponse post(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId,
|
||||
final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType) throws IOException
|
||||
{
|
||||
@@ -498,10 +504,16 @@ public class PublicApiHttpClient
|
||||
}
|
||||
|
||||
public HttpResponse post(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId,
|
||||
final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType) throws IOException
|
||||
final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType) throws IOException
|
||||
{
|
||||
return post(rq, scope, version, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, body, contentType, null);
|
||||
}
|
||||
|
||||
public HttpResponse post(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId,
|
||||
final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType, final Map<String, String> params) throws IOException
|
||||
{
|
||||
RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName,
|
||||
relationshipEntityId, null);
|
||||
relationshipEntityId, params);
|
||||
String url = endpoint.getUrl();
|
||||
|
||||
PostMethod req = new PostMethod(url.toString());
|
||||
|
@@ -32,7 +32,6 @@ import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging;
|
||||
import org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse;
|
||||
@@ -73,12 +72,12 @@ public class Group extends org.alfresco.rest.api.model.Group implements Serializ
|
||||
|
||||
if (getParentIds() != null)
|
||||
{
|
||||
groupJson.put("parentIds", getParentIds());
|
||||
groupJson.put("parentIds", new ArrayList(getParentIds()));
|
||||
}
|
||||
|
||||
if (getZones() != null)
|
||||
{
|
||||
groupJson.put("zones", getZones());
|
||||
groupJson.put("zones", new ArrayList(getZones()));
|
||||
}
|
||||
|
||||
return groupJson;
|
||||
|
Reference in New Issue
Block a user