Search in sources :

Example 1 with Step

use of io.gravitee.definition.model.flow.Step in project gravitee-management-rest-api by gravitee-io.

the class PolicyServiceTest method shouldAcceptValidJsonConfigurationWithNoSchemaStep.

@Test
public void shouldAcceptValidJsonConfigurationWithNoSchemaStep() throws Exception {
    Step step = new Step();
    step.setPolicy("my-policy");
    step.setConfiguration("{ \"name\": \"test\", \"valid\": true }");
    when(policyManager.getSchema("my-policy")).thenReturn("");
    policyService.validatePolicyConfiguration(step);
}
Also used : Step(io.gravitee.definition.model.flow.Step) Test(org.junit.Test)

Example 2 with Step

use of io.gravitee.definition.model.flow.Step in project gravitee-management-rest-api by gravitee-io.

the class PolicyServiceTest method shouldRejectInvalidJsonStep.

@Test(expected = InvalidDataException.class)
public void shouldRejectInvalidJsonStep() throws Exception {
    Step step = new Step();
    step.setPolicy("my-policy");
    step.setConfiguration("{ Invalid: \"test\", \"valid\": false }");
    policyService.validatePolicyConfiguration(step);
}
Also used : Step(io.gravitee.definition.model.flow.Step) Test(org.junit.Test)

Example 3 with Step

use of io.gravitee.definition.model.flow.Step in project gravitee-management-rest-api by gravitee-io.

the class PolicyServiceTest method shouldAcceptNoConfigurationStep.

@Test
public void shouldAcceptNoConfigurationStep() {
    Step step = new Step();
    step.setPolicy("my-policy");
    step.setConfiguration(null);
    policyService.validatePolicyConfiguration(step);
}
Also used : Step(io.gravitee.definition.model.flow.Step) Test(org.junit.Test)

Example 4 with Step

use of io.gravitee.definition.model.flow.Step in project gravitee-management-rest-api by gravitee-io.

the class APIV1toAPIV2Converter method configurePolicies.

/**
 * Configure Flow's Steps from Rule.
 * @param policies, the list of available policies, containing available scopes
 * @param rule, the rule to transform into Step
 * @param flow, the current Flow
 */
private void configurePolicies(Set<PolicyEntity> policies, Rule rule, Flow flow) {
    policies.stream().filter(policy -> policy.getId().equals(rule.getPolicy().getName())).findFirst().ifPresent(policy -> {
        String rulePolicyConfiguration = rule.getPolicy().getConfiguration();
        String safeRulePolicyConfiguration = clearNullValues(rulePolicyConfiguration);
        if (policy.getDevelopment().getOnRequestMethod() != null && policy.getDevelopment().getOnResponseMethod() != null) {
            try {
                JsonNode jsonRulePolicyConfiguration = JsonLoader.fromString(safeRulePolicyConfiguration);
                JsonNode scope = jsonRulePolicyConfiguration.get("scope");
                if (scope != null) {
                    switch(scope.asText()) {
                        case "REQUEST":
                        case "REQUEST_CONTENT":
                            {
                                final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
                                flow.getPre().add(step);
                                break;
                            }
                        case "RESPONSE":
                        case "RESPONSE_CONTENT":
                            {
                                final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
                                flow.getPost().add(step);
                                break;
                            }
                    }
                }
            } catch (IOException e) {
                throw new InvalidDataException("Unable to validate policy configuration", e);
            }
        } else if (policy.getDevelopment().getOnRequestMethod() != null) {
            final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
            flow.getPre().add(step);
        } else if (policy.getDevelopment().getOnResponseMethod() != null) {
            final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
            flow.getPost().add(step);
        }
    });
}
Also used : InvalidDataException(io.gravitee.rest.api.service.exceptions.InvalidDataException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Step(io.gravitee.definition.model.flow.Step) IOException(java.io.IOException)

Example 5 with Step

use of io.gravitee.definition.model.flow.Step in project gravitee-management-rest-api by gravitee-io.

the class SwaggerService_CreateAPIV2Test method validateRules.

@Override
protected void validateRules(SwaggerApiEntity api, String path, int expectedRuleSize, List<HttpMethod> firstRuleMethods, String firstRuleDescription) {
    List<Flow> flows = api.getFlows().stream().filter(flow1 -> flow1.getPath().equals(path)).collect(Collectors.toList());
    // assertEquals(1, flows.size());
    assertEquals(expectedRuleSize, flows.get(0).getPre().size());
    assertTrue(flows.get(0).getMethods().containsAll(firstRuleMethods));
    Step step = flows.get(0).getPre().get(0);
    assertNotNull(step);
    assertEquals(firstRuleDescription, step.getDescription());
}
Also used : Step(io.gravitee.definition.model.flow.Step) java.util(java.util) Flow(io.gravitee.definition.model.flow.Flow) RunWith(org.junit.runner.RunWith) IOException(java.io.IOException) Test(org.junit.Test) io.gravitee.definition.model(io.gravitee.definition.model) Collectors(java.util.stream.Collectors) HttpMethod(io.gravitee.common.http.HttpMethod) Arrays.asList(java.util.Arrays.asList) SwaggerApiEntity(io.gravitee.rest.api.model.api.SwaggerApiEntity) io.gravitee.rest.api.model(io.gravitee.rest.api.model) Assert(org.junit.Assert) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Step(io.gravitee.definition.model.flow.Step) Flow(io.gravitee.definition.model.flow.Flow)

Aggregations

Step (io.gravitee.definition.model.flow.Step)7 Test (org.junit.Test)4 HttpMethod (io.gravitee.common.http.HttpMethod)2 Flow (io.gravitee.definition.model.flow.Flow)2 SwaggerApiEntity (io.gravitee.rest.api.model.api.SwaggerApiEntity)2 IOException (java.io.IOException)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 io.gravitee.definition.model (io.gravitee.definition.model)1 DefinitionVersion (io.gravitee.definition.model.DefinitionVersion)1 Operator (io.gravitee.definition.model.flow.Operator)1 PathOperator (io.gravitee.definition.model.flow.PathOperator)1 Policy (io.gravitee.policy.api.swagger.Policy)1 io.gravitee.rest.api.model (io.gravitee.rest.api.model)1 ImportSwaggerDescriptorEntity (io.gravitee.rest.api.model.ImportSwaggerDescriptorEntity)1 GroupService (io.gravitee.rest.api.service.GroupService)1 TagService (io.gravitee.rest.api.service.TagService)1 InvalidDataException (io.gravitee.rest.api.service.exceptions.InvalidDataException)1 PolicyOperationVisitorManager (io.gravitee.rest.api.service.impl.swagger.policy.PolicyOperationVisitorManager)1