From c3eeea134f5e688b3f37c8e73cf6b74b7bb42981 Mon Sep 17 00:00:00 2001 From: Alexandru Epure Date: Tue, 9 Aug 2016 14:12:44 +0000 Subject: [PATCH] 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 --- .../webscripts/ResourceWebScriptPost.java | 41 ++++++++---- .../alfresco/rest/api/tests/NodeApiTest.java | 22 ++++++ .../rest/api/tests/TestNodeRatings.java | 67 ++++++++++++------- .../alfresco/rest/api/tests/TestSites.java | 11 +++ 4 files changed, 103 insertions(+), 38 deletions(-) diff --git a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java index 37bd8b2bf0..11025f5b36 100644 --- a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java +++ b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java @@ -103,14 +103,8 @@ 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)) { 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) { - List params = operation.getParameters(); + if (operation == null) + { + return null; + } + Class objType = resourceMeta.getObjectType(operation); + List 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) { diff --git a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java index cd3a15873e..674a739da3 100644 --- a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java @@ -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 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 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 diff --git a/source/test-java/org/alfresco/rest/api/tests/TestNodeRatings.java b/source/test-java/org/alfresco/rest/api/tests/TestNodeRatings.java index e799ac532f..3258e286fb 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestNodeRatings.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestNodeRatings.java @@ -1,28 +1,28 @@ -/* - * #%L - * Alfresco Remote API - * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited - * %% - * This file is part of the Alfresco software. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2016 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.tests; 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.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 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 resp = nodesProxy.getNodeRatings(nodeRef1.getId(), createParams(getPaging(0, Integer.MAX_VALUE), null)); diff --git a/source/test-java/org/alfresco/rest/api/tests/TestSites.java b/source/test-java/org/alfresco/rest/api/tests/TestSites.java index 30cd08db4d..6b71db658d 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestSites.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestSites.java @@ -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; @@ -311,6 +313,15 @@ public class TestSites extends EnterpriseTestApi sitesProxy.removeSite(siteId); // cleanup } + + // -ve test - cannot create multiple sites in single POST call (unsupported) + { + List 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 {