Search in sources :

Example 26 with ResourceMethodDescriptor

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;
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)

Example 27 with ResourceMethodDescriptor

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));
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Test(org.testng.annotations.Test)

Example 28 with ResourceMethodDescriptor

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);
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) Test(org.testng.annotations.Test)

Example 29 with ResourceMethodDescriptor

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());
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) 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) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) BatchResult(com.linkedin.restli.server.BatchResult) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) Test(org.testng.annotations.Test)

Example 30 with ResourceMethodDescriptor

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"));
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) BatchResponse(com.linkedin.restli.common.BatchResponse) Foo(com.linkedin.pegasus.generator.examples.Foo) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) StringDataSchema(com.linkedin.data.schema.StringDataSchema) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) AlternativeKey(com.linkedin.restli.server.AlternativeKey) Test(org.testng.annotations.Test)

Aggregations

ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)195 Test (org.testng.annotations.Test)171 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)140 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)116 AfterTest (org.testng.annotations.AfterTest)105 BeforeTest (org.testng.annotations.BeforeTest)105 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)66 PromiseStatusCollectionResource (com.linkedin.restli.server.twitter.PromiseStatusCollectionResource)47 RestRequest (com.linkedin.r2.message.rest.RestRequest)41 ByteString (com.linkedin.data.ByteString)40 AsyncStatusCollectionResource (com.linkedin.restli.server.twitter.AsyncStatusCollectionResource)38 ResourceContext (com.linkedin.restli.server.ResourceContext)37 CustomString (com.linkedin.restli.server.custom.types.CustomString)37 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)31 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)27 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)27 EasyMock.anyObject (org.easymock.EasyMock.anyObject)27 Callback (com.linkedin.common.callback.Callback)26 RequestExecutionCallback (com.linkedin.restli.server.RequestExecutionCallback)26 CustomStatusCollectionResource (com.linkedin.restli.server.twitter.CustomStatusCollectionResource)26