Search in sources :

Example 6 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method unsupportedNullKeyMapTest.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "unsupportedNullKeyMapData")
@SuppressWarnings("unchecked")
public void unsupportedNullKeyMapTest(Object results, ProtocolVersion protocolVersion, Map<String, Foo> expectedTransformedResult) {
    ResourceContext mockContext = getMockResourceContext(protocolVersion, Collections.<Object, RestLiServiceException>emptyMap(), null, null, null);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
    BatchResponse<Foo> entity = (BatchResponse<Foo>) restResponse.getEntity();
    Assert.assertEquals(entity.getResults(), expectedTransformedResult);
    if (results instanceof BatchResult) {
        Map<String, Integer> expectedStatuses = new HashMap<String, Integer>();
        for (String key : entity.getResults().keySet()) {
            expectedStatuses.put(key, HttpStatus.S_200_OK.getCode());
        }
        Assert.assertEquals(entity.getStatuses(), expectedStatuses);
    } else {
        // if the resource returns a Map we don't have a separate status map in the BatchResponse
        Assert.assertEquals(entity.getStatuses(), Collections.emptyMap());
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) BatchResult(com.linkedin.restli.server.BatchResult) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) Test(org.testng.annotations.Test)

Example 7 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method testBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "testData")
@SuppressWarnings("unchecked")
public void testBuilder(Object results, ProtocolVersion protocolVersion, Map<String, Foo> expectedTransformedResult, Map<String, ErrorResponse> expectedErrors, Map<Object, RestLiServiceException> expectedExceptionsWithUntypedKey, MaskTree maskTree, ProjectionMode projectionMode) {
    ResourceContext mockContext = getMockResourceContext(protocolVersion, expectedExceptionsWithUntypedKey, null, maskTree, projectionMode);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
    BatchResponse<Foo> entity = (BatchResponse<Foo>) restResponse.getEntity();
    Assert.assertEquals(entity.getResults(), expectedTransformedResult);
    if (results instanceof BatchResult) {
        Map<String, Integer> expectedStatuses = new HashMap<String, Integer>();
        for (String key : entity.getResults().keySet()) {
            expectedStatuses.put(key, HttpStatus.S_200_OK.getCode());
        }
        Assert.assertEquals(entity.getStatuses(), expectedStatuses);
    } else {
        // if the resource returns a Map we don't have a separate status map in the BatchResponse
        Assert.assertEquals(entity.getStatuses(), Collections.emptyMap());
    }
    Assert.assertEquals(entity.getErrors().size(), expectedErrors.size());
    for (Map.Entry<String, ErrorResponse> entry : entity.getErrors().entrySet()) {
        String key = entry.getKey();
        ErrorResponse value = entry.getValue();
        Assert.assertEquals(value.getStatus(), expectedErrors.get(key).getStatus());
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) BatchResult(com.linkedin.restli.server.BatchResult) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Example 8 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class StringKeysResource method batchGet.

@RestMethod.BatchGet
public Map<String, Message> batchGet(Set<String> ids) {
    Map<String, Message> batch = new HashMap<String, Message>();
    Map<String, RestLiServiceException> errors = new HashMap<String, RestLiServiceException>();
    for (String id : ids) {
        Message g = _db.get(id);
        if (g != null) {
            batch.put(id, g);
        } else {
            errors.put(id, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<String, Message>(batch, errors);
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BatchResult(com.linkedin.restli.server.BatchResult)

Example 9 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class GreetingsResourceImpl method batchGet.

@RestMethod.BatchGet
public Map<Long, Greeting> batchGet(Set<Long> ids) {
    Map<Long, Greeting> batch = new HashMap<Long, Greeting>();
    Map<Long, RestLiServiceException> errors = new HashMap<Long, RestLiServiceException>();
    for (long id : ids) {
        Greeting g = _db.get(id);
        if (g != null) {
            batch.put(id, g);
        } else {
            errors.put(id, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<Long, Greeting>(batch, errors);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) BatchResult(com.linkedin.restli.server.BatchResult)

Example 10 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class PhotoResource method batchGet.

@Override
public BatchResult<Long, Photo> batchGet(Set<Long> ids) {
    Map<Long, Photo> result = new HashMap<Long, Photo>();
    Map<Long, RestLiServiceException> errors = new HashMap<Long, RestLiServiceException>();
    for (Long key : ids) {
        if (get(key) != null) {
            result.put(key, get(key));
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "No photo with id=" + key + " has been found."));
        }
    }
    return new BatchResult<Long, Photo>(result, errors);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) Photo(com.linkedin.restli.example.Photo) BatchResult(com.linkedin.restli.server.BatchResult)

Aggregations

BatchResult (com.linkedin.restli.server.BatchResult)13 HashMap (java.util.HashMap)12 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)10 Foo (com.linkedin.pegasus.generator.examples.Foo)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 DataMap (com.linkedin.data.DataMap)3 CompoundKey (com.linkedin.restli.common.CompoundKey)3 HttpStatus (com.linkedin.restli.common.HttpStatus)3 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)3 DataProvider (org.testng.annotations.DataProvider)3 BatchResponse (com.linkedin.restli.common.BatchResponse)2 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)2 ErrorResponse (com.linkedin.restli.common.ErrorResponse)2 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)2 Message (com.linkedin.restli.examples.greetings.api.Message)2 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)2 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)2 ResourceContext (com.linkedin.restli.server.ResourceContext)2 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)2 LinkedHashMap (java.util.LinkedHashMap)2