use of com.linkedin.restli.server.twitter.StatusCollectionResource 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(anyObject(FilterRequestContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
_mockFilter.onResponse(anyObject(FilterRequestContext.class), anyObject(FilterResponseContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
replay(_mockFilter);
}
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.restli.server.twitter.StatusCollectionResource in project rest.li by linkedin.
the class TestRestLiServer method testHandleRequestWithRestLiResponseSuccess.
@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseSuccess(final RestOrStream restOrStream) throws Exception {
Status status = new Status();
status.data().put("test", "this is a test");
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(status).once();
replay(statusResource);
Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {
@Override
public void onSuccess(RestLiResponse restLiResponse) {
assertEquals(restLiResponse.getDataMap(), status.data());
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
@Override
public void onError(Throwable e) {
fail("We should not get an error here. The server should have returned a 200!");
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_server.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
_server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
}
}
Aggregations