use of com.linkedin.restli.server.RestLiConfig in project rest.li by linkedin.
the class TestInjectMockResourceFactory method testMockInjectionViaServer.
@Test
public void testMockInjectionViaServer() {
final CounterBean counter = new CounterBean();
InjectMockResourceFactory factory = new InjectMockResourceFactory(new SimpleBeanProvider().add("counterBean", counter).add("mySpecialBean", new SpecialBean()));
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames("com.linkedin.restli.server.mock");
RestLiServer server = new RestLiServer(config, factory);
server.handleRequest(new MockRequest(), new RequestContext(), new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail("expected success response");
}
@Override
public void onSuccess(RestResponse result) {
Assert.assertEquals(counter._counter, 1);
}
});
}
use of com.linkedin.restli.server.RestLiConfig in project rest.li by linkedin.
the class TestAsyncMethodInvocationPlanClass method testAsyncGet.
@Test(dataProvider = "requestData")
public void testAsyncGet(String uri, String method, String expectedPlanClass) throws URISyntaxException, IOException, InterruptedException {
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames("com.linkedin.restli.server.multiplexer.resources");
SettablePromise<Trace> traceHolder = Promises.settable();
Engine engine = engine(traceHolder);
RestLiServer server = new RestLiServer(config, resourceFactory(), engine);
RestRequest request = new RestRequestBuilder(new URI(uri)).setMethod(method).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
CountDownLatch latch = new CountDownLatch(1);
server.handleRequest(request, new RequestContext(), callback(latch));
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
assertEquals(traceHolder.get().getPlanClass(), expectedPlanClass);
}
use of com.linkedin.restli.server.RestLiConfig in project rest.li by linkedin.
the class TestAsyncMethodInvocationPlanClass method testMultiplexedAsyncGet.
@Test(dataProvider = "multiplexerConfigurations")
public void testMultiplexedAsyncGet(MultiplexerRunMode multiplexerRunMode) throws URISyntaxException, IOException, InterruptedException {
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames("com.linkedin.restli.server.multiplexer.resources");
config.setMultiplexerRunMode(multiplexerRunMode);
SettablePromise<Trace> traceHolder = Promises.settable();
Engine engine = engine(traceHolder);
RestLiServer server = new RestLiServer(config, resourceFactory(), engine);
IndividualRequest r0 = individualRequest("/users/0", null, Collections.<String, IndividualRequest>emptyMap());
IndividualRequest r1 = individualRequest("/users/1", null, Collections.<String, IndividualRequest>emptyMap());
IndividualRequest r2 = individualRequest("/users/2", null, ImmutableMap.of("0", r0, "1", r1));
// request is seq(par(r0, r1), r2)
RestRequest request = muxRestRequest(ImmutableMap.of("2", r2));
CountDownLatch latch = new CountDownLatch(1);
server.handleRequest(request, new RequestContext(), callback(latch));
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
if (multiplexerRunMode == MultiplexerRunMode.SINGLE_PLAN) {
//For multiplexed requests in SINGLE_PLAN mode there is only one plan with class "mux"
assertEquals(traceHolder.get().getPlanClass(), "mux");
} else {
//For multiplexed requests in MULTIPLE_PLANS mode there are multiple plans, first one is with class "mux",
//following 3 are with class "resource=users,method=get", last one will have class "resource=users,method=get"
assertEquals(traceHolder.get().getPlanClass(), "resource=users,method=get");
}
}
use of com.linkedin.restli.server.RestLiConfig in project rest.li by linkedin.
the class RestLiIntTestServer method createServer.
public static HttpServer createServer(final Engine engine, int port, boolean useAsyncServletApi, int asyncTimeOut, List<? extends Filter> filters, final FilterChain filterChain, final boolean forceUseRestServer) {
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames(RESOURCE_PACKAGE_NAMES);
config.setServerNodeUri(URI.create("http://localhost:" + port));
config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
config.addDebugRequestHandlers(new ParseqTraceDebugRequestHandler());
config.setFilters(filters);
GroupMembershipMgr membershipMgr = new HashGroupMembershipMgr();
GroupMgr groupMgr = new HashMapGroupMgr(membershipMgr);
GroupsRestApplication app = new GroupsRestApplication(groupMgr, membershipMgr);
SimpleBeanProvider beanProvider = new SimpleBeanProvider();
beanProvider.add("GroupsRestApplication", app);
//using InjectMockResourceFactory to keep examples spring-free
ResourceFactory factory = new InjectMockResourceFactory(beanProvider);
//Todo this will have to change further to accomodate streaming tests - this is temporary
final TransportDispatcher dispatcher;
if (forceUseRestServer) {
dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, factory, engine));
} else {
final StreamRequestHandler streamRequestHandler = new RestLiServer(config, factory, engine);
dispatcher = new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
throw new UnsupportedOperationException("This server only accepts streaming");
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
try {
streamRequestHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<>(callback));
} catch (Exception e) {
final Exception ex = RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e);
callback.onResponse(TransportResponseImpl.<StreamResponse>error(ex));
}
}
};
}
return new HttpServerFactory(filterChain).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, HttpServerFactory.DEFAULT_THREAD_POOL_SIZE, dispatcher, useAsyncServletApi ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, asyncTimeOut, !forceUseRestServer);
}
use of com.linkedin.restli.server.RestLiConfig in project rest.li by linkedin.
the class MockHttpServerFactory method create.
/**
* Creates a {@link HttpServer} that contains a {@link RestLiServer} to be used for testing a set of Rest.li
* resources.
*
* The {@link HttpServer} uses an empty {@link FilterChain} and uses "/" as the context path.
*
* If the server is run in async mode (by calling this function with the last parameter {@code true}), the
* timeout used is {@link #ASYNC_TIMEOUT}.
*
* Both the async and sync servers will use {@link #NUM_THREADS} threads.
*
* The server is started by calling {@link com.linkedin.r2.transport.http.server.HttpServer#start()}
* The server is stopped by calling {@link com.linkedin.r2.transport.http.server.HttpServer#stop()}
*
* @param port the port the server will run on on localhost
* @param resourceClasses the Rest.li resource classes
* @param beans beans you want to inject into your Rest.li resource.
* @param enableAsync true if the server should be async , false otherwise
* @return
*/
public static HttpServer create(int port, Set<Class<?>> resourceClasses, Map<String, ?> beans, boolean enableAsync) {
RestLiConfig config = createConfig(port);
Set<String> resourceClassNames = new HashSet<String>();
for (Class<?> clazz : resourceClasses) {
resourceClassNames.add(clazz.getName());
}
config.setResourceClassNamesSet(resourceClassNames);
return create(port, config, beans, enableAsync);
}
Aggregations