Search in sources :

Example 1 with PolicyEntity

use of io.gravitee.rest.api.model.PolicyEntity in project gravitee-management-rest-api by gravitee-io.

the class APIV1toAPIV2Converter method migratePathsToFlows.

/**
 * Migrate apiEntity.paths to Flow model.
 * @param paths, the map of paths to migrate
 * @param policies, the list of available policies, containing available scopes
 * @return the list of Flows
 */
private List<Flow> migratePathsToFlows(Map<String, List<Rule>> paths, Set<PolicyEntity> policies) {
    List<Flow> flows = new ArrayList<>();
    if (!CollectionUtils.isEmpty(paths)) {
        paths.forEach((pathKey, pathValue) -> {
            // if all rules for a path have the same set of HttpMethods, then we have a unique flow for this path.
            // else, we have a flow per rule in the path.
            boolean oneFlowPerPathMode = pathValue.stream().map(rule -> {
                Set<HttpMethod> methods = new HashSet<>(rule.getMethods());
                methods.retainAll(HTTP_METHODS);
                return methods;
            }).distinct().count() == 1;
            if (oneFlowPerPathMode) {
                // since, all HttpMethods are the same in this case, we can use `pathValue.getRules().get(0).getMethods()`
                final Flow flow = createFlow(pathKey, pathValue.get(0).getMethods());
                pathValue.forEach(rule -> {
                    configurePolicies(policies, rule, flow);
                });
                // reverse policies of the Post steps otherwise, flow are displayed in the wrong order into the policy studio
                Collections.reverse(flow.getPost());
                flows.add(flow);
            } else {
                pathValue.forEach(rule -> {
                    final Flow flow = createFlow(pathKey, rule.getMethods());
                    configurePolicies(policies, rule, flow);
                    // reverse policies of the Post steps otherwise, flow are displayed in the wrong order into the policy studio
                    Collections.reverse(flow.getPost());
                    flows.add(flow);
                });
            }
        });
    }
    return flows;
}
Also used : PATCH(io.gravitee.common.http.HttpMethod.PATCH) Arrays(java.util.Arrays) PUT(io.gravitee.common.http.HttpMethod.PUT) GET(io.gravitee.common.http.HttpMethod.GET) OPTIONS(io.gravitee.common.http.HttpMethod.OPTIONS) PathOperator(io.gravitee.definition.model.flow.PathOperator) Flow(io.gravitee.definition.model.flow.Flow) HEAD(io.gravitee.common.http.HttpMethod.HEAD) HashMap(java.util.HashMap) InvalidDataException(io.gravitee.rest.api.service.exceptions.InvalidDataException) Operator(io.gravitee.definition.model.flow.Operator) PlanStatus(io.gravitee.rest.api.model.PlanStatus) ArrayList(java.util.ArrayList) Rule(io.gravitee.definition.model.Rule) HashSet(java.util.HashSet) PolicyHelper.clearNullValues(io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues) POST(io.gravitee.common.http.HttpMethod.POST) Collections.reverseOrder(java.util.Collections.reverseOrder) Map(java.util.Map) JsonLoader(com.github.fge.jackson.JsonLoader) JsonNode(com.fasterxml.jackson.databind.JsonNode) Step(io.gravitee.definition.model.flow.Step) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) CONNECT(io.gravitee.common.http.HttpMethod.CONNECT) DELETE(io.gravitee.common.http.HttpMethod.DELETE) PlanEntity(io.gravitee.rest.api.model.PlanEntity) Set(java.util.Set) IOException(java.io.IOException) NavigableMap(java.util.NavigableMap) Collectors(java.util.stream.Collectors) FlowMode(io.gravitee.definition.model.FlowMode) List(java.util.List) Component(org.springframework.stereotype.Component) HttpMethod(io.gravitee.common.http.HttpMethod) DefinitionVersion(io.gravitee.definition.model.DefinitionVersion) TreeMap(java.util.TreeMap) CollectionUtils(org.springframework.util.CollectionUtils) PolicyEntity(io.gravitee.rest.api.model.PolicyEntity) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) TRACE(io.gravitee.common.http.HttpMethod.TRACE) Plan(io.gravitee.definition.model.Plan) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Flow(io.gravitee.definition.model.flow.Flow)

Example 2 with PolicyEntity

use of io.gravitee.rest.api.model.PolicyEntity in project gravitee-management-rest-api by gravitee-io.

the class PoliciesResourceTest method shouldGetPoliciesList.

@Test
public void shouldGetPoliciesList() {
    HashSet<PolicyEntity> policyEntities = new HashSet<>();
    PolicyEntity policyEntity = new PolicyEntity();
    policyEntities.add(policyEntity);
    policyEntity.setId("my-api");
    policyEntity.setName("My Api");
    when(policyService.findAll(null)).thenReturn(policyEntities);
    final Response response = envTarget().request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    Set entity = response.readEntity(Set.class);
    assertFalse("not empty", entity.isEmpty());
    assertEquals("one element", 1, entity.size());
    Object o = entity.iterator().next();
    assertTrue(o instanceof LinkedHashMap);
    LinkedHashMap<String, String> elt = (LinkedHashMap<String, String>) o;
    assertEquals("id", "my-api", elt.get("id"));
    assertEquals("name", "My Api", elt.get("name"));
}
Also used : Response(javax.ws.rs.core.Response) HashSet(java.util.HashSet) Set(java.util.Set) PolicyEntity(io.gravitee.rest.api.model.PolicyEntity) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with PolicyEntity

use of io.gravitee.rest.api.model.PolicyEntity in project gravitee-management-rest-api by gravitee-io.

the class PoliciesResourceTest method shouldGetPoliciesListWithUnknownExpand.

@Test
public void shouldGetPoliciesListWithUnknownExpand() {
    HashSet<PolicyEntity> policyEntities = new HashSet<>();
    PolicyEntity policyEntity = new PolicyEntity();
    policyEntities.add(policyEntity);
    policyEntity.setId("my-api");
    when(policyService.findAll(null)).thenReturn(policyEntities);
    final Response response = envTarget().queryParam("expand", "unknown").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    Set entity = response.readEntity(Set.class);
    assertFalse("not empty", entity.isEmpty());
    assertEquals("one element", 1, entity.size());
    Object o = entity.iterator().next();
    assertTrue(o instanceof LinkedHashMap);
    LinkedHashMap<String, String> elt = (LinkedHashMap<String, String>) o;
    assertEquals("id", "my-api", elt.get("id"));
    assertFalse("unknown expand", elt.containsKey("schema"));
    assertFalse("unknown expand", elt.containsKey("unknown"));
}
Also used : Response(javax.ws.rs.core.Response) HashSet(java.util.HashSet) Set(java.util.Set) PolicyEntity(io.gravitee.rest.api.model.PolicyEntity) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 4 with PolicyEntity

use of io.gravitee.rest.api.model.PolicyEntity in project gravitee-management-rest-api by gravitee-io.

the class PoliciesResourceTest method shouldGetPoliciesListWithSchema.

@Test
public void shouldGetPoliciesListWithSchema() {
    HashSet<PolicyEntity> policyEntities = new HashSet<>();
    PolicyEntity policyEntity = new PolicyEntity();
    policyEntities.add(policyEntity);
    policyEntity.setId("my-api");
    when(policyService.findAll(null)).thenReturn(policyEntities);
    when(policyService.getSchema(any())).thenReturn("policy schema");
    final Response response = envTarget().queryParam("expand", "schema").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    Set entity = response.readEntity(Set.class);
    assertFalse("not empty", entity.isEmpty());
    assertEquals("one element", 1, entity.size());
    Object o = entity.iterator().next();
    assertTrue(o instanceof LinkedHashMap);
    LinkedHashMap<String, String> elt = (LinkedHashMap<String, String>) o;
    assertEquals("id", "my-api", elt.get("id"));
    assertEquals("schema", "policy schema", elt.get("schema"));
}
Also used : Response(javax.ws.rs.core.Response) HashSet(java.util.HashSet) Set(java.util.Set) PolicyEntity(io.gravitee.rest.api.model.PolicyEntity) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with PolicyEntity

use of io.gravitee.rest.api.model.PolicyEntity in project gravitee-management-rest-api by gravitee-io.

the class PolicyServiceImpl method convert.

private PolicyEntity convert(PolicyPlugin policyPlugin, Boolean withPlugin) {
    PolicyEntity entity = new PolicyEntity();
    entity.setId(policyPlugin.id());
    entity.setDescription(policyPlugin.manifest().description());
    entity.setName(policyPlugin.manifest().name());
    entity.setVersion(policyPlugin.manifest().version());
    entity.setCategory(policyPlugin.manifest().category());
    if (withPlugin) {
        // Plugin information
        PluginEntity pluginEntity = new PluginEntity();
        pluginEntity.setPlugin(policyPlugin.clazz());
        pluginEntity.setPath(policyPlugin.path().toString());
        pluginEntity.setType(((Plugin) policyPlugin).type().toLowerCase());
        pluginEntity.setDependencies(policyPlugin.dependencies());
        entity.setPlugin(pluginEntity);
        entity.setDevelopment(loadPolicy(policyPlugin));
    }
    return entity;
}
Also used : PluginEntity(io.gravitee.rest.api.model.PluginEntity) PolicyEntity(io.gravitee.rest.api.model.PolicyEntity) Plugin(io.gravitee.plugin.core.api.Plugin) PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin)

Aggregations

PolicyEntity (io.gravitee.rest.api.model.PolicyEntity)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 LinkedHashMap (java.util.LinkedHashMap)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonLoader (com.github.fge.jackson.JsonLoader)1 HttpMethod (io.gravitee.common.http.HttpMethod)1 CONNECT (io.gravitee.common.http.HttpMethod.CONNECT)1 DELETE (io.gravitee.common.http.HttpMethod.DELETE)1 GET (io.gravitee.common.http.HttpMethod.GET)1 HEAD (io.gravitee.common.http.HttpMethod.HEAD)1 OPTIONS (io.gravitee.common.http.HttpMethod.OPTIONS)1 PATCH (io.gravitee.common.http.HttpMethod.PATCH)1 POST (io.gravitee.common.http.HttpMethod.POST)1 PUT (io.gravitee.common.http.HttpMethod.PUT)1 TRACE (io.gravitee.common.http.HttpMethod.TRACE)1 DefinitionVersion (io.gravitee.definition.model.DefinitionVersion)1 FlowMode (io.gravitee.definition.model.FlowMode)1