use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testDeleteExtractor.
@Test
public void testDeleteExtractor() throws IOException {
ParamsExtractor extractor = new ResourceWebScriptDelete();
Map<String, String> templateVars = new HashMap<String, String>();
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
Params params = null;
params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
assertNull(params.getEntityId());
assertNull(params.getRelationshipId());
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
assertNull(params.getRelationshipId());
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "45678");
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
assertEquals("45678", params.getRelationshipId());
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
testExtractAddressedParams(templateVars, request, extractor);
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testPostExtractor.
@SuppressWarnings("unchecked")
@Test
public void testPostExtractor() throws IOException {
// Put together the stubs
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
extractor.setAssistant(assistant);
Map<String, String> templateVars = new HashMap<String, String>();
Content content = mock(Content.class);
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
when(request.getContent()).thenReturn(content);
Params params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
Object passed = params.getPassedIn();
assertNotNull(passed);
assertTrue(List.class.isAssignableFrom(passed.getClass()));
List<Object> passedObjs = (List<Object>) passed;
assertTrue(passedObjs.size() == 1);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
// No entity id for POST
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
try {
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here. No entity id for POST");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
passed = params.getPassedIn();
assertNotNull(passed);
passedObjs = (List<Object>) passed;
assertTrue(passedObjs.size() == 1);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
try {
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "45678");
params = extractor.extractParams(mockRelationship(), request);
fail("Should not get here.");
} catch (UnsupportedResourceOperationException iae) {
// Must throw this exception
assertNotNull("POSTING to a relationship collection by id is not correct.", iae);
}
templateVars.clear();
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "codfish");
try {
// POST does not support addressed parameters.
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here.");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
testExtractOperationParams(templateVars, request, extractor);
templateVars.clear();
Method aMethod = ResourceInspector.findMethod(EntityResourceAction.Create.class, GrassEntityResource.class);
ResourceOperation op = ResourceInspector.inspectOperation(GrassEntityResource.class, aMethod, HttpMethod.POST);
List<ResourceMetadata> metainfo = ResourceInspector.inspect(GrassEntityResource.class);
assertNotNull(op);
assertTrue("Create method should have two params", op.getParameters().size() == 2);
ResourceParameter singleParam = op.getParameters().get(0);
assertTrue(ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(singleParam.getParamType()));
assertFalse("Create grass does not support multiple grass creations", singleParam.isAllowMultiple());
assertFalse(singleParam.isRequired());
// Test context when the request body is null and 'required' webApiParam is false
when(request.getHeader("content-length")).thenReturn("0");
params = extractor.extractParams(metainfo.get(0), request);
assertNotNull(params);
// Test context when the request body is provided and 'required' property is false
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.GRASS_JSON));
params = extractor.extractParams(metainfo.get(0), request);
assertNotNull(params);
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testGetExtractor.
@Test
public void testGetExtractor() {
ParamsExtractor extractor = new ResourceWebScriptGet();
Map<String, String> templateVars = new HashMap<String, String>();
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
Params params = extractor.extractParams(mockEntity(), request);
assertNull("For getting a Collection there should be no entity params.", params.getEntityId());
assertNull("For getting a Collection there should be no passed params.", params.getPassedIn());
assertNull("For getting a Collection there should be no relationshipId params.", params.getRelationshipId());
assertEquals(Paging.DEFAULT_SKIP_COUNT, params.getPaging().getSkipCount());
assertEquals(Paging.DEFAULT_MAX_ITEMS, params.getPaging().getMaxItems());
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
assertNotNull(params.getRelationsFilter());
assertFalse(params.includeSource());
templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "codfish");
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertNull("For getting a Collection there should be no relationshipId params.", params.getRelationshipId());
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "45678");
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
assertEquals("45678", params.getRelationshipId());
assertFalse(params.includeSource());
testExtractAddressedParams(templateVars, request, extractor);
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testSpecialChars.
@Test
public void testSpecialChars() throws IOException {
String specialChars = new String(new char[] { (char) '香' }) + " 香蕉";
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
extractor.setAssistant(assistant);
Map<String, String> templateVars = new HashMap<String, String>();
String mockMe = "{\"name\":\"" + specialChars + "\",\"created\":\"2012-03-23T15:56:18.552+0000\",\"age\":54,\"id\":\"1234A3\",\"farm\":\"LARGE\"}";
Content content = mock(Content.class);
when(content.getReader()).thenReturn(new StringReader(mockMe));
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
when(request.getContent()).thenReturn(content);
Params params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
Object passed = params.getPassedIn();
assertTrue(List.class.isAssignableFrom(passed.getClass()));
@SuppressWarnings("unchecked") List<Object> passedObjs = (List<Object>) passed;
assertTrue(passedObjs.size() == 1);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
Farmer f = (Farmer) passedObjs.get(0);
assertTrue(f.getName().equals("香 香蕉"));
// Test passing in special characters as a param.
ResourceWebScriptGet getExtractor = new ResourceWebScriptGet();
getExtractor.setAssistant(assistant);
Map<String, String> getTemplateVars = new HashMap<String, String>();
WebScriptRequest getRequest = mock(WebScriptRequest.class);
when(getRequest.getServiceMatch()).thenReturn(new Match(null, getTemplateVars, null));
when(getRequest.getParameterNames()).thenReturn(new String[] { "aParam" });
when(getRequest.getParameterValues("aParam")).thenReturn(new String[] { specialChars });
Params pGet = getExtractor.extractParams(mockEntity(), getRequest);
assertNotNull(pGet);
String pVal = pGet.getParameter("aParam");
assertTrue(pVal.equals("香 香蕉"));
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class RecognizedParamsExtractorTest method paramsTest.
@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);
}
Aggregations