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);
}
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);
}
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());
}
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);
}
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());
}
}
Aggregations