Search in sources :

Example 81 with RoutingResult

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

the class TestBatchPatchArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(ProtocolVersion version, Key primaryKey, Key[] associationKeys, String requestEntity, Object[] keys, PatchRequest<MyComplexKey>[] patches) throws Exception {
    Set<Object> batchKeys = new HashSet<>(Arrays.asList(keys));
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(requestEntity, version);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, primaryKey, associationKeys, batchKeys);
    @SuppressWarnings("rawtypes") Parameter<BatchPatchRequest> param = new Parameter<>("", BatchPatchRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, Collections.singletonList(param), CollectionResourceAsyncTemplate.class.getMethod("batchUpdate", BatchPatchRequest.class, Callback.class));
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(batchKeys, version, true, false);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, context);
    RestLiArgumentBuilder argumentBuilder = new BatchPatchArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    assertEquals(args.length, 1);
    assertTrue(args[0] instanceof BatchPatchRequest);
    Map<?, ?> data = ((BatchPatchRequest) args[0]).getData();
    assertEquals(data.size(), keys.length);
    for (int i = 0; i < keys.length; i++) {
        assertEquals(data.get(keys[i]), patches[i]);
    }
    verify(request, descriptor, context, routingResult);
}
Also used : BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) CollectionResourceAsyncTemplate(com.linkedin.restli.server.resources.CollectionResourceAsyncTemplate) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) HashSet(java.util.HashSet) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 82 with RoutingResult

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

the class TestCollectionArgumentBuilder method testProjectionParams.

@Test
public void testProjectionParams() throws IOException {
    List<Parameter<?>> finderWithProjectionParams = new ArrayList<>();
    finderWithProjectionParams.add(new Parameter<>("key", String.class, new StringDataSchema(), false, null, Parameter.ParamType.QUERY, true, new AnnotationSet(new Annotation[] {})));
    finderWithProjectionParams.add(new Parameter<>("", PagingContext.class, null, false, new PagingContext(0, 10), Parameter.ParamType.PAGING_CONTEXT_PARAM, false, new AnnotationSet(new Annotation[] {})));
    finderWithProjectionParams.add(new Parameter<>("", MaskTree.class, null, false, null, Parameter.ParamType.PROJECTION_PARAM, false, new AnnotationSet(new Annotation[] {})));
    finderWithProjectionParams.add(new Parameter<>("", MaskTree.class, null, false, null, Parameter.ParamType.METADATA_PROJECTION_PARAM, false, new AnnotationSet(new Annotation[] {})));
    finderWithProjectionParams.add(new Parameter<>("", MaskTree.class, null, false, null, Parameter.ParamType.PAGING_PROJECTION_PARAM, false, new AnnotationSet(new Annotation[] {})));
    Map<String, String> finderWithProjectionContextParams = new HashMap<>();
    finderWithProjectionContextParams.put("start", "100");
    finderWithProjectionContextParams.put("count", "15");
    finderWithProjectionContextParams.put("key", "keyString");
    Map<String, Integer> projectionMap = new HashMap<>();
    projectionMap.put("a", 1);
    Map<String, Integer> metadataMap = new HashMap<>();
    metadataMap.put("intField", 1);
    metadataMap.put("booleanField", 1);
    Map<String, Integer> pagingMap = new HashMap<>();
    pagingMap.put("total", 1);
    MaskTree projectionMask = new MaskTree(new DataMap(projectionMap));
    MaskTree metadataMask = new MaskTree(new DataMap(metadataMap));
    MaskTree pagingMask = new MaskTree(new DataMap(pagingMap));
    Object[] expectedArgs = new Object[] { "keyString", new PagingContext(100, 15), projectionMask, metadataMask, pagingMask };
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, 1, finderWithProjectionParams);
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(finderWithProjectionContextParams, projectionMask, metadataMask, pagingMask, true);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 1);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
    RestLiArgumentBuilder argumentBuilder = new CollectionArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    assertEquals(args, expectedArgs);
    verify(descriptor, context, routingResult, request);
}
Also used : HashMap(java.util.HashMap) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) DataMap(com.linkedin.data.DataMap) StringDataSchema(com.linkedin.data.schema.StringDataSchema) 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) PagingContext(com.linkedin.restli.server.PagingContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 83 with RoutingResult

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

the class TestCollectionArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(List<Parameter<?>> params, Map<String, String> contextParams, MutablePathKeys pathKeys, Object[] expectedArgs) throws IOException {
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, 1, params);
    ServerResourceContext context;
    if (contextParams != null) {
        context = RestLiArgumentBuilderTestHelper.getMockResourceContext(contextParams, true);
    } else {
        context = RestLiArgumentBuilderTestHelper.getMockResourceContext(pathKeys, false, true);
    }
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 1);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
    RestLiArgumentBuilder argumentBuilder = new CollectionArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    assertEquals(args, expectedArgs);
    verify(descriptor, context, routingResult, request);
}
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) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 84 with RoutingResult

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

the class TestCollectionArgumentBuilder method testArrayArgument.

@Test(dataProvider = "arrayArgument")
public void testArrayArgument(Parameter<?> param, String parameterKey, List<String> parameterValues, Object[] expectedArgs) throws IOException {
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, param);
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(parameterKey, parameterValues, true);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 1);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
    RestLiArgumentBuilder argumentBuilder = new CollectionArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    ArrayAsserts.assertArrayEquals(args, expectedArgs);
    verify(descriptor, context, routingResult, request);
}
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) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 85 with RoutingResult

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

the class TestBatchCreateResponseBuilder method testCreateKVResultBuilder.

@Test(dataProvider = "createKVResultBuilderTestData")
public void testCreateKVResultBuilder(String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdEntityStatus<?, Foo>> expectedResponses) throws URISyntaxException {
    List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<>(2);
    Foo foo1 = new Foo();
    foo1.setStringField("foo1");
    Foo foo2 = new Foo();
    foo2.setStringField("foo2");
    createKVResponses.add(new CreateKVResponse<>(1L, foo1));
    createKVResponses.add(new CreateKVResponse<>(2L, foo2));
    BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<>(createKVResponses);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ServerResourceContext mockContext = getMockKVResourceContext(altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    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.assertTrue(responseData.getResponseEnvelope().isGetAfterCreate());
    List<CreateIdEntityStatus<Object, Foo>> items = new ArrayList<>();
    for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getResponseEnvelope().getCreateResponses()) {
        @SuppressWarnings("unchecked") CreateIdEntityStatus<Object, Foo> record = (CreateIdEntityStatus<Object, Foo>) item.getRecord();
        items.add(record);
    }
    Assert.assertEquals(items, expectedResponses);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) Foo(com.linkedin.pegasus.generator.examples.Foo) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) 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