Search in sources :

Example 21 with Operation

use of io.github.microcks.domain.Operation in project microcks by microcks.

the class ServiceService method copyOverridenOperations.

/**
 * Recopy overriden operation mutable properties into newService.
 */
private void copyOverridenOperations(Service existingService, Service newService) {
    for (Operation existingOperation : existingService.getOperations()) {
        if (existingOperation.hasOverride()) {
            for (Operation op : newService.getOperations()) {
                if (existingOperation.getName().equals(op.getName())) {
                    op.setDefaultDelay(existingOperation.getDefaultDelay());
                    op.setDispatcher(existingOperation.getDispatcher());
                    op.setDispatcherRules(existingOperation.getDispatcherRules());
                    op.setParameterConstraints(existingOperation.getParameterConstraints());
                    op.setOverride(true);
                }
            }
        }
    }
}
Also used : Operation(io.github.microcks.domain.Operation)

Example 22 with Operation

use of io.github.microcks.domain.Operation in project microcks by microcks.

the class AsyncAPIImporter method getResourceDefinitions.

@Override
public List<Resource> getResourceDefinitions(Service service) {
    List<Resource> results = new ArrayList<>();
    // Build a suitable name.
    String name = service.getName() + "-" + service.getVersion();
    if (isYaml) {
        name += ".yaml";
    } else {
        name += ".json";
    }
    // Build a brand new resource just with spec content.
    Resource resource = new Resource();
    resource.setName(name);
    resource.setType(ResourceType.ASYNC_API_SPEC);
    resource.setContent(specContent);
    results.add(resource);
    // references for external schemas only if we have a resolver available.
    if (referenceResolver != null) {
        for (Operation operation : service.getOperations()) {
            String[] operationElements = operation.getName().split(" ");
            String messageNamePtr = "/channels/" + operationElements[1].replaceAll("/", "~1");
            messageNamePtr += "/" + operationElements[0].toLowerCase() + "/message";
            JsonNode messageNode = spec.at(messageNamePtr);
            if (messageNode != null) {
                // If it's a $ref, then navigate to it.
                messageNode = followRefIfAny(messageNode);
                // Extract payload schema here.
                if (messageNode.has("payload")) {
                    JsonNode payloadNode = messageNode.path("payload");
                    // Check we have a reference that is not a local one.
                    if (payloadNode.has("$ref") && !payloadNode.path("$ref").asText().startsWith("#")) {
                        String ref = payloadNode.path("$ref").asText();
                        // to indicate exact Resource)
                        if (ref.contains("#")) {
                            ref = ref.substring(0, ref.indexOf("#"));
                        }
                        try {
                            // Extract content using resolver.
                            String content = referenceResolver.getHttpReferenceContent(ref, "UTF-8");
                            // Build a new resource from content. Use the escaped operation path.
                            Resource schemaResource = new Resource();
                            schemaResource.setName(IdBuilder.buildResourceFullName(service, operation));
                            schemaResource.setPath(ref);
                            schemaResource.setContent(content);
                            // We have to look at schema format to know the type.
                            // Default value is set at first.
                            String schemaFormat = "application/vnd.aai.asyncapi";
                            if (messageNode.has("schemaFormat")) {
                                schemaFormat = messageNode.path("schemaFormat").asText();
                            }
                            if (schemaFormat.startsWith("application/vnd.aai.asyncapi")) {
                                schemaResource.setType(ResourceType.ASYNC_API_SCHEMA);
                            } else if (schemaFormat.startsWith("application/vnd.oai.openapi")) {
                                schemaResource.setType(ResourceType.OPEN_API_SCHEMA);
                            } else if (schemaFormat.startsWith("application/schema+json") || schemaFormat.startsWith("application/schema+yaml")) {
                                schemaResource.setType(ResourceType.JSON_SCHEMA);
                            } else if (schemaFormat.startsWith("application/vnd.apache.avro")) {
                                schemaResource.setType(ResourceType.AVRO_SCHEMA);
                            }
                            results.add(schemaResource);
                        } catch (IOException ioe) {
                            log.error("IOException while trying to resolve reference " + ref, ioe);
                            log.info("Ignoring the reference {} cause it could not be resolved", ref);
                        }
                    }
                }
            }
        }
        // Finally try to clean up resolved references and associated resources (files)
        referenceResolver.cleanResolvedReferences();
    }
    return results;
}
Also used : Resource(io.github.microcks.domain.Resource) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Operation(io.github.microcks.domain.Operation) IOException(java.io.IOException)

Example 23 with Operation

use of io.github.microcks.domain.Operation in project microcks by microcks.

the class ProtobufImporter method extractOperations.

/**
 * Extract the operations from GRPC service methods.
 */
private List<Operation> extractOperations(DescriptorProtos.ServiceDescriptorProto service) {
    List<Operation> results = new ArrayList<>();
    for (DescriptorProtos.MethodDescriptorProto method : service.getMethodList()) {
        Operation operation = new Operation();
        operation.setName(method.getName());
        operation.setInputName(method.getInputType());
        operation.setOutputName(method.getOutputType());
        results.add(operation);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) DescriptorProtos(com.google.protobuf.DescriptorProtos) Operation(io.github.microcks.domain.Operation)

Example 24 with Operation

use of io.github.microcks.domain.Operation in project microcks by microcks.

the class MetadataImporter method extractOperations.

/**
 * Extract the list of operations from Specification.
 */
private List<Operation> extractOperations() throws MockRepositoryImportException {
    List<Operation> results = new ArrayList<>();
    // Iterate on specification "operations" nodes.
    Iterator<Map.Entry<String, JsonNode>> operations = spec.path("operations").fields();
    while (operations.hasNext()) {
        Map.Entry<String, JsonNode> operation = operations.next();
        // Build a new operation.
        Operation op = new Operation();
        op.setName(operation.getKey());
        JsonNode operationValue = operation.getValue();
        MetadataExtractor.completeOperationProperties(op, operationValue);
        results.add(op);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Operation(io.github.microcks.domain.Operation) Map(java.util.Map)

Example 25 with Operation

use of io.github.microcks.domain.Operation in project microcks by microcks.

the class PostmanCollectionImporter method extractOperationsV2.

private List<Operation> extractOperationsV2() {
    // Items corresponding to same operations may be defined multiple times in Postman
    // with different names and resource path. We have to track them to complete them in second step.
    Map<String, Operation> collectedOperations = new HashMap<String, Operation>();
    Iterator<JsonNode> items = collection.path("item").elements();
    while (items.hasNext()) {
        JsonNode item = items.next();
        extractOperationV2("", item, collectedOperations);
    }
    return new ArrayList<>(collectedOperations.values());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Operation(io.github.microcks.domain.Operation)

Aggregations

Operation (io.github.microcks.domain.Operation)26 Service (io.github.microcks.domain.Service)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 MockRepositoryImportException (io.github.microcks.util.MockRepositoryImportException)6 Resource (io.github.microcks.domain.Resource)5 UnidirectionalEvent (io.github.microcks.domain.UnidirectionalEvent)5 Exchange (io.github.microcks.domain.Exchange)4 RequestResponsePair (io.github.microcks.domain.RequestResponsePair)4 Response (io.github.microcks.domain.Response)4 ServiceType (io.github.microcks.domain.ServiceType)4 Test (org.junit.Test)4 Header (io.github.microcks.domain.Header)3 ServiceView (io.github.microcks.domain.ServiceView)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 FieldDefinition (graphql.language.FieldDefinition)2 InputValueDefinition (graphql.language.InputValueDefinition)2 Metadata (io.github.microcks.domain.Metadata)2