use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestRestLiServer method testDebugRequestHandlers.
@Test(dataProvider = "restOrStream")
public void testDebugRequestHandlers(final RestOrStream restOrStream) throws URISyntaxException {
//Without a resource
final Callback<RestResponse> noResourceRestResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
assertEquals(restResponse.getStatus(), 200);
String responseString = restResponse.getEntity().asString(Charset.defaultCharset());
Assert.assertEquals(responseString, DEBUG_HANDLER_RESPONSE_A);
}
@Override
public void onError(Throwable e) {
fail();
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1/__debug/a/s")).build();
_server.handleRequest(request, new RequestContext(), noResourceRestResponseCallback);
} else {
StreamRequest request = new StreamRequestBuilder(new URI("/statuses/1/__debug/a/s")).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
noResourceRestResponseCallback.onSuccess(result);
}
});
}
@Override
public void onError(Throwable e) {
fail();
}
};
_server.handleRequest(request, new RequestContext(), callback);
}
//With a resource this time
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
EasyMock.replay(statusResource);
final Callback<RestResponse> resourceRestResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
assertEquals(restResponse.getStatus(), 200);
String responseString = restResponse.getEntity().asString(Charset.defaultCharset());
Assert.assertEquals(responseString, DEBUG_HANDLER_RESPONSE_B);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
@Override
public void onError(Throwable e) {
fail();
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1/__debug/b")).build();
_server.handleRequest(request, new RequestContext(), resourceRestResponseCallback);
} else {
StreamRequest request = new StreamRequestBuilder(new URI("/statuses/1/__debug/b")).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
resourceRestResponseCallback.onSuccess(result);
}
});
}
@Override
public void onError(Throwable e) {
fail();
}
};
_server.handleRequest(request, new RequestContext(), callback);
}
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestRestLiServer method testValidRequest.
private void testValidRequest(RestLiServer restLiServer, final ProtocolVersion clientProtocolVersion, boolean filters, final String headerConstant, final RestOrStream restOrStream) throws URISyntaxException {
RestRequest request = null;
StreamRequest streamRequest = null;
if (clientProtocolVersion != null) {
if (restOrStream == RestOrStream.REST) {
request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build();
} else {
streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
}
} else {
if (restOrStream == RestOrStream.REST) {
request = new RestRequestBuilder(new URI("/statuses/1")).build();
} else {
streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).build(EntityStreams.emptyStream());
}
}
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
if (filters) {
_mockFilter.onRequest(EasyMock.anyObject(FilterRequestContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
_mockFilter.onResponse(EasyMock.anyObject(FilterRequestContext.class), EasyMock.anyObject(FilterResponseContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
EasyMock.replay(_mockFilter);
}
EasyMock.replay(statusResource);
final Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
assertEquals(restResponse.getStatus(), 200);
assertTrue(restResponse.getEntity().length() > 0);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
if (clientProtocolVersion != null) {
assertEquals(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, headerConstant, "Rest.li protocol header name is unexpected.");
}
}
@Override
public void onError(Throwable e) {
fail();
}
};
if (restOrStream == RestOrStream.REST) {
restLiServer.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
Callback<StreamResponse> streamResponseCallback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
restResponseCallback.onSuccess(result);
}
});
}
@Override
public void onError(Throwable e) {
fail();
}
};
restLiServer.handleRequest(streamRequest, new RequestContext(), streamResponseCallback);
}
if (filters) {
EasyMock.verify(_mockFilter, _mockFilter);
}
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestRestLiServer method testStreamRequestMultiplexedRequestMultiPartAcceptType.
@Test
public void testStreamRequestMultiplexedRequestMultiPartAcceptType() throws Exception {
//This test verifies that a StreamRequest sent to the RestLiServer throws an exception if the accept type contains
//multipart/related.
StreamRequest streamRequestMux = new StreamRequestBuilder(new URI("/mux")).setHeader(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse restResponse) {
fail();
}
@Override
public void onError(Throwable e) {
assertTrue(e instanceof StreamException);
StreamException streamException = (StreamException) e;
StreamResponse streamResponse = streamException.getResponse();
assertEquals(streamResponse.getStatus(), 406);
final FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(ByteString result) {
//We have the body so assert
assertTrue(result.length() > 0);
assertEquals(result.asString(Charset.defaultCharset()), "This server cannot handle multiplexed requests that have an accept type of multipart/related");
}
});
streamResponse.getEntityStream().setReader(fullEntityReader);
}
};
_server.handleRequest(streamRequestMux, new RequestContext(), callback);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestRestLiServer method testRestRequestAttemptVerifyParseFailed.
@Test
public void testRestRequestAttemptVerifyParseFailed() throws Exception {
//This test verifies that a RestRequest sent to the RestLiServer throws an exception if the content type or accept types
//fail to parse properly. This occurs when we try to verify that the request's content type or accept types do
//not include multipart/related.
RestRequest invalidContentTypeRequest = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_CONTENT_TYPE, "©").build();
Callback<RestResponse> callback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
fail();
}
@Override
public void onError(Throwable e) {
assertTrue(e instanceof RestException);
RestException restException = (RestException) e;
RestResponse restResponse = restException.getResponse();
assertEquals(restResponse.getStatus(), 400);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getEntity().asString(Charset.defaultCharset()), "Unable to parse content or accept types.");
}
};
_server.handleRequest(invalidContentTypeRequest, new RequestContext(), callback);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class ExampleD2Server method startAnnouncers.
private static void startAnnouncers(final ZooKeeperConnectionManager manager, ExecutorService executorService, Long timeout) {
Future task = executorService.submit(new Runnable() {
@Override
public void run() {
manager.start(new Callback<None>() {
@Override
public void onError(Throwable e) {
System.err.println("problem starting D2 announcers. Aborting...");
e.printStackTrace();
System.exit(1);
}
@Override
public void onSuccess(None result) {
System.out.println("D2 announcers successfully started. ");
System.out.println("Press enter to stop echo servers and d2 announcers...");
}
});
}
});
try {
task.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
System.err.println("Cannot start zookeeper announcer. Timeout is set to " + timeout + " ms");
e.printStackTrace();
System.exit(1);
}
}
Aggregations