Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)

122759 gjames: RA-211: Changing execution actions to handle WithResponse. Rework>Java7


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126504 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2016-05-10 11:14:27 +00:00
parent 26201557fc
commit dfdba82274
13 changed files with 400 additions and 73 deletions

View File

@@ -0,0 +1,49 @@
package org.alfresco.rest.framework.tests.api.mocks;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.resource.EntityResource;
import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.webscripts.WithResponse;
import java.util.Arrays;
import java.util.List;
@EntityResource(name="cow", title="Cow")
public class CowEntityResource implements EntityResourceAction.ReadByIdWithResponse<Goat>,
EntityResourceAction.ReadWithResponse<Goat>,
EntityResourceAction.CreateWithResponse<Goat>,
EntityResourceAction.UpdateWithResponse<Goat>,
EntityResourceAction.DeleteWithResponse{
@Override
public Goat readById(String id, Parameters parameters, WithResponse withResponse)
{
return new Goat("Goat"+id);
}
@Override
public CollectionWithPagingInfo<Goat> readAll(Parameters params, WithResponse withResponse)
{
return CollectionWithPagingInfo.asPaged(params.getPaging(), Arrays.asList(new Goat("Cow1")));
}
@Override
public List<Goat> create(List<Goat> entities, Parameters parameters, WithResponse withResponse)
{
return entities;
}
@Override
public void delete(String id, Parameters parameters, WithResponse withResponse)
{
}
@Override
public Goat update(String id, Goat entity, Parameters parameters, WithResponse withResponse)
{
return entity;
}
}

View File

@@ -7,6 +7,7 @@ import org.alfresco.rest.framework.core.ResourceParameter;
import org.alfresco.rest.framework.resource.EntityResource;
import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.springframework.extensions.webscripts.Status;
import java.util.List;
@@ -24,14 +25,14 @@ public class GrassEntityResource implements EntityResourceAction.ReadById<Grass>
@Operation("cut")
@WebApiDescription(title = "Cut the grass",successStatus = Status.STATUS_NOT_IMPLEMENTED)
public String cutLawn(String id, Void notused, Parameters parameters) {
public String cutLawn(String id, Void notused, Parameters parameters, WithResponse withResponse) {
return "All done";
}
@Operation("grow")
@WebApiDescription(title = "Grow the grass",successStatus = Status.STATUS_ACCEPTED)
@WebApiParam(name = "Grass", title = "The grass.",required=true, kind = ResourceParameter.KIND.HTTP_BODY_OBJECT)
public String growTheLawn(String id, Grass grass, Parameters parameters) {
public String growTheLawn(String id, Grass grass, Parameters parameters, WithResponse withResponse) {
return "Growing well";
}

View File

@@ -16,6 +16,7 @@ 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;
import org.alfresco.rest.framework.webscripts.WithResponse;
/**
* Implements Get
@@ -65,7 +66,7 @@ public class SheepBaaaahResource implements RelationshipResourceAction.Read<Shee
}
@Operation("chew")
public String chewTheGrass(String entityId, String id, Void notused, Parameters parameters) {
public String chewTheGrass(String entityId, String id, Void notused, Parameters parameters, WithResponse withResponse) {
return "All done";
}
}

View File

@@ -6,6 +6,7 @@ import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import org.alfresco.rest.framework.Api;
import org.alfresco.rest.framework.core.ResourceLocator;
import org.alfresco.rest.framework.core.ResourceLookupDictionary;
import org.alfresco.rest.framework.core.ResourceWithMetadata;
import org.alfresco.rest.framework.jacksonextensions.ExecutionResult;
@@ -13,6 +14,7 @@ import org.alfresco.rest.framework.resource.actions.ActionExecutor;
import org.alfresco.rest.framework.resource.content.ContentInfo;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.tests.api.mocks.Goat;
import org.alfresco.rest.framework.tests.api.mocks.Grass;
import org.alfresco.rest.framework.tests.api.mocks.Sheep;
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
@@ -28,6 +30,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Tests the execution of resources
@@ -45,6 +49,13 @@ public class ExecutionTests extends AbstractContextTest
Object result = executor.execute(entityResource, Params.valueOf((String)null, null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
assertNotNull(result);
entityResource = locator.locateEntityResource(api,"cow", HttpMethod.GET);
result = executor.execute(entityResource, Params.valueOf((String)null, null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
assertNotNull(result);
result = executor.execute(entityResource, Params.valueOf("543", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
assertNotNull(result);
ResourceWithMetadata baa = locator.locateRelationResource(api,"sheep", "baaahh", HttpMethod.GET);
result = executor.execute(baa, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
assertNotNull(result);
@@ -74,9 +85,22 @@ public class ExecutionTests extends AbstractContextTest
result = executor.execute(grassResource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(grr), mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertEquals(grr,((ExecutionResult)result).getRoot());
ResourceWithMetadata cowresource = locator.locateEntityResource(api,"cow", HttpMethod.POST);
result = executor.execute(cowresource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(grr), mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertEquals(grr,((ExecutionResult)result).getRoot());
ResourceWithMetadata entityResource = locator.locateRelationResource(api,"grass", "grow", HttpMethod.POST);
result = executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, grr, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertEquals("Growing well",result);
Map<String, String> 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");
ResourceWithMetadata collResource = locator.locateResource(api, templateVars, HttpMethod.POST);
result = executor.execute(collResource, Params.valueOf("654", "345", NULL_PARAMS, null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertEquals("All done",result);
}
@Test
@@ -87,6 +111,10 @@ public class ExecutionTests extends AbstractContextTest
Object result = executor.execute(grassResource, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
ResourceWithMetadata cowResource = locator.locateEntityResource(api,"cow", HttpMethod.DELETE);
result = executor.execute(cowResource, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.DELETE);
result = executor.execute(resource, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
@@ -103,11 +131,16 @@ public class ExecutionTests extends AbstractContextTest
ResourceWithMetadata entityResource = locator.locateEntityResource(api,"sheep", HttpMethod.PUT);
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfPut");
final Sheep aSheep = new Sheep("xyz");
Object result = executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(aSheep,((ExecutionResult)result).getRoot());
final Goat goat = new Goat("xyz");
ResourceWithMetadata cowResource = locator.locateEntityResource(api,"cow", HttpMethod.PUT);
result = executor.execute(cowResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(goat,((ExecutionResult)result).getRoot());
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.PUT);
result = executor.execute(resource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);

View File

@@ -51,6 +51,8 @@ import org.alfresco.rest.framework.tests.api.mocks3.GrassEntityResourceNowDelete
import org.alfresco.rest.framework.tests.api.mocks3.SheepBlackSheepResourceIsNoMore;
import org.alfresco.rest.framework.tests.api.mocks3.SheepEntityResourceWithDeletedMethods;
import org.alfresco.rest.framework.tests.api.mocks3.SlimGoat;
import org.alfresco.rest.framework.webscripts.ApiWebScript;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.alfresco.util.Pair;
import org.junit.Test;
import org.springframework.extensions.webscripts.Status;
@@ -438,6 +440,8 @@ public class InspectorTests
OperationResourceMetaData operationResourceMetaData = (OperationResourceMetaData) resourceMetadata;
Method actionMethod = operationResourceMetaData.getOperationMethod();
String result = null;
final WithResponse wr = new WithResponse(Status.STATUS_OK, ApiWebScript.DEFAULT_JSON_CONTENT, ApiWebScript.CACHE_NEVER);
switch (resourceMetadata.getUniqueId())
{
case "/-root-/{id}/grow":
@@ -447,7 +451,7 @@ public class InspectorTests
assertEquals("grow should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
Class paramType = resourceMetadata.getObjectType(op);
Object paramObj = paramType.newInstance();
result = (String) ResourceInspectorUtil.invokeMethod(actionMethod,grassEntityResource, "xyz", paramObj, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)));
result = (String) ResourceInspectorUtil.invokeMethod(actionMethod,grassEntityResource, "xyz", paramObj, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
assertEquals("Growing well",result);
break;
case "/-root-/{id}/cut":
@@ -456,7 +460,7 @@ public class InspectorTests
op = resourceMetadata.getOperation(HttpMethod.POST);
assertNull(resourceMetadata.getObjectType(op));
assertEquals("cut should return ACCEPTED", Status.STATUS_NOT_IMPLEMENTED, op.getSuccessStatus());
result = (String) ResourceInspectorUtil.invokeMethod(actionMethod,grassEntityResource, "xyz", null, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)));
result = (String) ResourceInspectorUtil.invokeMethod(actionMethod,grassEntityResource, "xyz", null, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
assertEquals("All done",result);
break;
default: