use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method getMockResourceMethodDescriptor.
private static ResourceMethodDescriptor getMockResourceMethodDescriptor(Map<String, AlternativeKey<?, ?>> alternativeKeyMap) {
ResourceMethodDescriptor mockDescriptor = EasyMock.createMock(ResourceMethodDescriptor.class);
if (alternativeKeyMap != null) {
EasyMock.expect(mockDescriptor.getResourceModel()).andReturn(getMockResourceModel(alternativeKeyMap)).atLeastOnce();
}
EasyMock.replay(mockDescriptor);
return mockDescriptor;
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testBuilderExceptions.
@Test(dataProvider = "exceptionTestData")
public void testBuilderExceptions(Object result, String expectedErrorMessage) {
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
ResourceContext mockContext = getMockResourceContext(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
try {
responseBuilder.buildRestLiResponseData(null, routingResult, result, headers, Collections.<HttpCookie>emptyList());
Assert.fail("buildRestLiResponseData should have thrown an exception because of null elements");
} catch (RestLiServiceException e) {
Assert.assertTrue(e.getMessage().contains(expectedErrorMessage));
}
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testProjectionInBuildRestLiResponseData.
@Test
public void testProjectionInBuildRestLiResponseData() {
MaskTree maskTree = new MaskTree();
maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false);
EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
EasyMock.replay(mockContext);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<CreateKVResponse<Long, Foo>>();
Foo foo = new Foo();
foo.setStringField("foo1");
foo.setFruitsField(Fruits.APPLE);
createKVResponses.add(new CreateKVResponse<Long, Foo>(1L, foo));
BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<Long, Foo>(createKVResponses);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
Assert.assertTrue(responseData.getBatchCreateResponseEnvelope().isGetAfterCreate());
DataMap dataMap = responseData.getBatchCreateResponseEnvelope().getCreateResponses().get(0).getRecord().data().getDataMap("entity");
Assert.assertEquals(dataMap.size(), 1);
Assert.assertEquals(dataMap.get("fruitsField"), Fruits.APPLE.toString());
EasyMock.verify(mockContext);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor 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.internal.server.model.ResourceMethodDescriptor 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"));
}
Aggregations