Search in sources :

Example 46 with ResourceMethod

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

the class TestRestLiValidationFilter method testAllowWhitelistedFieldsInMask.

@Test(dataProvider = "projectionDataWithWhitelistFields")
@SuppressWarnings({ "unchecked" })
public void testAllowWhitelistedFieldsInMask(ResourceModel resourceModel, RestLiResponseData<RestLiResponseEnvelope> responseData, MaskTree projectionMask, boolean expectError) {
    ResourceMethod resourceMethod = responseData.getResourceMethod();
    when(filterRequestContext.getRequestData()).thenReturn(new RestLiRequestDataImpl.Builder().entity(makeTestRecord()).build());
    when(filterRequestContext.getMethodType()).thenReturn(resourceMethod);
    when(filterRequestContext.getFilterResourceModel()).thenReturn(new FilterResourceModelImpl(resourceModel));
    when(filterRequestContext.getProjectionMask()).thenReturn(projectionMask);
    when(filterResponseContext.getResponseData()).thenReturn((RestLiResponseData) responseData);
    RestLiValidationFilter validationFilter = new RestLiValidationFilter(Lists.newArrayList(WHITELISTED_FIELD_NAME));
    try {
        validationFilter.onRequest(filterRequestContext);
        if (expectError) {
            Assert.fail("Expected an error to be thrown on request in the validation filter, but none was thrown.");
        }
    } catch (RestLiServiceException e) {
        if (expectError) {
            Assert.assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST);
            return;
        } else {
            Assert.fail("An unexpected exception was thrown on request in the validation filter.", e);
        }
    }
    validationFilter.onResponse(filterRequestContext, filterResponseContext);
}
Also used : FilterResourceModelImpl(com.linkedin.restli.internal.server.filter.FilterResourceModelImpl) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiRequestDataImpl(com.linkedin.restli.server.RestLiRequestDataImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 47 with ResourceMethod

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

the class TestCollectionResponseBuilder method dataProvider.

@DataProvider(name = "testData")
public Object[][] dataProvider() throws CloneNotSupportedException {
    Foo metadata = new Foo().setStringField("metadata").setIntField(7);
    Foo projectedMetadata = new Foo().setIntField(7);
    final List<Foo> generatedList = generateTestList();
    final List<Foo> testListWithProjection = generateTestListWithProjection();
    CollectionResult<Foo, Foo> collectionResult = new CollectionResult<>(generatedList, generatedList.size(), metadata);
    DataMap dataProjectionDataMap = new DataMap();
    dataProjectionDataMap.put("stringField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree dataMaskTree = new MaskTree(dataProjectionDataMap);
    DataMap metadataProjectionDataMap = new DataMap();
    metadataProjectionDataMap.put("intField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree metadataMaskTree = new MaskTree(metadataProjectionDataMap);
    DataMap pagingProjectDataMap = new DataMap();
    pagingProjectDataMap.put("count", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree pagingMaskTree = new MaskTree(pagingProjectDataMap);
    CollectionMetadata collectionMetadata1 = new CollectionMetadata().setCount(10).setStart(0).setLinks(new LinkArray());
    CollectionMetadata collectionMetadata2 = collectionMetadata1.clone().setTotal(2);
    CollectionMetadata collectionMetadataWithProjection = new CollectionMetadata().setCount(10);
    ProjectionMode auto = ProjectionMode.AUTOMATIC;
    ProjectionMode manual = ProjectionMode.MANUAL;
    List<Object[]> data = new ArrayList<>();
    for (ResourceMethod resourceMethod : BUILDERS.keySet()) {
        // auto projection for data and metadata with null projection masks
        data.add(new Object[] { generatedList, null, generatedList, collectionMetadata1, null, null, null, auto, auto, resourceMethod });
        data.add(new Object[] { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, auto, auto, resourceMethod });
        // manual projection for data and metadata with null projection masks
        data.add(new Object[] { generatedList, null, generatedList, collectionMetadata1, null, null, null, manual, manual, resourceMethod });
        data.add(new Object[] { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, manual, manual, resourceMethod });
        // NOTE - we always apply projections to the CollectionMetaData if the paging MaskTree is non-null
        // since ProjectionMode.AUTOMATIC is used.
        // manual projection for data and metadata with non-null projection masks
        data.add(new Object[] { generatedList, null, generatedList, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual, resourceMethod });
        data.add(new Object[] { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual, resourceMethod });
        // auto projection for data with non-null data and paging projection masks
        data.add(new Object[] { generatedList, null, testListWithProjection, collectionMetadataWithProjection, dataMaskTree, null, pagingMaskTree, auto, auto, resourceMethod });
        // auto projection for data and metadata with non-null projection masks
        data.add(new Object[] { collectionResult, projectedMetadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, auto, resourceMethod });
        // auto data projection, manual metadata projection, and auto (default) paging projection
        data.add(new Object[] { collectionResult, metadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, manual, resourceMethod });
    }
    return data.toArray(new Object[data.size()][]);
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) LinkArray(com.linkedin.restli.common.LinkArray) ProjectionMode(com.linkedin.restli.server.ProjectionMode) Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataMap(com.linkedin.data.DataMap) DataProvider(org.testng.annotations.DataProvider)

Example 48 with ResourceMethod

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

the class RestLiAnnotationReader method addCrudResourceMethod.

/**
 * Find which rest method annotation is present in the method of the resource, if any,
 * and add MethodDescriptor to ResourceModel.
 *
 * @param resourceClass
 * @param model
 * @param method
 */
private static void addCrudResourceMethod(final Class<?> resourceClass, final ResourceModel model, final Method method) {
    boolean restMethodAnnotationFound = false;
    for (Annotation methodAnnotation : method.getAnnotations()) {
        ResourceMethod resourceMethod = RestMethod.getResourceMethod(methodAnnotation.annotationType());
        if (resourceMethod != null) {
            if (restMethodAnnotationFound) {
                throw new ResourceConfigException("Multiple rest method annotations in method " + method.getName());
            }
            restMethodAnnotationFound = true;
            if (!Modifier.isPublic(method.getModifiers())) {
                throw new ResourceConfigException(String.format("Resource '%s' contains non-public CRUD method '%s'.", model.getName(), method.getName()));
            }
            Class<? extends RecordTemplate> metadataType = null;
            if (ResourceMethod.GET_ALL.equals(resourceMethod)) {
                metadataType = getCustomCollectionMetadata(method, DEFAULT_METADATA_PARAMETER_INDEX);
            }
            DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(method.getAnnotations());
            addDeprecatedAnnotation(annotationsMap, method);
            List<Parameter<?>> parameters = getParameters(model, method, resourceMethod);
            ResourceMethodDescriptor resourceMethodDescriptor = ResourceMethodDescriptor.createForRestful(resourceMethod, method, parameters, metadataType, getInterfaceType(method), annotationsMap);
            addServiceErrors(resourceMethodDescriptor, method);
            addSuccessStatuses(resourceMethodDescriptor, method);
            addMaxBatchSize(resourceMethodDescriptor, method, resourceMethod);
            model.addResourceMethodDescriptor(resourceMethodDescriptor);
        }
    }
}
Also used : ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) Annotation(java.lang.annotation.Annotation) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataMap(com.linkedin.data.DataMap)

Example 49 with ResourceMethod

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

the class DefaultMethodAdapterProvider method buildAdapterRegistry.

private Map<ResourceMethod, RestLiArgumentBuilder> buildAdapterRegistry() {
    Map<ResourceMethod, RestLiArgumentBuilder> result = new HashMap<>(ResourceMethod.values().length);
    result.put(ResourceMethod.GET, new GetArgumentBuilder());
    result.put(ResourceMethod.BATCH_GET, new BatchGetArgumentBuilder());
    result.put(ResourceMethod.FINDER, new CollectionArgumentBuilder());
    result.put(ResourceMethod.BATCH_FINDER, new CollectionArgumentBuilder());
    result.put(ResourceMethod.CREATE, new CreateArgumentBuilder());
    result.put(ResourceMethod.PARTIAL_UPDATE, new PatchArgumentBuilder());
    result.put(ResourceMethod.UPDATE, new UpdateArgumentBuilder());
    result.put(ResourceMethod.DELETE, new GetArgumentBuilder());
    result.put(ResourceMethod.ACTION, new ActionArgumentBuilder());
    result.put(ResourceMethod.BATCH_UPDATE, new BatchUpdateArgumentBuilder());
    result.put(ResourceMethod.BATCH_PARTIAL_UPDATE, new BatchPatchArgumentBuilder());
    result.put(ResourceMethod.BATCH_CREATE, new BatchCreateArgumentBuilder());
    result.put(ResourceMethod.BATCH_DELETE, new BatchDeleteArgumentBuilder());
    result.put(ResourceMethod.GET_ALL, new CollectionArgumentBuilder());
    return Collections.unmodifiableMap(result);
}
Also used : PatchArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.PatchArgumentBuilder) BatchPatchArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchPatchArgumentBuilder) CreateArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.CreateArgumentBuilder) BatchCreateArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchCreateArgumentBuilder) HashMap(java.util.HashMap) BatchPatchArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchPatchArgumentBuilder) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) UpdateArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.UpdateArgumentBuilder) BatchUpdateArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchUpdateArgumentBuilder) BatchGetArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchGetArgumentBuilder) BatchGetArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchGetArgumentBuilder) GetArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.GetArgumentBuilder) BatchCreateArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchCreateArgumentBuilder) BatchDeleteArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchDeleteArgumentBuilder) ActionArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.ActionArgumentBuilder) ResourceMethod(com.linkedin.restli.common.ResourceMethod) CollectionArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.CollectionArgumentBuilder) BatchUpdateArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.BatchUpdateArgumentBuilder)

Example 50 with ResourceMethod

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

the class DefaultMethodAdapterProvider method buildResponseBuilders.

private Map<ResourceMethod, RestLiResponseBuilder<?>> buildResponseBuilders(ErrorResponseBuilder errorResponseBuilder) {
    Map<ResourceMethod, RestLiResponseBuilder<?>> result = new HashMap<>(ResourceMethod.values().length);
    result.put(ResourceMethod.GET, new GetResponseBuilder());
    result.put(ResourceMethod.BATCH_GET, new BatchGetResponseBuilder(errorResponseBuilder));
    result.put(ResourceMethod.FINDER, new FinderResponseBuilder());
    result.put(ResourceMethod.CREATE, new CreateResponseBuilder());
    result.put(ResourceMethod.PARTIAL_UPDATE, new PartialUpdateResponseBuilder());
    result.put(ResourceMethod.UPDATE, new UpdateResponseBuilder());
    result.put(ResourceMethod.DELETE, new DeleteResponseBuilder());
    result.put(ResourceMethod.ACTION, new ActionResponseBuilder());
    result.put(ResourceMethod.BATCH_UPDATE, new BatchUpdateResponseBuilder(errorResponseBuilder));
    result.put(ResourceMethod.BATCH_PARTIAL_UPDATE, new BatchPartialUpdateResponseBuilder(errorResponseBuilder));
    result.put(ResourceMethod.BATCH_CREATE, new BatchCreateResponseBuilder(errorResponseBuilder));
    result.put(ResourceMethod.BATCH_DELETE, new BatchDeleteResponseBuilder(errorResponseBuilder));
    result.put(ResourceMethod.BATCH_FINDER, new BatchFinderResponseBuilder(errorResponseBuilder));
    result.put(ResourceMethod.GET_ALL, new GetAllResponseBuilder());
    return Collections.unmodifiableMap(result);
}
Also used : HashMap(java.util.HashMap) UpdateResponseBuilder(com.linkedin.restli.internal.server.response.UpdateResponseBuilder) BatchUpdateResponseBuilder(com.linkedin.restli.internal.server.response.BatchUpdateResponseBuilder) BatchPartialUpdateResponseBuilder(com.linkedin.restli.internal.server.response.BatchPartialUpdateResponseBuilder) PartialUpdateResponseBuilder(com.linkedin.restli.internal.server.response.PartialUpdateResponseBuilder) BatchPartialUpdateResponseBuilder(com.linkedin.restli.internal.server.response.BatchPartialUpdateResponseBuilder) BatchCreateResponseBuilder(com.linkedin.restli.internal.server.response.BatchCreateResponseBuilder) BatchFinderResponseBuilder(com.linkedin.restli.internal.server.response.BatchFinderResponseBuilder) RestLiResponseBuilder(com.linkedin.restli.internal.server.response.RestLiResponseBuilder) FinderResponseBuilder(com.linkedin.restli.internal.server.response.FinderResponseBuilder) BatchFinderResponseBuilder(com.linkedin.restli.internal.server.response.BatchFinderResponseBuilder) GetAllResponseBuilder(com.linkedin.restli.internal.server.response.GetAllResponseBuilder) BatchGetResponseBuilder(com.linkedin.restli.internal.server.response.BatchGetResponseBuilder) BatchPartialUpdateResponseBuilder(com.linkedin.restli.internal.server.response.BatchPartialUpdateResponseBuilder) PartialUpdateResponseBuilder(com.linkedin.restli.internal.server.response.PartialUpdateResponseBuilder) ActionResponseBuilder(com.linkedin.restli.internal.server.response.ActionResponseBuilder) BatchUpdateResponseBuilder(com.linkedin.restli.internal.server.response.BatchUpdateResponseBuilder) BatchDeleteResponseBuilder(com.linkedin.restli.internal.server.response.BatchDeleteResponseBuilder) BatchCreateResponseBuilder(com.linkedin.restli.internal.server.response.BatchCreateResponseBuilder) CreateResponseBuilder(com.linkedin.restli.internal.server.response.CreateResponseBuilder) BatchDeleteResponseBuilder(com.linkedin.restli.internal.server.response.BatchDeleteResponseBuilder) DeleteResponseBuilder(com.linkedin.restli.internal.server.response.DeleteResponseBuilder) BatchGetResponseBuilder(com.linkedin.restli.internal.server.response.BatchGetResponseBuilder) GetResponseBuilder(com.linkedin.restli.internal.server.response.GetResponseBuilder) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

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