Search in sources :

Example 1 with Request

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

the class ServiceServiceTest method testImportServiceDefinition.

@Test
public void testImportServiceDefinition() {
    List<Service> services = null;
    try {
        File artifactFile = new File("target/test-classes/io/github/microcks/service/weather-forecast-openapi.yaml");
        services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("weather-forecast-openapi.yaml", true));
    } catch (MockRepositoryImportException mrie) {
        mrie.printStackTrace();
        fail("No MockRepositoryImportException should have be thrown");
    }
    assertNotNull(services);
    assertEquals(1, services.size());
    // Inspect Service own attributes.
    Service importedSvc = services.get(0);
    assertEquals("WeatherForecast API", importedSvc.getName());
    assertEquals("1.0.0", importedSvc.getVersion());
    assertEquals("weather-forecast-openapi.yaml", importedSvc.getSourceArtifact());
    assertNotNull(importedSvc.getMetadata());
    assertEquals(1, importedSvc.getOperations().size());
    assertEquals("GET /forecast/{region}", importedSvc.getOperations().get(0).getName());
    assertEquals(5, importedSvc.getOperations().get(0).getResourcePaths().size());
    // Inspect and check resources.
    List<Resource> resources = resourceRepository.findByServiceId(importedSvc.getId());
    assertEquals(1, resources.size());
    Resource resource = resources.get(0);
    assertEquals("WeatherForecast API-1.0.0.yaml", resource.getName());
    assertEquals("weather-forecast-openapi.yaml", resource.getSourceArtifact());
    // Inspect and check requests.
    List<Request> requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(5, requests.size());
    for (Request request : requests) {
        assertEquals("weather-forecast-openapi.yaml", request.getSourceArtifact());
    }
    // Inspect and check responses.
    List<Response> responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(5, requests.size());
    for (Response response : responses) {
        assertEquals("weather-forecast-openapi.yaml", response.getSourceArtifact());
    }
}
Also used : Response(io.github.microcks.domain.Response) Resource(io.github.microcks.domain.Resource) Request(io.github.microcks.domain.Request) Service(io.github.microcks.domain.Service) File(java.io.File) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) Test(org.junit.Test)

Example 2 with Request

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

the class ServiceServiceTest method testImportServiceDefinitionMainGraphQLAndSecondaryPostman.

@Test
public void testImportServiceDefinitionMainGraphQLAndSecondaryPostman() {
    List<Service> services = null;
    try {
        File artifactFile = new File("target/test-classes/io/github/microcks/util/graphql/films.graphql");
        services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("films.graphql", true));
    } catch (MockRepositoryImportException mrie) {
        mrie.printStackTrace();
        fail("No MockRepositoryImportException should have be thrown");
    }
    assertNotNull(services);
    assertEquals(1, services.size());
    // Inspect Service own attributes.
    Service importedSvc = services.get(0);
    assertEquals("Movie Graph API", importedSvc.getName());
    assertEquals("1.0", importedSvc.getVersion());
    assertEquals("films.graphql", importedSvc.getSourceArtifact());
    assertNotNull(importedSvc.getMetadata());
    assertEquals(4, importedSvc.getOperations().size());
    // Inspect and check requests.
    List<Request> requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(0, requests.size());
    // Inspect and check responses.
    List<Response> responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(0, responses.size());
    try {
        File artifactFile = new File("target/test-classes/io/github/microcks/util/graphql/films-postman.json");
        services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("films-postman.json", false));
    } catch (MockRepositoryImportException mrie) {
        mrie.printStackTrace();
        fail("No MockRepositoryImportException should have be thrown");
    }
    // Inspect Service own attributes.
    importedSvc = services.get(0);
    assertEquals("Movie Graph API", importedSvc.getName());
    assertEquals("1.0", importedSvc.getVersion());
    assertEquals("films.graphql", importedSvc.getSourceArtifact());
    assertNotNull(importedSvc.getMetadata());
    assertEquals(4, importedSvc.getOperations().size());
    // Inspect and check requests.
    requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(1, requests.size());
    for (Request request : requests) {
        assertEquals("films-postman.json", request.getSourceArtifact());
    }
    // Inspect and check responses.
    responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(1, requests.size());
    for (Response response : responses) {
        assertEquals("films-postman.json", response.getSourceArtifact());
    }
}
Also used : Response(io.github.microcks.domain.Response) Request(io.github.microcks.domain.Request) Service(io.github.microcks.domain.Service) File(java.io.File) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) Test(org.junit.Test)

Example 3 with Request

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

the class EvaluationContextTest method testVariableRegistrationAndLookup.

@Test
public void testVariableRegistrationAndLookup() throws Exception {
    // Create a context, register and retrieve a variable.
    EvaluationContext context = new EvaluationContext();
    context.setVariable("request", new Request());
    Object requestObj = context.lookupVariable("request");
    assertTrue(requestObj instanceof Request);
}
Also used : Request(io.github.microcks.domain.Request) Test(org.junit.Test)

Example 4 with Request

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

the class PostmanCollectionImporter method buildRequest.

private Request buildRequest(JsonNode requestNode, String name) {
    Request request = new Request();
    request.setName(name);
    if (isV2Collection) {
        request.setHeaders(buildHeaders(requestNode.path("header")));
        if (requestNode.has("body") && requestNode.path("body").has("raw")) {
            request.setContent(requestNode.path("body").path("raw").asText());
        } else if (requestNode.has("body") && requestNode.path("body").has("graphql")) {
            String content = requestNode.path("body").path("graphql").path("query").asText();
            String variables = requestNode.path("body").path("graphql").path("variables").asText();
            content = replaceGraphQLVariables(content, variables);
            request.setContent(content);
        }
        if (requestNode.path("url").has("variable")) {
            JsonNode variablesNode = requestNode.path("url").path("variable");
            for (JsonNode variableNode : variablesNode) {
                Parameter param = new Parameter();
                param.setName(variableNode.path("key").asText());
                param.setValue(variableNode.path("value").asText());
                request.addQueryParameter(param);
            }
        }
        if (requestNode.path("url").has("query")) {
            JsonNode queryNode = requestNode.path("url").path("query");
            for (JsonNode variableNode : queryNode) {
                Parameter param = new Parameter();
                param.setName(variableNode.path("key").asText());
                param.setValue(variableNode.path("value").asText());
                request.addQueryParameter(param);
            }
        }
    } else {
        request.setHeaders(buildHeaders(requestNode.path("headers")));
    }
    return request;
}
Also used : Request(io.github.microcks.domain.Request) Parameter(io.github.microcks.domain.Parameter) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with Request

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

the class PostmanCollectionImporter method getMessageDefinitionsV2.

private Map<Request, Response> getMessageDefinitionsV2(String folderName, JsonNode itemNode, Operation operation) {
    log.debug("Extracting message definitions in folder " + folderName);
    Map<Request, Response> result = new HashMap<Request, Response>();
    String itemNodeName = itemNode.path("name").asText();
    if (!itemNode.has("request")) {
        // Item is simply a folder that may contain some other folders recursively.
        Iterator<JsonNode> items = itemNode.path("item").elements();
        while (items.hasNext()) {
            JsonNode item = items.next();
            result.putAll(getMessageDefinitionsV2(folderName + "/" + itemNodeName, item, operation));
        }
    } else {
        // Item is here an operation description.
        String operationName = buildOperationName(itemNode, folderName);
        // Select item based onto operation name.
        if (areOperationsEquivalent(operation.getName(), operationName)) {
            Iterator<JsonNode> responses = itemNode.path("response").elements();
            while (responses.hasNext()) {
                JsonNode responseNode = responses.next();
                JsonNode requestNode = responseNode.path("originalRequest");
                // Build dispatchCriteria and complete operation resourcePaths.
                String dispatchCriteria = null;
                String requestUrl = requestNode.path("url").path("raw").asText();
                if (DispatchStyles.URI_PARAMS.equals(operation.getDispatcher())) {
                    dispatchCriteria = DispatchCriteriaHelper.extractFromURIParams(operation.getDispatcherRules(), requestUrl);
                    // We only need the pattern here.
                    operation.addResourcePath(extractResourcePath(requestUrl, null));
                } else if (DispatchStyles.URI_PARTS.equals(operation.getDispatcher())) {
                    Map<String, String> parts = buildRequestParts(requestNode);
                    dispatchCriteria = DispatchCriteriaHelper.buildFromPartsMap(parts);
                    // We should complete resourcePath here.
                    String resourcePath = extractResourcePath(requestUrl, null);
                    operation.addResourcePath(URIBuilder.buildURIFromPattern(resourcePath, parts));
                } else if (DispatchStyles.URI_ELEMENTS.equals(operation.getDispatcher())) {
                    Map<String, String> parts = buildRequestParts(requestNode);
                    dispatchCriteria = DispatchCriteriaHelper.buildFromPartsMap(parts);
                    dispatchCriteria += DispatchCriteriaHelper.extractFromURIParams(operation.getDispatcherRules(), requestUrl);
                    // We should complete resourcePath here.
                    String resourcePath = extractResourcePath(requestUrl, null);
                    operation.addResourcePath(URIBuilder.buildURIFromPattern(resourcePath, parts));
                } else if (DispatchStyles.QUERY_ARGS.equals(operation.getDispatcher())) {
                    // This dispatcher is used for GraphQL
                    if (requestNode.path("body").has("graphql")) {
                        // We must transform JSON representing variables into a Map<String, String>
                        // before building a dispatchCriteria matching the rules.
                        String variables = requestNode.path("body").path("graphql").path("variables").asText();
                        dispatchCriteria = extractGraphQLCriteria(operation.getDispatcherRules(), variables);
                    }
                }
                Request request = buildRequest(requestNode, responseNode.path("name").asText());
                Response response = buildResponse(responseNode, dispatchCriteria);
                result.put(request, response);
            }
        }
    }
    return result;
}
Also used : Response(io.github.microcks.domain.Response) Request(io.github.microcks.domain.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

Request (io.github.microcks.domain.Request)13 Response (io.github.microcks.domain.Response)8 ArrayList (java.util.ArrayList)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Resource (io.github.microcks.domain.Resource)4 Service (io.github.microcks.domain.Service)4 TestReturn (io.github.microcks.domain.TestReturn)4 MockRepositoryImportException (io.github.microcks.util.MockRepositoryImportException)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 Header (io.github.microcks.domain.Header)3 File (java.io.File)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Operation (io.github.microcks.domain.Operation)2 Parameter (io.github.microcks.domain.Parameter)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashSet (java.util.HashSet)2 PageRequest (org.springframework.data.domain.PageRequest)2 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)2