use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestRestLiResponseHandler method testActions.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusActionData")
public void testActions(AcceptTypeData acceptTypeData, String response1, String response2, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
RestLiResponse response;
// #1 simple record template
RoutingResult routing = buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders);
response = buildPartialRestResponse(request, routing, buildStatusRecord());
checkResponse(response, 200, 1, true, errorResponseHeaderName);
if (acceptTypeData != AcceptTypeData.PSON) {
assertEquals(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).asAvroString(), response1);
}
RestResponse restResponse = ResponseUtils.buildResponse(routing, response);
assertEquals(restResponse.getEntity().asAvroString(), response1);
// #2 DataTemplate response
StringMap map = new StringMap();
map.put("key1", "value1");
map.put("key2", "value2");
response = buildPartialRestResponse(request, buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders), map);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
// Convert both of these back into maps depending on their response type
final DataMap actualMap;
final DataMap expectedMap;
if (acceptTypeData == AcceptTypeData.PSON) {
routing = buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders);
restResponse = ResponseUtils.buildResponse(routing, response);
actualMap = PSON_DATA_CODEC.bytesToMap(restResponse.getEntity().copyBytes());
expectedMap = PSON_DATA_CODEC.bytesToMap(ByteString.copyAvroString(response2, false).copyBytes());
} else {
actualMap = JACKSON_DATA_CODEC.bytesToMap(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).copyBytes());
expectedMap = JACKSON_DATA_CODEC.stringToMap(response2);
}
assertEquals(actualMap, expectedMap);
// #3 empty response
response = buildPartialRestResponse(request, buildRoutingResultAction(Void.TYPE, request, acceptTypeData.acceptHeaders), null);
checkResponse(response, 200, 1, false, errorResponseHeaderName);
assertNull(response.getDataMap());
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestRestLiResponseHandler method buildRoutingResultAction.
/**
* Creates a RoutingResult for an Action with the given returnType.
*
* @param actionReturnType the return type of the action.
* @return a RoutingResult
*/
private RoutingResult buildRoutingResultAction(Class<?> actionReturnType, RestRequest request, Map<String, String> headers) throws NoSuchMethodException, RestLiSyntaxException, URISyntaxException {
if (actionReturnType == Void.class) {
actionReturnType = Void.TYPE;
}
// actual method passed in is irrelevant, since we are constructing a ResourceMethodDescriptor by hand.
Method method = ProjectionTestFixture.class.getMethod("batchGet", Set.class);
ResourceModel model = RestLiTestHelper.buildResourceModel(StatusCollectionResource.class);
String actionName = "return" + actionReturnType.getSimpleName();
List<Parameter<?>> parameters = Collections.<Parameter<?>>emptyList();
RecordDataSchema actionReturnRecordDataSchema;
FieldDef<?> returnFieldDef;
if (actionReturnType != Void.TYPE) {
@SuppressWarnings({ "unchecked", "rawtypes" }) FieldDef<?> nonVoidFieldDef = new FieldDef(ActionResponse.VALUE_NAME, actionReturnType, DataTemplateUtil.getSchema(actionReturnType));
returnFieldDef = nonVoidFieldDef;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.singleton(returnFieldDef));
} else {
returnFieldDef = null;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.<FieldDef<?>>emptyList());
}
ResourceMethodDescriptor methodDescriptor = ResourceMethodDescriptor.createForAction(method, parameters, actionName, ResourceLevel.COLLECTION, returnFieldDef, actionReturnRecordDataSchema, DynamicRecordMetadata.buildSchema(actionName, parameters), InterfaceType.SYNC, new DataMap());
model.addResourceMethodDescriptor(methodDescriptor);
ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
RestUtils.validateRequestHeadersAndUpdateResourceContext(headers, Collections.emptySet(), resourceContext);
return new RoutingResult(resourceContext, methodDescriptor);
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestRestLiResponseHandler method testRestLiResponseData.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusActionDataPartial")
@SuppressWarnings("unchecked")
public void testRestLiResponseData(AcceptTypeData acceptTypeData, String response1, String response2, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
RestLiResponseData<ActionResponseEnvelope> responseData;
RoutingResult routingResult1 = buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders);
// #1 simple record template
responseData = (RestLiResponseData<ActionResponseEnvelope>) _responseHandler.buildRestLiResponseData(request, routingResult1, buildStatusRecord());
checkResponseData(responseData, HttpStatus.S_200_OK, 1, false, true, errorResponseHeaderName);
assertEquals(responseData.getResponseEnvelope().getRecord().toString(), response1);
// #2 DataTemplate response
StringMap map = new StringMap();
map.put("key1", "value1");
map.put("key2", "value2");
RoutingResult routingResult2 = buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders);
responseData = (RestLiResponseData<ActionResponseEnvelope>) _responseHandler.buildRestLiResponseData(request, routingResult2, map);
checkResponseData(responseData, HttpStatus.S_200_OK, 1, false, true, errorResponseHeaderName);
// Obtain the maps necessary for comparison
final DataMap actualMap;
final DataMap expectedMap;
actualMap = responseData.getResponseEnvelope().getRecord().data();
expectedMap = JACKSON_DATA_CODEC.stringToMap(response2);
assertEquals(actualMap, expectedMap);
RoutingResult routingResult3 = buildRoutingResultAction(Void.TYPE, request, acceptTypeData.acceptHeaders);
// #3 empty response
responseData = (RestLiResponseData<ActionResponseEnvelope>) _responseHandler.buildRestLiResponseData(request, routingResult3, null);
checkResponseData(responseData, HttpStatus.S_200_OK, 1, false, false, errorResponseHeaderName);
assertEquals(responseData.getResponseEnvelope().getRecord(), null);
}
use of com.linkedin.restli.internal.server.RoutingResult 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.internal.server.RoutingResult in project rest.li by linkedin.
the class TestRestLiResponseHandler method invokeResponseHandler.
private RestLiResponse invokeResponseHandler(String uri, ResourceMethodDescriptor methodDescriptor, Object result, Map<String, String> headers, ProtocolVersion protocolVersion) throws IOException, URISyntaxException, RestLiSyntaxException {
RestRequest request = buildRequest(uri, headers, protocolVersion);
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
RestUtils.validateRequestHeadersAndUpdateResourceContext(headers, Collections.emptySet(), context);
RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
return buildPartialRestResponse(request, routingResult, result);
}
Aggregations