use of org.alfresco.rest.framework.webscripts.AbstractResourceWebScript in project alfresco-remote-api by Alfresco.
the class AbstractContextTest method getExecutor.
protected AbstractResourceWebScript getExecutor(String beanName) {
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean(beanName);
executor.setLocator(locator);
executor.setAssistant(apiAssistant);
return executor;
}
use of org.alfresco.rest.framework.webscripts.AbstractResourceWebScript in project alfresco-remote-api by Alfresco.
the class ExecutionTests method testInvokePut.
@Test
public void testInvokePut() throws IOException {
ResourceWithMetadata entityResource = locator.locateEntityResource(api, "sheep", HttpMethod.PUT);
AbstractResourceWebScript executor = getExecutor("executorOfPut");
final Sheep aSheep = new Sheep("xyz");
Object result = executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(aSheep, ((ExecutionResult) result).getRoot());
final Goat goat = new Goat("xyz");
ResourceWithMetadata cowResource = locator.locateEntityResource(api, "cow", HttpMethod.PUT);
result = executor.execute(cowResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(goat, ((ExecutionResult) result).getRoot());
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.PUT);
result = executor.execute(resource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(aSheep, ((ExecutionResult) result).getRoot());
ResourceWithMetadata baaPhoto = locator.locateRelationResource(api, "sheep/{entityId}/baaahh", "photo", HttpMethod.PUT);
result = executor.execute(baaPhoto, Params.valueOf("4", "56", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
ResourceWithMetadata flockEntityResource = locator.locateRelationResource(api3, "flock", "photo", HttpMethod.PUT);
result = executor.execute(flockEntityResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
ResourceWithMetadata calf = locator.locateRelationResource(api, "cow", "calf", HttpMethod.PUT);
result = executor.execute(calf, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(goat, ((ExecutionResult) result).getRoot());
cowResource = locator.locateRelationResource(api, "cow", "photo", HttpMethod.PUT);
result = executor.execute(cowResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
calf = locator.locateRelationResource(api, "cow/{entityId}/calf", "photo", HttpMethod.PUT);
result = executor.execute(calf, Params.valueOf("4", "56", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
}
use of org.alfresco.rest.framework.webscripts.AbstractResourceWebScript in project alfresco-remote-api by Alfresco.
the class AbstractContextTest method setUp.
@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];
return cb.execute();
}
});
executor.setTransactionService(transerv);
postExecutor.setTransactionService(transerv);
putExecutor.setTransactionService(transerv);
deleteExecutor.setTransactionService(transerv);
}
use of org.alfresco.rest.framework.webscripts.AbstractResourceWebScript in project alfresco-remote-api by Alfresco.
the class ExecutionTests method testRenderError.
@Test
public void testRenderError() throws IOException {
AbstractResourceWebScript executor = getExecutor();
ErrorResponse defaultError = new DefaultExceptionResolver().resolveException(new NullPointerException());
ByteArrayOutputStream out = new ByteArrayOutputStream();
renderErrorResponse(defaultError, mockResponse(out), apiAssistant.getJsonHelper());
String errorMessage = out.toString();
// System.out.println(errorMessage);
assertTrue(errorMessage.contains("\"errorKey\":\"framework.exception.ApiDefault\""));
assertTrue(errorMessage.contains("\"statusCode\":500"));
assertTrue(errorMessage.contains("\"logId\":\""));
assertTrue(errorMessage.contains("\"stackTrace\":\"For security reasons the stack trace is no longer displayed"));
assertTrue(errorMessage.contains("\"descriptionURL\":\"" + DefaultExceptionResolver.ERROR_URL + "\""));
ErrorResponse anError = simpleMappingExceptionResolver.resolveException(new ApiException("nothing"));
out = new ByteArrayOutputStream();
renderErrorResponse(anError, mockResponse(out), apiAssistant.getJsonHelper());
errorMessage = out.toString();
// System.out.println(errorMessage);
assertTrue(errorMessage.contains("\"errorKey\":\"nothing\""));
assertTrue(errorMessage.contains("\"statusCode\":500"));
assertTrue(errorMessage.contains("\"stackTrace\":\"For security reasons the stack trace is no longer displayed"));
assertTrue(errorMessage.contains("\"logId\":\""));
anError = simpleMappingExceptionResolver.resolveException(new EntityNotFoundException("2"));
out = new ByteArrayOutputStream();
renderErrorResponse(anError, mockResponse(out), apiAssistant.getJsonHelper());
errorMessage = out.toString();
System.out.println(errorMessage);
assertTrue(errorMessage.contains("\"errorKey\":\"framework.exception.EntityNotFound\""));
assertTrue(errorMessage.contains("\"statusCode\":404"));
assertFalse("Only 500 errors should have a logId", errorMessage.contains("\"logId\":\" \""));
}
use of org.alfresco.rest.framework.webscripts.AbstractResourceWebScript in project alfresco-remote-api by Alfresco.
the class ExecutionTests method testInvalidUrls.
@Test
public void testInvalidUrls() throws IOException {
AbstractResourceWebScript executor = getExecutor();
Map<String, String> templateVars = new HashMap();
templateVars.put("apiScope", "private");
templateVars.put("apiVersion", "1");
templateVars.put("apiName", "alfrescomock");
WebScriptResponse response = mockResponse();
templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "blah:");
executor.execute(api, mockRequest(templateVars, new HashMap<String, List<String>>(1)), response);
// Can't find it so a 404
verify(response, times(1)).setStatus(HttpServletResponse.SC_NOT_FOUND);
}
Aggregations