Search in sources :

Example 26 with PolicyFailure

use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.

the class TransferQuotaPolicyTest method testDownloadLimit.

@Test
@Configuration("{" + "  \"limit\" : 1000," + "  \"direction\" : \"download\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"X-DBytes-Remaining\"," + "  \"headerLimit\" : \"X-DBytes-Limit\"," + "  \"headerReset\" : \"X-DBytes-Reset\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimit() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    request.header("X-Payload-Size", "389");
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("1000", response.header("X-DBytes-Remaining"));
    Assert.assertEquals("1000", response.header("X-DBytes-Limit"));
    send(request);
    send(request);
    // Now if we try it one more time, we'll get a failure!
    try {
        send(request);
        Assert.fail("Expected a policy failure!");
    } catch (PolicyFailureError e) {
        PolicyFailure failure = e.getFailure();
        Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
        Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
        Assert.assertEquals(429, failure.getResponseCode());
    }
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 27 with PolicyFailure

use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.

the class AuthorizationPolicyTest method doTest.

private void doTest(String json, HashSet<String> userRoles, String verb, String path, boolean shouldSucceed) {
    AuthorizationPolicy policy = new AuthorizationPolicy();
    Object config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType(verb);
    request.setDestination(path);
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    Mockito.when(context.getAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, (HashSet<String>) null)).thenReturn(userRoles);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            failure.setFailureCode(failureCode);
            failure.setType(type);
            failure.setMessage(message);
            return failure;
        }
    });
    // Success
    policy.apply(request, context, config, chain);
    if (shouldSucceed) {
        Mockito.verify(chain).doApply(request);
    } else {
        Mockito.verify(chain).doFailure(failure);
    }
}
Also used : IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)

Example 28 with PolicyFailure

use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.

the class BeanSharedStatePolicy method apply.

/**
 * @see io.apiman.gateway.engine.policy.IPolicy#apply(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Override
public void apply(final ApiResponse response, IPolicyContext context, Object config, final IPolicyChain<ApiResponse> chain) {
    final ISharedStateComponent sharedState = context.getComponent(ISharedStateComponent.class);
    final String namespace = "urn:" + getClass().getName();
    final String propName = "bean-property";
    final DataBean notfound = new DataBean();
    sharedState.getProperty(namespace, propName, notfound, new IAsyncResultHandler<DataBean>() {

        @Override
        public void handle(IAsyncResult<DataBean> result) {
            DataBean value = result.getResult();
            if (value == notfound) {
                PolicyFailure failure = new PolicyFailure(PolicyFailureType.NotFound, 0, "DataBean not found.");
                chain.doFailure(failure);
            } else {
                response.getHeaders().put("X-Property-1", value.getProperty1());
                response.getHeaders().put("X-Property-2", String.valueOf(value.isProperty2()));
                response.getHeaders().put("X-Property-3", String.valueOf(value.getProperty3()));
                chain.doApply(response);
            }
        }
    });
}
Also used : ISharedStateComponent(io.apiman.gateway.engine.components.ISharedStateComponent) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) DataBean(io.apiman.gateway.test.policies.beans.DataBean)

Example 29 with PolicyFailure

use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.

the class PolicyChainTest method shouldCallFailureHandlerOnDoFail.

@Test
public void shouldCallFailureHandlerOnDoFail() {
    policies.add(pwcOne);
    policies.add(pwcTwo);
    requestChain = new RequestChain(policies, mockContext);
    IAsyncHandler<PolicyFailure> mPolicyFailureHandler = mock(IAsyncHandler.class);
    PolicyFailure mPolicyFailure = mock(PolicyFailure.class);
    requestChain.policyFailureHandler(mPolicyFailureHandler);
    requestChain.bodyHandler(mockBodyHandler);
    requestChain.endHandler(mockEndHandler);
    requestChain.doApply(mockRequest);
    requestChain.doFailure(mPolicyFailure);
    verify(mPolicyFailureHandler).handle(mPolicyFailure);
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) RequestChain(io.apiman.gateway.engine.policy.RequestChain) Test(org.junit.Test)

Example 30 with PolicyFailure

use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.

the class TransferQuotaPolicyTest method testDownloadLimitEmptyHeaderConfig.

@Test
@Configuration("{" + "  \"limit\" : 1000," + "  \"direction\" : \"download\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"\"," + "  \"headerLimit\" : \"\"," + "  \"headerReset\" : \"\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimitEmptyHeaderConfig() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    request.header("X-Payload-Size", "389");
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("1000", response.header("X-TransferQuota-Remaining"));
    Assert.assertEquals("1000", response.header("X-TransferQuota-Limit"));
    send(request);
    send(request);
    // Now if we try it one more time, we'll get a failure!
    try {
        send(request);
        Assert.fail("Expected a policy failure!");
    } catch (PolicyFailureError e) {
        PolicyFailure failure = e.getFailure();
        Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
        Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
        Assert.assertEquals(429, failure.getResponseCode());
    }
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Aggregations

PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)54 Test (org.junit.Test)26 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)19 ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)19 Configuration (io.apiman.test.policies.Configuration)19 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)19 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)19 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)11 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)11 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)11 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)9 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)8 EchoResponse (io.apiman.test.common.mock.EchoResponse)7 HashSet (java.util.HashSet)6 BackEndApi (io.apiman.test.policies.BackEndApi)4 IPolicyTestBackEndApi (io.apiman.test.policies.IPolicyTestBackEndApi)4 IRateLimiterComponent (io.apiman.gateway.engine.components.IRateLimiterComponent)2 RateLimitResponse (io.apiman.gateway.engine.components.rate.RateLimitResponse)2 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)2 IPListConfig (io.apiman.gateway.engine.policies.config.IPListConfig)2