Search in sources :

Example 1 with AbstractResourceWebScript

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;
}
Also used : AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript)

Example 2 with AbstractResourceWebScript

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);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) Goat(org.alfresco.rest.framework.tests.api.mocks.Goat) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Example 3 with AbstractResourceWebScript

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);
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Before(org.junit.Before)

Example 4 with AbstractResourceWebScript

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\":\" \""));
}
Also used : AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 5 with AbstractResourceWebScript

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);
}
Also used : HashMap(java.util.HashMap) AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

AbstractResourceWebScript (org.alfresco.rest.framework.webscripts.AbstractResourceWebScript)11 Test (org.junit.Test)9 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)8 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)5 Matchers.anyString (org.mockito.Matchers.anyString)5 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)4 HashMap (java.util.HashMap)3 Goat (org.alfresco.rest.framework.tests.api.mocks.Goat)2 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)1 Api (org.alfresco.rest.framework.Api)1 Grass (org.alfresco.rest.framework.tests.api.mocks.Grass)1 ApiAssistant (org.alfresco.rest.framework.tools.ApiAssistant)1 ResourceWebScriptDelete (org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete)1 WithResponse (org.alfresco.rest.framework.webscripts.WithResponse)1 TransactionService (org.alfresco.service.transaction.TransactionService)1 JSONObject (org.json.JSONObject)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1