use of com.linkedin.restli.internal.server.filter.FilterResourceModelImpl in project rest.li by linkedin.
the class TestRestLiValidationFilter method setUpMocks.
@BeforeMethod
public void setUpMocks() {
MockitoAnnotations.initMocks(this);
when(filterRequestContext.getRequestHeaders()).thenReturn(Collections.emptyMap());
when(filterRequestContext.getFilterResourceModel()).thenReturn(new FilterResourceModelImpl(RestLiAnnotationReader.processResource(CollectionResource.class)));
when(filterRequestContext.getCustomAnnotations()).thenReturn(new DataMap());
when(filterRequestContext.isReturnEntityMethod()).thenReturn(false);
}
use of com.linkedin.restli.internal.server.filter.FilterResourceModelImpl in project rest.li by linkedin.
the class TestRestLiValidationFilter method testHandleProjection.
/**
* Ensures that the validation filter safely and correctly reacts to projections given a variety of resource types,
* resource methods, and projection masks. This was motivated by a bug that caused an NPE in the validation filter
* when the resource being queried was a {@link RestLiActions} resource and thus had no value class.
*/
@Test(dataProvider = "validateWithProjectionData")
@SuppressWarnings({ "unchecked" })
public void testHandleProjection(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();
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.internal.server.filter.FilterResourceModelImpl in project rest.li by linkedin.
the class TestRestLiValidationFilter method testSkipResponseValidation.
/**
* Ensures that the validation filter skips response validation by feeding it with invalid data, and ensuring that it
* never throws an error.
*/
@Test(dataProvider = "invalidResponses")
@SuppressWarnings({ "unchecked" })
public void testSkipResponseValidation(ResourceModel resourceModel, ResourceMethod resourceMethod, RestLiRequestData restLiRequestData, RestLiResponseData<RestLiResponseEnvelope> responseData) {
when(filterRequestContext.getRequestHeaders()).thenReturn(Collections.singletonMap(RestConstants.HEADER_SKIP_RESPONSE_VALIDATION, "true"));
when(filterRequestContext.getRequestData()).thenReturn(restLiRequestData);
when(filterRequestContext.getMethodType()).thenReturn(resourceMethod);
when(filterRequestContext.getFilterResourceModel()).thenReturn(new FilterResourceModelImpl(resourceModel));
when(filterRequestContext.getRestliProtocolVersion()).thenReturn(AllProtocolVersions.LATEST_PROTOCOL_VERSION);
when(filterResponseContext.getResponseData()).thenReturn((RestLiResponseData) responseData);
RestLiValidationFilter validationFilter = new RestLiValidationFilter(Collections.emptyList(), new MockValidationErrorHandler());
try {
validationFilter.onResponse(filterRequestContext, filterResponseContext);
} catch (RestLiServiceException ex) {
Assert.fail("An unexpected exception was thrown on response in the validation filter.", ex);
}
}
use of com.linkedin.restli.internal.server.filter.FilterResourceModelImpl in project rest.li by linkedin.
the class TestRestLiValidationFilter method testEntityValidateOnRequest.
/**
* Ensures that the validation filter correctly validates input entity given a variety of resource types,
* resource methods and RestLi request data.
*/
@Test(dataProvider = "validateWithPdscValidation")
@SuppressWarnings({ "unchecked" })
public void testEntityValidateOnRequest(ResourceModel resourceModel, ResourceMethod resourceMethod, RestLiRequestData restLiRequestData, RestLiResponseData<RestLiResponseEnvelope> responseData, boolean expectError) {
when(filterRequestContext.getRequestData()).thenReturn(restLiRequestData);
when(filterRequestContext.getMethodType()).thenReturn(resourceMethod);
when(filterRequestContext.getFilterResourceModel()).thenReturn(new FilterResourceModelImpl(resourceModel));
when(filterRequestContext.getRestliProtocolVersion()).thenReturn(AllProtocolVersions.LATEST_PROTOCOL_VERSION);
when(filterResponseContext.getResponseData()).thenReturn((RestLiResponseData) responseData);
RestLiValidationFilter validationFilter = new RestLiValidationFilter(Collections.emptyList(), new MockValidationErrorHandler());
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 ex) {
if (expectError) {
Assert.assertEquals(ex.getStatus(), HttpStatus.S_422_UNPROCESSABLE_ENTITY);
Assert.assertEquals(ex.getCode(), "BAD_REQUEST");
if (ex.getErrorDetailsRecord() != null) {
Assert.assertEquals(ex.getErrorDetailsRecord().getClass(), MockBadRequest.class);
}
return;
} else {
Assert.fail("An unexpected exception was thrown on request in the validation filter.", ex);
}
}
validationFilter.onResponse(filterRequestContext, filterResponseContext);
}
use of com.linkedin.restli.internal.server.filter.FilterResourceModelImpl 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);
}
Aggregations