Search in sources :

Example 51 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.

the class TestUpdateArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(List<Parameter<?>> params, Key key, String keyName, Object keyValue) throws IOException, NoSuchMethodException {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"a\":\"xyz\",\"b\":123}");
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, key, true);
    ResourceMethodDescriptor descriptor;
    if (key != null) {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, params, CollectionResourceAsyncTemplate.class.getMethod("update", Object.class, PatchRequest.class, Callback.class));
    } else {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, params, CollectionResourceAsyncTemplate.class.getMethod("update", Object.class, PatchRequest.class, Callback.class));
    }
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
    RoutingResult routingResult;
    if (key != null) {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 5, context, 2);
    } else {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 4, context, 1);
    }
    RestLiArgumentBuilder argumentBuilder = new UpdateArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    if (keyValue != null) {
        assertEquals(args.length, 2);
        assertEquals(args[0], keyValue);
    }
    assertTrue(args[args.length - 1] instanceof MyComplexKey);
    assertEquals(((MyComplexKey) args[args.length - 1]).getA(), "xyz");
    assertEquals((long) ((MyComplexKey) args[args.length - 1]).getB(), 123L);
    verify(request, model, descriptor, context, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 52 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.

the class TestUpdateArgumentBuilder method testUnstructuredDataArgumentBuilder.

@Test(dataProvider = "argumentData")
public void testUnstructuredDataArgumentBuilder(List<Parameter<?>> params, Key key, String keyName, Object keyValue) throws IOException, NoSuchMethodException {
    params.remove(0);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"a\":\"xyz\",\"b\":123}");
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, key, true);
    ResourceMethodDescriptor descriptor;
    if (key != null) {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, params, UnstructuredDataCollectionResourceReactive.class.getMethod("update", Object.class, UnstructuredDataReactiveReader.class, Callback.class));
    } else {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, params, UnstructuredDataCollectionResourceReactive.class.getMethod("update", Object.class, UnstructuredDataReactiveReader.class, Callback.class));
    }
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
    RoutingResult routingResult;
    if (key != null) {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 5, context, 2);
    } else {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 4, context, 1);
    }
    RestLiArgumentBuilder argumentBuilder = new UpdateArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    if (keyValue != null) {
        assertEquals(args.length, 1);
        assertEquals(args[0], keyValue);
    }
    assertEquals(requestData.hasEntity(), false);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 53 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.

the class TestBatchCreateResponseBuilder method testBuilderExceptions.

@Test(dataProvider = "exceptionTestData")
public void testBuilderExceptions(Object result, String expectedErrorMessage) throws URISyntaxException {
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    ServerResourceContext mockContext = getMockResourceContext(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    try {
        responseBuilder.buildRestLiResponseData(request, routingResult, result, headers, Collections.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) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 54 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.

the class TestBatchCreateResponseBuilder method testCreateResultBuilder.

@Test(dataProvider = "createResultBuilderTestData")
@SuppressWarnings("unchecked")
public void testCreateResultBuilder(String uriString, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdStatus<Object>> expectedStatuses) throws URISyntaxException {
    List<CreateResponse> createResponses = Arrays.asList(new CreateResponse(1L), new CreateResponse(2L));
    BatchCreateResult<Long, Foo> results = new BatchCreateResult<>(createResponses);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ServerResourceContext mockContext = getMockResourceContext(altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    RestRequest request = new RestRequestBuilder(new URI(uriString)).build();
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
    RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, headers, Collections.emptyList());
    RestLiResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertFalse(responseData.getResponseEnvelope().isGetAfterCreate());
    List<com.linkedin.restli.common.CreateIdStatus<Object>> items = new ArrayList<>();
    for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getResponseEnvelope().getCreateResponses()) {
        items.add((CreateIdStatus<Object>) item.getRecord());
    }
    Assert.assertEquals(restResponse.getEntity(), new BatchCreateIdResponse<>(items));
    Assert.assertEquals(expectedStatuses, items);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
Also used : CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) Foo(com.linkedin.pegasus.generator.examples.Foo) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 55 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.

the class TestBatchCreateResponseBuilder method testProjectionInBuildRestLiResponseData.

@Test
@SuppressWarnings("unchecked")
public void testProjectionInBuildRestLiResponseData() throws URISyntaxException {
    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).atLeastOnce();
    EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(true);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
    EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.replay(mockContext);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<>();
    Foo foo = new Foo();
    foo.setStringField("foo1");
    foo.setFruitsField(Fruits.APPLE);
    createKVResponses.add(new CreateKVResponse<>(1L, foo));
    BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<>(createKVResponses);
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, Collections.emptyMap(), Collections.emptyList());
    Assert.assertTrue(responseData.getResponseEnvelope().isGetAfterCreate());
    CreateIdEntityStatus<Long, Foo> item = (CreateIdEntityStatus<Long, Foo>) responseData.getResponseEnvelope().getCreateResponses().get(0).getRecord();
    Assert.assertEquals(item.getLocation(), "/foo/1");
    DataMap dataMap = item.data().getDataMap("entity");
    Assert.assertEquals(dataMap.size(), 1);
    Assert.assertEquals(dataMap.get("fruitsField"), Fruits.APPLE.toString());
    EasyMock.verify(mockContext);
}
Also used : CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) PathSpec(com.linkedin.data.schema.PathSpec) URI(java.net.URI) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) Test(org.testng.annotations.Test)

Aggregations

RoutingResult (com.linkedin.restli.internal.server.RoutingResult)114 Test (org.testng.annotations.Test)89 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)72 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)71 RestRequest (com.linkedin.r2.message.rest.RestRequest)56 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)28 RequestContext (com.linkedin.r2.message.RequestContext)24 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)20 URI (java.net.URI)20 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)19 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)18 RoutingException (com.linkedin.restli.server.RoutingException)18 HashMap (java.util.HashMap)17 DataMap (com.linkedin.data.DataMap)16 Foo (com.linkedin.pegasus.generator.examples.Foo)16 RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)14 Parameter (com.linkedin.restli.internal.server.model.Parameter)12 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 Callback (com.linkedin.common.callback.Callback)10