diff --git a/config/alfresco/templates/publicapi/org/alfresco/api/MetaDataWebScript.options.desc.xml b/config/alfresco/templates/publicapi/org/alfresco/api/MetaDataWebScript.options.desc.xml index c968701a6c..5d4a71d3b2 100644 --- a/config/alfresco/templates/publicapi/org/alfresco/api/MetaDataWebScript.options.desc.xml +++ b/config/alfresco/templates/publicapi/org/alfresco/api/MetaDataWebScript.options.desc.xml @@ -10,6 +10,8 @@ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource} /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property}/ user argument none diff --git a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.delete.desc.xml b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.delete.desc.xml index 062e7a1b4a..247b58f32d 100644 --- a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.delete.desc.xml +++ b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.delete.desc.xml @@ -10,6 +10,8 @@ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource} /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property}/ user argument diff --git a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.get.desc.xml b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.get.desc.xml index 41d4b52d55..9828e3bf30 100644 --- a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.get.desc.xml +++ b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.get.desc.xml @@ -10,6 +10,8 @@ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource} /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property}/ user argument diff --git a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.post.desc.xml b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.post.desc.xml index 6d4e7acc16..460eac4aa4 100644 --- a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.post.desc.xml +++ b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.post.desc.xml @@ -10,6 +10,8 @@ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource} /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property}/ user argument diff --git a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.put.desc.xml b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.put.desc.xml index f9486471dc..5e531c1c19 100644 --- a/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.put.desc.xml +++ b/config/alfresco/templates/publicapi/org/alfresco/api/ResourceWebScript.put.desc.xml @@ -10,6 +10,8 @@ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource} /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/ /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property} + /{apiScope}/{apiName}/versions/{apiVersion}/{collectionResource}/{entityId}/{relationResource}/{relationshipId}/{property}/ user argument diff --git a/source/java/org/alfresco/rest/framework/core/ResourceDictionary.java b/source/java/org/alfresco/rest/framework/core/ResourceDictionary.java index 947aafd4cc..b985caab5e 100644 --- a/source/java/org/alfresco/rest/framework/core/ResourceDictionary.java +++ b/source/java/org/alfresco/rest/framework/core/ResourceDictionary.java @@ -42,11 +42,12 @@ public class ResourceDictionary } /* - * Return a key by combining the entity and property ids + * Return a key by combining the rootEntity and property ids */ public static String propertyResourceKey(String entity, String property) { - return "/"+entity+"/"+property; + String rootEntity = entity.startsWith("/")?entity:"/"+entity; + return rootEntity+"/{id}/"+property; } /** diff --git a/source/java/org/alfresco/rest/framework/core/ResourceInspector.java b/source/java/org/alfresco/rest/framework/core/ResourceInspector.java index 826ec05d44..f47aa58470 100644 --- a/source/java/org/alfresco/rest/framework/core/ResourceInspector.java +++ b/source/java/org/alfresco/rest/framework/core/ResourceInspector.java @@ -195,6 +195,9 @@ public class ResourceInspector Map annotAttribs = AnnotationUtils.getAnnotationAttributes(annot); String urlPath = String.valueOf(annotAttribs.get("name")); String entityPath = findEntityNameByAnnotationAttributes(annotAttribs); + String relationshipKey = ResourceDictionary.resourceKey(entityPath,urlPath); + Api api = inspectApi(resource); + List metainfo = new ArrayList(); MetaHelper helper = new MetaHelper(resource); findOperation(RelationshipResourceAction.Create.class, HttpMethod.POST, helper); @@ -214,13 +217,16 @@ public class ResourceInspector if (resource.isAnnotationPresent(WebApiDeleted.class)) { - return Arrays.asList(new ResourceMetadata(ResourceDictionary.resourceKey(entityPath,urlPath), RESOURCE_TYPE.RELATIONSHIP, null, inspectApi(resource), ALL_RELATIONSHIP_RESOURCE_INTERFACES, apiNoAuth, entityPath)); + metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, null, inspectApi(resource), ALL_RELATIONSHIP_RESOURCE_INTERFACES, apiNoAuth, entityPath)); } else { - return Arrays.asList(new ResourceMetadata(ResourceDictionary.resourceKey(entityPath,urlPath), RESOURCE_TYPE.RELATIONSHIP, helper.operations, inspectApi(resource), helper.apiDeleted, apiNoAuth, entityPath)); + metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, helper.operations, inspectApi(resource), helper.apiDeleted, apiNoAuth, entityPath)); } - + + inspectAddressedProperties(api, resource, relationshipKey, metainfo); + inspectOperations(api, resource, relationshipKey, metainfo); + return metainfo; } /** @@ -599,7 +605,7 @@ public class ResourceInspector { Map annotAttribs = AnnotationUtils.getAnnotationAttributes(annot); String actionName = String.valueOf(annotAttribs.get("value")); - String actionPath = ResourceDictionary.resourceKey(entityPath,actionName); + String actionPath = ResourceDictionary.propertyResourceKey(entityPath,actionName); ResourceOperation ro = inspectOperation(anyClass, annotatedMethod, HttpMethod.POST); embeds.put(actionPath, new Pair(ro,annotatedMethod)); } diff --git a/source/java/org/alfresco/rest/framework/core/ResourceLocator.java b/source/java/org/alfresco/rest/framework/core/ResourceLocator.java index 5d81505349..6f2ad1efd2 100644 --- a/source/java/org/alfresco/rest/framework/core/ResourceLocator.java +++ b/source/java/org/alfresco/rest/framework/core/ResourceLocator.java @@ -20,6 +20,7 @@ public interface ResourceLocator public static final String ENTITY_ID = "entityId"; public static final String RELATIONSHIP_RESOURCE = "relationResource"; public static final String RELATIONSHIP_ID = "relationshipId"; + public static final String PROPERTY = "property"; /** * Finds an Entity Resource and returns it in ResourceWithMetadata wrapper. @@ -32,6 +33,19 @@ public interface ResourceLocator */ ResourceWithMetadata locateEntityResource(Api api, String resourceName, HttpMethod httpMethod) throws InvalidArgumentException, UnsupportedResourceOperationException; + /** + * Finds a property or action on a Relationship Resource and returns it in ResourceWithMetadata wrapper. + * @param api - The API being used. + * @param resourceName - The entity resource name - this is the "entityResourceName" property on the @RelationshipResource annotation. + * @param relationName - The relationship resource name - this is the "name" property on the @RelationshipResource annotation. + * @param property - The property resource name - can be either an action or a @BinaryProperty + * @param httpMethod - A permitted HttpMethod + * @return ResourceWithMetadata - The resource and its metadata. + * @throws InvalidArgumentException - thrown if either the api or resourceName's are invalid. ie. A resource doesn't exist. + * @throws UnsupportedResourceOperationException - throw if the resource does not support the specified HttpMethod. + */ + ResourceWithMetadata locateRelationPropertyResource(Api api, String entityResource, String relationResource, String property, HttpMethod httpMethod) throws InvalidArgumentException,UnsupportedResourceOperationException; + /** * Finds an Relationship Resource and returns it in ResourceWithMetadata wrapper. * @param api - The API being used. diff --git a/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java b/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java index c90303318b..6b92c84df9 100644 --- a/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java +++ b/source/java/org/alfresco/rest/framework/core/ResourceLookupDictionary.java @@ -33,6 +33,29 @@ public class ResourceLookupDictionary implements ResourceLocator return locateRelationResource(api, entityResource, (String)null, httpMethod); } + @Override + public ResourceWithMetadata locateRelationPropertyResource(Api api, String entityResource, String relationResource, String property, HttpMethod httpMethod) throws InvalidArgumentException,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); + } + + ResourceWithMetadata resource = apiResources.get(propertyResourceKey); + if (resource != null) + { + if (!resource.getMetaData().supports(httpMethod)) { throw new UnsupportedResourceOperationException(); } + return resource; + } + + 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)); + + } + @Override public ResourceWithMetadata locateRelationResource(Api api, String entityResource, String relationResource, HttpMethod httpMethod) throws InvalidArgumentException,UnsupportedResourceOperationException { @@ -83,7 +106,12 @@ public class ResourceLookupDictionary implements ResourceLocator String collectionName = templateVars.get(COLLECTION_RESOURCE); String entityId = templateVars.get(ENTITY_ID); String resourceName = templateVars.get(RELATIONSHIP_RESOURCE); - + String property = templateVars.get(PROPERTY); + + if (StringUtils.isNotBlank(property)) + { + return locateRelationPropertyResource(api,collectionName ,resourceName, property,httpMethod); + } if (StringUtils.isNotBlank(resourceName)) { return locateRelationResource(api,collectionName ,resourceName,httpMethod); diff --git a/source/test-java/org/alfresco/rest/framework/tests/api/mocks/SheepBaaaahResource.java b/source/test-java/org/alfresco/rest/framework/tests/api/mocks/SheepBaaaahResource.java index 6a58de2acd..d6d14eac33 100644 --- a/source/test-java/org/alfresco/rest/framework/tests/api/mocks/SheepBaaaahResource.java +++ b/source/test-java/org/alfresco/rest/framework/tests/api/mocks/SheepBaaaahResource.java @@ -1,10 +1,18 @@ package org.alfresco.rest.framework.tests.api.mocks; +import java.io.InputStream; import java.util.Arrays; import java.util.List; +import org.alfresco.rest.framework.BinaryProperties; +import org.alfresco.rest.framework.Operation; +import org.alfresco.rest.framework.WebApiDescription; +import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; import org.alfresco.rest.framework.resource.RelationshipResource; +import org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction; import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction; +import org.alfresco.rest.framework.resource.content.BasicContentInfo; +import org.alfresco.rest.framework.resource.content.BinaryResource; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.Parameters; @@ -14,7 +22,7 @@ import org.alfresco.rest.framework.resource.parameters.Parameters; * @author Gethin James */ @RelationshipResource(name = "baaahh", entityResource=SheepEntityResource.class, title = "Sheep baaah") -public class SheepBaaaahResource implements RelationshipResourceAction.Read, RelationshipResourceAction.ReadById +public class SheepBaaaahResource implements RelationshipResourceAction.Read, RelationshipResourceAction.ReadById, BinaryResourceAction.Read, BinaryResourceAction.Delete,BinaryResourceAction.Update { @Override @@ -31,4 +39,33 @@ public class SheepBaaaahResource implements RelationshipResourceAction.Read +public class GoatRelationshipResource implements RelationshipResourceAction.Read, BinaryResourceAction.Read { @Override public CollectionWithPagingInfo readAll(String entityResourceId, Parameters params) @@ -19,4 +28,12 @@ public class GoatRelationshipResource implements RelationshipResourceAction.Read return CollectionWithPagingInfo.asPagedCollection(new Herd("bigun")); } + @WebApiDescription(title = "Download content", description = "Download content") + @BinaryProperties({"content"}) + public BinaryResource readProperty(String herdId, Parameters parameters) throws EntityNotFoundException + { + File file = TempFileProvider.createTempFile("Its a goat", ".txt"); + return new FileBinaryResource(file); + } + } diff --git a/source/test-java/org/alfresco/rest/framework/tests/core/InspectorTests.java b/source/test-java/org/alfresco/rest/framework/tests/core/InspectorTests.java index 753313ce29..17510f247f 100644 --- a/source/test-java/org/alfresco/rest/framework/tests/core/InspectorTests.java +++ b/source/test-java/org/alfresco/rest/framework/tests/core/InspectorTests.java @@ -15,6 +15,7 @@ import java.util.Map; import org.alfresco.rest.api.model.Comment; import org.alfresco.rest.api.nodes.NodeCommentsRelation; +import org.alfresco.rest.api.nodes.NodesEntityResource; import org.alfresco.rest.framework.Api; import org.alfresco.rest.framework.core.OperationResourceMetaData; import org.alfresco.rest.framework.core.ResourceInspector; @@ -34,6 +35,7 @@ import org.alfresco.rest.framework.tests.api.mocks.MultiPartTestEntityResource; import org.alfresco.rest.framework.tests.api.mocks.MultiPartTestRelationshipResource; import org.alfresco.rest.framework.tests.api.mocks.MultiPartTestResponse; import org.alfresco.rest.framework.tests.api.mocks.Sheep; +import org.alfresco.rest.framework.tests.api.mocks.SheepBaaaahResource; import org.alfresco.rest.framework.tests.api.mocks.SheepBlackSheepResource; import org.alfresco.rest.framework.tests.api.mocks.SheepEntityResource; import org.alfresco.rest.framework.tests.api.mocks.SheepNoActionEntityResource; @@ -43,6 +45,7 @@ import org.alfresco.rest.framework.tests.api.mocks2.FarmersSon; import org.alfresco.rest.framework.tests.api.mocks3.Flock; import org.alfresco.rest.framework.tests.api.mocks3.FlockEntityResource; import org.alfresco.rest.framework.tests.api.mocks3.FlocketEntityResource; +import org.alfresco.rest.framework.tests.api.mocks3.GoatRelationshipResource; import org.alfresco.rest.framework.tests.api.mocks3.GrassEntityResourceNowDeleted; import org.alfresco.rest.framework.tests.api.mocks3.SheepBlackSheepResourceIsNoMore; import org.alfresco.rest.framework.tests.api.mocks3.SheepEntityResourceWithDeletedMethods; @@ -335,12 +338,27 @@ public class InspectorTests assertTrue(op.getTitle().startsWith("Reads a photo as a Stream")); aMethod = ResourceInspector.findMethod(BinaryResourceAction.Delete.class, FlockEntityResource.class); - op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.GET); + op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.DELETE); assertNotNull(op); assertTrue(op.getTitle().startsWith("Deletes a photo")); aMethod = ResourceInspector.findMethod(BinaryResourceAction.Update.class, FlockEntityResource.class); - op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.GET); + op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.PUT); + assertNotNull(op); + assertTrue(op.getTitle().startsWith("Updates a photo")); + + aMethod = ResourceInspector.findMethod(BinaryResourceAction.Read.class, SheepBaaaahResource.class); + op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.GET); + assertNotNull(op); + assertTrue(op.getTitle().startsWith("Reads a photo")); + + aMethod = ResourceInspector.findMethod(BinaryResourceAction.Delete.class, SheepBaaaahResource.class); + op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.DELETE); + assertNotNull(op); + assertTrue(op.getTitle().startsWith("Deletes a photo")); + + aMethod = ResourceInspector.findMethod(BinaryResourceAction.Update.class, SheepBaaaahResource.class); + op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.PUT); assertNotNull(op); assertTrue(op.getTitle().startsWith("Updates a photo")); } @@ -406,7 +424,7 @@ public class InspectorTests String result = null; switch (resourceMetadata.getUniqueId()) { - case "/-root-/{entityId}/grow": + case "/-root-/{id}/grow": assertTrue("GrassEntityResource supports POST", resourceMetadata.supports(HttpMethod.POST)); assertFalse("GrassEntityResource does not support DELETE", resourceMetadata.supports(HttpMethod.DELETE)); Class paramType = resourceMetadata.getObjectType(HttpMethod.POST); @@ -414,7 +432,7 @@ public class InspectorTests result = (String) ResourceInspectorUtil.invokeMethod(actionMethod,grassEntityResource, "xyz", paramObj, Params.valueOf("notUsed", null)); assertEquals("Growing well",result); break; - case "/-root-/{entityId}/cut": + case "/-root-/{id}/cut": assertTrue("GrassEntityResource supports POST", resourceMetadata.supports(HttpMethod.POST)); assertFalse("GrassEntityResource does not support GET", resourceMetadata.supports(HttpMethod.GET)); assertNull(resourceMetadata.getObjectType(HttpMethod.POST)); @@ -425,8 +443,29 @@ public class InspectorTests fail("Invalid action information."); } + } + } + + @Test + public void testInspectRelationshipProperties() + { + List metainfo = ResourceInspector.inspect(GoatRelationshipResource.class); + assertTrue(metainfo.size()==2); + for (ResourceMetadata resourceMetadata : metainfo) + { + switch (resourceMetadata.getUniqueId()) + { + case "/goat/{entityId}/herd": + assertTrue("GoatRelationshipResource supports GET", resourceMetadata.supports(HttpMethod.GET)); + break; + case "/goat/{entityId}/herd/{id}/content": + assertTrue("GoatRelationshipResource supports GET", resourceMetadata.supports(HttpMethod.GET)); + break; + default: + fail("Invalid information."); + } } } @@ -439,7 +478,7 @@ public class InspectorTests ResourceInspector.inspectAddressedProperties(api, FlockEntityResource.class, "myroot", metainfo); assertTrue(metainfo.size()==1); ResourceMetadata metaData = metainfo.get(0); - assertEquals("/myroot/photo",metaData.getUniqueId()); + assertEquals("/myroot/{id}/photo",metaData.getUniqueId()); assertTrue(metaData.getOperations().size()==3); assertTrue("FlockEntityResource supports GET", metaData.supports(HttpMethod.GET)); assertTrue("FlockEntityResource supports PUT", metaData.supports(HttpMethod.PUT)); @@ -472,7 +511,7 @@ public class InspectorTests // fail("Invalid address property information."); // } - if ("/myroot/photo".equals(resourceMetadata.getUniqueId())) + if ("/myroot/{id}/photo".equals(resourceMetadata.getUniqueId())) { assertTrue("FlocketEntityResource supports GET", resourceMetadata.supports(HttpMethod.GET)); assertTrue("FlocketEntityResource supports PUT", resourceMetadata.supports(HttpMethod.PUT)); @@ -480,7 +519,7 @@ public class InspectorTests } else { - if ("/myroot/album".equals(resourceMetadata.getUniqueId())) + if ("/myroot/{id}/album".equals(resourceMetadata.getUniqueId())) { assertTrue("FlocketEntityResource supports GET", resourceMetadata.supports(HttpMethod.GET)); assertTrue("FlocketEntityResource supports PUT", resourceMetadata.supports(HttpMethod.PUT)); @@ -488,7 +527,7 @@ public class InspectorTests } else { - if ("/myroot/madeUpProp".equals(resourceMetadata.getUniqueId())) + if ("/myroot/{id}/madeUpProp".equals(resourceMetadata.getUniqueId())) { assertTrue("FlocketEntityResource supports GET", resourceMetadata.supports(HttpMethod.GET)); assertTrue("FlocketEntityResource does not supports PUT", !resourceMetadata.supports(HttpMethod.PUT)); 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 0ade2ee61e..5697c475b8 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 @@ -146,6 +146,15 @@ public class ResourceLocatorTests collResource = locator.locateResource(api, templateVars, HttpMethod.POST); assertEquals(GrassEntityResource.class, collResource.getResource().getClass()); assertEquals(ResourceMetadata.RESOURCE_TYPE.OPERATION, collResource.getMetaData().getType()); + + templateVars = new HashMap(); + templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "sheep"); + templateVars.put(ResourceLocator.ENTITY_ID, "sheepId"); + templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "baaahh"); + templateVars.put(ResourceLocator.PROPERTY, "chew"); + collResource = locator.locateResource(api, templateVars, HttpMethod.POST); + assertEquals(SheepBaaaahResource.class, collResource.getResource().getClass()); + assertEquals(ResourceMetadata.RESOURCE_TYPE.OPERATION, collResource.getMetaData().getType()); } @Test @@ -205,6 +214,56 @@ public class ResourceLocatorTests assertTrue(collResource.getMetaData().supports(HttpMethod.PUT)); assertEquals(FlocketEntityResource.class, collResource.getResource().getClass()); } + + @Test + public void testLocateRelationshipProperties() + { + Api api3 = Api.valueOf("alfrescomock", "private", "3"); + Map templateVars = new HashMap(); + templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "goat"); + templateVars.put(ResourceLocator.ENTITY_ID, "herdId"); + templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "herd"); + + ResourceWithMetadata collResource; + try + { + collResource = locator.locateResource(api3, templateVars, HttpMethod.PUT); + fail("Should throw an UnsupportedResourceOperationException"); + } + catch (UnsupportedResourceOperationException error) + { + //this is correct + } + + templateVars.put(ResourceLocator.PROPERTY, "content"); + collResource = locator.locateResource(api3, templateVars, HttpMethod.GET); + assertNotNull(collResource); + assertTrue(collResource.getMetaData().supports(HttpMethod.GET)); + + templateVars = new HashMap(); + templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "sheep"); + templateVars.put(ResourceLocator.ENTITY_ID, "sheepId"); + templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "baaahh"); + templateVars.put(ResourceLocator.PROPERTY, "content"); + + try + { + //Tests by passing invalid propery + collResource = locator.locateResource(api, templateVars, HttpMethod.GET); + fail("Should throw an InvalidArgumentException"); + } + catch (InvalidArgumentException error) + { + //this is correct + } + + templateVars.put(ResourceLocator.PROPERTY, "photo"); + collResource = locator.locateResource(api, templateVars, HttpMethod.GET); + assertNotNull(collResource); + assertTrue(collResource.getMetaData().supports(HttpMethod.GET)); + assertTrue(collResource.getMetaData().supports(HttpMethod.PUT)); + assertTrue(collResource.getMetaData().supports(HttpMethod.DELETE)); + } @Test public void testApiValues()