From d9144eb71274c5197ccaa27287746a62ecd978d2 Mon Sep 17 00:00:00 2001 From: Jamal Kaabi-Mofrad Date: Tue, 10 May 2016 11:23:22 +0000 Subject: [PATCH] Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2) 123214 gjames: RA-812: Changed http 400 to 404 git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126536 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../core/ResourceLookupDictionary.java | 15 ++++++------ .../framework/tests/core/ExecutionTests.java | 19 +++++++++++++++ .../tests/core/ResourceLocatorTests.java | 23 ++++++++++--------- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java b/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java index 46e8ef5139..46a0198fd6 100644 --- a/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java +++ b/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java @@ -7,7 +7,6 @@ import java.util.Map; import java.util.Map.Entry; import org.alfresco.rest.framework.Api; -import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.NotFoundException; import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException; import org.apache.commons.lang.StringUtils; @@ -28,20 +27,20 @@ public class ResourceLookupDictionary implements ResourceLocator private ResourceDictionary dictionary; @Override - public ResourceWithMetadata locateEntityResource(Api api, String entityResource, HttpMethod httpMethod) throws InvalidArgumentException, UnsupportedResourceOperationException + public ResourceWithMetadata locateEntityResource(Api api, String entityResource, HttpMethod httpMethod) throws NotFoundException, UnsupportedResourceOperationException { return locateRelationResource(api, entityResource, (String)null, httpMethod); } @Override - public ResourceWithMetadata locateRelationPropertyResource(Api api, String entityResource, String relationResource, String property, HttpMethod httpMethod) throws InvalidArgumentException,UnsupportedResourceOperationException + public ResourceWithMetadata locateRelationPropertyResource(Api api, String entityResource, String relationResource, String property, HttpMethod httpMethod) throws NotFoundException,UnsupportedResourceOperationException { String resourceKey = ResourceDictionary.resourceKey(entityResource, relationResource); String propertyResourceKey = ResourceDictionary.propertyResourceKey(resourceKey, property); Map apiResources = dictionary.getAllResources().get(api); if (apiResources == null) { - throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API); + throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID); } ResourceWithMetadata resource = apiResources.get(propertyResourceKey); @@ -53,12 +52,12 @@ public class ResourceLookupDictionary implements ResourceLocator } logger.warn("Unable to locate resource resource for :"+entityResource+" "+relationResource==null?"":relationResource+" "+property==null?"":property); - throw new InvalidArgumentException("Unable to locate resource resource for :"+entityResource+" "+(relationResource==null?"":relationResource+" "+property==null?"":property)); + throw new NotFoundException("Unable to locate resource resource for :"+entityResource+" "+(relationResource==null?"":relationResource+" "+property==null?"":property)); } @Override - public ResourceWithMetadata locateRelationResource(Api api, String entityResource, String relationResource, HttpMethod httpMethod) throws InvalidArgumentException,UnsupportedResourceOperationException + public ResourceWithMetadata locateRelationResource(Api api, String entityResource, String relationResource, HttpMethod httpMethod) throws NotFoundException,UnsupportedResourceOperationException { String resourceKey = ResourceDictionary.resourceKey(entityResource, relationResource); if (logger.isDebugEnabled()) @@ -68,7 +67,7 @@ public class ResourceLookupDictionary implements ResourceLocator Map apiResources = dictionary.getAllResources().get(api); if (apiResources == null) { - throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API); + throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID); } ResourceWithMetadata resource = apiResources.get(resourceKey); if (resource == null) @@ -86,7 +85,7 @@ public class ResourceLookupDictionary implements ResourceLocator } } logger.warn("Unable to locate resource resource for :"+entityResource+" "+relationResource==null?"":relationResource); - throw new InvalidArgumentException("Unable to locate resource resource for :"+entityResource+" "+(relationResource==null?"":relationResource)); + throw new NotFoundException("Unable to locate resource resource for :"+entityResource+" "+(relationResource==null?"":relationResource)); } else { diff --git a/source/test-java/org/alfresco/rest/framework/tests/core/ExecutionTests.java b/source/test-java/org/alfresco/rest/framework/tests/core/ExecutionTests.java index da20c068d9..0162613334 100644 --- a/source/test-java/org/alfresco/rest/framework/tests/core/ExecutionTests.java +++ b/source/test-java/org/alfresco/rest/framework/tests/core/ExecutionTests.java @@ -259,6 +259,25 @@ public class ExecutionTests extends AbstractContextTest } + @Test + public void testInvalidUrls() throws IOException + { + AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets"); + executor.setLocator(locator); + executor.setResolver(simpleMappingExceptionResolver); + executor.setJsonHelper(jsonHelper); + Map templateVars = new HashMap(); + templateVars.put("apiScope", "private"); + templateVars.put("apiVersion", "1"); + templateVars.put("apiName", "alfrescomock"); + + WebScriptResponse response = mockResponse(); + templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "blah:"); + executor.execute(api, mockRequest(templateVars, new HashMap>(1)), response); + //Can't find it so a 404 + verify(response, times(1)).setStatus(HttpServletResponse.SC_NOT_FOUND); + } + private WebScriptResponse mockResponse() throws IOException { WebScriptResponse res = mock(WebScriptResponse.class); diff --git a/source/test-java/org/alfresco/rest/framework/tests/core/ResourceLocatorTests.java b/source/test-java/org/alfresco/rest/framework/tests/core/ResourceLocatorTests.java index f2c6fe4895..21efa192d5 100644 --- a/source/test-java/org/alfresco/rest/framework/tests/core/ResourceLocatorTests.java +++ b/source/test-java/org/alfresco/rest/framework/tests/core/ResourceLocatorTests.java @@ -22,6 +22,7 @@ import org.alfresco.rest.framework.core.ResourceMetadata; import org.alfresco.rest.framework.core.ResourceOperation; import org.alfresco.rest.framework.core.ResourceWithMetadata; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; +import org.alfresco.rest.framework.core.exceptions.NotFoundException; import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException; import org.alfresco.rest.framework.resource.EntityResource; import org.alfresco.rest.framework.resource.RelationshipResource; @@ -251,9 +252,9 @@ public class ResourceLocatorTests { //Tests by passing invalid propery collResource = locator.locateResource(api, templateVars, HttpMethod.GET); - fail("Should throw an InvalidArgumentException"); + fail("Should throw an NotFoundException"); } - catch (InvalidArgumentException error) + catch (NotFoundException error) { //this is correct } @@ -328,9 +329,9 @@ public class ResourceLocatorTests try { entityResource = locator.locateEntityResource(Api.valueOf("alfrescomock", "public", "1"),"sheep", HttpMethod.GET); - fail("Should throw an InvalidArgumentException"); + fail("Should throw an NotFoundException"); } - catch (InvalidArgumentException error) + catch (NotFoundException error) { //this is correct } @@ -338,9 +339,9 @@ public class ResourceLocatorTests try { entityResource = locator.locateEntityResource(Api.valueOf("alfrescomock", "public", "999"),"sheep", HttpMethod.GET); - fail("Should throw an InvalidArgumentException"); + fail("Should throw an NotFoundException"); } - catch (InvalidArgumentException error) + catch (NotFoundException error) { //this is correct } @@ -358,9 +359,9 @@ public class ResourceLocatorTests try { aResource = locator.locateEntityResource(api, "sheepnoaction", HttpMethod.GET); - fail("Should throw an InvalidArgumentException"); + fail("Should throw an NotFoundException"); } - catch (InvalidArgumentException error) + catch (NotFoundException error) { //this is correct } @@ -380,7 +381,7 @@ public class ResourceLocatorTests aResource = locator.locateRelationResource(api, "sheepnoaction","v3isaresource", HttpMethod.GET); fail("Only available in v3"); } - catch (InvalidArgumentException error) + catch (NotFoundException error) { //this is correct } @@ -391,7 +392,7 @@ public class ResourceLocatorTests aResource = locator.locateRelationResource(v2, "sheepnoaction","v3isaresource", HttpMethod.GET); fail("Only available in v3"); } - catch (InvalidArgumentException error) + catch (NotFoundException error) { //this is correct } @@ -449,7 +450,7 @@ public class ResourceLocatorTests assertEquals ("sheepnoaction", name); } - @Test(expected=org.alfresco.rest.framework.core.exceptions.InvalidArgumentException.class) + @Test(expected=org.alfresco.rest.framework.core.exceptions.NotFoundException.class) public void testLocateRelationResource() { Collection relKeys = Arrays.asList("blacksheep","baaahh");