Search in sources :

Example 1 with OAIOperationVisitor

use of io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor in project gravitee-management-rest-api by gravitee-io.

the class OAIToAPIV2Converter method fill.

@Override
protected SwaggerApiEntity fill(SwaggerApiEntity apiEntity, OpenAPI oai) {
    // flows
    apiEntity.setGraviteeDefinitionVersion(DefinitionVersion.V2.getLabel());
    if (swaggerDescriptor.isWithPolicyPaths()) {
        List<Flow> allFlows = new ArrayList();
        oai.getPaths().forEach((key, pathItem) -> {
            String path = key.replaceAll("\\{(.[^/\\}]*)\\}", ":$1");
            Map<PathItem.HttpMethod, Operation> operations = pathItem.readOperationsMap();
            operations.forEach((httpMethod, operation) -> {
                final Flow flow = createFlow(path, Collections.singleton(HttpMethod.valueOf(httpMethod.name())));
                getVisitors().forEach((Consumer<OAIOperationVisitor>) oaiOperationVisitor -> {
                    Optional<Policy> policy = (Optional<Policy>) oaiOperationVisitor.visit(oai, operation);
                    if (policy.isPresent()) {
                        final Step step = new Step();
                        step.setName(policy.get().getName());
                        step.setEnabled(true);
                        step.setDescription(operation.getSummary() == null ? (operation.getOperationId() == null ? operation.getDescription() : operation.getOperationId()) : operation.getSummary());
                        step.setPolicy(policy.get().getName());
                        String configuration = clearNullValues(policy.get().getConfiguration());
                        step.setConfiguration(configuration);
                        String scope = getScope(configuration);
                        if (scope != null && scope.toLowerCase().equals("response")) {
                            flow.getPost().add(step);
                        } else {
                            flow.getPre().add(step);
                        }
                    }
                });
                allFlows.add(flow);
            });
        });
        apiEntity.setFlows(allFlows);
        if (swaggerDescriptor.isWithPathMapping() && allFlows.size() > 0) {
            apiEntity.setPathMappings(allFlows.stream().map(flow -> flow.getPath()).collect(Collectors.toSet()));
        }
    }
    final String defaultDeclaredPath = "/";
    if (!swaggerDescriptor.isWithPathMapping()) {
        apiEntity.setPathMappings(singleton(defaultDeclaredPath));
    }
    return apiEntity;
}
Also used : Step(io.gravitee.definition.model.flow.Step) GroupService(io.gravitee.rest.api.service.GroupService) java.util(java.util) PathOperator(io.gravitee.definition.model.flow.PathOperator) Flow(io.gravitee.definition.model.flow.Flow) PathItem(io.swagger.v3.oas.models.PathItem) Operation(io.swagger.v3.oas.models.Operation) PolicyOperationVisitorManager(io.gravitee.rest.api.service.impl.swagger.policy.PolicyOperationVisitorManager) Collectors(java.util.stream.Collectors) Operator(io.gravitee.definition.model.flow.Operator) OAIOperationVisitor(io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor) Consumer(java.util.function.Consumer) ImportSwaggerDescriptorEntity(io.gravitee.rest.api.model.ImportSwaggerDescriptorEntity) PolicyHelper.clearNullValues(io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues) Collections.singleton(java.util.Collections.singleton) HttpMethod(io.gravitee.common.http.HttpMethod) DefinitionVersion(io.gravitee.definition.model.DefinitionVersion) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SwaggerApiEntity(io.gravitee.rest.api.model.api.SwaggerApiEntity) PolicyHelper.getScope(io.gravitee.rest.api.service.validator.PolicyHelper.getScope) Policy(io.gravitee.policy.api.swagger.Policy) TagService(io.gravitee.rest.api.service.TagService) Policy(io.gravitee.policy.api.swagger.Policy) Operation(io.swagger.v3.oas.models.Operation) Step(io.gravitee.definition.model.flow.Step) OAIOperationVisitor(io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor) HttpMethod(io.gravitee.common.http.HttpMethod) Flow(io.gravitee.definition.model.flow.Flow)

Example 2 with OAIOperationVisitor

use of io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor in project gravitee-management-rest-api by gravitee-io.

the class SwaggerService_CreateAPITest method setup.

@Before
public void setup() {
    PolicyOperationVisitor swaggerPolicyOperationVisitor = mock(PolicyOperationVisitor.class);
    when(swaggerPolicyOperationVisitor.getId()).thenReturn("mock");
    PolicyOperationVisitor oaiPolicyOperationVisitor = mock(PolicyOperationVisitor.class);
    when(oaiPolicyOperationVisitor.getId()).thenReturn("mock");
    io.gravitee.policy.api.swagger.v3.OAIOperationVisitor oaiPolicyOperationVisitorImpl = mock(io.gravitee.policy.api.swagger.v3.OAIOperationVisitor.class);
    when(policyOperationVisitorManager.getPolicyVisitors()).thenReturn(asList(swaggerPolicyOperationVisitor, oaiPolicyOperationVisitor));
    OAIOperationVisitor op = mock(OAIPolicyOperationVisitor.class);
    when(op.visit(any(), any())).thenReturn(Optional.of(new Policy()));
    when(policyOperationVisitorManager.getOAIOperationVisitor(anyString())).thenReturn(op);
    GroupEntity grp1 = new GroupEntity();
    grp1.setId("group1");
    GroupEntity grp2 = new GroupEntity();
    grp2.setId("group2");
    when(groupService.findByName("group1")).thenReturn(Arrays.asList(grp1));
    when(groupService.findByName("group2")).thenReturn(Arrays.asList(grp2));
    TagEntity tag1 = new TagEntity();
    tag1.setId("tagId1");
    tag1.setName("tag1");
    TagEntity tag2 = new TagEntity();
    tag2.setId("tagId2");
    tag2.setName("tag2");
    when(tagService.findByReference(any(), any())).thenReturn(Arrays.asList(tag1, tag2));
}
Also used : Policy(io.gravitee.policy.api.swagger.Policy) OAIPolicyOperationVisitor(io.gravitee.rest.api.service.impl.swagger.policy.impl.OAIPolicyOperationVisitor) PolicyOperationVisitor(io.gravitee.rest.api.service.impl.swagger.policy.PolicyOperationVisitor) OAIOperationVisitor(io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor) Before(org.junit.Before)

Aggregations

Policy (io.gravitee.policy.api.swagger.Policy)2 OAIOperationVisitor (io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor)2 HttpMethod (io.gravitee.common.http.HttpMethod)1 DefinitionVersion (io.gravitee.definition.model.DefinitionVersion)1 Flow (io.gravitee.definition.model.flow.Flow)1 Operator (io.gravitee.definition.model.flow.Operator)1 PathOperator (io.gravitee.definition.model.flow.PathOperator)1 Step (io.gravitee.definition.model.flow.Step)1 ImportSwaggerDescriptorEntity (io.gravitee.rest.api.model.ImportSwaggerDescriptorEntity)1 SwaggerApiEntity (io.gravitee.rest.api.model.api.SwaggerApiEntity)1 GroupService (io.gravitee.rest.api.service.GroupService)1 TagService (io.gravitee.rest.api.service.TagService)1 PolicyOperationVisitor (io.gravitee.rest.api.service.impl.swagger.policy.PolicyOperationVisitor)1 PolicyOperationVisitorManager (io.gravitee.rest.api.service.impl.swagger.policy.PolicyOperationVisitorManager)1 OAIPolicyOperationVisitor (io.gravitee.rest.api.service.impl.swagger.policy.impl.OAIPolicyOperationVisitor)1 PolicyHelper.clearNullValues (io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues)1 PolicyHelper.getScope (io.gravitee.rest.api.service.validator.PolicyHelper.getScope)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 Operation (io.swagger.v3.oas.models.Operation)1 PathItem (io.swagger.v3.oas.models.PathItem)1