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