Search in sources :

Example 16 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class BatchGetRequestBuilderTest method testBatchingWithDifferentRequestOptionsFailure.

@Test
public void testBatchingWithDifferentRequestOptionsFailure() {
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder1 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
    batchRequestBuilder1.ids(1);
    RestliRequestOptions customOptions = new RestliRequestOptionsBuilder().setProtocolVersionOption(ProtocolVersionOption.FORCE_USE_NEXT).build();
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder2 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), customOptions);
    batchRequestBuilder2.ids(2, 3);
    try {
        @SuppressWarnings("unchecked") List<BatchGetRequest<TestRecord>> requests = Arrays.asList(batchRequestBuilder1.build(), batchRequestBuilder2.build());
        BatchGetRequestBuilder.batch(requests, false);
        Assert.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException ignored) {
        Assert.assertTrue(ignored.getMessage().contains("Requests must have the same RestliRequestOptions to batch!"));
    }
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 17 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class BatchGetRequestBuilderTest method testNoFieldBatching.

@Test
public void testNoFieldBatching() throws URISyntaxException {
    String expectedProtocol1Uri = "/?fields=id&ids=1&ids=2&ids=3";
    String expectedProtocol2Uri = "/?fields=id&ids=List(1,2,3)";
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder1 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
    batchRequestBuilder1.ids(1);
    batchRequestBuilder1.fields(FIELDS.id());
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder2 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
    batchRequestBuilder2.ids(2, 3);
    batchRequestBuilder2.fields(FIELDS.id());
    BatchGetRequest<TestRecord> batchRequest1 = batchRequestBuilder1.build();
    @SuppressWarnings("unchecked") BatchGetRequest<TestRecord> batchingRequest = BatchGetRequestBuilder.batch(Arrays.asList(batchRequest1, batchRequestBuilder2.build()), false);
    Assert.assertEquals(batchingRequest.getBaseUriTemplate(), batchRequest1.getBaseUriTemplate());
    Assert.assertEquals(batchingRequest.getPathKeys(), batchRequest1.getPathKeys());
    Assert.assertEquals(batchingRequest.getFields(), new HashSet<>(Arrays.asList(FIELDS.id())));
    Assert.assertEquals(batchingRequest.getObjectIds(), new HashSet<>(Arrays.asList(1, 2, 3)));
    testUriGeneration(batchingRequest, expectedProtocol1Uri, expectedProtocol2Uri);
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 18 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class BatchGetRequestBuilderTest method testBatchingWithDiffUris.

@Test
public void testBatchingWithDiffUris() {
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder1 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder2 = new BatchGetRequestBuilder<>("/a/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
    @SuppressWarnings("unchecked") List<BatchGetRequest<TestRecord>> requests = Arrays.asList(batchRequestBuilder1.build(), batchRequestBuilder2.build());
    try {
        BatchGetRequestBuilder.batch(requests);
        Assert.fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException ignored) {
    // Expected
    }
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 19 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class BatchGetRequestBuilderTest method testBatchingWithNullProjectionLast.

@Test
public void testBatchingWithNullProjectionLast() {
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder1 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
    batchRequestBuilder1.ids(1);
    batchRequestBuilder1.fields(FIELDS.message());
    BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder2 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
    batchRequestBuilder2.ids(2, 3);
    BatchGetRequest<TestRecord> batchRequest1 = batchRequestBuilder1.build();
    @SuppressWarnings("unchecked") BatchGetRequest<TestRecord> batchingRequest = BatchGetRequestBuilder.batch(Arrays.asList(batchRequest1, batchRequestBuilder2.build()));
    Assert.assertEquals(batchingRequest.getBaseUriTemplate(), batchRequest1.getBaseUriTemplate());
    Assert.assertEquals(batchingRequest.getPathKeys(), batchRequest1.getPathKeys());
    Assert.assertEquals(batchingRequest.getFields(), Collections.emptySet());
    Assert.assertEquals(batchingRequest.getObjectIds(), new HashSet<>(Arrays.asList(1, 2, 3)));
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 20 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class RestLiAnnotationReader method addTemplateResourceMethod.

/**
 * Handle method that overrides resource template method. Only meaningful for classes
 * that extend a resource template class and only for methods that are NOT annotated
 * with RestMethod. Those are handled in addCrudResourceMethod()
 */
private static void addTemplateResourceMethod(final Class<?> resourceClass, final ResourceModel model, final Method method) {
    // Check if the resource class is derived from one of the resource template classes.
    if (!isResourceTemplateClass(resourceClass)) {
        return;
    }
    // addCrudResourceMethod
    if (isRestMethodAnnotated(method)) {
        return;
    }
    List<Class<?>> parameterTypes = Arrays.asList(method.getParameterTypes());
    boolean partial = parameterTypes.contains(PatchRequest.class) || parameterTypes.contains(BatchPatchRequest.class);
    ResourceMethod resourceMethod = ResourceMethodLookup.fromResourceMethodName(method.getName(), partial);
    if (resourceMethod != null) {
        if (!Modifier.isPublic(method.getModifiers())) {
            throw new ResourceConfigException(String.format("Resource '%s' contains non-public CRUD method '%s'.", model.getName(), method.getName()));
        }
        DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(method.getAnnotations());
        addDeprecatedAnnotation(annotationsMap, method);
        List<Parameter<?>> parameters = getParameters(model, method, resourceMethod);
        ResourceMethodDescriptor resourceMethodDescriptor = ResourceMethodDescriptor.createForRestful(resourceMethod, method, parameters, null, getInterfaceType(method), annotationsMap);
        addServiceErrors(resourceMethodDescriptor, method);
        addSuccessStatuses(resourceMethodDescriptor, method);
        addMaxBatchSize(resourceMethodDescriptor, method, resourceMethod);
        model.addResourceMethodDescriptor(resourceMethodDescriptor);
    }
}
Also used : BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) PatchRequest(com.linkedin.restli.common.PatchRequest) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataMap(com.linkedin.data.DataMap)

Aggregations

ResourceMethod (com.linkedin.restli.common.ResourceMethod)54 Test (org.testng.annotations.Test)19 HashMap (java.util.HashMap)18 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)13 DataMap (com.linkedin.data.DataMap)12 TestRecord (com.linkedin.restli.client.test.TestRecord)12 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)10 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)6 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 Foo (com.linkedin.pegasus.generator.examples.Foo)4 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)4 ResourceSchemaArray (com.linkedin.restli.restspec.ResourceSchemaArray)4 JClass (com.sun.codemodel.JClass)4 JDefinedClass (com.sun.codemodel.JDefinedClass)4 ByteString (com.linkedin.data.ByteString)3