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