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);
}
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;
}
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);
}
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());
}
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;
}
Aggregations