Search in sources :

Example 1 with RestLiMethodInvoker

use of com.linkedin.restli.internal.server.RestLiMethodInvoker in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testInvokerWithFilters.

@Test(dataProvider = "provideFilterConfig")
public void testInvokerWithFilters(final boolean throwExceptionFromFirstFilter) throws Exception {
    RestLiArgumentBuilder mockBuilder = createMock(RestLiArgumentBuilder.class);
    Filter mockFilter = createMock(Filter.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());
    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);
    mockFilterContext.setRequestData(requestData);
    expectLastCall();
    expect(mockBuilder.extractRequestData(routingResult, request)).andReturn(requestData);
    FilterChainCallback filterChainCallback = new FilterChainCallback() {

        @Override
        public void onRequestSuccess(RestLiRequestData requestData, RestLiCallback<Object> restLiCallback) {
            // only invoke if filter chain's requests were successful
            invokerWithFilters.invoke(requestData, routingResult, mockBuilder, restLiCallback, null);
        }

        @Override
        public void onResponseSuccess(RestLiResponseData responseData, RestLiResponseAttachments responseAttachments) {
        // unused
        }

        @Override
        public void onError(Throwable th, RestLiResponseData responseData, RestLiResponseAttachments responseAttachments) {
        // unused
        }
    };
    final Exception exFromFilter = new RuntimeException("Exception from filter!");
    if (throwExceptionFromFirstFilter) {
        mockFilter.onRequest(eq(mockFilterContext));
        expectLastCall().andThrow(exFromFilter);
        mockCallback.onError(eq(exFromFilter), anyObject(RequestExecutionReport.class), EasyMock.isNull(RestLiAttachmentReader.class), EasyMock.isNull(RestLiResponseAttachments.class));
    } else {
        expect(mockFilterContext.getRequestData()).andReturn(requestData).times(3);
        mockFilter.onRequest(eq(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 CompletableFuture.completedFuture(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 CompletableFuture.completedFuture(null);
            }
        });
        Long[] argsArray = { 1L };
        expect(mockBuilder.buildArguments(requestData, routingResult)).andReturn(argsArray);
        expect(resource.get(eq(1L))).andReturn(null).once();
        mockCallback.onSuccess(eq(null), anyObject(RequestExecutionReport.class), anyObject(RestLiResponseAttachments.class));
    }
    replay(resource, mockBuilder, mockFilterContext, mockFilter, mockCallback);
    RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
    mockFilterContext.setRequestData(mockBuilder.extractRequestData(routingResult, request));
    RestLiFilterChain filterChain = new RestLiFilterChain(Arrays.asList(mockFilter, mockFilter), filterChainCallback);
    filterChain.onRequest(mockFilterContext, new RestLiFilterResponseContextFactory<Object>(request, routingResult, new RestLiResponseHandler.Builder().build()));
    verify(mockBuilder, mockFilterContext, mockFilter);
    if (throwExceptionFromFirstFilter) {
        assertEquals(requestData.getKey(), "Key");
    } else {
        assertEquals(requestData.getKey(), "Key-Filter2");
        verify(resource);
    }
    EasyMock.reset(resource);
    EasyMock.makeThreadSafe(resource, true);
}
Also used : RestLiMethodInvoker(com.linkedin.restli.internal.server.RestLiMethodInvoker) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) EngineBuilder(com.linkedin.parseq.EngineBuilder) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ErrorResponseBuilder(com.linkedin.restli.internal.server.response.ErrorResponseBuilder) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) URI(java.net.URI) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiResponseHandler(com.linkedin.restli.internal.server.response.RestLiResponseHandler) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) TaskStatusCollectionResource(com.linkedin.restli.server.twitter.TaskStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) CustomStatusCollectionResource(com.linkedin.restli.server.twitter.CustomStatusCollectionResource) PromiseStatusCollectionResource(com.linkedin.restli.server.twitter.PromiseStatusCollectionResource) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) ErrorResponseBuilder(com.linkedin.restli.internal.server.response.ErrorResponseBuilder) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestException(com.linkedin.r2.message.rest.RestException) URISyntaxException(java.net.URISyntaxException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) IAnswer(org.easymock.IAnswer) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) RestRequest(com.linkedin.r2.message.rest.RestRequest) Filter(com.linkedin.restli.server.filter.Filter) CustomLong(com.linkedin.restli.server.custom.types.CustomLong) EasyMock.anyObject(org.easymock.EasyMock.anyObject) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with RestLiMethodInvoker

use of com.linkedin.restli.internal.server.RestLiMethodInvoker in project rest.li by linkedin.

the class TestRestLiMethodInvocation method setUp.

@BeforeTest
protected void setUp() {
    _scheduler = Executors.newSingleThreadScheduledExecutor();
    _engine = new EngineBuilder().setTaskExecutor(_scheduler).setTimerScheduler(_scheduler).build();
    _resourceFactory = new EasyMockResourceFactory();
    _errorResponseBuilder = new ErrorResponseBuilder();
    _methodAdapterRegistry = new MethodAdapterRegistry(_errorResponseBuilder);
    // Add filters to the invoker.
    _invoker = new RestLiMethodInvoker(_resourceFactory, _engine, _errorResponseBuilder);
}
Also used : RestLiMethodInvoker(com.linkedin.restli.internal.server.RestLiMethodInvoker) ErrorResponseBuilder(com.linkedin.restli.internal.server.response.ErrorResponseBuilder) EngineBuilder(com.linkedin.parseq.EngineBuilder) MethodAdapterRegistry(com.linkedin.restli.internal.server.methods.MethodAdapterRegistry) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

EngineBuilder (com.linkedin.parseq.EngineBuilder)2 RestLiMethodInvoker (com.linkedin.restli.internal.server.RestLiMethodInvoker)2 ErrorResponseBuilder (com.linkedin.restli.internal.server.response.ErrorResponseBuilder)2 BeforeTest (org.testng.annotations.BeforeTest)2 ByteString (com.linkedin.data.ByteString)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestException (com.linkedin.r2.message.rest.RestException)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)1 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)1 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)1 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)1 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)1 FilterRequestContextInternal (com.linkedin.restli.internal.server.filter.FilterRequestContextInternal)1 RestLiFilterChain (com.linkedin.restli.internal.server.filter.RestLiFilterChain)1 MethodAdapterRegistry (com.linkedin.restli.internal.server.methods.MethodAdapterRegistry)1 RestLiArgumentBuilder (com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder)1 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)1 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)1