use of com.linkedin.restli.server.twitter.StatusCollectionResource in project rest.li by linkedin.
the class TestRestLiServer method testPreprocessingError.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testPreprocessingError(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
//Bad key type will generate a routing error
final StatusCollectionResource statusResource = _resourceFactory.getMock(StatusCollectionResource.class);
EasyMock.replay(statusResource);
Callback<RestResponse> restResponseCallback = 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.getHeader(errorResponseHeaderName), RestConstants.HEADER_VALUE_ERROR);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
fail();
}
@Override
public void onError(Throwable e) {
Messages.toRestException((StreamException) e, new Callback<RestException>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestException result) {
restResponseCallback.onError(result);
}
});
}
};
_server.handleRequest(streamRequest, new RequestContext(), callback);
}
}
use of com.linkedin.restli.server.twitter.StatusCollectionResource in project rest.li by linkedin.
the class TestRestLiServer method testMessageAndDetailsErrorFormat.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testMessageAndDetailsErrorFormat(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
final DataMap details = new DataMap();
details.put("errorKey", "errorDetail");
EasyMock.expect(statusResource.get(eq(1L))).andThrow(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Mock Exception").setErrorDetails(details)).once();
EasyMock.replay(statusResource);
Callback<RestResponse> restResponseCallback = 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();
try {
assertEquals(restResponse.getStatus(), 500);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getHeader(errorResponseHeaderName), RestConstants.HEADER_VALUE_ERROR);
ErrorResponse responseBody = DataMapUtils.read(restResponse.getEntity().asInputStream(), ErrorResponse.class);
// in this test, we're using the _serverWithCustomErrorResponseConfig (see below), which has been configure to use the
// MESSAGE_AND_DETAILS ErrorResponseFormat, so stack trace and other error response parts should be absent
assertEquals(responseBody.getMessage(), "Mock Exception");
assertEquals(responseBody.getErrorDetails().data().getString("errorKey"), "errorDetail");
assertFalse(responseBody.hasExceptionClass());
assertFalse(responseBody.hasStackTrace());
assertFalse(responseBody.hasStatus());
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
} catch (Exception e2) {
fail(e2.toString());
}
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build();
_serverWithCustomErrorResponseConfig.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
fail();
}
@Override
public void onError(Throwable e) {
Messages.toRestException((StreamException) e, new Callback<RestException>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestException result) {
restResponseCallback.onError(result);
}
});
}
};
_serverWithCustomErrorResponseConfig.handleRequest(streamRequest, new RequestContext(), callback);
}
}
use of com.linkedin.restli.server.twitter.StatusCollectionResource 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.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(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);
}
}
Aggregations