use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestRestLiResponseHandler method testBuildRestLiUnstructuredDataResponse.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
@SuppressWarnings("unchecked")
public void testBuildRestLiUnstructuredDataResponse(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
final RestRequest request = buildRequest(Collections.EMPTY_MAP, protocolVersion);
RoutingResult routingResult = buildUnstructuredDataRoutingResult(request);
RestLiResponseData<GetResponseEnvelope> responseData = (RestLiResponseData<GetResponseEnvelope>) _responseHandler.buildRestLiResponseData(request, routingResult, null);
assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_200_OK);
assertEquals(responseData.getResponseEnvelope().getRecord(), new EmptyRecord());
RestLiResponse restResponse = buildPartialRestResponse(request, routingResult, null);
assertNotNull(restResponse);
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class RestLiFilterResponseContextFactory method fromThrowable.
/**
* Create a {@link FilterResponseContext} based on the given error.
*
* @param throwable Error obtained from the resource method invocation.
*
* @return {@link FilterResponseContext} corresponding to the given input.
*/
public FilterResponseContext fromThrowable(Throwable throwable) {
RestLiServiceException restLiServiceException;
if (throwable instanceof RestLiServiceException) {
restLiServiceException = (RestLiServiceException) throwable;
} else if (throwable instanceof RoutingException) {
RoutingException routingException = (RoutingException) throwable;
restLiServiceException = new RestLiServiceException(HttpStatus.fromCode(routingException.getStatus()), routingException.getMessage(), routingException);
} else {
restLiServiceException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, throwable.getMessage(), throwable);
}
Map<String, String> requestHeaders = _request.getHeaders();
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString());
headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR);
final RestLiResponseData responseData = _responseHandler.buildExceptionResponseData(_request, _method, restLiServiceException, headers, Collections.<HttpCookie>emptyList());
return new FilterResponseContext() {
@Override
public RestLiResponseData getResponseData() {
return responseData;
}
};
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method testBuilder.
@Test(dataProvider = "testData")
@SuppressWarnings("unchecked")
public void testBuilder(Object results, ProtocolVersion protocolVersion, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, Map<String, UpdateStatus> expectedResults, Map<String, ErrorResponse> expectedErrors) {
ResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
BatchUpdateResponseBuilder batchUpdateResponseBuilder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
PartialRestResponse restResponse = batchUpdateResponseBuilder.buildResponse(routingResult, responseData);
BatchResponse<UpdateStatus> batchResponse = (BatchResponse<UpdateStatus>) restResponse.getEntity();
EasyMock.verify(mockContext, mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertEquals(batchResponse.getResults(), expectedResults);
Assert.assertEquals(batchResponse.getErrors().size(), expectedErrors.size());
for (Map.Entry<String, ErrorResponse> entry : batchResponse.getErrors().entrySet()) {
String key = entry.getKey();
ErrorResponse value = entry.getValue();
Assert.assertEquals(value.getStatus(), expectedErrors.get(key).getStatus());
}
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestCollectionResponseBuilder method testBuilder.
@SuppressWarnings("unchecked")
@Test(dataProvider = "testData")
public void testBuilder(Object results, DataMap expectedMetadata, List<Foo> expectedElements, CollectionMetadata expectedPaging, MaskTree dataMaskTree, MaskTree metaDataMaskTree, MaskTree pagingMaskTree, ProjectionMode dataProjectionMode, ProjectionMode metadataProjectionMode) throws URISyntaxException {
for (ResourceMethod resourceMethod : Arrays.asList(ResourceMethod.GET_ALL, ResourceMethod.FINDER)) {
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceContext mockContext = getMockResourceContext(dataMaskTree, metaDataMaskTree, pagingMaskTree, dataProjectionMode, metadataProjectionMode);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(resourceMethod);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
CollectionResponseBuilder responseBuilder = new CollectionResponseBuilder();
RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(getRestRequest(), routingResult, results, headers, Collections.<HttpCookie>emptyList());
PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockContext, mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
CollectionResponse<Foo> actualResults = (CollectionResponse<Foo>) restResponse.getEntity();
Assert.assertEquals(actualResults.getElements(), expectedElements);
Assert.assertEquals(actualResults.getMetadataRaw(), expectedMetadata);
Assert.assertEquals(actualResults.getPaging(), expectedPaging);
EasyMock.verify(mockContext);
}
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestUpdateResponseBuilder method testBuilder.
@Test
public void testBuilder() {
HttpStatus status = HttpStatus.S_200_OK;
UpdateResponse updateResponse = new UpdateResponse(status);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor();
RoutingResult routingResult = new RoutingResult(null, mockDescriptor);
UpdateResponseBuilder updateResponseBuilder = new UpdateResponseBuilder();
RestLiResponseData responseData = updateResponseBuilder.buildRestLiResponseData(null, routingResult, updateResponse, headers, Collections.<HttpCookie>emptyList());
PartialRestResponse partialRestResponse = updateResponseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockDescriptor);
ResponseBuilderUtil.validateHeaders(partialRestResponse, headers);
Assert.assertEquals(partialRestResponse.getStatus(), status);
}
Aggregations