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

129168 mmuller: Merged RETURN-OF-THE-API (5.2.0) to 5.2.N (5.2.1)
      128517 jvonka: REPO-874: Improve REST fwk: improve error message if a POST operation is attempted with multiple items
      - rationalise the code when handling POST for an API "operation" or POST to a collection that is marked as "allowMultiple=false"
      - they should now consistently return the same existing error message: "Only 1 entity is supported in the HTTP request body"
      - add api sanity tests to "create site", "create rating" and some of the node op's, such as "/move" & "/copy"


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@129342 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alexandru Epure
2016-08-09 14:12:44 +00:00
parent 2254b1fa0e
commit c3eeea134f
4 changed files with 103 additions and 38 deletions

View File

@@ -103,13 +103,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(operationName))
{
Class objectType = resourceMeta.getObjectType(operation);
Object postedObj = null;
if (objectType!= null)
{
//Operations don't support a List as json body
postedObj = ResourceWebScriptHelper.extractJsonContent(req, assistant.getJsonHelper(), objectType);
}
Object postedObj = processRequest(resourceMeta, operation, req);
if (StringUtils.isNotBlank(propertyName))
{
@@ -150,20 +144,41 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
*/
private Object extractObjFromJson(ResourceMetadata resourceMeta, ResourceOperation operation, WebScriptRequest req)
{
List<ResourceParameter> params = operation.getParameters();
if (operation == null)
{
return null;
}
Class<?> objType = resourceMeta.getObjectType(operation);
List<ResourceParameter> params = operation.getParameters();
if (!params.isEmpty())
{
for (ResourceParameter resourceParameter : params)
{
if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(resourceParameter.getParamType()) && !resourceParameter.isAllowMultiple())
// POST to collection may or may not support List as json body, Operations don't support a List as json body
boolean isTypeOperation = resourceMeta.getType().equals(ResourceMetadata.RESOURCE_TYPE.OPERATION);
boolean notMultiple = ((! resourceParameter.isAllowMultiple()) || isTypeOperation);
if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(resourceParameter.getParamType()) && notMultiple)
{
// Only allow 1 value.
try
{
Object content = ResourceWebScriptHelper.extractJsonContent(req,assistant.getJsonHelper(), objType);
return Arrays.asList(content);
Object jsonContent = null;
if (objType != null)
{
jsonContent = ResourceWebScriptHelper.extractJsonContent(req,assistant.getJsonHelper(), objType);
}
if (isTypeOperation)
{
return jsonContent;
}
else
{
return Arrays.asList(jsonContent);
}
}
catch (InvalidArgumentException iae)
{

View File

@@ -1659,6 +1659,17 @@ public class NodeApiTest extends AbstractBaseApiTest
publicApiClient.setRequestContext(new RequestContext("-default-", "admin", "admin"));
response = publicApiClient.post(getScope(), "nodes/"+ddNodeId+"/move", null, null, null, toJsonAsStringNonNull(tgt));
checkStatus(403, response.getStatusCode());
// -ve test - cannot move to multiple destinations in single POST call (unsupported)
List<NodeTarget> nodeDestinations = new ArrayList<>(2);
NodeTarget nodeTarget = new NodeTarget();
nodeTarget.setTargetParentId(f1Id);
nodeDestinations.add(nodeTarget);
nodeTarget = new NodeTarget();
nodeTarget.setTargetParentId(f2Id);
nodeDestinations.add(nodeTarget);
post("nodes/"+d1Id+"/move", user1, toJsonAsStringNonNull(nodeDestinations), null, 405);
}
/**
@@ -1746,6 +1757,17 @@ public class NodeApiTest extends AbstractBaseApiTest
tgt = new NodeTarget();
tgt.setTargetParentId(rootNodeId);
post("nodes/"+d1Id+"/copy", user1, toJsonAsStringNonNull(tgt), null, 403);
// -ve test - cannot copy to multiple destinations in single POST call (unsupported)
List<NodeTarget> nodeDestinations = new ArrayList<>(2);
NodeTarget nodeTarget = new NodeTarget();
nodeTarget.setTargetParentId(sourceId);
nodeDestinations.add(nodeTarget);
nodeTarget = new NodeTarget();
nodeTarget.setTargetParentId(targetId);
nodeDestinations.add(nodeTarget);
post("nodes/"+d1Id+"/copy", user1, toJsonAsStringNonNull(nodeDestinations), null, 405);
}
@Test

View File

@@ -59,6 +59,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.site.SiteVisibility;
import org.alfresco.util.GUID;
import org.apache.commons.httpclient.HttpStatus;
import org.json.simple.JSONArray;
import org.junit.Test;
public class TestNodeRatings extends EnterpriseTestApi
@@ -467,6 +468,22 @@ public class TestNodeRatings extends EnterpriseTestApi
// Test Case cloud-1977
// invalid methods
{
try
{
// -ve test - cannot create multiple ratings in single POST call (unsupported)
List<NodeRating> ratings = new ArrayList<>(2);
ratings.add(new NodeRating("likes", true));
ratings.add(new NodeRating("likes", false));
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
nodesProxy.create("nodes", nodeRef1.getId(), "ratings", null, JSONArray.toJSONString(ratings), "Unable to POST to multiple ratings");
fail();
}
catch(PublicApiException e)
{
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
// get an arbitrary rating
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(), createParams(getPaging(0, Integer.MAX_VALUE), null));

View File

@@ -29,6 +29,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -47,6 +48,7 @@ import org.alfresco.rest.api.tests.client.data.SiteRole;
import org.alfresco.service.cmr.site.SiteVisibility;
import org.alfresco.util.GUID;
import org.apache.commons.httpclient.HttpStatus;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
@@ -312,6 +314,15 @@ public class TestSites extends EnterpriseTestApi
sitesProxy.removeSite(siteId); // cleanup
}
// -ve test - cannot create multiple sites in single POST call (unsupported)
{
List<Site> sites = new ArrayList<>(2);
sites.add(new SiteImpl(null, "siteA1", null, "siteA1", null, SiteVisibility.PRIVATE.toString(), null, null));
sites.add(new SiteImpl(null, "siteB1", null, "siteB1", null, SiteVisibility.PRIVATE.toString(), null, null));
sitesProxy.create("sites", null, null, null, JSONArray.toJSONString(sites), null, 405);
}
// -ve tests - belts-and-braces for unsupported methods
{
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));