Search in sources :

Example 1 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)

Example 2 with BatchResponse

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

the class TestBatchUpdateResponseBuilder method unsupportedNullKeyMapTest.

/* Note that we use also need to test using java.util.concurrent.ConcurrentHashMap. This is because rest.li checks
  * for the presence of nulls returned from maps which are returned from resource methods. The checking for nulls
  * is prone to a NullPointerException since contains(null) can throw an NPE from certain map implementations such as
  * java.util.concurrent.ConcurrentHashMap. We want to make sure our check for the presence of nulls is done in a
  * way that doesn't throw an NullPointerException.
  */
@Test(dataProvider = "unsupportedNullKeyMapData")
@SuppressWarnings("unchecked")
public void unsupportedNullKeyMapTest(Object results, ProtocolVersion protocolVersion, Map<String, UpdateStatus> expectedResults) {
    ResourceContext mockContext = getMockResourceContext(protocolVersion, null);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchUpdateResponseBuilder batchUpdateResponseBuilder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = batchUpdateResponseBuilder.buildResponse(routingResult, responseData);
    BatchResponse<UpdateStatus> batchResponse = (BatchResponse<UpdateStatus>) restResponse.getEntity();
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(batchResponse.getResults(), expectedResults);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) Test(org.testng.annotations.Test)

Example 3 with BatchResponse

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

the class TestSimpleResourceHierarchy method testSubCollectionBatchGet.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testSubCollectionBatchGet(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    List<Long> ids = Arrays.asList(1L, 2L, 3L, 4L);
    Request<BatchResponse<Greeting>> request = new SubgreetingsBuilders(requestOptions).batchGet().ids(ids).build();
    Response<BatchResponse<Greeting>> response = getClient().sendRequest(request).getResponse();
    BatchResponse<Greeting> batchResponse = response.getEntity();
    Assert.assertEquals(batchResponse.getResults().size(), ids.size());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) BatchResponse(com.linkedin.restli.common.BatchResponse) SubgreetingsBuilders(com.linkedin.restli.examples.greetings.client.SubgreetingsBuilders) Test(org.testng.annotations.Test)

Example 4 with BatchResponse

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

the class TestRestLiValidation method testBatchGetAutoWithException.

@Test
public void testBatchGetAutoWithException() throws RemoteInvocationException {
    // The resource returns an error for id=0 but a normal result for id=1
    ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
    union.setMyRecord(new myRecord().setFoo1(100).setFoo2(200));
    ValidationDemo expectedResult = new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(union);
    BatchGetRequest<ValidationDemo> request = new AutoValidationDemosBuilders().batchGet().ids(0, 1).build();
    Response<BatchResponse<ValidationDemo>> response = _restClientAuto.sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals((int) response.getEntity().getErrors().get("0").getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
    Assert.assertEquals(response.getEntity().getResults().get("1"), expectedResult);
    BatchGetEntityRequest<Integer, ValidationDemo> request2 = new AutoValidationDemosRequestBuilders().batchGet().ids(0, 1).build();
    Response<BatchKVResponse<Integer, EntityResponse<ValidationDemo>>> response2 = _restClientAuto.sendRequest(request2).getResponse();
    Assert.assertEquals(response2.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals((int) response2.getEntity().getErrors().get(0).getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
    Assert.assertEquals(response2.getEntity().getResults().get(1).getEntity(), expectedResult);
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) AutoValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosRequestBuilders) BatchResponse(com.linkedin.restli.common.BatchResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) AutoValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosBuilders) Test(org.testng.annotations.Test)

Example 5 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) {
    ServerResourceContext 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<BatchGetResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.emptyList());
    RestLiResponse 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<>();
        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 : 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) BatchResult(com.linkedin.restli.server.BatchResult) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Aggregations

BatchResponse (com.linkedin.restli.common.BatchResponse)32 Test (org.testng.annotations.Test)23 HashMap (java.util.HashMap)11 DataMap (com.linkedin.data.DataMap)10 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)9 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)9 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)9 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)9 Map (java.util.Map)8 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)6 ErrorResponse (com.linkedin.restli.common.ErrorResponse)6 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)5 EntityResponse (com.linkedin.restli.common.EntityResponse)4 HashSet (java.util.HashSet)4 Callback (com.linkedin.common.callback.Callback)3 Foo (com.linkedin.pegasus.generator.examples.Foo)3 Set (java.util.Set)3 PathSpec (com.linkedin.data.schema.PathSpec)2