Search in sources :

Example 6 with WebScriptResponse

use of org.springframework.extensions.webscripts.WebScriptResponse in project alfresco-remote-api by Alfresco.

the class ExecutionTests method testInvokeGet.

@Test
public void testInvokeGet() throws IOException {
    ResourceWithMetadata entityResource = locator.locateEntityResource(api, "sheep", HttpMethod.GET);
    AbstractResourceWebScript executor = getExecutor();
    Object result = executor.execute(entityResource, Params.valueOf((String) null, null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNotNull(result);
    WebScriptResponse response = mock(WebScriptResponse.class);
    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) ResponseWriter.CACHE_NEVER);
    response = mock(WebScriptResponse.class);
    result = executor.execute(entityResource, Params.valueOf("543", null, mock(WebScriptRequest.class)), response, true);
    assertNotNull(result);
    verify(response, times(1)).setCache((Cache) CowEntityResource.CACHE_COW);
    ResourceWithMetadata baa = locator.locateRelationResource(api, "sheep", "baaahh", HttpMethod.GET);
    result = executor.execute(baa, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNotNull(result);
    executor.execute(baa, Params.valueOf("4", "45", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNotNull(result);
    ResourceWithMetadata cowResource = locator.locateRelationResource(api, "cow", "photo", HttpMethod.GET);
    result = executor.execute(cowResource, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNull(result);
    ResourceWithMetadata calf = locator.locateRelationResource(api, "cow", "calf", HttpMethod.GET);
    result = executor.execute(calf, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNotNull(result);
    executor.execute(calf, Params.valueOf("4", "45", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNotNull(result);
    calf = locator.locateRelationResource(api, "cow/{entityId}/calf", "photo", HttpMethod.GET);
    executor.execute(calf, Params.valueOf("4", "45", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNotNull(result);
    ResourceWithMetadata baaPhoto = locator.locateRelationResource(api, "sheep/{entityId}/baaahh", "photo", HttpMethod.GET);
    executor.execute(baaPhoto, Params.valueOf("4", "45", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
    assertNotNull(result);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) Matchers.anyString(org.mockito.Matchers.anyString) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Example 7 with WebScriptResponse

use of org.springframework.extensions.webscripts.WebScriptResponse in project alfresco-remote-api by Alfresco.

the class ExecutionTests method mockResponse.

private WebScriptResponse mockResponse(ByteArrayOutputStream byteArrayOutputStream) throws IOException {
    WebScriptResponse res = mock(WebScriptResponse.class);
    when(res.getOutputStream()).thenReturn(byteArrayOutputStream);
    return res;
}
Also used : WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse)

Example 8 with WebScriptResponse

use of org.springframework.extensions.webscripts.WebScriptResponse in project alfresco-remote-api by Alfresco.

the class ExecutionTests method testInvokePost.

@Test
public void testInvokePost() throws IOException {
    AbstractResourceWebScript executor = getExecutor("executorOfPost");
    ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.POST);
    final Sheep aSheep = new Sheep("xyz");
    Object result = executor.execute(resource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(aSheep), mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNotNull(result);
    assertEquals(aSheep, ((ExecutionResult) result).getRoot());
    ResourceWithMetadata grassResource = locator.locateEntityResource(api, "grass", HttpMethod.POST);
    final Grass grr = new Grass("grr");
    result = executor.execute(grassResource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(grr), mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertEquals(grr, ((ExecutionResult) result).getRoot());
    final Goat goat = new Goat("xyz");
    ResourceWithMetadata cowresource = locator.locateEntityResource(api, "cow", HttpMethod.POST);
    WebScriptResponse response = mock(WebScriptResponse.class);
    result = executor.execute(cowresource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(goat), mock(WebScriptRequest.class)), response, false);
    assertEquals(goat, ((ExecutionResult) result).getRoot());
    verify(response, times(1)).setStatus(Status.STATUS_ACCEPTED);
    ResourceWithMetadata entityResource = locator.locateRelationResource(api, "grass", "grow", HttpMethod.POST);
    result = executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, grr, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertEquals("Growing well", result);
    ResourceWithMetadata calfResource = locator.locateRelationResource(api, "cow", "calf", HttpMethod.POST);
    result = executor.execute(calfResource, Params.valueOf("654", null, NULL_PARAMS, Arrays.asList(goat), mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertEquals(goat, ((ExecutionResult) result).getRoot());
    Map<String, String> templateVars = new HashMap();
    templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "sheep");
    templateVars.put(ResourceLocator.ENTITY_ID, "sheepId");
    templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "baaahh");
    templateVars.put(ResourceLocator.PROPERTY, "chew");
    ResourceWithMetadata collResource = locator.locateResource(api, templateVars, HttpMethod.POST);
    result = executor.execute(collResource, Params.valueOf("654", "345", NULL_PARAMS, null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertEquals("All done", result);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) HashMap(java.util.HashMap) 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) Grass(org.alfresco.rest.framework.tests.api.mocks.Grass) Matchers.anyString(org.mockito.Matchers.anyString) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Example 9 with WebScriptResponse

use of org.springframework.extensions.webscripts.WebScriptResponse in project alfresco-remote-api by Alfresco.

the class WithResponseTest method testSetResponse.

@Test
public void testSetResponse() throws Exception {
    AbstractResourceWebScript responseWriter = new ResourceWebScriptDelete();
    responseWriter.setAssistant(new ApiAssistant());
    WithResponse wr = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
    WebScriptResponse response = mock(WebScriptResponse.class);
    responseWriter.setResponse(response, wr.getStatus(), wr.getCache(), wr.getContentInfo(), wr.getHeaders());
    verify(response, times(1)).setStatus(anyInt());
    verify(response, times(1)).setCache((Cache) any());
    verify(response, times(1)).setContentType(anyString());
    verify(response, times(0)).setHeader(anyString(), anyString());
    response = mock(WebScriptResponse.class);
    responseWriter.setResponse(response, wr.getStatus(), null, null, null);
    verify(response, times(1)).setStatus(anyInt());
    verify(response, times(0)).setCache((Cache) any());
    verify(response, times(0)).setContentType(anyString());
    verify(response, times(0)).setHeader(anyString(), anyString());
    response = mock(WebScriptResponse.class);
    wr.addHeader("king", "can");
    wr.addHeader("king", "kong");
    responseWriter.setResponse(response, wr.getStatus(), null, null, wr.getHeaders());
    verify(response, times(1)).setStatus(anyInt());
    verify(response, times(0)).setCache((Cache) any());
    verify(response, times(0)).setContentType(anyString());
    verify(response, times(1)).setHeader(eq("king"), anyString());
    verify(response, times(1)).addHeader(eq("king"), anyString());
    response = mock(WebScriptResponse.class);
    wr.addHeader("king", "kin");
    wr.setHeader("ping", "ping");
    responseWriter.setResponse(response, wr.getStatus(), null, null, wr.getHeaders());
    verify(response, times(1)).setStatus(anyInt());
    verify(response, times(0)).setCache((Cache) any());
    verify(response, times(0)).setContentType(anyString());
    verify(response, times(1)).setHeader(eq("king"), anyString());
    verify(response, times(1)).setHeader(eq("ping"), anyString());
    verify(response, times(2)).addHeader(eq("king"), anyString());
}
Also used : ApiAssistant(org.alfresco.rest.framework.tools.ApiAssistant) WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) ResourceWebScriptDelete(org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete) Test(org.junit.Test)

Example 10 with WebScriptResponse

use of org.springframework.extensions.webscripts.WebScriptResponse in project records-management by Alfresco.

the class BaseWebScriptUnitTest method getMockedWebScriptResponse.

/**
 * Helper method to get mocked web script response
 *
 * @return  {@link WebScriptResponse}   mocked web script response
 */
protected WebScriptResponse getMockedWebScriptResponse() throws Exception {
    WebScriptResponse mockedResponse = mock(WebScriptResponse.class);
    StringWriter writer = new StringWriter();
    doReturn(writer).when(mockedResponse).getWriter();
    return mockedResponse;
}
Also used : StringWriter(java.io.StringWriter) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse)

Aggregations

WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)10 AbstractResourceWebScript (org.alfresco.rest.framework.webscripts.AbstractResourceWebScript)5 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)5 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)2 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)2 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 Goat (org.alfresco.rest.framework.tests.api.mocks.Goat)1 Grass (org.alfresco.rest.framework.tests.api.mocks.Grass)1 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)1 ApiAssistant (org.alfresco.rest.framework.tools.ApiAssistant)1 ResourceWebScriptDelete (org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete)1