Search in sources :

Example 1 with ResourceContextImpl

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

the class TestRestLiMethodInvocation method checkInvocation.

private void checkInvocation(Object resource, ResourceMethodDescriptor resourceMethodDescriptor, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, final RequestExecutionCallback<RestResponse> callback, final boolean isDebugMode, final boolean expectRoutingException, final RestLiAttachmentReader expectedRequestAttachments, final RestLiResponseAttachments expectedResponseAttachments) throws URISyntaxException, RestLiSyntaxException {
    assertNotNull(resource);
    assertNotNull(resourceMethodDescriptor);
    try {
        EasyMock.replay(resource);
        RestRequestBuilder builder = new RestRequestBuilder(new URI(uri)).setMethod(httpMethod).addHeaderValue("Accept", "application/json").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
        if (entityBody != null) {
            builder.setEntity(entityBody.getBytes(Data.UTF_8_CHARSET));
        }
        RestRequest request = builder.build();
        final ResourceContext resourceContext = new ResourceContextImpl(pathkeys, request, new RequestContext(), true, expectedRequestAttachments);
        resourceContext.setResponseAttachments(expectedResponseAttachments);
        RoutingResult routingResult = new RoutingResult(resourceContext, resourceMethodDescriptor);
        FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), resourceMethodDescriptor);
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch expectedRoutingExceptionLatch = new CountDownLatch(1);
        RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(resourceMethodDescriptor.getType());
        RestLiRequestData requestData = adapter.extractRequestData(routingResult, request);
        filterContext.setRequestData(requestData);
        RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler.Builder().build();
        RequestExecutionReportBuilder requestExecutionReportBuilder = null;
        if (isDebugMode) {
            requestExecutionReportBuilder = new RequestExecutionReportBuilder();
        }
        RequestExecutionCallback<RestResponse> executionCallback = new RequestExecutionCallback<RestResponse>() {

            @Override
            public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
                if (isDebugMode) {
                    Assert.assertNotNull(executionReport);
                } else {
                    Assert.assertNull(executionReport);
                }
                if (e.getCause().getCause() instanceof RoutingException) {
                    expectedRoutingExceptionLatch.countDown();
                }
                if (callback != null) {
                    callback.onError(e, executionReport, null, null);
                }
                Assert.assertEquals(requestAttachmentReader, expectedRequestAttachments);
                Assert.assertEquals(responseAttachments, expectedResponseAttachments);
                latch.countDown();
            }

            @Override
            public void onSuccess(final RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
                if (isDebugMode) {
                    Assert.assertNotNull(executionReport);
                } else {
                    Assert.assertNull(executionReport);
                }
                if (callback != null) {
                    callback.onSuccess(result, executionReport, null);
                }
                Assert.assertEquals(responseAttachments, expectedResponseAttachments);
                latch.countDown();
            }
        };
        FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(routingResult, _invoker, adapter, requestExecutionReportBuilder, expectedRequestAttachments, restLiResponseHandler, executionCallback);
        final RestLiCallback<Object> outerCallback = new RestLiCallback<Object>(filterContext, new RestLiFilterResponseContextFactory<Object>(request, routingResult, restLiResponseHandler), new RestLiFilterChain(null, filterChainCallback));
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
        filterContext.setRequestData(adapter.extractRequestData(routingResult, request));
        _invoker.invoke(filterContext.getRequestData(), routingResult, adapter, outerCallback, requestExecutionReportBuilder);
        try {
            latch.await();
            if (expectRoutingException) {
                expectedRoutingExceptionLatch.await();
            }
        } catch (InterruptedException e) {
        // Ignore
        }
        EasyMock.verify(resource);
        Assert.assertEquals(((ServerResourceContext) routingResult.getContext()).getResponseMimeType(), "application/json");
    } catch (RestLiSyntaxException e) {
        throw new RoutingException("syntax exception", 400);
    } finally {
        EasyMock.reset(resource);
        EasyMock.makeThreadSafe(resource, true);
    }
}
Also used : FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) RoutingException(com.linkedin.restli.server.RoutingException) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) URI(java.net.URI) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RequestExecutionCallback(com.linkedin.restli.server.RequestExecutionCallback) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiResponseHandler(com.linkedin.restli.internal.server.response.RestLiResponseHandler) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestRequest(com.linkedin.r2.message.rest.RestRequest) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) EasyMock.anyObject(org.easymock.EasyMock.anyObject) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData)

Example 2 with ResourceContextImpl

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

the class TestRestLiResponseHandler method buildRoutingResult.

private final RoutingResult buildRoutingResult(ResourceMethod resourceMethod, RestRequest request, Map<String, String> acceptHeaders) throws SecurityException, NoSuchMethodException, RestLiSyntaxException {
    Method method = ProjectionTestFixture.class.getMethod("batchGet", Set.class);
    ResourceModel model = RestLiTestHelper.buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = ResourceMethodDescriptor.createForRestful(resourceMethod, method, InterfaceType.SYNC);
    model.addResourceMethodDescriptor(methodDescriptor);
    ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptHeaders, context);
    return new RoutingResult(context, methodDescriptor);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 3 with ResourceContextImpl

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

the class TestRestLiFilterChain method init.

@BeforeMethod
protected void init() {
    _filters = new CountFilter[] { new CountFilter(), new CountFilter(), new CountFilter() };
    when(_mockFilterResponseContextFactory.getRequestContext()).thenAnswer(invocation -> new RequestContext());
    when(_method.getContext()).thenAnswer(invocation -> new ResourceContextImpl());
}
Also used : FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) CountFilter(com.linkedin.restli.internal.server.filter.testfilters.CountFilter) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with ResourceContextImpl

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

the class TestRestLiRouting method testDefaultPathKeyUniqueness.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "uniqueness")
public void testDefaultPathKeyUniqueness(ProtocolVersion version, String uri) throws Exception {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(CombinedResources.CombinedCollectionWithSubresources.class, CombinedResources.SubCollectionResource.class);
    _router = new RestLiRouter(pathRootResourceMap, new RestLiConfig());
    RestRequest request;
    // #1 simple GET
    request = createRequest(uri, "GET", version);
    ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    ResourceMethodDescriptor method = _router.process(context);
    assertNotNull(method);
    PathKeys keys = context.getPathKeys();
    assertEquals(keys.getAsString("testId"), "foo");
    assertEquals(keys.getAsString("subId"), "bar");
}
Also used : CombinedResources(com.linkedin.restli.server.combined.CombinedResources) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) PathKeys(com.linkedin.restli.server.PathKeys) RestLiRouter(com.linkedin.restli.internal.server.RestLiRouter) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RequestContext(com.linkedin.r2.message.RequestContext) RestLiConfig(com.linkedin.restli.server.RestLiConfig) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Example 5 with ResourceContextImpl

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

the class TestRestLiRouting method testRoutingDetailsSimpleGet.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsSimple")
public void testRoutingDetailsSimpleGet(ProtocolVersion version, String uri) throws Exception {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(TrendingResource.class);
    _router = new RestLiRouter(pathRootResourceMap, new RestLiConfig());
    RestRequest request = createRequest(uri, "GET", version);
    ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    ResourceMethodDescriptor resourceMethodDescriptor = _router.process(context);
    assertNotNull(resourceMethodDescriptor);
    assertEquals(resourceMethodDescriptor.getType(), ResourceMethod.GET);
    assertNull(resourceMethodDescriptor.getActionName());
    assertNull(resourceMethodDescriptor.getFinderName());
    assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), TrendingResource.class);
    assertEquals(resourceMethodDescriptor.getMethod().getName(), "get");
    assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] {});
    assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "trending");
    PathKeys keys = context.getPathKeys();
    assertNull(keys.getBatchIds());
}
Also used : PathKeys(com.linkedin.restli.server.PathKeys) RestLiRouter(com.linkedin.restli.internal.server.RestLiRouter) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RequestContext(com.linkedin.r2.message.RequestContext) RestLiConfig(com.linkedin.restli.server.RestLiConfig) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Aggregations

ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)52 RequestContext (com.linkedin.r2.message.RequestContext)41 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)40 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)34 Test (org.testng.annotations.Test)31 RestRequest (com.linkedin.r2.message.rest.RestRequest)29 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)25 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)20 URI (java.net.URI)15 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)14 RestLiRouter (com.linkedin.restli.internal.server.RestLiRouter)14 RestLiConfig (com.linkedin.restli.server.RestLiConfig)14 ByteString (com.linkedin.data.ByteString)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)10 PathKeys (com.linkedin.restli.server.PathKeys)10 RoutingException (com.linkedin.restli.server.RoutingException)9 DataMap (com.linkedin.data.DataMap)7 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)7