Search in sources :

Example 6 with BatchResponse

use of com.linkedin.restli.common.BatchResponse in project rest.li by linkedin.

the class TestEscapeCharsInStringKeys method testBatchGetWithSimpleKey.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestStringKeysOptionsDataProvider")
public void testBatchGetWithSimpleKey(RestliRequestOptions requestOptions) throws Exception {
    Set<String> keys = new HashSet<String>();
    keys.add(key1());
    keys.add(key2());
    Request<BatchResponse<Message>> req = new StringKeysBuilders(requestOptions).batchGet().ids(keys).build();
    BatchResponse<Message> response = getClient().sendRequest(req).get().getEntity();
    Map<String, Message> results = response.getResults();
    Assert.assertEquals(results.get(key1()).getMessage(), key1(), "Message should match key for key1");
    Assert.assertEquals(results.get(key2()).getMessage(), key2(), "Message should match key for key2");
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) StringKeysBuilders(com.linkedin.restli.examples.greetings.client.StringKeysBuilders) BatchResponse(com.linkedin.restli.common.BatchResponse) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 7 with BatchResponse

use of com.linkedin.restli.common.BatchResponse 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 8 with BatchResponse

use of com.linkedin.restli.common.BatchResponse in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method testAlternativeKeyBuilder.

@Test
public void testAlternativeKeyBuilder() {
    Map<CompoundKey, Foo> rawResults = new HashMap<CompoundKey, Foo>();
    CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    CompoundKey c2 = new CompoundKey().append("a", "a2").append("b", 2);
    Foo record1 = new Foo().setStringField("record1").setFruitsField(Fruits.APPLE);
    Foo record2 = new Foo().setStringField("record2").setIntField(7);
    rawResults.put(c1, record1);
    rawResults.put(c2, record2);
    Map<String, AlternativeKey<?, ?>> alternativeKeyMap = new HashMap<String, AlternativeKey<?, ?>>();
    alternativeKeyMap.put("alt", new AlternativeKey<String, CompoundKey>(new TestKeyCoercer(), String.class, new StringDataSchema()));
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceContext mockContext = getMockResourceContext(AllProtocolVersions.LATEST_PROTOCOL_VERSION, Collections.<Object, RestLiServiceException>emptyMap(), "alt", null, null);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    BatchGetResponseBuilder batchGetResponseBuilder = new BatchGetResponseBuilder(null);
    RestLiResponseData responseData = batchGetResponseBuilder.buildRestLiResponseData(null, routingResult, rawResults, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = batchGetResponseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
    @SuppressWarnings("unchecked") Map<String, Foo> results = ((BatchResponse<Foo>) restResponse.getEntity()).getResults();
    Assert.assertEquals(results.size(), 2);
    Assert.assertTrue(results.containsKey("aa1xb1"));
    Assert.assertTrue(results.containsKey("aa2xb2"));
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) BatchResponse(com.linkedin.restli.common.BatchResponse) Foo(com.linkedin.pegasus.generator.examples.Foo) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) StringDataSchema(com.linkedin.data.schema.StringDataSchema) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) AlternativeKey(com.linkedin.restli.server.AlternativeKey) Test(org.testng.annotations.Test)

Example 9 with BatchResponse

use of com.linkedin.restli.common.BatchResponse 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 10 with BatchResponse

use of com.linkedin.restli.common.BatchResponse in project rest.li by linkedin.

the class TestBatchUpdateResponseBuilder method testUpdateStatusInstantiation.

@Test(dataProvider = "updateStatusInstantiation")
public void testUpdateStatusInstantiation(RestLiResponseData responseData, UpdateStatus expectedResult) {
    ResourceContext mockContext = getMockResourceContext(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), null);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    PartialRestResponse response = new BatchUpdateResponseBuilder(new ErrorResponseBuilder()).buildResponse(routingResult, responseData);
    Assert.assertEquals(((BatchResponse) response.getEntity()).getResults().get("key"), expectedResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Test(org.testng.annotations.Test)

Aggregations

BatchResponse (com.linkedin.restli.common.BatchResponse)25 Test (org.testng.annotations.Test)19 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)9 HashMap (java.util.HashMap)8 DataMap (com.linkedin.data.DataMap)6 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)6 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)6 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)6 ResourceContext (com.linkedin.restli.server.ResourceContext)6 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)5 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)5 Map (java.util.Map)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)4 UpdateStatus (com.linkedin.restli.common.UpdateStatus)4 Foo (com.linkedin.pegasus.generator.examples.Foo)3 HashSet (java.util.HashSet)3 RestClient (com.linkedin.restli.client.RestClient)2 CreateStatus (com.linkedin.restli.common.CreateStatus)2 EntityResponse (com.linkedin.restli.common.EntityResponse)2