Search in sources :

Example 1 with IPolicyContext

use of io.apiman.gateway.engine.policy.IPolicyContext in project apiman by apiman.

the class PolicyChainTest method shouldEndChainImmediatelyWhenSkipCalled.

@Test
public void shouldEndChainImmediatelyWhenSkipCalled() {
    IPolicy skipPolicy = spy(new IPolicy() {

        @Override
        public Object parseConfiguration(String jsonConfiguration) throws ConfigurationParseException {
            return null;
        }

        @Override
        public void apply(ApiRequest request, IPolicyContext context, Object config, IPolicyChain<ApiRequest> chain) {
            chain.doSkip(request);
        }

        @Override
        public void apply(ApiResponse response, IPolicyContext context, Object config, IPolicyChain<ApiResponse> chain) {
            chain.doSkip(response);
        }
    });
    PolicyWithConfiguration pwcSkip = new PolicyWithConfiguration(skipPolicy, null);
    policies.add(pwcSkip);
    policies.add(pwcTwo);
    requestChain = new RequestChain(policies, mockContext);
    requestChain.bodyHandler(mockBodyHandler);
    requestChain.endHandler(mockEndHandler);
    requestChain.doApply(mockRequest);
    requestChain.end();
    verify(mockEndHandler, times(1)).handle((Void) null);
    // Should only be called once, as the second is skipped
    verify(skipPolicy, times(1)).apply(mockRequest, mockContext, null, requestChain);
    verify(policyOne, never()).apply(mockRequest, mockContext, null, requestChain);
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) ConfigurationParseException(io.apiman.gateway.engine.beans.exceptions.ConfigurationParseException) RequestChain(io.apiman.gateway.engine.policy.RequestChain) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Test(org.junit.Test)

Example 2 with IPolicyContext

use of io.apiman.gateway.engine.policy.IPolicyContext in project apiman by apiman.

the class ApimanPolicyTest method getContext.

/**
 * @param executor
 */
private IPolicyContext getContext(IApiRequestExecutor executor) {
    try {
        // $NON-NLS-1$
        Field field = executor.getClass().getDeclaredField("context");
        field.setAccessible(true);
        Object fieldValue = field.get(executor);
        return (IPolicyContext) fieldValue;
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext)

Example 3 with IPolicyContext

use of io.apiman.gateway.engine.policy.IPolicyContext in project apiman by apiman.

the class IPBlacklistPolicyTest method testApply.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApply() {
    IPBlacklistPolicy policy = new IPBlacklistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"1.2.3.4\"," + "    \"3.4.5.6\"," + "    \"10.0.0.11\"" + "  ]" + "}";
    IPListConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Failure
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Success
    request.setRemoteAddr("9.8.7.6");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) Test(org.junit.Test)

Example 4 with IPolicyContext

use of io.apiman.gateway.engine.policy.IPolicyContext in project apiman by apiman.

the class IPBlacklistPolicyTest method testApplyWithWildcards.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApplyWithWildcards() {
    IPBlacklistPolicy policy = new IPBlacklistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"10.0.*.*\"" + "  ]" + "}";
    IPListConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Failures
    request.setRemoteAddr("10.0.123.87");
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    request.setRemoteAddr("10.0.97.1");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Successes
    request.setRemoteAddr("9.8.7.6");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
    request.setRemoteAddr("10.10.15.123");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) Test(org.junit.Test)

Example 5 with IPolicyContext

use of io.apiman.gateway.engine.policy.IPolicyContext in project apiman by apiman.

the class IPWhitelistPolicyTest method testApply.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPWhitelistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApply() {
    IPWhitelistPolicy policy = new IPWhitelistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"1.2.3.4\"," + "    \"3.4.5.6\"," + "    \"10.0.0.11\"" + "  ]" + "}";
    Object config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Success
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
    // Failure
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    chain = Mockito.mock(IPolicyChain.class);
    request.setRemoteAddr("9.8.7.6");
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) 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) Test(org.junit.Test)

Aggregations

IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)16 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)15 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)11 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)10 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)8 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)7 Test (org.junit.Test)7 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)3 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)3 ArrayList (java.util.ArrayList)3 DefaultJdbcComponent (io.apiman.gateway.engine.impl.DefaultJdbcComponent)2 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)2 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)2 IPListConfig (io.apiman.gateway.engine.policies.config.IPListConfig)2 IgnoredResource (io.apiman.gateway.engine.policies.config.IgnoredResource)2 IgnoredResourcesConfig (io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig)2 HashSet (java.util.HashSet)2 IApiRequestExecutor (io.apiman.gateway.engine.IApiRequestExecutor)1 IEngine (io.apiman.gateway.engine.IEngine)1 IEngineResult (io.apiman.gateway.engine.IEngineResult)1