Search in sources :

Example 1 with Parameter

use of io.github.microcks.domain.Parameter 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 2 with Parameter

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

the class TestRunnerCommons method renderRequestContent.

/**
 * Render the request content using the Expression Language compatible {@code TemplateEngine} if required.
 * If rendering template fails, we just produce a log error message and stick to templatized content.
 * @param request The request that will be sent for test.
 * @param headers The set of computed headers to use for request body evaluation
 * @return The rendered response body payload.
 */
public static String renderRequestContent(Request request, Set<Header> headers) {
    if (request.getContent().contains(TemplateEngine.DEFAULT_EXPRESSION_PREFIX)) {
        log.debug("Response contains dynamic EL expression, rendering it...");
        TemplateEngine engine = TemplateEngineFactory.getTemplateEngine();
        // Create and fill an evaluable request object.
        EvaluableRequest evaluableRequest = new EvaluableRequest(request.getContent(), null);
        // Adding query parameters...
        Map<String, String> evaluableParams = new HashMap<>();
        for (Parameter parameter : request.getQueryParameters()) {
            evaluableParams.put(parameter.getName(), parameter.getValue());
        }
        evaluableRequest.setParams(evaluableParams);
        // Adding headers...
        Map<String, String> evaluableHeaders = new HashMap<>();
        for (Header header : request.getHeaders()) {
            evaluableHeaders.put(header.getName(), String.join(",", header.getValues()));
        }
        evaluableRequest.setHeaders(evaluableHeaders);
        // Register the request variable and evaluate the request body.
        engine.getContext().setVariable("request", evaluableRequest);
        try {
            return engine.getValue(request.getContent());
        } catch (Throwable t) {
            log.error("Failing at evaluating template " + request.getContent(), t);
            return request.getContent();
        }
    }
    return request.getContent();
}
Also used : TemplateEngine(io.github.microcks.util.el.TemplateEngine) Header(io.github.microcks.domain.Header) HashMap(java.util.HashMap) Parameter(io.github.microcks.domain.Parameter) EvaluableRequest(io.github.microcks.util.el.EvaluableRequest)

Example 3 with Parameter

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

the class PostmanTestStepsRunner method buildQueryParams.

private ArrayNode buildQueryParams(List<Parameter> queryParameters) {
    ArrayNode jsonQPS = mapper.createArrayNode();
    for (Parameter parameter : queryParameters) {
        ObjectNode jsonQP = mapper.createObjectNode();
        jsonQP.put("key", parameter.getName());
        jsonQP.put("value", parameter.getValue());
        jsonQPS.add(jsonQP);
    }
    return jsonQPS;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Parameter(io.github.microcks.domain.Parameter) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with Parameter

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

the class URIBuilderTest method testBuildURIFromPatternWithEncoding.

@Test
public void testBuildURIFromPatternWithEncoding() {
    // Prepare a bunch of parameters.
    Parameter nameParam = new Parameter();
    nameParam.setName("name");
    nameParam.setValue("Eclair Cafe");
    Parameter descriptionParam = new Parameter();
    descriptionParam.setName("description");
    descriptionParam.setValue("My desc");
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(nameParam);
    parameters.add(descriptionParam);
    // Test with old wadl like template format.
    String pattern = "http://localhost:8080/pastry/{name}";
    String uri = URIBuilder.buildURIFromPattern(pattern, parameters);
    assertTrue("http://localhost:8080/pastry/Eclair%20Cafe?description=My+desc".equals(uri));
    // Test with new swagger like template format.
    pattern = "http://localhost:8080/pastry/:name";
    uri = URIBuilder.buildURIFromPattern(pattern, parameters);
    assertTrue("http://localhost:8080/pastry/Eclair%20Cafe?description=My+desc".equals(uri));
}
Also used : ArrayList(java.util.ArrayList) Parameter(io.github.microcks.domain.Parameter) Test(org.junit.Test)

Example 5 with Parameter

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

the class URIBuilderTest method testBuildURIFromPattern.

@Test
public void testBuildURIFromPattern() {
    // Prepare a bunch of parameters.
    Parameter yearParam = new Parameter();
    yearParam.setName("year");
    yearParam.setValue("2017");
    Parameter monthParam = new Parameter();
    monthParam.setName("month");
    monthParam.setValue("08");
    Parameter statusParam = new Parameter();
    statusParam.setName("status");
    statusParam.setValue("published");
    Parameter pageParam = new Parameter();
    pageParam.setName("page");
    pageParam.setValue("0");
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(yearParam);
    parameters.add(monthParam);
    parameters.add(statusParam);
    parameters.add(pageParam);
    // Test with old wadl like template format.
    String pattern = "http://localhost:8080/blog/{year}/{month}";
    String uri = URIBuilder.buildURIFromPattern(pattern, parameters);
    assertTrue("http://localhost:8080/blog/2017/08?page=0&status=published".equals(uri) || "http://localhost:8080/blog/2017/08?status=published&page=0".equals(uri));
    // Test with new swagger like template format.
    pattern = "http://localhost:8080/blog/:year/:month";
    uri = URIBuilder.buildURIFromPattern(pattern, parameters);
    assertTrue("http://localhost:8080/blog/2017/08?page=0&status=published".equals(uri) || "http://localhost:8080/blog/2017/08?status=published&page=0".equals(uri));
}
Also used : ArrayList(java.util.ArrayList) Parameter(io.github.microcks.domain.Parameter) Test(org.junit.Test)

Aggregations

Parameter (io.github.microcks.domain.Parameter)6 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Header (io.github.microcks.domain.Header)1 Request (io.github.microcks.domain.Request)1 EvaluableRequest (io.github.microcks.util.el.EvaluableRequest)1 TemplateEngine (io.github.microcks.util.el.TemplateEngine)1 HashMap (java.util.HashMap)1