Search in sources :

Example 6 with Request

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

the class PostmanCollectionImporter method getMessageDefinitionsV2.

private List<Exchange> getMessageDefinitionsV2(Service service, Operation operation) {
    Map<Request, Response> result = new HashMap<Request, Response>();
    Iterator<JsonNode> items = collection.path("item").elements();
    while (items.hasNext()) {
        JsonNode item = items.next();
        result.putAll(getMessageDefinitionsV2("", item, operation));
    }
    // Adapt map to list of Exchanges.
    return result.entrySet().stream().map(entry -> new RequestResponsePair(entry.getKey(), entry.getValue())).collect(Collectors.toList());
}
Also used : Response(io.github.microcks.domain.Response) java.util(java.util) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) LoggerFactory(org.slf4j.LoggerFactory) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DispatchCriteriaHelper(io.github.microcks.util.DispatchCriteriaHelper) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) Exchange(io.github.microcks.domain.Exchange) MockRepositoryImporter(io.github.microcks.util.MockRepositoryImporter) JsonNode(com.fasterxml.jackson.databind.JsonNode) Resource(io.github.microcks.domain.Resource) Parameter(io.github.microcks.domain.Parameter) Response(io.github.microcks.domain.Response) DispatchStyles(io.github.microcks.util.DispatchStyles) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) Request(io.github.microcks.domain.Request) URIBuilder(io.github.microcks.util.URIBuilder) Logger(org.slf4j.Logger) PatternSyntaxException(java.util.regex.PatternSyntaxException) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Service(io.github.microcks.domain.Service) Header(io.github.microcks.domain.Header) Operation(io.github.microcks.domain.Operation) ServiceType(io.github.microcks.domain.ServiceType) Paths(java.nio.file.Paths) JsonWriteFeature(com.fasterxml.jackson.core.json.JsonWriteFeature) RequestResponsePair(io.github.microcks.domain.RequestResponsePair) RequestResponsePair(io.github.microcks.domain.RequestResponsePair) Request(io.github.microcks.domain.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 7 with Request

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

the class PostmanTestStepsRunner method runTest.

@Override
public List<TestReturn> runTest(Service service, Operation operation, TestResult testResult, List<Request> requests, String endpointUrl, HttpMethod method) throws URISyntaxException, IOException {
    if (log.isDebugEnabled()) {
        log.debug("Launching test run on " + endpointUrl + " for " + requests.size() + " request(s)");
    }
    if (endpointUrl.endsWith("/")) {
        endpointUrl = endpointUrl.substring(0, endpointUrl.length() - 1);
    }
    // Microcks-postman-runner interface object building.
    ObjectNode jsonArg = mapper.createObjectNode();
    jsonArg.put("operation", operation.getName());
    jsonArg.put("callbackUrl", testsCallbackUrl + "/api/tests/" + testResult.getId() + "/testCaseResult");
    // First we have to retrieved and add the test script for this operation from within Postman collection.
    JsonNode testScript = extractOperationTestScript(operation);
    if (testScript != null) {
        log.debug("Found a testScript for this operation !");
        jsonArg.set("testScript", testScript);
    }
    // Then we have to add the corresponding 'requests' objects.
    ArrayNode jsonRequests = mapper.createArrayNode();
    for (Request request : requests) {
        ObjectNode jsonRequest = mapper.createObjectNode();
        String operationName = operation.getName().substring(operation.getName().indexOf(" ") + 1);
        String customizedEndpointUrl = endpointUrl + URIBuilder.buildURIFromPattern(operationName, request.getQueryParameters());
        log.debug("Using customized endpoint url: " + customizedEndpointUrl);
        jsonRequest.put("endpointUrl", customizedEndpointUrl);
        jsonRequest.put("method", operation.getMethod());
        jsonRequest.put("name", request.getName());
        if (request.getContent() != null && request.getContent().length() > 0) {
            jsonRequest.put("body", request.getContent());
        }
        if (request.getQueryParameters() != null && request.getQueryParameters().size() > 0) {
            ArrayNode jsonParams = buildQueryParams(request.getQueryParameters());
            jsonRequest.set("queryParams", jsonParams);
        }
        // Set headers to request if any. Start with those coming from request itself.
        // Add or override existing headers with test specific ones for operation and globals.
        Set<Header> headers = new HashSet<>();
        if (request.getHeaders() != null) {
            headers.addAll(request.getHeaders());
        }
        if (testResult.getOperationsHeaders() != null) {
            if (testResult.getOperationsHeaders().getGlobals() != null) {
                headers.addAll(testResult.getOperationsHeaders().getGlobals());
            }
            if (testResult.getOperationsHeaders().get(operation.getName()) != null) {
                headers.addAll(testResult.getOperationsHeaders().get(operation.getName()));
            }
        }
        if (headers != null && headers.size() > 0) {
            ArrayNode jsonHeaders = buildHeaders(headers);
            jsonRequest.set("headers", jsonHeaders);
        }
        jsonRequests.add(jsonRequest);
    }
    jsonArg.set("requests", jsonRequests);
    URI postmanRunnerURI = new URI(postmanRunnerUrl + "/tests/" + testResult.getId());
    ClientHttpRequest httpRequest = clientHttpRequestFactory.createRequest(postmanRunnerURI, HttpMethod.POST);
    httpRequest.getBody().write(mapper.writeValueAsBytes(jsonArg));
    httpRequest.getHeaders().add("Content-Type", "application/json");
    // Actually execute request.
    ClientHttpResponse httpResponse = null;
    try {
        httpResponse = httpRequest.execute();
    } catch (IOException ioe) {
        log.error("IOException while executing request ", ioe);
    } finally {
        if (httpResponse != null) {
            httpResponse.close();
        }
    }
    return new ArrayList<>();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) Request(io.github.microcks.domain.Request) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) Header(io.github.microcks.domain.Header) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HashSet(java.util.HashSet)

Example 8 with Request

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

the class HttpTestRunner method runTest.

@Override
public List<TestReturn> runTest(Service service, Operation operation, TestResult testResult, List<Request> requests, String endpointUrl, HttpMethod method) throws URISyntaxException, IOException {
    if (log.isDebugEnabled()) {
        log.debug("Launching test run on " + endpointUrl + " for " + requests.size() + " request(s)");
    }
    if (requests.isEmpty()) {
        return null;
    }
    // Initialize result container.
    List<TestReturn> result = new ArrayList<TestReturn>();
    for (Request request : requests) {
        // Reset status code, message and request each time.
        int code = TestReturn.SUCCESS_CODE;
        String message = null;
        String customizedEndpointUrl = endpointUrl;
        if (service.getType().equals(ServiceType.REST)) {
            String operationName = operation.getName();
            // Name may start with verb, remove it if present.
            if (operationName.indexOf(' ') > 0 && operationName.indexOf(' ') < operationName.length()) {
                operationName = operationName.split(" ")[1];
            }
            customizedEndpointUrl += URIBuilder.buildURIFromPattern(operationName, request.getQueryParameters());
            log.debug("Using customized endpoint url: " + customizedEndpointUrl);
        }
        ClientHttpRequest httpRequest = clientHttpRequestFactory.createRequest(new URI(customizedEndpointUrl), method);
        // Set headers to request if any. Start with those coming from request itself.
        // Add or override existing headers with test specific ones for operation and globals.
        Set<Header> headers = new HashSet<>();
        if (request.getHeaders() != null) {
            headers.addAll(request.getHeaders());
        }
        if (testResult.getOperationsHeaders() != null) {
            if (testResult.getOperationsHeaders().getGlobals() != null) {
                headers.addAll(testResult.getOperationsHeaders().getGlobals());
            }
            if (testResult.getOperationsHeaders().get(operation.getName()) != null) {
                headers.addAll(testResult.getOperationsHeaders().get(operation.getName()));
            }
        }
        if (headers.size() > 0) {
            for (Header header : headers) {
                log.debug("Adding header " + header.getName() + " to request");
                httpRequest.getHeaders().add(header.getName(), buildValue(header.getValues()));
            }
            // Update request headers for traceability of possibly added ones.
            request.setHeaders(headers);
        }
        // Now manage specific authorization headers if there's a secret.
        if (secret != null) {
            addAuthorizationHeadersFromSecret(httpRequest, request, secret);
        }
        // Allow extensions to realize some pre-processing of request.
        prepareRequest(request);
        // If there's input content, add it to request.
        if (request.getContent() != null) {
            // Update request content with rendered body if necessary.
            request.setContent(TestRunnerCommons.renderRequestContent(request, headers));
            log.trace("Sending following request content: " + request.getContent());
            httpRequest.getBody().write(request.getContent().getBytes());
        }
        // Actually execute request.
        long startTime = System.currentTimeMillis();
        ClientHttpResponse httpResponse = null;
        try {
            httpResponse = httpRequest.execute();
        } catch (IOException ioe) {
            log.error("IOException while executing request " + request.getName() + " on " + endpointUrl, ioe);
            code = TestReturn.FAILURE_CODE;
            message = ioe.getMessage();
        }
        long duration = System.currentTimeMillis() - startTime;
        // Extract and store response body so that stream may not be consumed more than o1 time ;-)
        String responseContent = null;
        if (httpResponse != null) {
            StringWriter writer = new StringWriter();
            IOUtils.copy(httpResponse.getBody(), writer);
            responseContent = writer.toString();
        }
        // If still in success, check if http code is out of correct ranges (20x and 30x).
        if (code == TestReturn.SUCCESS_CODE) {
            code = extractTestReturnCode(service, operation, request, httpResponse, responseContent);
            message = extractTestReturnMessage(service, operation, request, httpResponse);
        }
        // Create a Response object for returning.
        Response response = new Response();
        if (httpResponse != null) {
            response.setContent(responseContent);
            response.setStatus(String.valueOf(httpResponse.getRawStatusCode()));
            log.debug("Response Content-Type: " + httpResponse.getHeaders().getContentType());
            if (httpResponse.getHeaders().getContentType() != null) {
                response.setMediaType(httpResponse.getHeaders().getContentType().toString());
            }
            headers = buildHeaders(httpResponse);
            if (headers != null) {
                response.setHeaders(headers);
            }
            httpResponse.close();
        }
        result.add(new TestReturn(code, duration, message, request, response));
    }
    return result;
}
Also used : TestReturn(io.github.microcks.domain.TestReturn) ArrayList(java.util.ArrayList) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) Request(io.github.microcks.domain.Request) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) Response(io.github.microcks.domain.Response) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Header(io.github.microcks.domain.Header) StringWriter(java.io.StringWriter) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HashSet(java.util.HashSet)

Example 9 with Request

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

the class GrpcTestRunner method runTest.

@Override
public List<TestReturn> runTest(Service service, Operation operation, TestResult testResult, List<Request> requests, String endpointUrl, HttpMethod method) throws URISyntaxException, IOException {
    log.debug("Launching test run on {} for {} request(s)", endpointUrl, requests.size());
    if (requests.isEmpty()) {
        return null;
    }
    // Initialize results.
    List<TestReturn> results = new ArrayList<>();
    // Rebuild the GRPC fullMethodName.
    String fullMethodName = service.getXmlNS() + "." + service.getName() + "/" + operation.getName();
    // Build a new GRPC Channel from endpoint URL.
    URL endpoint = new URL(endpointUrl);
    ManagedChannel channel;
    if (endpointUrl.startsWith("https://") || endpoint.getPort() == 443) {
        TlsChannelCredentials.Builder tlsBuilder = TlsChannelCredentials.newBuilder();
        if (secret != null && secret.getCaCertPem() != null) {
            // Install a trust manager with custom CA certificate.
            tlsBuilder.trustManager(new ByteArrayInputStream(secret.getCaCertPem().getBytes(StandardCharsets.UTF_8)));
        } else {
            // Install a trust manager that accepts everything and does not validate certificate chains.
            tlsBuilder.trustManager(new TrustManager[] { new X509TrustManager() {

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } });
        }
        // Build a Channel using the TLS Builder.
        channel = Grpc.newChannelBuilderForAddress(endpoint.getHost(), endpoint.getPort(), tlsBuilder.build()).build();
    } else {
        // Build a simple Channel using plain text.
        channel = Grpc.newChannelBuilderForAddress(endpoint.getHost(), endpoint.getPort(), null).usePlaintext().build();
    }
    // In order to produce outgoing byte array, we need the Protobuf binary descriptor that should
    // have been processed while importing the .proto schema for the service.
    List<Resource> resources = resourceRepository.findByServiceIdAndType(service.getId(), ResourceType.PROTOBUF_DESCRIPTOR);
    if (resources == null || resources.size() != 1) {
        log.error("Could not found any pre-processed Protobuf binary descriptor...");
        results.add(new TestReturn(TestReturn.FAILURE_CODE, 0, "Could not found any pre-processed Protobuf binary descriptor...", null, null));
        return results;
    }
    Resource pbResource = resources.get(0);
    Descriptors.MethodDescriptor md = null;
    try {
        md = GrpcUtil.findMethodDescriptor(pbResource.getContent(), service.getName(), operation.getName());
    } catch (Exception e) {
        log.error("Protobuf descriptor cannot be read or parsed: " + e.getMessage());
        results.add(new TestReturn(TestReturn.FAILURE_CODE, 0, "Protobuf descriptor cannot be read or parsed: " + e.getMessage(), null, null));
        return results;
    }
    // Use a builder for out type with a Json parser to merge content and build outMsg.
    DynamicMessage.Builder reqBuilder = DynamicMessage.newBuilder(md.getInputType());
    DynamicMessage.Builder resBuilder = DynamicMessage.newBuilder(md.getOutputType());
    JsonFormat.Parser parser = JsonFormat.parser();
    JsonFormat.Printer printer = JsonFormat.printer();
    for (Request request : requests) {
        // Reset status code, message and request each time.
        int code = TestReturn.SUCCESS_CODE;
        String message = null;
        reqBuilder.clear();
        resBuilder.clear();
        // Now produce the request message byte array.
        parser.merge(request.getContent(), reqBuilder);
        byte[] requestBytes = reqBuilder.build().toByteArray();
        CallOptions callOptions = CallOptions.DEFAULT.withDeadline(Deadline.after(timeout, TimeUnit.MILLISECONDS));
        if (secret != null && secret.getToken() != null) {
            log.debug("Secret contains token and maybe token header, adding them as call credentials");
            callOptions.withCallCredentials(new TokenCallCredentials(secret.getToken(), secret.getTokenHeader()));
        }
        // Actually execute request.
        long startTime = System.currentTimeMillis();
        byte[] responseBytes = ClientCalls.blockingUnaryCall(channel, GrpcUtil.buildGenericUnaryMethodDescriptor(fullMethodName), callOptions, requestBytes);
        long duration = System.currentTimeMillis() - startTime;
        // Create a Response object for returning.
        Response response = new Response();
        response.setStatus("200");
        response.setMediaType("application/x-protobuf");
        response.setContent(new String(responseBytes, "UTF-8"));
        try {
            // Validate incoming message parsing a DynamicMessage.
            DynamicMessage respMsg = DynamicMessage.parseFrom(md.getOutputType(), responseBytes);
            // Now update response content with readable content.
            String respJson = printer.print(respMsg);
            response.setContent(respJson);
            results.add(new TestReturn(code, duration, message, request, response));
        } catch (InvalidProtocolBufferException ipbe) {
            log.error("Received bytes cannot be transformed in " + md.getOutputType().getFullName());
            results.add(new TestReturn(TestReturn.FAILURE_CODE, duration, "Received bytes cannot be transformed in \" + md.getOutputType().getFullName()", request, response));
        }
    }
    return results;
}
Also used : TestReturn(io.github.microcks.domain.TestReturn) ArrayList(java.util.ArrayList) DynamicMessage(com.google.protobuf.DynamicMessage) CallOptions(io.grpc.CallOptions) URL(java.net.URL) JsonFormat(com.google.protobuf.util.JsonFormat) ManagedChannel(io.grpc.ManagedChannel) Descriptors(com.google.protobuf.Descriptors) TlsChannelCredentials(io.grpc.TlsChannelCredentials) Resource(io.github.microcks.domain.Resource) Request(io.github.microcks.domain.Request) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) X509Certificate(java.security.cert.X509Certificate) URISyntaxException(java.net.URISyntaxException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) Response(io.github.microcks.domain.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 10 with Request

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

the class TestRunnerService method launchTestsInternal.

/**
 * Launch tests using asynchronous/completable future pattern.
 * @param testResult TestResults to aggregate results within
 * @param service Service to test
 * @param runnerType Type of runner for launching the tests
 * @return A Future wrapping test results
 */
@Async
public CompletableFuture<TestResult> launchTestsInternal(TestResult testResult, Service service, TestRunnerType runnerType) {
    // Found next build number for this test.
    List<TestResult> older = testResultRepository.findByServiceId(service.getId(), PageRequest.of(0, 2, Sort.Direction.DESC, "testNumber"));
    if (older != null && !older.isEmpty() && older.get(0).getTestNumber() != null) {
        testResult.setTestNumber(older.get(0).getTestNumber() + 1L);
    } else {
        testResult.setTestNumber(1L);
    }
    Secret secret = null;
    if (testResult.getSecretRef() != null) {
        secret = secretRepository.findById(testResult.getSecretRef().getSecretId()).orElse(null);
        log.debug("Using a secret to test endpoint? '{}'", secret != null ? secret.getName() : "none");
    }
    // Initialize runner once as it is shared for each test.
    AbstractTestRunner<HttpMethod> testRunner = retrieveRunner(runnerType, secret, testResult.getTimeout(), service.getId());
    if (testRunner == null) {
        // Set failure and stopped flags and save before exiting.
        testResult.setSuccess(false);
        testResult.setInProgress(false);
        testResult.setElapsedTime(0);
        testResultRepository.save(testResult);
        return CompletableFuture.completedFuture(testResult);
    }
    for (TestCaseResult testCaseResult : testResult.getTestCaseResults()) {
        // Retrieve operation corresponding to testCase.
        Operation operation = service.getOperations().stream().filter(o -> o.getName().equals(testCaseResult.getOperationName())).findFirst().get();
        String testCaseId = IdBuilder.buildTestCaseId(testResult, operation);
        // Prepare collection of requests to launch.
        List<Request> requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(service, operation));
        requests = cloneRequestsForTestCase(requests, testCaseId);
        List<TestReturn> results = new ArrayList<>();
        try {
            HttpMethod method = testRunner.buildMethod(operation.getMethod());
            results = testRunner.runTest(service, operation, testResult, requests, testResult.getTestedEndpoint(), method);
        } catch (URISyntaxException use) {
            log.error("URISyntaxException on endpoint {}, aborting current tests", testResult.getTestedEndpoint(), use);
            // Set flags and add to results before exiting loop.
            testCaseResult.setSuccess(false);
            testCaseResult.setElapsedTime(0);
            testResultRepository.save(testResult);
            break;
        } catch (Throwable t) {
            log.error("Throwable while testing operation {}", operation.getName(), t);
        }
        // sample request for that operation -> mark it as failed.
        if (results == null) {
            testCaseResult.setSuccess(false);
            testCaseResult.setElapsedTime(0);
            testResultRepository.save(testResult);
        } else if (!results.isEmpty()) {
            updateTestCaseResultWithReturns(testCaseResult, results, testCaseId);
            testResultRepository.save(testResult);
        }
    }
    // Update success, progress indicators and total time before saving and returning.
    updateTestResult(testResult);
    return CompletableFuture.completedFuture(testResult);
}
Also used : TestCaseResult(io.github.microcks.domain.TestCaseResult) TestReturn(io.github.microcks.domain.TestReturn) PageRequest(org.springframework.data.domain.PageRequest) Request(io.github.microcks.domain.Request) ArrayList(java.util.ArrayList) TestResult(io.github.microcks.domain.TestResult) Operation(io.github.microcks.domain.Operation) URISyntaxException(java.net.URISyntaxException) Secret(io.github.microcks.domain.Secret) HttpMethod(org.springframework.http.HttpMethod) Async(org.springframework.scheduling.annotation.Async)

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