RestLiArgumentBuilder mockBuilder = createMock(RestLiArgumentBuilder.class);
RequestFilter mockFilter = createMock(RequestFilter.class);
@SuppressWarnings("unchecked")
RequestExecutionCallback<Object> mockCallback = createMock(RequestExecutionCallback.class);
FilterRequestContextInternal mockFilterContext = createMock(FilterRequestContextInternal.class);
RestLiRequestData requestData = new RestLiRequestDataImpl.Builder().key("Key").build();
RestLiMethodInvoker invokerWithFilters =
new RestLiMethodInvoker(_resourceFactory,
_engine,
new ErrorResponseBuilder(),
mockRegistry,
Arrays.asList(mockFilter, mockFilter));
Map<String, ResourceModel> resourceModelMap =
buildResourceModels(StatusCollectionResource.class, LocationResource.class, DiscoveredItemsResource.class);
ResourceModel statusResourceModel = resourceModelMap.get("/statuses");
ResourceMethodDescriptor resourceMethodDescriptor = statusResourceModel.findMethod(ResourceMethod.GET);
final StatusCollectionResource resource = getMockResource(StatusCollectionResource.class);
RestRequestBuilder builder =
new RestRequestBuilder(new URI("/statuses/1")).setMethod("GET")
.addHeaderValue("Accept", "application/json")
.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION,
AllProtocolVersions.LATEST_PROTOCOL_VERSION.toString());
RestRequest request = builder.build();
RoutingResult routingResult =
new RoutingResult(new ResourceContextImpl(buildPathKeys("statusID", 1L), request, new RequestContext()),
resourceMethodDescriptor);
expect(mockRegistry.getArgumentBuilder(resourceMethodDescriptor.getType())).andReturn(mockBuilder);
expect(mockBuilder.extractRequestData(routingResult, request)).andReturn(requestData);
mockFilterContext.setRequestData(requestData);
final Exception exFromFilter = new RuntimeException("Exception from filter!");
if (throwExceptionFromFirstFilter)
{
mockFilter.onRequest(mockFilterContext);
expectLastCall().andThrow(exFromFilter);
mockCallback.onError(eq(exFromFilter), anyObject(RequestExecutionReport.class));
}
else
{
expect(mockFilterContext.getRequestData()).andReturn(requestData).times(3);
mockFilter.onRequest(mockFilterContext);
expectLastCall().andAnswer(new IAnswer<Object>()
{
@Override
public Object answer() throws Throwable
{
FilterRequestContext filterContext = (FilterRequestContext) getCurrentArguments()[0];
RestLiRequestData data = filterContext.getRequestData();
// Verify incoming data.
assertEquals(data.getKey(), "Key");
// Update data.
data.setKey("Key-Filter1");
return null;
}
}).andAnswer(new IAnswer<Object>()
{
@Override
public Object answer() throws Throwable
{
FilterRequestContext filterContext = (FilterRequestContext) getCurrentArguments()[0];
RestLiRequestData data = filterContext.getRequestData();
// Verify incoming data.
assertEquals(data.getKey(), "Key-Filter1");
// Update data.
data.setKey("Key-Filter2");
return null;
}
});
Long[] argsArray = { 1L };
expect(mockBuilder.buildArguments(requestData, routingResult)).andReturn(argsArray);