Search in sources :

Example 1 with IgnoredResourcesConfig

use of io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig in project apiman by apiman.

the class IgnoredResourcesPolicyTest method testParseConfiguration.

@Test
public void testParseConfiguration() throws Exception {
    IgnoredResourcesPolicy policy = new IgnoredResourcesPolicy();
    String config = "{}";
    Object parsed = policy.parseConfiguration(config);
    assertNotNull(parsed);
    assertEquals(IgnoredResourcesConfig.class, parsed.getClass());
    IgnoredResourcesConfig parsedConfig = (IgnoredResourcesConfig) parsed;
    assertNotNull(parsedConfig.getRules());
    assertTrue(parsedConfig.getRules().isEmpty());
    IgnoredResourcesConfig configObj = new IgnoredResourcesConfig();
    ArrayList<IgnoredResource> elements = new ArrayList<>(2);
    elements.add(createResource(IgnoredResource.VERB_MATCH_ALL, firstPath));
    configObj.setRules(elements);
    // Single Path
    assertConfigurationParseResults(policy, configObj);
    elements.add(createResource(IgnoredResource.VERB_MATCH_ALL, secondPath));
    configObj.setRules(elements);
    assertConfigurationParseResults(policy, configObj);
}
Also used : IgnoredResource(io.apiman.gateway.engine.policies.config.IgnoredResource) IgnoredResourcesConfig(io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with IgnoredResourcesConfig

use of io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig in project apiman by apiman.

the class IgnoredResourcesPolicyTest method assertConfigurationParseResults.

private void assertConfigurationParseResults(IgnoredResourcesPolicy policy, IgnoredResourcesConfig configObj) throws Exception {
    IgnoredResourcesConfig parsedConfig;
    List<IgnoredResource> expectedConfiguration;
    String config = mapper.writeValueAsString(configObj);
    parsedConfig = policy.parseConfiguration(config);
    assertNotNull(parsedConfig.getRules());
    expectedConfiguration = configObj.getRules();
    assertEquals(expectedConfiguration, parsedConfig.getRules());
}
Also used : IgnoredResource(io.apiman.gateway.engine.policies.config.IgnoredResource) IgnoredResourcesConfig(io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig)

Example 3 with IgnoredResourcesConfig

use of io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig in project apiman by apiman.

the class IgnoredResourcesPolicyTest method requestWithVerb.

private ApiRequest requestWithVerb(IPolicyChain<ApiRequest> chain, String verb) throws IOException, JsonGenerationException, JsonMappingException {
    IgnoredResourcesPolicy policy = new IgnoredResourcesPolicy();
    IgnoredResourcesConfig configObj = new IgnoredResourcesConfig();
    ArrayList<IgnoredResource> elements = new ArrayList<>(2);
    elements.add(createResource(verb, firstPath));
    elements.add(createResource(verb, secondPath));
    configObj.setRules(elements);
    String json = mapper.writeValueAsString(configObj);
    Object config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/invoices/1");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    createFailurePolicyObject(context);
    // Success
    policy.apply(request, context, config, chain);
    // Fail
    request.setDestination("/invoices/23/items/43");
    policy.apply(request, context, config, chain);
    // Fail
    request.setDestination("/items/43");
    policy.apply(request, context, config, chain);
    return request;
}
Also used : IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) IgnoredResource(io.apiman.gateway.engine.policies.config.IgnoredResource) IgnoredResourcesConfig(io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig) ArrayList(java.util.ArrayList) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest)

Example 4 with IgnoredResourcesConfig

use of io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig in project apiman by apiman.

the class IgnoredResourcesPolicyTest method testApply.

@Test
public void testApply() throws Exception {
    IgnoredResourcesPolicy policy = new IgnoredResourcesPolicy();
    IgnoredResourcesConfig configObj = new IgnoredResourcesConfig();
    ArrayList<IgnoredResource> elements = new ArrayList<>(2);
    elements.add(createResource(IgnoredResource.VERB_MATCH_ALL, firstPath));
    elements.add(createResource(IgnoredResource.VERB_MATCH_ALL, secondPath));
    configObj.setRules(elements);
    String json = mapper.writeValueAsString(configObj);
    Object config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/invoices/1");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Success
    policy.apply(request, context, config, chain);
    // Success
    request.setDestination("/invoices");
    policy.apply(request, context, config, chain);
    // Success
    request.setDestination("/invoices/items");
    policy.apply(request, context, config, chain);
    // Success
    request.setDestination("/invoices/items/13");
    policy.apply(request, context, config, chain);
    Mockito.verify(chain, Mockito.times(4)).doApply(request);
    Mockito.verify(chain, Mockito.never()).doFailure(Mockito.<PolicyFailure>any());
    final PolicyFailure failure = createFailurePolicyObject(context);
    chain = Mockito.mock(IPolicyChain.class);
    request.setDestination("/invoices/23/items/43");
    policy.apply(request, context, config, chain);
    // Failure
    request.setDestination("/items/43");
    policy.apply(request, context, config, chain);
    Mockito.verify(chain, Mockito.times(2)).doFailure(failure);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) IgnoredResource(io.apiman.gateway.engine.policies.config.IgnoredResource) IgnoredResourcesConfig(io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) ArrayList(java.util.ArrayList) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) Test(org.junit.Test)

Aggregations

IgnoredResource (io.apiman.gateway.engine.policies.config.IgnoredResource)4 IgnoredResourcesConfig (io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig)4 ArrayList (java.util.ArrayList)3 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)2 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)2 Test (org.junit.Test)2 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)1 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)1