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,14 +103,8 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(operationName)) if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(operationName))
{ {
Class objectType = resourceMeta.getObjectType(operation); Object postedObj = processRequest(resourceMeta, operation, req);
Object postedObj = null;
if (objectType!= null)
{
//Operations don't support a List as json body
postedObj = ResourceWebScriptHelper.extractJsonContent(req, assistant.getJsonHelper(), objectType);
}
if (StringUtils.isNotBlank(propertyName)) if (StringUtils.isNotBlank(propertyName))
{ {
return Params.valueOf(entityId, relationshipId, params, postedObj, req); return Params.valueOf(entityId, relationshipId, params, postedObj, req);
@@ -150,20 +144,41 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
*/ */
private Object extractObjFromJson(ResourceMetadata resourceMeta, ResourceOperation operation, WebScriptRequest req) private Object extractObjFromJson(ResourceMetadata resourceMeta, ResourceOperation operation, WebScriptRequest req)
{ {
List<ResourceParameter> params = operation.getParameters(); if (operation == null)
{
return null;
}
Class<?> objType = resourceMeta.getObjectType(operation); Class<?> objType = resourceMeta.getObjectType(operation);
List<ResourceParameter> params = operation.getParameters();
if (!params.isEmpty()) if (!params.isEmpty())
{ {
for (ResourceParameter resourceParameter : params) 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. // Only allow 1 value.
try try
{ {
Object content = ResourceWebScriptHelper.extractJsonContent(req,assistant.getJsonHelper(), objType); Object jsonContent = null;
return Arrays.asList(content); if (objType != null)
{
jsonContent = ResourceWebScriptHelper.extractJsonContent(req,assistant.getJsonHelper(), objType);
}
if (isTypeOperation)
{
return jsonContent;
}
else
{
return Arrays.asList(jsonContent);
}
} }
catch (InvalidArgumentException iae) catch (InvalidArgumentException iae)
{ {

View File

@@ -1659,6 +1659,17 @@ public class NodeApiTest extends AbstractBaseApiTest
publicApiClient.setRequestContext(new RequestContext("-default-", "admin", "admin")); publicApiClient.setRequestContext(new RequestContext("-default-", "admin", "admin"));
response = publicApiClient.post(getScope(), "nodes/"+ddNodeId+"/move", null, null, null, toJsonAsStringNonNull(tgt)); response = publicApiClient.post(getScope(), "nodes/"+ddNodeId+"/move", null, null, null, toJsonAsStringNonNull(tgt));
checkStatus(403, response.getStatusCode()); 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 = new NodeTarget();
tgt.setTargetParentId(rootNodeId); tgt.setTargetParentId(rootNodeId);
post("nodes/"+d1Id+"/copy", user1, toJsonAsStringNonNull(tgt), null, 403); 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 @Test

View File

@@ -1,28 +1,28 @@
/* /*
* #%L * #%L
* Alfresco Remote API * Alfresco Remote API
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.api.tests; package org.alfresco.rest.api.tests;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -59,6 +59,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.site.SiteVisibility; import org.alfresco.service.cmr.site.SiteVisibility;
import org.alfresco.util.GUID; import org.alfresco.util.GUID;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.json.simple.JSONArray;
import org.junit.Test; import org.junit.Test;
public class TestNodeRatings extends EnterpriseTestApi public class TestNodeRatings extends EnterpriseTestApi
@@ -467,6 +468,22 @@ public class TestNodeRatings extends EnterpriseTestApi
// Test Case cloud-1977 // Test Case cloud-1977
// invalid methods // 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 // get an arbitrary rating
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId())); publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(), createParams(getPaging(0, Integer.MAX_VALUE), null)); 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.assertNotNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; 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.service.cmr.site.SiteVisibility;
import org.alfresco.util.GUID; import org.alfresco.util.GUID;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -311,6 +313,15 @@ public class TestSites extends EnterpriseTestApi
sitesProxy.removeSite(siteId); // cleanup 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 // -ve tests - belts-and-braces for unsupported methods
{ {