use of com.linkedin.restli.server.twitter.StatusCollectionResource in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testExecutionReport.
@Test
public void testExecutionReport() throws RestLiSyntaxException, URISyntaxException {
Map<String, ResourceModel> resourceModelMap = buildResourceModels(StatusCollectionResource.class, AsyncStatusCollectionResource.class, PromiseStatusCollectionResource.class, TaskStatusCollectionResource.class);
ResourceModel statusResourceModel = resourceModelMap.get("/statuses");
ResourceModel asyncStatusResourceModel = resourceModelMap.get("/asyncstatuses");
ResourceModel promiseStatusResourceModel = resourceModelMap.get("/promisestatuses");
ResourceModel taskStatusResourceModel = resourceModelMap.get("/taskstatuses");
ResourceMethodDescriptor methodDescriptor;
StatusCollectionResource statusResource;
AsyncStatusCollectionResource asyncStatusResource;
PromiseStatusCollectionResource promiseStatusResource;
TaskStatusCollectionResource taskStatusResource;
// #1: Sync Method Execution
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.GET);
statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
checkInvocation(statusResource, methodDescriptor, "GET", version, "/statuses/1", null, buildPathKeys("statusID", 1L), new RequestExecutionCallback<RestResponse>() {
//A 404 is considered an error by rest.li
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.assertNull(executionReport.getParseqTrace(), "There should be no parseq trace!");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.fail("Request failed unexpectedly.");
}
}, true, false);
// #2: Callback based Async Method Execution
Capture<RequestExecutionReport> requestExecutionReportCapture = new Capture<RequestExecutionReport>();
RestLiCallback<?> callback = getCallback(requestExecutionReportCapture);
methodDescriptor = asyncStatusResourceModel.findMethod(ResourceMethod.GET);
asyncStatusResource = getMockResource(AsyncStatusCollectionResource.class);
asyncStatusResource.get(eq(1L), EasyMock.<Callback<Status>>anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<Status> callback = (Callback<Status>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(asyncStatusResource);
checkAsyncInvocation(asyncStatusResource, callback, methodDescriptor, "GET", version, "/asyncstatuses/1", null, buildPathKeys("statusID", 1L), true);
Assert.assertNull(requestExecutionReportCapture.getValue().getParseqTrace());
// #3: Promise based Async Method Execution
methodDescriptor = promiseStatusResourceModel.findMethod(ResourceMethod.GET);
promiseStatusResource = getMockResource(PromiseStatusCollectionResource.class);
EasyMock.expect(promiseStatusResource.get(eq(1L))).andReturn(Promises.<Status>value(null)).once();
checkInvocation(promiseStatusResource, methodDescriptor, "GET", version, "/promisestatuses/1", null, buildPathKeys("statusID", 1L), new RequestExecutionCallback<RestResponse>() {
//A 404 is considered an error by rest.li
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.assertNotNull(executionReport.getParseqTrace(), "There should be a valid parseq trace!");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.fail("Request failed unexpectedly.");
}
}, true, false);
// #4: Task based Async Method Execution
methodDescriptor = taskStatusResourceModel.findMethod(ResourceMethod.GET);
taskStatusResource = getMockResource(TaskStatusCollectionResource.class);
EasyMock.expect(taskStatusResource.get(eq(1L))).andReturn(Task.callable("myTask", new Callable<Status>() {
@Override
public Status call() throws Exception {
return new Status();
}
})).once();
checkInvocation(taskStatusResource, methodDescriptor, "GET", version, "/taskstatuses/1", null, buildPathKeys("statusID", 1L), new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.fail("Request failed unexpectedly.");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.assertNotNull(executionReport.getParseqTrace());
}
}, true, false);
}
use of com.linkedin.restli.server.twitter.StatusCollectionResource in project rest.li by linkedin.
the class TestRestLiServer method testApplicationException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testApplicationException(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andThrow(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Mock Exception")).once();
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, restResponse.getHeaders());
assertEquals(responseBody.getMessage(), "Mock Exception");
assertEquals(responseBody.getExceptionClass(), "com.linkedin.restli.server.RestLiServiceException");
assertTrue(responseBody.getStackTrace().startsWith("com.linkedin.restli.server.RestLiServiceException [HTTP Status:500]: Mock Exception"));
assertEquals(responseBody.getStatus().intValue(), 500);
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();
_server.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);
}
});
}
};
_server.handleRequest(streamRequest, new RequestContext(), callback);
}
}
use of com.linkedin.restli.server.twitter.StatusCollectionResource in project rest.li by linkedin.
the class TestRestLiServer method testHandleRequestWithRestLiResponseError.
@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseError(final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
replay(statusResource);
Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {
@Override
public void onSuccess(RestLiResponse restLiResponse) {
fail("We should not get a success here. The server should have returned a 404!");
}
@Override
public void onError(Throwable e) {
RestLiResponseException restLiResponseException = (RestLiResponseException) e;
assertEquals(restLiResponseException.getRestLiResponse().getStatus(), HttpStatus.S_404_NOT_FOUND, "We should get a 404 back here!");
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
};
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);
}
}
use of com.linkedin.restli.server.twitter.StatusCollectionResource in project rest.li by linkedin.
the class TestRestLiServer method testInternalErrorMessage.
@Test(dataProvider = "restOrStream")
public void testInternalErrorMessage(final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andThrow(new IllegalArgumentException("oops")).once();
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 {
ErrorResponse responseBody = DataMapUtils.read(restResponse.getEntity().asInputStream(), ErrorResponse.class, restResponse.getHeaders());
assertEquals(responseBody.getMessage(), ErrorResponseBuilder.DEFAULT_INTERNAL_ERROR_MESSAGE);
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, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.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 testCustomizedInternalErrorMessage.
@Test(dataProvider = "restOrStream")
public void testCustomizedInternalErrorMessage(final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andThrow(new IllegalArgumentException("oops")).once();
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 {
ErrorResponse responseBody = DataMapUtils.read(restResponse.getEntity().asInputStream(), ErrorResponse.class, restResponse.getHeaders());
assertEquals(responseBody.getMessage(), "kthxbye.");
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, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_serverWithCustomErrorResponseConfig.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.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);
}
}
Aggregations