Search in sources :

Example 86 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.

the class TestRestLiResponseHandler method testFieldProjectionRecordsPALSyntax.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjectionRecordsPALSyntax(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestResponse response;
    DataMap data = new DataMap(asMap("f1", "value", "f2", new DataMap(asMap("f3", "value", "f4", "value"))));
    // #1 all fields
    Status status = new Status(data);
    RestRequest request1 = buildRequest("/test?fields=f1,f2:(f3,f4)", acceptTypeData.acceptHeaders, protocolVersion);
    response = _responseHandler.buildResponse(request1, buildRoutingResult(request1, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f1", "f2", "f3" }, new String[0]);
    // #2 some fields
    RestRequest request2 = buildRequest("/test?fields=f1,f2:(f3)", acceptTypeData.acceptHeaders, protocolVersion);
    response = _responseHandler.buildResponse(request2, buildRoutingResult(request2, acceptTypeData.acceptHeaders), status);
    assertTrue(status.data().containsKey("f2"));
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f1", "f2", "f3" }, new String[] { "f4" });
    // #3 no fields
    RestRequest request3 = buildRequest("/test?fields=", acceptTypeData.acceptHeaders, protocolVersion);
    response = _responseHandler.buildResponse(request3, buildRoutingResult(request3, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] {}, new String[] { "f1", "f2", "f3", "f4" });
    assertTrue(status.data().containsKey("f1"));
    assertTrue(status.data().containsKey("f2"));
    // #4 fields not in schema
    RestRequest request4 = buildRequest("/test?fields=f2:(f99)", acceptTypeData.acceptHeaders, protocolVersion);
    response = _responseHandler.buildResponse(request4, buildRoutingResult(request4, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f2" }, new String[] { "f1", "f3", "f99" });
    assertTrue(status.data().containsKey("f2"));
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 87 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest 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);
    RestResponse response;
    // #1 simple record template
    response = _responseHandler.buildResponse(request, buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders), buildStatusRecord());
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, ActionResponse.class.getName(), Status.class.getName(), true, errorResponseHeaderName);
    assertEquals(response.getEntity().asAvroString(), response1);
    // #2 DataTemplate response
    StringMap map = new StringMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    response = _responseHandler.buildResponse(request, buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders), map);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, ActionResponse.class.getName(), StringMap.class.getName(), true, errorResponseHeaderName);
    //Convert both of these back into maps depending on their response type
    final DataMap actualMap;
    final DataMap expectedMap;
    if (acceptTypeData == AcceptTypeData.PSON) {
        actualMap = PSON_DATA_CODEC.bytesToMap(response.getEntity().copyBytes());
        expectedMap = PSON_DATA_CODEC.bytesToMap(ByteString.copyAvroString(response2, false).copyBytes());
    } else {
        actualMap = JACKSON_DATA_CODEC.bytesToMap(response.getEntity().copyBytes());
        expectedMap = JACKSON_DATA_CODEC.stringToMap(response2);
    }
    assertEquals(actualMap, expectedMap);
    // #3 empty response
    response = _responseHandler.buildResponse(request, buildRoutingResultAction(Void.TYPE, request, acceptTypeData.acceptHeaders), null);
    checkResponse(response, 200, 1, null, null, null, false, errorResponseHeaderName);
    assertEquals(response.getEntity().asAvroString(), "");
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) StringMap(com.linkedin.data.template.StringMap) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) ActionResponse(com.linkedin.restli.common.ActionResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 88 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest 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 {
    RestResponse response;
    // #1 all fields
    RestRequest request1 = buildRequest("/test?fields=f1,f2,f3", acceptTypeData.acceptHeaders, protocolVersion);
    Status status = buildStatusWithFields("f1", "f2", "f3");
    response = _responseHandler.buildResponse(request1, buildRoutingResult(request1, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, 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 = _responseHandler.buildResponse(request2, buildRoutingResult(request2, acceptTypeData.acceptHeaders), status);
    assertTrue(status.data().containsKey("f2"));
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f1", "f3" }, new String[] { "f2" });
    // #3 no fields
    RestRequest request3 = buildRequest("/test?fields=", acceptTypeData.acceptHeaders, protocolVersion);
    response = _responseHandler.buildResponse(request3, buildRoutingResult(request3, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, 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 = _responseHandler.buildResponse(request4, buildRoutingResult(request4, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f1" }, new String[] { "f2", "f3", "f99" });
    assertTrue(status.data().containsKey("f2"));
    assertTrue(status.data().containsKey("f3"));
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) Test(org.testng.annotations.Test)

Example 89 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest 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 final 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, 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 90 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.

the class TestRestLiResponseHandler method testFieldProjection_collections_List.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjection_collections_List(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestResponse response;
    List<Status> statusCollection = buildStatusListResult(10, "f1", "f2", "f3");
    RestRequest request = buildRequest("/test?fields=f1,f2", acceptTypeData.acceptHeaders, protocolVersion);
    response = _responseHandler.buildResponse(request, buildRoutingResultFinder(request, acceptTypeData.acceptHeaders), statusCollection);
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, CollectionResponse.class.getName(), null, true, errorResponseHeaderName);
    DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
    CollectionResponse<Status> collectionResponse = new CollectionResponse<Status>(dataMap, Status.class);
    assertEquals(collectionResponse.getElements().size(), 10);
    for (Status status : collectionResponse.getElements()) {
        assertTrue(status.data().containsKey("f1"));
        assertTrue(status.data().containsKey("f2"));
        assertFalse(status.data().containsKey("f3"));
    }
    // ensure that output status objects were not modified by rest.li!
    Status status1 = statusCollection.get(1);
    assertNotNull(status1);
    assertTrue(status1.data().containsKey("f3"));
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) CollectionResponse(com.linkedin.restli.common.CollectionResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)293 Test (org.testng.annotations.Test)243 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)174 RequestContext (com.linkedin.r2.message.RequestContext)154 RestResponse (com.linkedin.r2.message.rest.RestResponse)147 URI (java.net.URI)124 ByteString (com.linkedin.data.ByteString)65 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)62 FutureCallback (com.linkedin.common.callback.FutureCallback)50 RestException (com.linkedin.r2.message.rest.RestException)49 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 ExecutionException (java.util.concurrent.ExecutionException)43 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)38 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)36 HashMap (java.util.HashMap)35 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)33 Map (java.util.Map)28 Callback (com.linkedin.common.callback.Callback)27 URISyntaxException (java.net.URISyntaxException)25 ResourceContext (com.linkedin.restli.server.ResourceContext)24