Search in sources :

Example 41 with RoutingResult

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

the class TestPatchArgumentBuilder method testFailure.

@Test(dataProvider = "failurePatchData")
public void testFailure(String entity) {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 1);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult();
    RestLiArgumentBuilder argumentBuilder = new PatchArgumentBuilder();
    try {
        argumentBuilder.extractRequestData(routingResult, request);
        Assert.fail("Expected RoutingException");
    } catch (RoutingException e) {
        Assert.assertTrue(e.getMessage().contains("Error parsing entity body"));
    }
    verify(request, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) Test(org.testng.annotations.Test)

Example 42 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult 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 43 with RoutingResult

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

the class TestRestLiMethodInvocation method testAction_BadParameterTypes.

@Test
public void testAction_BadParameterTypes() throws Exception {
    ResourceModel accountsResourceModel = buildResourceModel(TwitterAccountsResource.class);
    ResourceMethodDescriptor methodDescriptor;
    // #1 no defaults provided
    methodDescriptor = accountsResourceModel.findActionMethod("register", ResourceLevel.COLLECTION);
    String jsonEntityBody = RestLiTestHelper.doubleQuote("{'first': 42, 'last': 42, 'email': 42, " + "'company': 42, 'openToMarketingEmails': 'false'}");
    RestRequest request = new RestRequestBuilder(new URI("/accounts?action=register")).setMethod("POST").setEntity(jsonEntityBody.getBytes(Data.UTF_8_CHARSET)).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString()).build();
    RoutingResult routingResult = new RoutingResult(new ResourceContextImpl(null, request, new RequestContext()), methodDescriptor);
    final FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), routingResult.getResourceMethod());
    try {
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
        filterContext.setRequestData(_methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getMethodType()).extractRequestData(routingResult, request));
        _invoker.invoke(null, routingResult, _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getMethodType()), null, null);
        Assert.fail("expected routing exception");
    } catch (RoutingException e) {
        Assert.assertEquals(e.getStatus(), 400);
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) 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) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 44 with RoutingResult

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

the class TestRestLiMethodInvocation method testInvokeWithUnsupportedAcceptMimeType.

@Test
public void testInvokeWithUnsupportedAcceptMimeType() throws Exception {
    RestRequestBuilder builder = new RestRequestBuilder(new URI("")).addHeaderValue("Accept", "text/plain").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
    RestRequest request = builder.build();
    final RestLiAttachmentReader attachmentReader = new RestLiAttachmentReader(null);
    final CountDownLatch latch = new CountDownLatch(1);
    RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler.Builder().build();
    RequestExecutionCallback<RestResponse> executionCallback = new RequestExecutionCallback<RestResponse>() {

        @Override
        public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
            latch.countDown();
            Assert.assertTrue(e instanceof RestException);
            RestException ex = (RestException) e;
            Assert.assertEquals(ex.getResponse().getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
            Assert.assertEquals(requestAttachmentReader, attachmentReader);
            Assert.assertNull(responseAttachments);
        }

        @Override
        public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
            Assert.fail();
        }
    };
    ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), new RestRequestBuilder(URI.create("")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.LATEST_PROTOCOL_VERSION.toString()).build(), new RequestContext(), false, attachmentReader);
    try {
        RoutingResult routingResult = new RoutingResult(resourceContext, null);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
        FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(null, _invoker, null, null, null, restLiResponseHandler, executionCallback);
        final RestLiCallback<Object> callback = new RestLiCallback<Object>(null, new RestLiFilterResponseContextFactory<Object>(request, null, restLiResponseHandler), new RestLiFilterChain(null, filterChainCallback));
        _invoker.invoke(null, routingResult, null, callback, null);
        latch.await();
    } catch (Exception e) {
        // exception is expected
        Assert.assertTrue(e instanceof RestLiServiceException);
    }
    Assert.assertNull(resourceContext.getResponseMimeType());
}
Also used : 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) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) 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) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestResponse(com.linkedin.r2.message.rest.RestResponse) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) RestException(com.linkedin.r2.message.rest.RestException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) EasyMock.anyObject(org.easymock.EasyMock.anyObject) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 45 with RoutingResult

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

the class TestRestLiMethodInvocation method testAction_BadArrayElements.

@Test
public void testAction_BadArrayElements() throws Exception {
    ResourceModel accountsResourceModel = buildResourceModel(TwitterAccountsResource.class);
    ResourceMethodDescriptor methodDescriptor;
    // #1 no defaults provided
    methodDescriptor = accountsResourceModel.findActionMethod("spamTweets", ResourceLevel.COLLECTION);
    String jsonEntityBody = RestLiTestHelper.doubleQuote("{'statuses':[1,2,3]}");
    RestRequest request = new RestRequestBuilder(new URI("/accounts?action=spamTweets")).setMethod("POST").setEntity(jsonEntityBody.getBytes(Data.UTF_8_CHARSET)).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString()).build();
    RoutingResult routingResult = new RoutingResult(new ResourceContextImpl(null, request, new RequestContext()), methodDescriptor);
    final FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), routingResult.getResourceMethod());
    RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getType());
    try {
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
        filterContext.setRequestData(adapter.extractRequestData(routingResult, request));
        _invoker.invoke(filterContext.getRequestData(), routingResult, _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getMethodType()), null, null);
        Assert.fail("expected routing exception");
    } catch (RoutingException e) {
        Assert.assertEquals(e.getStatus(), 400);
    }
}
Also used : FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) RoutingException(com.linkedin.restli.server.RoutingException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RoutingResult (com.linkedin.restli.internal.server.RoutingResult)93 Test (org.testng.annotations.Test)78 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)66 RestRequest (com.linkedin.r2.message.rest.RestRequest)58 ResourceContext (com.linkedin.restli.server.ResourceContext)39 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)38 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)38 RequestContext (com.linkedin.r2.message.RequestContext)27 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)24 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)22 RoutingException (com.linkedin.restli.server.RoutingException)18 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)15 RestLiRouter (com.linkedin.restli.internal.server.RestLiRouter)14 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)13 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)13 HashMap (java.util.HashMap)13 Foo (com.linkedin.pegasus.generator.examples.Foo)11 Parameter (com.linkedin.restli.internal.server.model.Parameter)11 PathKeys (com.linkedin.restli.server.PathKeys)11 URI (java.net.URI)11