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);
}
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()][]);
}
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);
}
}
}
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);
}
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);
}
Aggregations