Search in sources :

Example 1 with TestCaseResult

use of io.github.microcks.domain.TestCaseResult 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)

Example 2 with TestCaseResult

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

the class TestRunnerService method updateTestResult.

/**
 */
private void updateTestResult(TestResult testResult) {
    log.debug("Updating total testResult");
    // Update success, progress indicators and total time before saving and returning.
    boolean globalSuccessFlag = true;
    boolean globalProgressFlag = false;
    long totalElapsedTime = 0;
    for (TestCaseResult testCaseResult : testResult.getTestCaseResults()) {
        totalElapsedTime += testCaseResult.getElapsedTime();
        if (!testCaseResult.isSuccess()) {
            globalSuccessFlag = false;
        }
        // progress because not updated yet.
        if (testCaseResult.getElapsedTime() == -1) {
            log.debug("testCaseResult.elapsedTime is -1, set globalProgressFlag to true");
            globalProgressFlag = true;
        }
    }
    // Update aggregated flags before saving whole testResult.
    testResult.setSuccess(globalSuccessFlag);
    testResult.setInProgress(globalProgressFlag);
    testResult.setElapsedTime(totalElapsedTime);
    testResultRepository.save(testResult);
}
Also used : TestCaseResult(io.github.microcks.domain.TestCaseResult)

Aggregations

TestCaseResult (io.github.microcks.domain.TestCaseResult)2 Operation (io.github.microcks.domain.Operation)1 Request (io.github.microcks.domain.Request)1 Secret (io.github.microcks.domain.Secret)1 TestResult (io.github.microcks.domain.TestResult)1 TestReturn (io.github.microcks.domain.TestReturn)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 PageRequest (org.springframework.data.domain.PageRequest)1 HttpMethod (org.springframework.http.HttpMethod)1 Async (org.springframework.scheduling.annotation.Async)1