Search in sources :

Example 6 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse 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 {
    RestLiResponse 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 = 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,f2:(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", "f2", "f3" }, new String[] { "f4" });
    // #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", "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 = buildPartialRestResponse(request4, buildRoutingResult(request4, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f2" }, new String[] { "f1", "f3", "f99" });
    assertTrue(status.data().containsKey("f2"));
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 7 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse 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 8 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testFieldProjection_batch.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicDataWithBatchUri")
public void testFieldProjection_batch(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String uri, String errorResponseHeaderName) throws Exception {
    RestLiResponse response;
    Map<Integer, Status> statusBatch = buildStatusBatchResponse(10, "f1", "f2", "f3");
    RestRequest request = buildRequest(uri, acceptTypeData.acceptHeaders, protocolVersion);
    response = buildPartialRestResponse(request, buildRoutingResult(ResourceMethod.BATCH_GET, request, acceptTypeData.acceptHeaders), statusBatch);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    DataMap dataMap = response.getDataMap();
    BatchResponse<Status> batchResponse = new BatchResponse<>(dataMap, Status.class);
    assertEquals(batchResponse.getResults().size(), 10);
    for (Status status : batchResponse.getResults().values()) {
        assertTrue(status.data().containsKey("f1"));
        assertTrue(status.data().containsKey("f2"));
        assertFalse(status.data().containsKey("f3"));
    }
    // ensure that output map was not modified by rest.li!
    Status status1 = statusBatch.get(1);
    assertNotNull(status1);
    assertTrue(status1.data().containsKey("f3"));
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) BatchResponse(com.linkedin.restli.common.BatchResponse) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 9 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse 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 {
    RestLiResponse response;
    List<Status> statusCollection = buildStatusListResult(10, "f1", "f2", "f3");
    RestRequest request = buildRequest("/test?fields=f1,f2", acceptTypeData.acceptHeaders, protocolVersion);
    response = buildPartialRestResponse(request, buildRoutingResultFinder(request, acceptTypeData.acceptHeaders), statusCollection);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    DataMap dataMap = response.getDataMap();
    CollectionResponse<Status> collectionResponse = new CollectionResponse<>(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 : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 10 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse 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)

Aggregations

RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)27 Test (org.testng.annotations.Test)21 RestRequest (com.linkedin.r2.message.rest.RestRequest)18 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)14 DataMap (com.linkedin.data.DataMap)9 HttpStatus (com.linkedin.restli.common.HttpStatus)9 Status (com.linkedin.restli.server.twitter.TwitterTestDataModels.Status)9 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)7 Callback (com.linkedin.common.callback.Callback)5 ByteString (com.linkedin.data.ByteString)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 URI (java.net.URI)5 RequestContext (com.linkedin.r2.message.RequestContext)4 RestLiResponseException (com.linkedin.restli.internal.server.response.RestLiResponseException)4 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)4 AfterTest (org.testng.annotations.AfterTest)4 BeforeTest (org.testng.annotations.BeforeTest)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)3