use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testBuildResponseWithExceptionObject.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
@SuppressWarnings("deprecation")
void testBuildResponseWithExceptionObject(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestLiResponse response;
RestLiServiceException ex;
final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
// #1
ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields");
response = buildPartialRestResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
checkResponse(response, 400, 2, true, true, errorResponseHeaderName);
DataMap dataMap = response.getDataMap();
assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
assertEquals(dataMap.getString("message"), "missing fields");
// #2
ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields").setServiceErrorCode(11);
response = buildPartialRestResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
checkResponse(response, 400, 2, true, true, errorResponseHeaderName);
dataMap = response.getDataMap();
assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
assertEquals(dataMap.getString("message"), "missing fields");
assertEquals(dataMap.getInteger("serviceErrorCode"), Integer.valueOf(11));
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testBatchResponses.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testBatchResponses(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestLiResponse response;
// #4 batch
Map<Long, Status> map = new HashMap<>();
map.put(1L, buildStatusRecord());
map.put(2L, buildStatusRecord());
map.put(3L, buildStatusRecord());
response = invokeResponseHandler("/test", map, ResourceMethod.BATCH_GET, acceptTypeData.acceptHeaders, protocolVersion);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
Map<Long, UpdateResponse> updateStatusMap = new HashMap<>();
updateStatusMap.put(1L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
updateStatusMap.put(2L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
updateStatusMap.put(3L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
BatchUpdateResult<Long, Status> batchUpdateResult = new BatchUpdateResult<>(updateStatusMap);
response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_PARTIAL_UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_DELETE, acceptTypeData.acceptHeaders, protocolVersion);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
List<CreateResponse> createResponses = new ArrayList<>();
createResponses.add(new CreateResponse("42", HttpStatus.S_204_NO_CONTENT));
createResponses.add(new CreateResponse(HttpStatus.S_400_BAD_REQUEST));
createResponses.add(new CreateResponse(HttpStatus.S_500_INTERNAL_SERVER_ERROR));
BatchCreateResult<Long, Status> batchCreateResult = new BatchCreateResult<>(createResponses);
// here
response = invokeResponseHandler("/test", batchCreateResult, ResourceMethod.BATCH_CREATE, acceptTypeData.acceptHeaders, protocolVersion);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testBuildRestException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
@SuppressWarnings("deprecation")
void testBuildRestException(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
final RoutingResult routingResult = buildRoutingResult(request, acceptTypeData.acceptHeaders);
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
RestLiServiceException ex = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "freeway not found").setServiceErrorCode(237);
RestLiResponseData<?> responseData = _responseHandler.buildExceptionResponseData(routingResult, ex, requestHeaders, Collections.emptyList());
RestLiResponse response = _responseHandler.buildPartialResponse(routingResult, responseData);
checkResponse(response, 404, 2, // The response Content-Type should always be 'application/json'
true, true, errorResponseHeaderName);
DataMap dataMap = response.getDataMap();
assertEquals(dataMap.getInteger("status"), Integer.valueOf(404));
assertEquals(dataMap.getString("message"), "freeway not found");
assertEquals(dataMap.getInteger("serviceErrorCode"), Integer.valueOf(237));
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testFieldProjection_records.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjection_records(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestLiResponse response;
// #1 all fields
RestRequest request1 = buildRequest("/test?fields=f1,f2,f3", acceptTypeData.acceptHeaders, protocolVersion);
Status status = buildStatusWithFields("f1", "f2", "f3");
response = buildPartialRestResponse(request1, buildRoutingResult(request1, acceptTypeData.acceptHeaders), status);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
checkProjectedFields(response, new String[] { "f1", "f2", "f3" }, new String[0]);
// #2 some fields
RestRequest request2 = buildRequest("/test?fields=f1,f3", acceptTypeData.acceptHeaders, protocolVersion);
response = buildPartialRestResponse(request2, buildRoutingResult(request2, acceptTypeData.acceptHeaders), status);
assertTrue(status.data().containsKey("f2"));
checkResponse(response, 200, 1, true, errorResponseHeaderName);
checkProjectedFields(response, new String[] { "f1", "f3" }, new String[] { "f2" });
// #3 no fields
RestRequest request3 = buildRequest("/test?fields=", acceptTypeData.acceptHeaders, protocolVersion);
response = buildPartialRestResponse(request3, buildRoutingResult(request3, acceptTypeData.acceptHeaders), status);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
checkProjectedFields(response, new String[] {}, new String[] { "f1", "f2", "f3" });
assertTrue(status.data().containsKey("f1"));
assertTrue(status.data().containsKey("f2"));
assertTrue(status.data().containsKey("f3"));
// #4 fields not in schema
RestRequest request4 = buildRequest("/test?fields=f1,f99", acceptTypeData.acceptHeaders, protocolVersion);
response = buildPartialRestResponse(request4, buildRoutingResult(request4, acceptTypeData.acceptHeaders), status);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
checkProjectedFields(response, new String[] { "f1" }, new String[] { "f2", "f3", "f99" });
assertTrue(status.data().containsKey("f2"));
assertTrue(status.data().containsKey("f3"));
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testInvokeWithInvalidAcceptMimeType.
@Test
public void testInvokeWithInvalidAcceptMimeType() throws Exception {
RestRequestBuilder builder = new RestRequestBuilder(new URI("")).addHeaderValue("Accept", "foo").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
RestRequest request = builder.build();
final CountDownLatch latch = new CountDownLatch(1);
RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler(_methodAdapterProvider, _errorResponseBuilder);
Callback<RestLiResponse> executionCallback = new Callback<RestLiResponse>() {
@Override
public void onError(Throwable e) {
latch.countDown();
Assert.assertTrue(e instanceof RestException);
RestException ex = (RestException) e;
Assert.assertEquals(ex.getResponse().getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
}
@Override
public void onSuccess(RestLiResponse result) {
}
};
ServerResourceContext context = new ResourceContextImpl();
try {
RoutingResult routingResult = new RoutingResult(context, null);
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _invoker, null);
FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(null, restLiResponseHandler, executionCallback, _errorResponseBuilder);
final RestLiCallback callback = new RestLiCallback(null, new RestLiFilterResponseContextFactory(request, null, restLiResponseHandler), new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback));
_invoker.invoke(null, routingResult, null, callback);
latch.await();
} catch (Exception e) {
// exception is expected
Assert.assertTrue(e instanceof RestLiServiceException);
}
Assert.assertNull(context.getResponseMimeType());
}
Aggregations