Search in sources :

Example 1 with TestResult

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

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

the class TestResultRepositoryTest method setUp.

@Before
public void setUp() {
    // Create a service...
    Service service = new Service();
    service.setName("HelloWorld");
    service.setVersion("1.0");
    serviceRepository.save(service);
    // Create a bunch of test results for this service.
    TestResult testResult = new TestResult();
    testResult.setInProgress(false);
    testResult.setRunnerType(TestRunnerType.HTTP);
    testResult.setTestNumber(1L);
    testResult.setServiceId(service.getId());
    testResult.setTestedEndpoint("http://localhost:8088/HelloWorld");
    repository.save(testResult);
    // ... another one.
    testResult = new TestResult();
    testResult.setInProgress(false);
    testResult.setRunnerType(TestRunnerType.HTTP);
    testResult.setTestNumber(2L);
    testResult.setServiceId(service.getId());
    testResult.setTestedEndpoint("http://localhost:8088/HelloWorld");
    repository.save(testResult);
    serviceId = service.getId();
}
Also used : Service(io.github.microcks.domain.Service) TestResult(io.github.microcks.domain.TestResult) Before(org.junit.Before)

Aggregations

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