Search in sources :

Example 61 with RoutingResult

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());
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) StringMap(com.linkedin.data.template.StringMap) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 62 with RoutingResult

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);
}
Also used : PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FieldDef(com.linkedin.data.template.FieldDef) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 63 with RoutingResult

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);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) StringMap(com.linkedin.data.template.StringMap) RestRequest(com.linkedin.r2.message.rest.RestRequest) ActionResponseEnvelope(com.linkedin.restli.internal.server.response.ActionResponseEnvelope) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 64 with RoutingResult

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);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) GetResponseEnvelope(com.linkedin.restli.internal.server.response.GetResponseEnvelope) Test(org.testng.annotations.Test)

Example 65 with RoutingResult

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);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Aggregations

RoutingResult (com.linkedin.restli.internal.server.RoutingResult)114 Test (org.testng.annotations.Test)89 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)72 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)71 RestRequest (com.linkedin.r2.message.rest.RestRequest)56 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)28 RequestContext (com.linkedin.r2.message.RequestContext)24 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)20 URI (java.net.URI)20 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)19 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)18 RoutingException (com.linkedin.restli.server.RoutingException)18 HashMap (java.util.HashMap)17 DataMap (com.linkedin.data.DataMap)16 Foo (com.linkedin.pegasus.generator.examples.Foo)16 RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)14 Parameter (com.linkedin.restli.internal.server.model.Parameter)12 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 Callback (com.linkedin.common.callback.Callback)10