Merged 5.2.N (5.2.1) to HEAD (5.2)

130165 gjames: Merged searchapi (5.2.1) to 5.2.N (5.2.1)
      129774 gjames: SEARCH-113: Moving more api logic to helper classes, using a "trait" style


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@130317 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alan Davis
2016-09-06 22:02:33 +00:00
parent a43d3a126f
commit c6dda1e7fa
26 changed files with 1521 additions and 1269 deletions

View File

@@ -39,6 +39,7 @@ 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.tools.ApiAssistant;
import org.alfresco.rest.framework.tools.ResponseWriter;
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
import org.alfresco.rest.framework.webscripts.ApiWebScript;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
@@ -81,7 +82,7 @@ public abstract class AbstractContextTest
static Params NOT_USED = Params.valueOf("notUsed", null, mock(WebScriptRequest.class));
static final Params.RecognizedParams NULL_PARAMS = new Params.RecognizedParams(null, null, null, null, null, null, null, null, false);
static final WithResponse callBack = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT,ApiAssistant.CACHE_NEVER);
static final WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT,ResponseWriter.CACHE_NEVER);
static Api api = Api.valueOf("alfrescomock", "private", "1");
@SuppressWarnings("unchecked")

View File

@@ -23,22 +23,23 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.framework.tests.core;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* Runs all the tests for the Public Rest Framework
* @author Gethin James
*/
@RunWith(Suite.class)
@SuiteClasses({ InspectorTests.class, JsonJacksonTests.class, ParamsExtractorTests.class,
ResourceLocatorTests.class, ResourceWebScriptHelperTests.class, SerializeTests.class,
WhereTests.class, ExecutionTests.class, WithResponseTest.class,
ExceptionResolverTests.class })
public class AllRestFrameworkTest
{
}
package org.alfresco.rest.framework.tests.core;
import org.alfresco.rest.framework.tools.RecognizedParamsExtractorTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* Runs all the tests for the Public Rest Framework
* @author Gethin James
*/
@RunWith(Suite.class)
@SuiteClasses({ InspectorTests.class, JsonJacksonTests.class, ParamsExtractorTests.class,
ResourceLocatorTests.class, ResourceWebScriptHelperTests.class, SerializeTests.class,
WhereTests.class, ExecutionTests.class, WithResponseTest.class,
ExceptionResolverTests.class, RecognizedParamsExtractorTest.class})
public class AllRestFrameworkTest
{
}

View File

@@ -51,6 +51,7 @@ import org.alfresco.rest.framework.tests.api.mocks.Grass;
import org.alfresco.rest.framework.tests.api.mocks.Sheep;
import org.alfresco.rest.framework.tests.api.mocks3.FlockEntityResource;
import org.alfresco.rest.framework.tools.ApiAssistant;
import org.alfresco.rest.framework.tools.ResponseWriter;
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
import org.alfresco.rest.framework.webscripts.ApiWebScript;
import org.junit.Test;
@@ -80,7 +81,7 @@ import java.util.Map;
* Tests the execution of resources
*/
public class ExecutionTests extends AbstractContextTest
public class ExecutionTests extends AbstractContextTest implements ResponseWriter
{
static final Api api3 = Api.valueOf("alfrescomock", "private", "3");
@@ -99,7 +100,7 @@ public class ExecutionTests extends AbstractContextTest
entityResource = locator.locateEntityResource(api,"cow", HttpMethod.GET);
result = executor.execute(entityResource, Params.valueOf((String)null, null, mock(WebScriptRequest.class)), response, true);
assertNotNull(result);
verify(response, times(1)).setCache((Cache) ApiAssistant.CACHE_NEVER);
verify(response, times(1)).setCache((Cache) ResponseWriter.CACHE_NEVER);
response = mock(WebScriptResponse.class);
result = executor.execute(entityResource, Params.valueOf("543", null, mock(WebScriptRequest.class)), response, true);
@@ -305,7 +306,7 @@ public class ExecutionTests extends AbstractContextTest
ErrorResponse defaultError = new DefaultExceptionResolver().resolveException(new NullPointerException());
ByteArrayOutputStream out = new ByteArrayOutputStream();
apiAssistant.renderErrorResponse(defaultError, mockResponse(out));
renderErrorResponse(defaultError, mockResponse(out), apiAssistant.getJsonHelper());
String errorMessage = out.toString();
// System.out.println(errorMessage);
assertTrue(errorMessage.contains("\"errorKey\":\"framework.exception.ApiDefault\""));
@@ -316,7 +317,7 @@ public class ExecutionTests extends AbstractContextTest
ErrorResponse anError = simpleMappingExceptionResolver.resolveException(new ApiException("nothing"));
out = new ByteArrayOutputStream();
apiAssistant.renderErrorResponse(anError, mockResponse(out));
renderErrorResponse(anError, mockResponse(out),apiAssistant.getJsonHelper());
errorMessage = out.toString();
// System.out.println(errorMessage);
assertTrue(errorMessage.contains("\"errorKey\":\"nothing\""));
@@ -326,7 +327,7 @@ public class ExecutionTests extends AbstractContextTest
anError = simpleMappingExceptionResolver.resolveException(new EntityNotFoundException("2"));
out = new ByteArrayOutputStream();
apiAssistant.renderErrorResponse(anError, mockResponse(out));
renderErrorResponse(anError, mockResponse(out),apiAssistant.getJsonHelper());
errorMessage = out.toString();
System.out.println(errorMessage);
assertTrue(errorMessage.contains("\"errorKey\":\"framework.exception.EntityNotFound\""));

View File

@@ -77,6 +77,7 @@ import org.alfresco.rest.framework.tests.api.mocks3.SheepBlackSheepResourceIsNoM
import org.alfresco.rest.framework.tests.api.mocks3.SheepEntityResourceWithDeletedMethods;
import org.alfresco.rest.framework.tests.api.mocks3.SlimGoat;
import org.alfresco.rest.framework.tools.ApiAssistant;
import org.alfresco.rest.framework.tools.ResponseWriter;
import org.alfresco.rest.framework.webscripts.ApiWebScript;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.alfresco.util.Pair;
@@ -466,7 +467,7 @@ public class InspectorTests
OperationResourceMetaData operationResourceMetaData = (OperationResourceMetaData) resourceMetadata;
Method actionMethod = operationResourceMetaData.getOperationMethod();
String result = null;
final WithResponse wr = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
final WithResponse wr = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
switch (resourceMetadata.getUniqueId())
{

View File

@@ -49,6 +49,7 @@ import org.alfresco.rest.framework.tests.api.mocks.Farmer;
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.UniqueIdMethodButNoSetter;
import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
@@ -63,329 +64,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
public class ResourceWebScriptHelperTests
{
@Test
public void getFilterTest()
{
BeanPropertiesFilter theFilter = ResourceWebScriptHelper.getFilter(null);
assertNotNull(theFilter);
assertTrue("Null passed in so must return the default BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(theFilter.getClass()));
theFilter = ResourceWebScriptHelper.getFilter("bob");
assertNotNull(theFilter);
assertTrue("Must return the BeanPropertiesFilter class", theFilter instanceof BeanPropertiesFilter);
theFilter = ResourceWebScriptHelper.getFilter("50,fred,b.z");
assertNotNull(theFilter);
assertTrue("Must return the BeanPropertiesFilter class", theFilter instanceof BeanPropertiesFilter);
theFilter = ResourceWebScriptHelper.getFilter("50,fred,");
assertNotNull(theFilter);
assertTrue("Must return the BeanPropertiesFilter class", theFilter instanceof BeanPropertiesFilter);
}
@Test
public void getSortingTest()
{
List<SortColumn> theSort = ResourceWebScriptHelper.getSort(null);
assertNotNull(theSort);
assertTrue("Null passed in so empty sort list should be returned.", theSort.isEmpty());
theSort = ResourceWebScriptHelper.getSort("name ASC");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
theSort = ResourceWebScriptHelper.getSort("name ");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
theSort = ResourceWebScriptHelper.getSort("name DESC");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
theSort = ResourceWebScriptHelper.getSort("name desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
theSort = ResourceWebScriptHelper.getSort("name,age desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
assertEquals("age", theSort.get(1).column);
assertTrue(!theSort.get(1).asc); //desc
theSort = ResourceWebScriptHelper.getSort(" name, age desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
assertEquals("age", theSort.get(1).column);
assertTrue(!theSort.get(1).asc); //desc
theSort = ResourceWebScriptHelper.getSort("name DESC, age desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("name", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
assertEquals("age", theSort.get(1).column);
assertTrue(!theSort.get(1).asc); //desc
theSort = ResourceWebScriptHelper.getSort("age Desc, name Asc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("age", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
assertEquals("name", theSort.get(1).column);
assertTrue(theSort.get(1).asc);
theSort = ResourceWebScriptHelper.getSort("name des"); //invalid, should be desc
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc); //Defaults to ascending because the sort order was invalid
theSort = ResourceWebScriptHelper.getSort("name asc,"); //invalid, should be desc
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
}
@Test
public void getIncludeClauseTest()
{
getClauseTest("include");
}
@Test
public void getSelectClauseTest()
{
getClauseTest("select");
}
// at the moment select and include are parsed the same way, hence common/shared test
private void getClauseTest(String paramName)
{
List<String> theClause = getClause(paramName, null);
assertNotNull(theClause);
assertFalse("Null passed in so nothing in the "+paramName, theClause.size() > 0);
try
{
theClause = getClause(paramName, ",,,");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getClause(paramName, "(,,,");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getClause(paramName, "(,,,)");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getClause(paramName, "x/,z");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getClause(paramName, "/x'n,/z");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getClause(paramName, "/foo/0");
fail("Should throw an InvalidSelectException. Legal identifiers must start with a letter not zero");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getClause(paramName, "/");
fail("Should throw an InvalidSelectException. No identifier specified.");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getClause(paramName, "path, isLink");
fail("Should throw an InvalidSelectException. No identifier specified.");
}
catch (InvalidSelectException error)
{
//this is correct
}
theClause = getClause(paramName, "king/kong");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("king/kong",theClause.get(0));
theClause = getClause(paramName, "x,y");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("x",theClause.get(0));
assertEquals("y",theClause.get(1));
theClause = getClause(paramName, "x,/z");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("x",theClause.get(0));
assertEquals("/z",theClause.get(1));
theClause = getClause(paramName, "/b");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/b",theClause.get(0));
theClause = getClause(paramName, "/be,/he");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("/be",theClause.get(0));
assertEquals("/he",theClause.get(1));
theClause = getClause(paramName, "/king/kong");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/king/kong",theClause.get(0));
theClause = getClause(paramName, "/name,/person/age");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("/name",theClause.get(0));
assertEquals("/person/age",theClause.get(1));
theClause = getClause(paramName, "/foo");
assertTrue("has a valid select",theClause.size() == 1);
assertEquals("/foo",theClause.get(0));
theClause = getClause(paramName, "/foo/anArray/x");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/foo/anArray/x",theClause.get(0));
theClause = getClause(paramName, "/foo/anArray/x,/person/age,/eggs/bacon/sausage,/p");
assertTrue("has a valid "+paramName, theClause.size() == 4);
assertEquals("/foo/anArray/x",theClause.get(0));
assertEquals("/person/age",theClause.get(1));
assertEquals("/eggs/bacon/sausage",theClause.get(2));
assertEquals("/p",theClause.get(3));
theClause = getClause(paramName, "/foo/_bar ");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/foo/_bar",theClause.get(0));
}
private List<String> getClause(String paramName, String paramValue)
{
if (paramName.equalsIgnoreCase("include"))
{
return ResourceWebScriptHelper.getIncludeClause(paramValue);
}
else if (paramName.equalsIgnoreCase("select"))
{
return ResourceWebScriptHelper.getSelectClause(paramValue);
}
fail("Unexpected clause: "+paramName);
return null;
}
@Test
public void getRelationFilterTest()
{
Map<String, BeanPropertiesFilter> theFilter = ResourceWebScriptHelper.getRelationFilter(null);
assertNotNull(theFilter);
assertTrue("Null passed in so nothing to filter.",theFilter.isEmpty());
theFilter = ResourceWebScriptHelper.getRelationFilter("bob");
assertNotNull(theFilter);
assertTrue("Must be a single relationship", theFilter.size() == 1);
assertTrue("Must be a single relationship called bob", theFilter.containsKey("bob"));
BeanPropertiesFilter aFilter = theFilter.get("bob");
assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
theFilter = ResourceWebScriptHelper.getRelationFilter("bob,hope");
assertNotNull(theFilter);
assertTrue("Must be a two relationships", theFilter.size() == 2);
assertTrue("Must have hope.", theFilter.containsKey("hope"));
aFilter = theFilter.get("hope");
assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
theFilter = ResourceWebScriptHelper.getRelationFilter("bob(name),hope");
assertNotNull(theFilter);
assertTrue("Must be a two relationships", theFilter.size() == 2);
assertTrue("Must have bob.", theFilter.containsKey("bob"));
aFilter = theFilter.get("bob");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
theFilter = ResourceWebScriptHelper.getRelationFilter("bob,hope(age,name)");
assertNotNull(theFilter);
assertTrue("Must be a two relationships", theFilter.size() == 2);
aFilter = theFilter.get("bob");
assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
aFilter = theFilter.get("hope");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
theFilter = ResourceWebScriptHelper.getRelationFilter("bob(name,age),nohope,hope(height,width)");
assertNotNull(theFilter);
assertTrue("Must be a three relationships", theFilter.size() == 3);
aFilter = theFilter.get("bob");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
aFilter = theFilter.get("nohope");
assertTrue("No bean properties specified so need a ReturnAllBeanProperties class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
aFilter = theFilter.get("hope");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
}
@Test
public void setUniqueIdTest()
{
@@ -405,162 +83,5 @@ public class ResourceWebScriptHelperTests
ResourceWebScriptHelper.setUniqueId(invalidbj, "error");
assertNotNull("There should not be an error, errors should be swallowed up.",invalidbj);
}
@Test
public void findPagingTest()
{
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("34");
when(request.getParameter("maxItems")).thenReturn("50");
Paging pagin = ResourceWebScriptHelper.findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getSkipCount() == 34);
assertTrue(pagin.getMaxItems() == 50);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn(null);
when(request.getParameter("maxItems")).thenReturn(null);
pagin = ResourceWebScriptHelper.findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("55");
pagin = ResourceWebScriptHelper.findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getSkipCount() == 55);
assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn(null);
when(request.getParameter("maxItems")).thenReturn("45");
pagin = ResourceWebScriptHelper.findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getMaxItems() == 45);
assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("apple");
when(request.getParameter("maxItems")).thenReturn("pear");
try
{
pagin = ResourceWebScriptHelper.findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("0");
when(request.getParameter("maxItems")).thenReturn("0");
try
{
pagin = ResourceWebScriptHelper.findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
//Test Case cloud-2198
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("0");
when(request.getParameter("maxItems")).thenReturn("a");
try
{
pagin = ResourceWebScriptHelper.findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("s");
when(request.getParameter("maxItems")).thenReturn("5");
try
{
pagin = ResourceWebScriptHelper.findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("0");
when(request.getParameter("maxItems")).thenReturn("-2");
try
{
pagin = ResourceWebScriptHelper.findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("-3");
when(request.getParameter("maxItems")).thenReturn("5");
try
{
pagin = ResourceWebScriptHelper.findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("maxItems")).thenReturn("5");
pagin = ResourceWebScriptHelper.findPaging(request);
assertNotNull(pagin);
assertTrue("skip count defaults to 0", pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
//End of Test Case cloud-2198
}
@Test
public void paramsTest()
{
Map<String,List<String>> mockParams = new HashMap<String,List<String>>();
mockParams.put("age", Arrays.asList("23","45"));
mockParams.put("name", Arrays.asList("fred"));
WebScriptRequest request = mockRequest(mockParams);
Map<String, String[]> params = ResourceWebScriptHelper.getRequestParameters(request);
assertNotNull(params);
Params paramObj = ParamsExtender.valueOf(params);
assertNotNull(paramObj);
String aValue = paramObj.getParameter("age");
assertEquals("23", aValue);
aValue = paramObj.getParameter("name");
assertEquals("fred", aValue);
}
private WebScriptRequest mockRequest(final Map<String,List<String>> params)
{
final String[] paramNames = params.keySet().toArray(new String[]{});
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getParameterNames()).thenReturn(paramNames);
when(request.getParameterValues(anyString())).thenAnswer(new Answer<String[]>() {
@Override
public String[] answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return params.get((String) args[0]).toArray(new String[]{});
}
});
return request;
}
}

View File

@@ -57,6 +57,7 @@ import org.alfresco.rest.framework.tests.api.mocks.Grass;
import org.alfresco.rest.framework.tests.api.mocks.Sheep;
import org.alfresco.rest.framework.tests.api.mocks3.Flock;
import org.alfresco.rest.framework.tests.api.mocks3.SlimGoat;
import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.alfresco.service.cmr.repository.NodeRef;
@@ -91,7 +92,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
public class SerializeTests extends AbstractContextTest
public class SerializeTests extends AbstractContextTest implements RecognizedParamsExtractor
{
@Test
@@ -197,7 +198,7 @@ public class SerializeTests extends AbstractContextTest
public void testExpandRelations() throws IOException
{
assertNotNull(helper);
Map<String, BeanPropertiesFilter> rFilter = ResourceWebScriptHelper.getRelationFilter("blacksheep,baaahh");
Map<String, BeanPropertiesFilter> rFilter = getRelationFilter("blacksheep,baaahh");
ExecutionResult res = (ExecutionResult) helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api,"sheep",ParamsExtender.valueOf(rFilter,"1"),new Farmer("180"));
assertNotNull(res);
String out = writeResponse(res);
@@ -239,7 +240,7 @@ public class SerializeTests extends AbstractContextTest
public void testExpandRecursiveRelations() throws IOException
{
ExecutionResult exec1 = new ExecutionResult(new Farmer("180"),null);
ExecutionResult exec2 = new ExecutionResult(new Farmer("456"),ResourceWebScriptHelper.getFilter("age"));
ExecutionResult exec2 = new ExecutionResult(new Farmer("456"),getFilter("age"));
CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1, exec2));
ExecutionResult execResult = new ExecutionResult(new Sheep("ssheep"),null);
Map<String,Object> related = new HashMap<String,Object>();
@@ -436,12 +437,12 @@ public class SerializeTests extends AbstractContextTest
public void testFilter() throws IOException, JSONException
{
assertNotNull(helper);
BeanPropertiesFilter theFilter = ResourceWebScriptHelper.getFilter("age");
BeanPropertiesFilter theFilter = getFilter("age");
Object res = new ExecutionResult(new Sheep("bob"),theFilter);
String out = writeResponse(res);
assertTrue("Filter must only return the age.", StringUtils.contains(out, "{\"age\":3}"));
theFilter = ResourceWebScriptHelper.getFilter("age,name");
theFilter = getFilter("age,name");
res = new ExecutionResult(new Sheep("bob"),theFilter);
out = writeResponse(res);
JSONObject jsonRsp = new JSONObject(new JSONTokener(out));
@@ -452,8 +453,8 @@ public class SerializeTests extends AbstractContextTest
assertTrue("The age should be 3", entry.getInt("age") == 3);
// unit test filter with "include" taking precendence over "fields" filter
List<String> theInclude = ResourceWebScriptHelper.getIncludeClause("name");
theFilter = ResourceWebScriptHelper.getFilter("age", theInclude);
List<String> theInclude = getIncludeClause("name");
theFilter = getFilter("age", theInclude);
res = new ExecutionResult(new Sheep("bob"),theFilter);
out = writeResponse(res);
jsonRsp = new JSONObject(new JSONTokener(out));
@@ -464,7 +465,7 @@ public class SerializeTests extends AbstractContextTest
assertTrue("The age should be 3", entry.getInt("age") == 3);
Api v3 = Api.valueOf(api.getName(), api.getScope().toString(), "3");
Map<String, BeanPropertiesFilter> relFiler = ResourceWebScriptHelper.getRelationFilter("herd");
Map<String, BeanPropertiesFilter> relFiler = getRelationFilter("herd");
res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3,"goat",ParamsExtender.valueOf(relFiler, "notUsed"),new SlimGoat());
out = writeResponse(res);
jsonRsp = new JSONObject(new JSONTokener(out));
@@ -476,7 +477,7 @@ public class SerializeTests extends AbstractContextTest
assertEquals("The name should be 'bigun'", "bigun", entry.getString("name"));
assertTrue("The quantity should be 56", entry.getInt("quantity") == 56);
relFiler = ResourceWebScriptHelper.getRelationFilter("herd(name)");
relFiler = getRelationFilter("herd(name)");
res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3,"goat",ParamsExtender.valueOf(relFiler, "notUsed"),new SlimGoat());
out = writeResponse(res);
assertTrue("Must return only the herd name.", StringUtils.contains(out, "{\"name\":\"bigun\"}"));

View File

@@ -37,16 +37,17 @@ import org.alfresco.rest.framework.resource.parameters.where.InvalidQueryExcepti
import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
import org.antlr.runtime.tree.CommonTree;
import org.junit.Test;
public class WhereTests {
public class WhereTests implements RecognizedParamsExtractor
{
@Test
public void basicTest() throws IOException {
Query theQuery = ResourceWebScriptHelper.getWhereClause(" ( fred > g ) ");
Query theQuery = getWhereClause(" ( fred > g ) ");
CommonTree ast = theQuery.getTree();
//check AST structure
assertEquals(WhereClauseParser.GREATERTHAN, ast.getType());
@@ -57,13 +58,13 @@ public class WhereTests {
@Test
public void existClauseTest()
{
Query theQuery = ResourceWebScriptHelper.getWhereClause(null);
Query theQuery = getWhereClause(null);
assertNotNull(theQuery);
assertTrue("Null passed in so nothing to theQuery.", theQuery.getTree() == null);
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("fred");
theQuery = getWhereClause("fred");
fail("Should throw an InvalidQueryException");
}
catch (InvalidQueryException error)
@@ -73,7 +74,7 @@ public class WhereTests {
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("(noClosingBracket");
theQuery = getWhereClause("(noClosingBracket");
fail("Should throw an InvalidQueryException");
}
catch (InvalidQueryException error)
@@ -83,7 +84,7 @@ public class WhereTests {
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("noOpeningBracket)");
theQuery = getWhereClause("noOpeningBracket)");
fail("Should throw an InvalidQueryException");
}
catch (InvalidQueryException error)
@@ -94,7 +95,7 @@ public class WhereTests {
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("(EXISTS(target.file))");
theQuery = getWhereClause("(EXISTS(target.file))");
fail("Should throw an InvalidQueryException");
}
catch (InvalidQueryException error)
@@ -102,34 +103,34 @@ public class WhereTests {
//this is correct
}
theQuery = ResourceWebScriptHelper.getWhereClause("(exists(/target/file))");
theQuery = getWhereClause("(exists(/target/file))");
assertExistsPropertyEquals("/target/file", theQuery, false);
theQuery = ResourceWebScriptHelper.getWhereClause("(EXISTS(b))");
theQuery = getWhereClause("(EXISTS(b))");
assertExistsPropertyEquals("b", theQuery, false);
theQuery = ResourceWebScriptHelper.getWhereClause(" ( EXISTS ( whitespace ) ) ");
theQuery = getWhereClause(" ( EXISTS ( whitespace ) ) ");
assertExistsPropertyEquals("whitespace", theQuery, false);
theQuery = ResourceWebScriptHelper.getWhereClause("(exists ( folder ))");
theQuery = getWhereClause("(exists ( folder ))");
assertExistsPropertyEquals("folder", theQuery, false);
theQuery = ResourceWebScriptHelper.getWhereClause("(NOT EXISTS(b))");
theQuery = getWhereClause("(NOT EXISTS(b))");
assertExistsPropertyEquals("b", theQuery, true);
theQuery = ResourceWebScriptHelper.getWhereClause(" (NOT EXISTS(b))");
theQuery = getWhereClause(" (NOT EXISTS(b))");
assertExistsPropertyEquals("b", theQuery, true);
theQuery = ResourceWebScriptHelper.getWhereClause("( NOT EXISTS(b))");
theQuery = getWhereClause("( NOT EXISTS(b))");
assertExistsPropertyEquals("b", theQuery, true);
theQuery = ResourceWebScriptHelper.getWhereClause(" ( NOT EXISTS(b))");
theQuery = getWhereClause(" ( NOT EXISTS(b))");
assertExistsPropertyEquals("b", theQuery, true);
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("(exists folder)");
theQuery = getWhereClause("(exists folder)");
fail("Should throw an InvalidQueryException, 'folder' should have a bracket around it");
}
catch (InvalidQueryException error)
@@ -137,7 +138,7 @@ public class WhereTests {
//this is correct
}
theQuery = ResourceWebScriptHelper.getWhereClause("(EXISTS(/target/folder) AND NOT EXISTS(/target/site))");
theQuery = getWhereClause("(EXISTS(/target/folder) AND NOT EXISTS(/target/site))");
assertNotNull(theQuery);
CommonTree tree = theQuery.getTree();
assertNotNull(tree);
@@ -166,7 +167,7 @@ public class WhereTests {
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("(EXISTS(/target/folder)OR EXISTS(/target/site))");
theQuery = getWhereClause("(EXISTS(/target/folder)OR EXISTS(/target/site))");
fail("Should throw an InvalidQueryException, the OR should have a space before it.");
}
catch (InvalidQueryException error)
@@ -174,7 +175,7 @@ public class WhereTests {
//this is correct
}
theQuery = ResourceWebScriptHelper.getWhereClause("(NOT EXISTS(/target/folder) OR EXISTS(/target/site))");
theQuery = getWhereClause("(NOT EXISTS(/target/folder) OR EXISTS(/target/site))");
QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
@Override
public void exists(String propertyName, boolean negated) {
@@ -196,7 +197,7 @@ public class WhereTests {
});
theQuery = ResourceWebScriptHelper.getWhereClause("(EXISTS ( /target/folder ) OR EXISTS( /target/site ) )");
theQuery = getWhereClause("(EXISTS ( /target/folder ) OR EXISTS( /target/site ) )");
QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
int i=0;
@Override
@@ -220,7 +221,7 @@ public class WhereTests {
});
theQuery = ResourceWebScriptHelper.getWhereClause("(EXISTS(target/file) AND EXISTS(target/folder) AND EXISTS(target/site))");
theQuery = getWhereClause("(EXISTS(target/file) AND EXISTS(target/folder) AND EXISTS(target/site))");
QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
int i=0;
@Override
@@ -254,25 +255,25 @@ public class WhereTests {
@Test
public void inClauseTest()
{
Query theQuery = ResourceWebScriptHelper.getWhereClause("( dueAt in (5,8) )");
Query theQuery = getWhereClause("( dueAt in (5,8) )");
inChecks(theQuery, "dueAt", "5", "8");
theQuery = ResourceWebScriptHelper.getWhereClause("( fred/bloggs in (head,elbow) )");
theQuery = getWhereClause("( fred/bloggs in (head,elbow) )");
inChecks(theQuery, "fred/bloggs", "head", "elbow");
theQuery = ResourceWebScriptHelper.getWhereClause("( nextOne in (5,8,4) )");
theQuery = getWhereClause("( nextOne in (5,8,4) )");
inChecks(theQuery, "nextOne", "5", "8", "4");
theQuery = ResourceWebScriptHelper.getWhereClause("( nextOne in (5,56,fred) )");
theQuery = getWhereClause("( nextOne in (5,56,fred) )");
inChecks(theQuery, "nextOne", "5", "56", "fred");
theQuery = ResourceWebScriptHelper.getWhereClause("( nextOne in (5,56,'fred&') )");
theQuery = getWhereClause("( nextOne in (5,56,'fred&') )");
inChecks(theQuery, "nextOne", "5", "56", "fred&");
theQuery = ResourceWebScriptHelper.getWhereClause("( nextOne in ('me , you',56,egg) )");
theQuery = getWhereClause("( nextOne in ('me , you',56,egg) )");
inChecks(theQuery, "nextOne", "me , you", "56", "egg");
theQuery = ResourceWebScriptHelper.getWhereClause("( NOT nextOne in (5,56,fred, king, kong, 'fred\\'^') )");
theQuery = getWhereClause("( NOT nextOne in (5,56,fred, king, kong, 'fred\\'^') )");
CommonTree tree = theQuery.getTree();
assertNotNull(tree);
QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
@@ -293,15 +294,15 @@ public class WhereTests {
@Test
public void betweenClauseTest()
{
Query theQuery = ResourceWebScriptHelper.getWhereClause("( dueAt between (5,8) )");
Query theQuery = getWhereClause("( dueAt between (5,8) )");
betweenChecks(theQuery, "dueAt", "5", "8");
theQuery = ResourceWebScriptHelper.getWhereClause("( fred/bloggs between (head,elbow) )");
theQuery = getWhereClause("( fred/bloggs between (head,elbow) )");
betweenChecks(theQuery, "fred/bloggs", "head", "elbow");
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("( nextOne between (5,8,4) )");
theQuery = getWhereClause("( nextOne between (5,8,4) )");
fail("Should throw an InvalidQueryException, between can have only two values.");
}
catch (InvalidQueryException error)
@@ -311,7 +312,7 @@ public class WhereTests {
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("( nextOne between 5,8 )");
theQuery = getWhereClause("( nextOne between 5,8 )");
fail("Should throw an InvalidQueryException, Need brackets.");
}
catch (InvalidQueryException error)
@@ -319,7 +320,7 @@ public class WhereTests {
//this is correct
}
theQuery = ResourceWebScriptHelper.getWhereClause("(NOT dueAt between (5,8) AND nextOne between (green,blue))");
theQuery = getWhereClause("(NOT dueAt between (5,8) AND nextOne between (green,blue))");
QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
@Override
public void between(String property, String firstVal, String secondVal, boolean negated) {
@@ -349,18 +350,18 @@ public class WhereTests {
@Test
public void matchesClauseTest()
{
Query theQuery = ResourceWebScriptHelper.getWhereClause("(fred matches(bob))");
Query theQuery = getWhereClause("(fred matches(bob))");
matchesChecks(theQuery, "fred", "bob");
theQuery = ResourceWebScriptHelper.getWhereClause("( king/kong/hair/shoulders/knees/toes matches ('fred%') )");
theQuery = getWhereClause("( king/kong/hair/shoulders/knees/toes matches ('fred%') )");
matchesChecks(theQuery, "king/kong/hair/shoulders/knees/toes", "fred%");
theQuery = ResourceWebScriptHelper.getWhereClause("( niceone matches (bob) )");
theQuery = getWhereClause("( niceone matches (bob) )");
matchesChecks(theQuery, "niceone", "bob");
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("( fred matches bob )");
theQuery = getWhereClause("( fred matches bob )");
fail("Should throw an InvalidQueryException, Need brackets.");
}
catch (InvalidQueryException error)
@@ -373,25 +374,25 @@ public class WhereTests {
@Test
public void comparisonClauseTest()
{
Query theQuery = ResourceWebScriptHelper.getWhereClause("( dueAt > '12.04.345' )");
Query theQuery = getWhereClause("( dueAt > '12.04.345' )");
int comparisonOperator = WhereClauseParser.GREATERTHAN;
comparisonChecks(theQuery, comparisonOperator, "dueAt", "12.04.345");
theQuery = ResourceWebScriptHelper.getWhereClause("( dueAt >= '12.04.345' )");
theQuery = getWhereClause("( dueAt >= '12.04.345' )");
comparisonOperator = WhereClauseParser.GREATERTHANOREQUALS;
comparisonChecks(theQuery, comparisonOperator, "dueAt", "12.04.345");
theQuery = ResourceWebScriptHelper.getWhereClause("( dueAt < '12.04.345' )");
theQuery = getWhereClause("( dueAt < '12.04.345' )");
comparisonOperator = WhereClauseParser.LESSTHAN;
comparisonChecks(theQuery, comparisonOperator, "dueAt", "12.04.345");
theQuery = ResourceWebScriptHelper.getWhereClause("( dueAt <= '12.04.345' )");
theQuery = getWhereClause("( dueAt <= '12.04.345' )");
comparisonOperator = WhereClauseParser.LESSTHANOREQUALS;
comparisonChecks(theQuery, comparisonOperator, "dueAt", "12.04.345");
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("( Fred/Bloggs = %$NICE&* )");
theQuery = getWhereClause("( Fred/Bloggs = %$NICE&* )");
fail("Should throw an InvalidQueryException, needs single quotes");
}
catch (InvalidQueryException error)
@@ -399,13 +400,13 @@ public class WhereTests {
//this is correct
}
theQuery = ResourceWebScriptHelper.getWhereClause("( Fred/Bloggs = '%$NICE&*' )");
theQuery = getWhereClause("( Fred/Bloggs = '%$NICE&*' )");
comparisonOperator = WhereClauseParser.EQUALS;
comparisonChecks(theQuery, comparisonOperator, "Fred/Bloggs", "%$NICE&*");
try
{
theQuery = ResourceWebScriptHelper.getWhereClause("( Ken = (456) )");
theQuery = getWhereClause("( Ken = (456) )");
fail("Should throw an InvalidQueryException, needs single quotes no brackets");
}
catch (InvalidQueryException error)
@@ -413,15 +414,15 @@ public class WhereTests {
//this is correct
}
theQuery = ResourceWebScriptHelper.getWhereClause("( Ken = '456' )");
theQuery = getWhereClause("( Ken = '456' )");
comparisonOperator = WhereClauseParser.EQUALS;
comparisonChecks(theQuery, comparisonOperator, "Ken", "456");
theQuery = ResourceWebScriptHelper.getWhereClause("( DogHouse = 'Cat\\\'s House' )");
theQuery = getWhereClause("( DogHouse = 'Cat\\\'s House' )");
comparisonOperator = WhereClauseParser.EQUALS;
comparisonChecks(theQuery, comparisonOperator, "DogHouse", "Cat\\\'s House");
theQuery = ResourceWebScriptHelper.getWhereClause("( KING_KONG >= 'Mighty Mouse' )");
theQuery = getWhereClause("( KING_KONG >= 'Mighty Mouse' )");
comparisonOperator = WhereClauseParser.GREATERTHANOREQUALS;
comparisonChecks(theQuery, comparisonOperator, "KING_KONG", "Mighty Mouse");
@@ -442,25 +443,25 @@ public class WhereTests {
@Test
public void getChildrenTests()
{
Query theQuery = ResourceWebScriptHelper.getWhereClause("(fred matches(bob))");
Query theQuery = getWhereClause("(fred matches(bob))");
assertNotNull(theQuery);
CommonTree tree = theQuery.getTree();
assertNotNull(tree);
assertTrue(2 == QueryHelper.getChildren(tree).size());
theQuery = ResourceWebScriptHelper.getWhereClause("( dueAt between (5,8) )");
theQuery = getWhereClause("( dueAt between (5,8) )");
assertNotNull(theQuery);
tree = theQuery.getTree();
assertNotNull(tree);
assertTrue(3 == QueryHelper.getChildren(tree).size());
theQuery = ResourceWebScriptHelper.getWhereClause("(NOT EXISTS(b))");
theQuery = getWhereClause("(NOT EXISTS(b))");
assertNotNull(theQuery);
tree = theQuery.getTree();
assertNotNull(tree);
assertTrue(1 == QueryHelper.getChildren(tree).size());
theQuery = ResourceWebScriptHelper.getWhereClause("(EXISTS(/target/folder) AND EXISTS(/target/site))");
theQuery = getWhereClause("(EXISTS(/target/folder) AND EXISTS(/target/site))");
assertNotNull(theQuery);
tree = theQuery.getTree();
assertNotNull(tree);

View File

@@ -39,6 +39,7 @@ import static org.mockito.Mockito.when;
import org.alfresco.rest.framework.resource.content.ContentInfo;
import org.alfresco.rest.framework.resource.content.ContentInfoImpl;
import org.alfresco.rest.framework.tools.ApiAssistant;
import org.alfresco.rest.framework.tools.ResponseWriter;
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
import org.alfresco.rest.framework.webscripts.ApiWebScript;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete;
@@ -70,17 +71,17 @@ public class WithResponseTest
@Test
public void testDefaults() throws Exception
{
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
assertEquals(Status.STATUS_OK, callBack.getStatus());
assertEquals(ApiAssistant.DEFAULT_JSON_CONTENT, callBack.getContentInfo());
assertEquals(ApiAssistant.CACHE_NEVER, callBack.getCache());
assertEquals(ResponseWriter.DEFAULT_JSON_CONTENT, callBack.getContentInfo());
assertEquals(ResponseWriter.CACHE_NEVER, callBack.getCache());
assertTrue(callBack.getHeaders().isEmpty());
}
@Test
public void testSetHeader() throws Exception
{
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
WithResponse callBack = new WithResponse(Status.STATUS_OK,ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
callBack.setHeader("king", "can");
callBack.setHeader("king", "kong");
assertTrue(callBack.getHeaders().size() == 1);
@@ -92,7 +93,7 @@ public class WithResponseTest
@Test
public void testAddHeader() throws Exception
{
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
WithResponse callBack = new WithResponse(Status.STATUS_OK,ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
callBack.addHeader("king", "can");
callBack.addHeader("king", "kong");
assertTrue(callBack.getHeaders().size() == 1);
@@ -105,7 +106,7 @@ public class WithResponseTest
@Test
public void testSetters() throws Exception
{
WithResponse callBack = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
callBack.setStatus(Status.STATUS_GONE);
Cache myCache = new Cache(new Description.RequiredCache()
{
@@ -142,7 +143,7 @@ public class WithResponseTest
{
AbstractResourceWebScript responseWriter = new ResourceWebScriptDelete();
responseWriter.setAssistant(new ApiAssistant());
WithResponse wr = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
WithResponse wr = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
WebScriptResponse response = mock(WebScriptResponse.class);

View File

@@ -0,0 +1,539 @@
/*
* #%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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.framework.tools;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter;
import org.alfresco.rest.framework.resource.parameters.InvalidSelectException;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.resource.parameters.SortColumn;
import org.alfresco.rest.framework.tests.core.ParamsExtender;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.extensions.webscripts.WebScriptRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Test the RecognizedParamsExtractor
*
* @author Gethin James
*/
public class RecognizedParamsExtractorTest implements RecognizedParamsExtractor
{
@Test
public void getFilterTest()
{
BeanPropertiesFilter theFilter = getFilter(null);
assertNotNull(theFilter);
assertTrue("Null passed in so must return the default BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(theFilter.getClass()));
theFilter = getFilter("bob");
assertNotNull(theFilter);
assertTrue("Must return the BeanPropertiesFilter class", theFilter instanceof BeanPropertiesFilter);
theFilter = getFilter("50,fred,b.z");
assertNotNull(theFilter);
assertTrue("Must return the BeanPropertiesFilter class", theFilter instanceof BeanPropertiesFilter);
theFilter = getFilter("50,fred,");
assertNotNull(theFilter);
assertTrue("Must return the BeanPropertiesFilter class", theFilter instanceof BeanPropertiesFilter);
}
@Test
public void getSortingTest()
{
List<SortColumn> theSort = getSort(null);
assertNotNull(theSort);
assertTrue("Null passed in so empty sort list should be returned.", theSort.isEmpty());
theSort = getSort("name ASC");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
theSort = getSort("name ");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
theSort = getSort("name DESC");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
theSort = getSort("name desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
theSort = getSort("name,age desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
assertEquals("age", theSort.get(1).column);
assertTrue(!theSort.get(1).asc); //desc
theSort = getSort(" name, age desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
assertEquals("age", theSort.get(1).column);
assertTrue(!theSort.get(1).asc); //desc
theSort = getSort("name DESC, age desc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("name", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
assertEquals("age", theSort.get(1).column);
assertTrue(!theSort.get(1).asc); //desc
theSort = getSort("age Desc, name Asc");
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 2);
assertEquals("age", theSort.get(0).column);
assertTrue(!theSort.get(0).asc); //desc
assertEquals("name", theSort.get(1).column);
assertTrue(theSort.get(1).asc);
theSort = getSort("name des"); //invalid, should be desc
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc); //Defaults to ascending because the sort order was invalid
theSort = getSort("name asc,"); //invalid, should be desc
assertNotNull(theSort);
assertTrue("Must have a value for column: NAME", !theSort.isEmpty());
assertTrue(theSort.size() == 1);
assertEquals("name", theSort.get(0).column);
assertTrue(theSort.get(0).asc);
}
@Test
public void getIncludeClauseTest()
{
getClauseTest("include");
}
@Test
public void getSelectClauseTest()
{
getClauseTest("select");
}
// at the moment select and include are parsed the same way, hence common/shared test
private void getClauseTest(String paramName)
{
List<String> theClause = getCorrectClause(paramName, null);
assertNotNull(theClause);
assertFalse("Null passed in so nothing in the "+paramName, theClause.size() > 0);
try
{
theClause = getCorrectClause(paramName, ",,,");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getCorrectClause(paramName, "(,,,");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getCorrectClause(paramName, "(,,,)");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getCorrectClause(paramName, "x/,z");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getCorrectClause(paramName, "/x'n,/z");
fail("Should throw an InvalidSelectException");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getCorrectClause(paramName, "/foo/0");
fail("Should throw an InvalidSelectException. Legal identifiers must start with a letter not zero");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getCorrectClause(paramName, "/");
fail("Should throw an InvalidSelectException. No identifier specified.");
}
catch (InvalidSelectException error)
{
//this is correct
}
try
{
theClause = getCorrectClause(paramName, "path, isLink");
fail("Should throw an InvalidSelectException. No identifier specified.");
}
catch (InvalidSelectException error)
{
//this is correct
}
theClause = getCorrectClause(paramName, "king/kong");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("king/kong",theClause.get(0));
theClause = getCorrectClause(paramName, "x,y");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("x",theClause.get(0));
assertEquals("y",theClause.get(1));
theClause = getCorrectClause(paramName, "x,/z");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("x",theClause.get(0));
assertEquals("/z",theClause.get(1));
theClause = getCorrectClause(paramName, "/b");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/b",theClause.get(0));
theClause = getCorrectClause(paramName, "/be,/he");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("/be",theClause.get(0));
assertEquals("/he",theClause.get(1));
theClause = getCorrectClause(paramName, "/king/kong");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/king/kong",theClause.get(0));
theClause = getCorrectClause(paramName, "/name,/person/age");
assertTrue("has a valid "+paramName, theClause.size() == 2);
assertEquals("/name",theClause.get(0));
assertEquals("/person/age",theClause.get(1));
theClause = getCorrectClause(paramName, "/foo");
assertTrue("has a valid select",theClause.size() == 1);
assertEquals("/foo",theClause.get(0));
theClause = getCorrectClause(paramName, "/foo/anArray/x");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/foo/anArray/x",theClause.get(0));
theClause = getCorrectClause(paramName, "/foo/anArray/x,/person/age,/eggs/bacon/sausage,/p");
assertTrue("has a valid "+paramName, theClause.size() == 4);
assertEquals("/foo/anArray/x",theClause.get(0));
assertEquals("/person/age",theClause.get(1));
assertEquals("/eggs/bacon/sausage",theClause.get(2));
assertEquals("/p",theClause.get(3));
theClause = getCorrectClause(paramName, "/foo/_bar ");
assertTrue("has a valid "+paramName, theClause.size() == 1);
assertEquals("/foo/_bar",theClause.get(0));
}
private List<String> getCorrectClause(String paramName, String paramValue)
{
if (paramName.equalsIgnoreCase("include"))
{
return getIncludeClause(paramValue);
}
else if (paramName.equalsIgnoreCase("select"))
{
return getSelectClause(paramValue);
}
fail("Unexpected clause: "+paramName);
return null;
}
@Test
public void getRelationFilterTest()
{
Map<String, BeanPropertiesFilter> theFilter = getRelationFilter(null);
assertNotNull(theFilter);
assertTrue("Null passed in so nothing to filter.",theFilter.isEmpty());
theFilter = getRelationFilter("bob");
assertNotNull(theFilter);
assertTrue("Must be a single relationship", theFilter.size() == 1);
assertTrue("Must be a single relationship called bob", theFilter.containsKey("bob"));
BeanPropertiesFilter aFilter = theFilter.get("bob");
assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
theFilter = getRelationFilter("bob,hope");
assertNotNull(theFilter);
assertTrue("Must be a two relationships", theFilter.size() == 2);
assertTrue("Must have hope.", theFilter.containsKey("hope"));
aFilter = theFilter.get("hope");
assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
theFilter = getRelationFilter("bob(name),hope");
assertNotNull(theFilter);
assertTrue("Must be a two relationships", theFilter.size() == 2);
assertTrue("Must have bob.", theFilter.containsKey("bob"));
aFilter = theFilter.get("bob");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
theFilter = getRelationFilter("bob,hope(age,name)");
assertNotNull(theFilter);
assertTrue("Must be a two relationships", theFilter.size() == 2);
aFilter = theFilter.get("bob");
assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
aFilter = theFilter.get("hope");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
theFilter = getRelationFilter("bob(name,age),nohope,hope(height,width)");
assertNotNull(theFilter);
assertTrue("Must be a three relationships", theFilter.size() == 3);
aFilter = theFilter.get("bob");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
aFilter = theFilter.get("nohope");
assertTrue("No bean properties specified so need a ReturnAllBeanProperties class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
aFilter = theFilter.get("hope");
assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
}
@Test
public void findPagingTest()
{
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("34");
when(request.getParameter("maxItems")).thenReturn("50");
Paging pagin = findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getSkipCount() == 34);
assertTrue(pagin.getMaxItems() == 50);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn(null);
when(request.getParameter("maxItems")).thenReturn(null);
pagin = findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("55");
pagin = findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getSkipCount() == 55);
assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn(null);
when(request.getParameter("maxItems")).thenReturn("45");
pagin = findPaging(request);
assertNotNull(pagin);
assertTrue(pagin.getMaxItems() == 45);
assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("apple");
when(request.getParameter("maxItems")).thenReturn("pear");
try
{
pagin = findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("0");
when(request.getParameter("maxItems")).thenReturn("0");
try
{
pagin = findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
//Test Case cloud-2198
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("0");
when(request.getParameter("maxItems")).thenReturn("a");
try
{
pagin = findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("s");
when(request.getParameter("maxItems")).thenReturn("5");
try
{
pagin = findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("0");
when(request.getParameter("maxItems")).thenReturn("-2");
try
{
pagin = findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("skipCount")).thenReturn("-3");
when(request.getParameter("maxItems")).thenReturn("5");
try
{
pagin = findPaging(request);
fail("Should not get here.");
}
catch (InvalidArgumentException iae)
{
assertNotNull(iae); // Must throw this exceptions
}
request = mock(WebScriptRequest.class);
when(request.getParameter("maxItems")).thenReturn("5");
pagin = findPaging(request);
assertNotNull(pagin);
assertTrue("skip count defaults to 0", pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
//End of Test Case cloud-2198
}
@Test
public void paramsTest()
{
Map<String,List<String>> mockParams = new HashMap<String,List<String>>();
mockParams.put("age", Arrays.asList("23","45"));
mockParams.put("name", Arrays.asList("fred"));
WebScriptRequest request = mockRequest(mockParams);
Map<String, String[]> params = getRequestParameters(request);
assertNotNull(params);
Params paramObj = ParamsExtender.valueOf(params);
assertNotNull(paramObj);
String aValue = paramObj.getParameter("age");
assertEquals("23", aValue);
aValue = paramObj.getParameter("name");
assertEquals("fred", aValue);
}
private WebScriptRequest mockRequest(final Map<String,List<String>> params)
{
final String[] paramNames = params.keySet().toArray(new String[]{});
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getParameterNames()).thenReturn(paramNames);
when(request.getParameterValues(anyString())).thenAnswer(new Answer<String[]>() {
@Override
public String[] answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return params.get((String) args[0]).toArray(new String[]{});
}
});
return request;
}
}

View File

@@ -43,7 +43,7 @@ import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams;
import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
import org.alfresco.rest.workflow.api.Processes;
import org.alfresco.rest.workflow.api.impl.ProcessesImpl;
import org.alfresco.rest.workflow.api.model.ProcessInfo;
@@ -67,7 +67,7 @@ import junit.framework.TestCase;
*
* @author Dmitry Velichkevich
*/
public class ProcessesImplTest extends TestCase
public class ProcessesImplTest extends TestCase implements RecognizedParamsExtractor
{
private static final String[] CONFIG_LOCATIONS = new String[ApplicationContextHelper.CONFIG_LOCATIONS.length + 2];
static
@@ -224,7 +224,7 @@ public class ProcessesImplTest extends TestCase
private CollectionWithPagingInfo<ProcessInfo> queryMatchesProcesses(String matchesString)
{
Query query = ResourceWebScriptHelper.getWhereClause(String.format(QUERY_WORKFLOWDESCRIPTION_MATCHES, matchesString));
Query query = getWhereClause(String.format(QUERY_WORKFLOWDESCRIPTION_MATCHES, matchesString));
Parameters parameters = Params.valueOf(new RecognizedParams(null, Paging.valueOf(0, ACTIVE_WORKFLOWS_INITIAL_AMOUNT), null, null, null, null, query, null, false), null, null, null);
return processes.getProcesses(parameters);
@@ -232,7 +232,7 @@ public class ProcessesImplTest extends TestCase
private CollectionWithPagingInfo<ProcessInfo> queryActiveProcessesAndAssertResult(int skipCount, int maxItems)
{
Query query = ResourceWebScriptHelper.getWhereClause(QUERY_STATUS_ACTIVE);
Query query = getWhereClause(QUERY_STATUS_ACTIVE);
Parameters parameters = Params.valueOf(new RecognizedParams(null, Paging.valueOf(skipCount, maxItems), null, null, null, null, query, null, false), null, null, null);
CollectionWithPagingInfo<ProcessInfo> result = processes.getProcesses(parameters);