Search in sources :

Example 1 with EasyMockResourceFactory

use of com.linkedin.restli.server.test.EasyMockResourceFactory in project rest.li by linkedin.

the class TestRestLiServer method setUpServer.

private void setUpServer(Engine engine) {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.server.twitter");
    _resourceFactory = new EasyMockResourceFactory();
    RestLiDebugRequestHandler debugRequestHandlerA = new RestLiDebugRequestHandler() {

        @Override
        public void handleRequest(final RestRequest request, final RequestContext context, final ResourceDebugRequestHandler resourceRequestHandler, final RestLiAttachmentReader attachmentReader, final RequestExecutionCallback<RestResponse> callback) {
            handleRequestWithCustomResponse(callback, DEBUG_HANDLER_RESPONSE_A);
        }

        @Override
        public String getHandlerId() {
            return "a";
        }
    };
    RestLiDebugRequestHandler debugRequestHandlerB = new RestLiDebugRequestHandler() {

        @Override
        @SuppressWarnings("unchecked")
        public void handleRequest(final RestRequest request, final RequestContext context, final ResourceDebugRequestHandler resourceRequestHandler, final RestLiAttachmentReader attachmentReader, final RequestExecutionCallback<RestResponse> callback) {
            resourceRequestHandler.handleRequest(request, context, EasyMock.createMock(RequestExecutionCallback.class));
            handleRequestWithCustomResponse(callback, DEBUG_HANDLER_RESPONSE_B);
        }

        @Override
        public String getHandlerId() {
            return "b";
        }
    };
    config.addDebugRequestHandlers(debugRequestHandlerA, debugRequestHandlerB);
    _server = new RestLiServer(config, _resourceFactory, engine);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) EasyMockResourceFactory(com.linkedin.restli.server.test.EasyMockResourceFactory)

Example 2 with EasyMockResourceFactory

use of com.linkedin.restli.server.test.EasyMockResourceFactory in project rest.li by linkedin.

the class TestRestLiServer method testRequestHandlers.

@Test(dataProvider = "requestHandlersData")
public void testRequestHandlers(URI uri, String expectedResponse) {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.server.twitter");
    config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler() {

        @Override
        public void initialize(RestLiConfig config, Map<String, ResourceModel> rootResources) {
        /* no-op */
        }

        @Override
        public void handleRequest(RestRequest req, RequestContext ctx, Callback<RestResponse> cb) {
            cb.onSuccess(new RestResponseBuilder().setEntity(toByteString(DOCUMENTATION_RESPONSE)).build());
        }
    });
    config.addCustomRequestHandlers(new CustomRequestHandler());
    config.addDebugRequestHandlers(new DebugRequestHandler("a", DEBUG_HANDLER_RESPONSE_A), new DebugRequestHandler("b", DEBUG_HANDLER_RESPONSE_B));
    RestLiServer server = new RestLiServer(config, new EasyMockResourceFactory(), createMock(Engine.class));
    RestRequest restReq = new RestRequestBuilder(uri).build();
    server.handleRequest(restReq, createMock(RequestContext.class), new RestResponseAssertionCallback(expectedResponse));
    StreamRequest streamReq = new StreamRequestBuilder(uri).build(EntityStreams.emptyStream());
    server.handleRequest(streamReq, createMock(RequestContext.class), new StreamResponseAssertionCallback(expectedResponse));
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) DefaultDocumentationRequestHandler(com.linkedin.restli.docgen.DefaultDocumentationRequestHandler) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) EasyMockResourceFactory(com.linkedin.restli.server.test.EasyMockResourceFactory) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Engine(com.linkedin.parseq.Engine) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with EasyMockResourceFactory

use of com.linkedin.restli.server.test.EasyMockResourceFactory in project rest.li by linkedin.

the class TestRestLiServer method setUp.

@BeforeTest
protected void setUp() {
    // silence null engine warning and get EasyMock failure if engine is used
    Engine fakeEngine = EasyMock.createMock(Engine.class);
    _mockFilter = EasyMock.createMock(Filter.class);
    _resourceFactory = new EasyMockResourceFactory();
    setUpServer(fakeEngine);
    setupServerWithFilters(fakeEngine);
    setupServerWithCustomErrorResponseConfig(fakeEngine);
    setUpServerWithNoExceptionStackTrace(fakeEngine);
    replay(fakeEngine);
}
Also used : Filter(com.linkedin.restli.server.filter.Filter) Engine(com.linkedin.parseq.Engine) EasyMockResourceFactory(com.linkedin.restli.server.test.EasyMockResourceFactory) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with EasyMockResourceFactory

use of com.linkedin.restli.server.test.EasyMockResourceFactory in project rest.li by linkedin.

the class TestRestLiServer method testResourceDefinitionListeners.

@Test
public void testResourceDefinitionListeners() throws Exception {
    int[] counts = new int[2];
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.server.twitter");
    config.addResourceDefinitionListener(new ResourceDefinitionListener() {

        @Override
        public void onInitialized(Map<String, ResourceDefinition> definitions) {
            int resourceCount = 0;
            for (ResourceDefinition definition : definitions.values()) {
                resourceCount = resourceCount + 1 + countSubResources(definition);
            }
            counts[0] = definitions.size();
            counts[1] = resourceCount;
        }
    });
    new RestLiServer(config, new EasyMockResourceFactory(), EasyMock.createMock(Engine.class));
    assertEquals(counts[0], 18);
    assertEquals(counts[1], 25);
}
Also used : ByteString(com.linkedin.data.ByteString) EasyMockResourceFactory(com.linkedin.restli.server.test.EasyMockResourceFactory) Engine(com.linkedin.parseq.Engine) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

EasyMockResourceFactory (com.linkedin.restli.server.test.EasyMockResourceFactory)4 Engine (com.linkedin.parseq.Engine)3 BeforeTest (org.testng.annotations.BeforeTest)3 ByteString (com.linkedin.data.ByteString)2 RequestContext (com.linkedin.r2.message.RequestContext)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)2 AfterTest (org.testng.annotations.AfterTest)2 Test (org.testng.annotations.Test)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)1 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)1 DefaultDocumentationRequestHandler (com.linkedin.restli.docgen.DefaultDocumentationRequestHandler)1 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)1 Filter (com.linkedin.restli.server.filter.Filter)1