mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
122317 gjames: Adding more test coverage of the webscript executors git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126465 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -8,8 +8,10 @@ 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 java.util.List;
|
||||
|
||||
@EntityResource(name = "grass", title="Grass")
|
||||
public class GrassEntityResource implements EntityResourceAction.ReadById<Grass>{
|
||||
public class GrassEntityResource implements EntityResourceAction.ReadById<Grass>, EntityResourceAction.Create<Grass>, EntityResourceAction.Delete {
|
||||
|
||||
@Override
|
||||
@WebApiDescription(title = "Gets grass by id")
|
||||
@@ -31,4 +33,16 @@ public class GrassEntityResource implements EntityResourceAction.ReadById<Grass>
|
||||
return "Growing well";
|
||||
}
|
||||
|
||||
@Override
|
||||
@WebApiDescription(title = "Create some grass")
|
||||
public List<Grass> create(List<Grass> entity, Parameters parameters)
|
||||
{
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String id, Parameters parameters)
|
||||
{
|
||||
//I did a delete
|
||||
}
|
||||
}
|
||||
|
@@ -21,7 +21,7 @@ import java.io.File;
|
||||
* @author Gethin James
|
||||
*/
|
||||
@RelationshipResource(name = "herd",entityResource=GoatEntityResourceForV3.class, title = "Goat Herd")
|
||||
public class GoatRelationshipResource implements RelationshipResourceAction.Read<Herd>, RelationshipResourceBinaryAction.Read
|
||||
public class GoatRelationshipResource implements RelationshipResourceAction.Read<Herd>, RelationshipResourceBinaryAction.Read, RelationshipResourceBinaryAction.Delete
|
||||
{
|
||||
@Override
|
||||
public CollectionWithPagingInfo<Herd> readAll(String entityResourceId, Parameters params)
|
||||
@@ -36,4 +36,11 @@ public class GoatRelationshipResource implements RelationshipResourceAction.Read
|
||||
File file = TempFileProvider.createTempFile("Its a goat", ".txt");
|
||||
return new FileBinaryResource(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
@BinaryProperties({"content"})
|
||||
public void deleteProperty(String entityId, String entityResourceId, Parameters parameters)
|
||||
{
|
||||
//Its deleted
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,84 @@
|
||||
package org.alfresco.rest.framework.tests.core;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||
import org.alfresco.rest.framework.Api;
|
||||
import org.alfresco.rest.framework.core.ResourceDictionaryBuilder;
|
||||
import org.alfresco.rest.framework.core.ResourceLookupDictionary;
|
||||
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
|
||||
import org.alfresco.rest.framework.resource.EntityResource;
|
||||
import org.alfresco.rest.framework.resource.RelationshipResource;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
|
||||
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Uses a test context and sets up some data.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:test-rest-context.xml" })
|
||||
public abstract class AbstractContextTest
|
||||
{
|
||||
@Autowired
|
||||
ResourceLookupDictionary locator;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
ResourceWebScriptHelper helper;
|
||||
|
||||
@Autowired
|
||||
JacksonHelper jsonHelper;
|
||||
|
||||
static Params NOT_USED = Params.valueOf("notUsed", null);
|
||||
static final Params.RecognizedParams NULL_PARAMS = new Params.RecognizedParams(null, null, null, null, null, null, null, false);
|
||||
static Api api = Api.valueOf("alfrescomock", "private", "1");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
Map<String, Object> entityResourceBeans = applicationContext.getBeansWithAnnotation(EntityResource.class);
|
||||
Map<String, Object> relationResourceBeans = applicationContext.getBeansWithAnnotation(RelationshipResource.class);
|
||||
locator.setDictionary(ResourceDictionaryBuilder.build(entityResourceBeans.values(), relationResourceBeans.values()));
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
AbstractResourceWebScript postExecutor = (AbstractResourceWebScript) applicationContext.getBean("executorOfPost");
|
||||
AbstractResourceWebScript putExecutor = (AbstractResourceWebScript) applicationContext.getBean("executorOfPut");
|
||||
AbstractResourceWebScript deleteExecutor = (AbstractResourceWebScript) applicationContext.getBean("executorOfDelete");
|
||||
|
||||
//Mock transaction service
|
||||
TransactionService transerv = mock(TransactionService.class);
|
||||
RetryingTransactionHelper tHelper = mock(RetryingTransactionHelper.class);
|
||||
when(transerv.getRetryingTransactionHelper()).thenReturn(tHelper);
|
||||
when(tHelper.doInTransaction(any(RetryingTransactionHelper.RetryingTransactionCallback.class), anyBoolean(), anyBoolean())).thenAnswer(new Answer<Object>() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
RetryingTransactionHelper.RetryingTransactionCallback cb = (RetryingTransactionHelper.RetryingTransactionCallback) args[0];
|
||||
cb.execute();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
executor.setTransactionService(transerv);
|
||||
postExecutor.setTransactionService(transerv);
|
||||
putExecutor.setTransactionService(transerv);
|
||||
deleteExecutor.setTransactionService(transerv);
|
||||
}
|
||||
}
|
@@ -10,7 +10,7 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ InspectorTests.class, JsonJacksonTests.class, ParamsExtractorTests.class,
|
||||
ResourceLocatorTests.class, ResourceWebScriptHelperTests.class, SerializeTests.class, WhereTests.class })
|
||||
ResourceLocatorTests.class, ResourceWebScriptHelperTests.class, SerializeTests.class, WhereTests.class, ExecutionTests.class })
|
||||
public class AllRestFrameworkTest
|
||||
{
|
||||
|
||||
|
@@ -0,0 +1,168 @@
|
||||
package org.alfresco.rest.framework.tests.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.alfresco.rest.framework.Api;
|
||||
import org.alfresco.rest.framework.core.ResourceLookupDictionary;
|
||||
import org.alfresco.rest.framework.core.ResourceWithMetadata;
|
||||
import org.alfresco.rest.framework.jacksonextensions.ExecutionResult;
|
||||
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.Grass;
|
||||
import org.alfresco.rest.framework.tests.api.mocks.Sheep;
|
||||
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Tests the execution of resources
|
||||
*/
|
||||
|
||||
public class ExecutionTests extends AbstractContextTest
|
||||
{
|
||||
static final Api api3 = Api.valueOf("alfrescomock", "private", "3");
|
||||
|
||||
@Test
|
||||
public void testInvokeGet() throws IOException
|
||||
{
|
||||
ResourceWithMetadata entityResource = locator.locateEntityResource(api,"sheep", HttpMethod.GET);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
executor.execute(entityResource, Params.valueOf((String)null, null), new ActionExecutor.ExecutionCallback<CollectionWithPagingInfo>(){
|
||||
@Override
|
||||
public void onSuccess(CollectionWithPagingInfo result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
}});
|
||||
|
||||
ResourceWithMetadata baa = locator.locateRelationResource(api,"sheep", "baaahh", HttpMethod.GET);
|
||||
executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
executor.execute(baa, Params.valueOf("4", null), new ActionExecutor.ExecutionCallback<CollectionWithPagingInfo>(){
|
||||
@Override
|
||||
public void onSuccess(CollectionWithPagingInfo result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
}});
|
||||
|
||||
executor.execute(baa, Params.valueOf("4", "45"), new ActionExecutor.ExecutionCallback<ExecutionResult>(){
|
||||
@Override
|
||||
public void onSuccess(ExecutionResult result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
}});
|
||||
|
||||
ResourceWithMetadata baaPhoto = locator.locateRelationResource(api,"sheep/{entityId}/baaahh", "photo", HttpMethod.GET);
|
||||
executor.execute(baaPhoto, Params.valueOf("4", "45"), new ActionExecutor.ExecutionCallback<CollectionWithPagingInfo>(){
|
||||
@Override
|
||||
public void onSuccess(CollectionWithPagingInfo result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNull(result);
|
||||
}});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokePost() throws IOException
|
||||
{
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfPost");
|
||||
|
||||
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.POST);
|
||||
final Sheep aSheep = new Sheep("xyz");
|
||||
executor.execute(resource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(aSheep)), new ActionExecutor.ExecutionCallback<ExecutionResult>(){
|
||||
@Override
|
||||
public void onSuccess(ExecutionResult result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
assertEquals(aSheep,result.getRoot());
|
||||
}});
|
||||
|
||||
ResourceWithMetadata grassResource = locator.locateEntityResource(api,"grass", HttpMethod.POST);
|
||||
final Grass grr = new Grass("grr");
|
||||
executor.execute(grassResource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(grr)), new ActionExecutor.ExecutionCallback<ExecutionResult>(){
|
||||
@Override
|
||||
public void onSuccess(ExecutionResult result, ContentInfo contentInfo)
|
||||
{
|
||||
assertEquals(grr,result.getRoot());
|
||||
}});
|
||||
|
||||
ResourceWithMetadata entityResource = locator.locateRelationResource(api,"grass", "grow", HttpMethod.POST);
|
||||
executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, grr), new ActionExecutor.ExecutionCallback<String>(){
|
||||
@Override
|
||||
public void onSuccess(String result, ContentInfo contentInfo)
|
||||
{
|
||||
assertEquals("Growing well",result);
|
||||
}});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeDelete() throws IOException
|
||||
{
|
||||
ResourceWithMetadata grassResource = locator.locateEntityResource(api,"grass", HttpMethod.DELETE);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfDelete");
|
||||
executor.execute(grassResource, Params.valueOf("4", null), new ActionExecutor.ExecutionCallback<Object>(){
|
||||
@Override
|
||||
public void onSuccess(Object result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNull(result);
|
||||
}});
|
||||
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.DELETE);
|
||||
executor.execute(resource, Params.valueOf("4", null), new ActionExecutor.ExecutionCallback<Object>(){
|
||||
@Override
|
||||
public void onSuccess(Object result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNull(result);
|
||||
}});
|
||||
|
||||
ResourceWithMetadata goatDelete = locator.locateRelationResource(api3,"goat/{entityId}/herd", "content", HttpMethod.DELETE);
|
||||
executor.execute(goatDelete, Params.valueOf("4", "56"), new ActionExecutor.ExecutionCallback<Object>(){
|
||||
@Override
|
||||
public void onSuccess(Object result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNull(result);
|
||||
}});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInvokePut() throws IOException
|
||||
{
|
||||
ResourceWithMetadata entityResource = locator.locateEntityResource(api,"sheep", HttpMethod.PUT);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfPut");
|
||||
final Sheep aSheep = new Sheep("xyz");
|
||||
executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, aSheep), new ActionExecutor.ExecutionCallback<ExecutionResult>(){
|
||||
@Override
|
||||
public void onSuccess(ExecutionResult result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
assertEquals(aSheep,result.getRoot());
|
||||
}});
|
||||
|
||||
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.PUT);
|
||||
executor.execute(resource, Params.valueOf("654", null, NULL_PARAMS, aSheep), new ActionExecutor.ExecutionCallback<ExecutionResult>(){
|
||||
@Override
|
||||
public void onSuccess(ExecutionResult result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
assertEquals(aSheep,result.getRoot());
|
||||
}});
|
||||
ResourceWithMetadata baaPhoto = locator.locateRelationResource(api,"sheep/{entityId}/baaahh", "photo", HttpMethod.PUT);
|
||||
executor.execute(baaPhoto, Params.valueOf("4", "56"), new ActionExecutor.ExecutionCallback<Object>(){
|
||||
@Override
|
||||
public void onSuccess(Object result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNull(result);
|
||||
}});
|
||||
}
|
||||
}
|
@@ -106,50 +106,8 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:test-rest-context.xml" })
|
||||
public class SerializeTests
|
||||
public class SerializeTests extends AbstractContextTest
|
||||
{
|
||||
@Autowired
|
||||
private ResourceLookupDictionary locator;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
private ResourceWebScriptHelper helper;
|
||||
|
||||
private static Api api = Api.valueOf("alfrescomock", "private", "1");
|
||||
private static Params NOT_USED = Params.valueOf("notUsed", null);
|
||||
|
||||
@Autowired
|
||||
protected JacksonHelper jsonHelper;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
Map<String, Object> entityResourceBeans = applicationContext.getBeansWithAnnotation(EntityResource.class);
|
||||
Map<String, Object> relationResourceBeans = applicationContext.getBeansWithAnnotation(RelationshipResource.class);
|
||||
locator.setDictionary(ResourceDictionaryBuilder.build(entityResourceBeans.values(), relationResourceBeans.values()));
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
|
||||
//Mock transaction service
|
||||
TransactionService transerv = mock(TransactionService.class);
|
||||
RetryingTransactionHelper tHelper = mock(RetryingTransactionHelper.class);
|
||||
when(transerv.getRetryingTransactionHelper()).thenReturn(tHelper);
|
||||
when(tHelper.doInTransaction(any(RetryingTransactionCallback.class), anyBoolean(), anyBoolean())).thenAnswer(new Answer<Object>() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
RetryingTransactionCallback cb = (RetryingTransactionCallback) args[0];
|
||||
cb.execute();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
executor.setTransactionService(transerv);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeEntity() throws IOException
|
||||
@@ -450,44 +408,6 @@ public class SerializeTests
|
||||
assertNotNull(result);
|
||||
}});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeEntities() throws IOException
|
||||
{
|
||||
ResourceWithMetadata entityResource = locator.locateEntityResource(api,"sheep", HttpMethod.GET);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
executor.execute(entityResource, Params.valueOf((String)null, null), new ExecutionCallback<CollectionWithPagingInfo>(){
|
||||
@Override
|
||||
public void onSuccess(CollectionWithPagingInfo result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
}});
|
||||
|
||||
ResourceWithMetadata baa = locator.locateRelationResource(api,"sheep", "baaahh", HttpMethod.GET);
|
||||
executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
executor.execute(baa, Params.valueOf("4", null), new ExecutionCallback<CollectionWithPagingInfo>(){
|
||||
@Override
|
||||
public void onSuccess(CollectionWithPagingInfo result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
}});
|
||||
|
||||
executor.execute(baa, Params.valueOf("4", "45"), new ExecutionCallback<ExecutionResult>(){
|
||||
@Override
|
||||
public void onSuccess(ExecutionResult result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNotNull(result);
|
||||
}});
|
||||
|
||||
ResourceWithMetadata baaPhoto = locator.locateRelationResource(api,"sheep/{entityId}/baaahh", "photo", HttpMethod.GET);
|
||||
executor.execute(baaPhoto, Params.valueOf("4", "45"), new ExecutionCallback<ExecutionResult>(){
|
||||
@Override
|
||||
public void onSuccess(ExecutionResult result, ContentInfo contentInfo)
|
||||
{
|
||||
assertNull(result);
|
||||
}});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -56,6 +56,18 @@
|
||||
<property name="helper" ref="webscriptHelper" />
|
||||
</bean>
|
||||
|
||||
<bean id="executorOfPost" class="org.alfresco.rest.framework.webscripts.ResourceWebScriptPost">
|
||||
<property name="helper" ref="webscriptHelper" />
|
||||
</bean>
|
||||
|
||||
<bean id="executorOfPut" class="org.alfresco.rest.framework.webscripts.ResourceWebScriptPut">
|
||||
<property name="helper" ref="webscriptHelper" />
|
||||
</bean>
|
||||
|
||||
<bean id="executorOfDelete" class="org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete">
|
||||
<property name="helper" ref="webscriptHelper" />
|
||||
</bean>
|
||||
|
||||
<bean id="defaultMetaWriter" class="org.alfresco.rest.framework.webscripts.metadata.WebScriptOptionsMetaData">
|
||||
<property name="lookupDictionary" ref="apiLookup" />
|
||||
<property name="resolver" ref="simpleMappingExceptionResolver" />
|
||||
|
Reference in New Issue
Block a user