Search in sources :

Example 1 with FilterResourceModelImpl

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);
}
Also used : FilterResourceModelImpl(com.linkedin.restli.internal.server.filter.FilterResourceModelImpl) DataMap(com.linkedin.data.DataMap) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with FilterResourceModelImpl

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);
}
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 3 with FilterResourceModelImpl

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);
    }
}
Also used : FilterResourceModelImpl(com.linkedin.restli.internal.server.filter.FilterResourceModelImpl) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Test(org.testng.annotations.Test)

Example 4 with FilterResourceModelImpl

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);
}
Also used : FilterResourceModelImpl(com.linkedin.restli.internal.server.filter.FilterResourceModelImpl) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) MockBadRequest(com.linkedin.restli.server.errors.MockBadRequest) Test(org.testng.annotations.Test)

Example 5 with FilterResourceModelImpl

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);
}
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)

Aggregations

FilterResourceModelImpl (com.linkedin.restli.internal.server.filter.FilterResourceModelImpl)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)5 Test (org.testng.annotations.Test)5 ResourceMethod (com.linkedin.restli.common.ResourceMethod)2 RestLiRequestDataImpl (com.linkedin.restli.server.RestLiRequestDataImpl)2 DataMap (com.linkedin.data.DataMap)1 MockBadRequest (com.linkedin.restli.server.errors.MockBadRequest)1 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)1 BeforeMethod (org.testng.annotations.BeforeMethod)1