All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.orangebeard.client.v3.OrangebeardAsyncV3Client Maven / Gradle / Ivy

There is a newer version: 3.0.6
Show newest version
package io.orangebeard.client.v3;

import io.orangebeard.client.OrangebeardV3Client;
import io.orangebeard.client.entity.FinishV3TestRun;
import io.orangebeard.client.entity.StartV3TestRun;
import io.orangebeard.client.entity.attachment.Attachment;
import io.orangebeard.client.entity.log.Log;
import io.orangebeard.client.entity.step.FinishStep;
import io.orangebeard.client.entity.step.StartStep;
import io.orangebeard.client.entity.suite.StartSuite;
import io.orangebeard.client.entity.suite.Suite;
import io.orangebeard.client.entity.test.FinishTest;
import io.orangebeard.client.entity.test.StartTest;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class OrangebeardAsyncV3Client implements V3Client {
    private final ConcurrentHashMap> tasks;
    private final ConcurrentHashMap uuidMap;
    private final OrangebeardV3Client client;

    public OrangebeardAsyncV3Client(String endpoint, UUID accessToken, String projectName, boolean connectionWithOrangebeardIsValid) {
        client = new OrangebeardV3Client(endpoint, accessToken, projectName, connectionWithOrangebeardIsValid);
        tasks = new ConcurrentHashMap<>();
        uuidMap = new ConcurrentHashMap<>();
    }

    private CompletableFuture parentTask(UUID taskUUID) {
        return tasks.get(taskUUID);
    }

    @Override
    public UUID startTestRun(StartV3TestRun testRun) {
        UUID temporaryUUID = UUID.randomUUID();
        CompletableFuture startTestRunTask = new CompletableFuture<>();
        tasks.put(temporaryUUID, startTestRunTask);

        CompletableFuture.runAsync(() -> {
            UUID actualUUID = client.startTestRun(testRun);
            uuidMap.put(temporaryUUID, actualUUID);
            startTestRunTask.complete(actualUUID);
        });

        return temporaryUUID;
    }

    @Override
    public void startAnnouncedTestRun(UUID testRunUUID) {
        CompletableFuture startTestRunTask = new CompletableFuture<>();
        tasks.put(testRunUUID, startTestRunTask);

        CompletableFuture.runAsync(() -> {
            client.startAnnouncedTestRun(testRunUUID);
            startTestRunTask.complete(testRunUUID);
        });

        uuidMap.put(testRunUUID, testRunUUID);
    }

    @Override
    public void finishTestRun(UUID testRunUUID, FinishV3TestRun finishTestRun) {
        CompletableFuture allTasks = CompletableFuture.allOf(tasks.values().toArray(new CompletableFuture[0]));
        allTasks.join(); //await completion of all tasks
        client.finishTestRun(uuidMap.get(testRunUUID), finishTestRun);
    }

    @Override
    public List startSuite(StartSuite startSuite) {
        List tempUUIDs = startSuite.getSuiteNames().stream().map(
                suite -> UUID.randomUUID()).collect(Collectors.toCollection(LinkedList::new));

        CompletableFuture parent = startSuite.getParentSuiteUUID() != null ?
                parentTask(startSuite.getParentSuiteUUID()) :
                parentTask(startSuite.getTestRunUUID());

        CompletableFuture startSuiteTask = new CompletableFuture<>();
        tempUUIDs.forEach(tempUUID -> tasks.put(tempUUID, startSuiteTask));

        parent.thenCompose(parentUUID -> {
            StartSuite realStartSuite;
            UUID testRunUUID;
            UUID parentSuiteUUID = null;
            if (startSuite.getParentSuiteUUID() == null) {
                testRunUUID = (UUID) parentUUID;
            } else {
                testRunUUID = uuidMap.get(startSuite.getTestRunUUID());
                parentSuiteUUID = parentUUID instanceof List ? //If the parent is a suite, a list of uuid's is returned instead of just a uuid.
                        (UUID) ((List) parentUUID).get(((List) parentUUID).size() - 1) :
                        (UUID) parentUUID;
            }

            realStartSuite = new StartSuite(
                    testRunUUID,
                    parentSuiteUUID,
                    startSuite.getDescription(),
                    startSuite.getAttributes(),
                    startSuite.getSuiteNames());

            List suites = client.startSuite(realStartSuite);
            List actualUUIDs = suites.stream().map(
                    Suite::getSuiteUUID).collect(Collectors.toList());

            startSuiteTask.complete(actualUUIDs);
            return CompletableFuture.supplyAsync(() -> actualUUIDs);
        });
        return tempUUIDs;
    }

    @Override
    public UUID startTest(StartTest startTest) {
        UUID temporaryUUID = UUID.randomUUID();
        CompletableFuture parent = parentTask(startTest.getSuiteUUID());
        CompletableFuture startTestTask = new CompletableFuture<>();
        tasks.put(temporaryUUID, startTestTask);

        parent.thenCompose(parentUUID -> {
            UUID parentSuiteUUID = (UUID) ((List) parentUUID).get(((List) parentUUID).size() - 1);

            StartTest realStartTest = new StartTest(
                    uuidMap.get(startTest.getTestRunUUID()),
                    parentSuiteUUID,
                    startTest.getTestName(),
                    startTest.getTestType(),
                    startTest.getDescription(),
                    startTest.getAttributes(),
                    startTest.getStartTime());

            UUID actualUUID = client.startTest(realStartTest);
            uuidMap.put(temporaryUUID, actualUUID);
            startTestTask.complete(actualUUID);
            return CompletableFuture.supplyAsync(() -> actualUUID);
        });
        return temporaryUUID;
    }

    @Override
    public void finishTest(UUID testUUID, FinishTest finishTest) {
        CompletableFuture parent = parentTask(testUUID);
        CompletableFuture finishTestTask = new CompletableFuture<>();
        tasks.put(UUID.randomUUID(), finishTestTask);

        parent.thenCompose(parentUUID -> {
            FinishTest realFinishTest = new FinishTest(
                    uuidMap.get(finishTest.getTestRunUUID()),
                    finishTest.getStatus(),
                    finishTest.getEndTime());
            client.finishTest((UUID) parentUUID, realFinishTest);
            finishTestTask.complete(null);
            return null;
        });
    }

    @Override
    public UUID startStep(StartStep startStep) {
        UUID temporaryUUID = UUID.randomUUID();
        CompletableFuture parent = startStep.getParentStepUUID() != null ?
                parentTask(startStep.getParentStepUUID()) :
                parentTask(startStep.getTestUUID());

        CompletableFuture startStepTask = new CompletableFuture<>();
        tasks.put(temporaryUUID, startStepTask);

        parent.thenCompose(parentUUID -> {
            UUID testUUID;
            UUID parentStepUUID = null;
            if (startStep.getParentStepUUID() == null) {
                testUUID = (UUID) parentUUID;
            } else {
                testUUID = uuidMap.get(startStep.getTestUUID());
                parentStepUUID = (UUID) parentUUID;
            }
            StartStep realStartStep = new StartStep(
                    uuidMap.get(startStep.getTestRunUUID()),
                    testUUID,
                    parentStepUUID,
                    startStep.getStepName(),
                    startStep.getDescription(),
                    startStep.getStartTime()
            );
            UUID actualUUID = client.startStep(realStartStep);
            startStepTask.complete(actualUUID);
            uuidMap.put(temporaryUUID, actualUUID);
            return CompletableFuture.supplyAsync(() -> actualUUID);
        });

        return temporaryUUID;
    }

    @Override
    public void finishStep(UUID stepUUID, FinishStep finishStep) {
        CompletableFuture parent = parentTask(stepUUID);
        CompletableFuture finishStepTask = new CompletableFuture<>();
        tasks.put(UUID.randomUUID(), finishStepTask);

        parent.thenCompose(parentUUID -> {
            FinishStep realFinishStep = new FinishStep(
                    uuidMap.get(finishStep.getTestRunUUID()),
                    finishStep.getStatus(),
                    finishStep.getEndTime());
            client.finishStep((UUID) parentUUID, realFinishStep);
            finishStepTask.complete(null);
            return null;
        });
    }

    @Override
    public UUID log(Log log) {
        UUID temporaryUUID = UUID.randomUUID();
        CompletableFuture parent = log.getStepUUID() != null ?
                parentTask(log.getStepUUID()) :
                parentTask(log.getTestUUID());

        CompletableFuture logTask = new CompletableFuture<>();
        tasks.put(temporaryUUID, logTask);

        parent.thenCompose(parentUUID -> {
            UUID testUUID;
            UUID stepUUID = null;
            if (log.getStepUUID() == null) {
                testUUID = (UUID) parentUUID;
            } else {
                testUUID = uuidMap.get(log.getTestUUID());
                stepUUID = (UUID) parentUUID;
            }
            Log realLog = new Log(
                    uuidMap.get(log.getTestRunUUID()),
                    testUUID,
                    stepUUID,
                    log.getMessage(),
                    log.getLogLevel(),
                    log.getLogTime(),
                    log.getLogFormat()
            );
            UUID actualUUID = client.log(realLog);
            logTask.complete(actualUUID);
            uuidMap.put(temporaryUUID, actualUUID);
            return CompletableFuture.supplyAsync(() -> actualUUID);
        });
        return temporaryUUID;
    }

    /**
     * @param logs list of log entries to send
     * @deprecated Use single async log calls to ensure synchronization. This method now acts as a forwarder
     */
    @Override
    @Deprecated(since = "2.0.0")
    public void sendLogBatch(List logs) {
        logs.forEach(this::log);
    }

    @Override
    public UUID sendAttachment(Attachment attachment) {
        UUID temporaryUUID = UUID.randomUUID();
        Attachment.AttachmentMetaData meta = attachment.getMetaData();
        CompletableFuture parent = parentTask(meta.getLogUUID());

        CompletableFuture attachmentTask = new CompletableFuture<>();
        tasks.put(temporaryUUID, attachmentTask);

        parent.thenCompose(logUUID -> {
            Attachment realAttachment = new Attachment(
                    attachment.getFile(),
                    new Attachment.AttachmentMetaData(
                            uuidMap.get(meta.getTestRunUUID()),
                            uuidMap.get(meta.getTestUUID()),
                            meta.getStepUUID() == null ? null : uuidMap.get(meta.getStepUUID()),
                            (UUID) logUUID,
                            meta.getAttachmentTime()
                    )
            );
            UUID actualUUID = client.sendAttachment(realAttachment);
            attachmentTask.complete(actualUUID);
            uuidMap.put(temporaryUUID, actualUUID);
            return CompletableFuture.supplyAsync(() -> actualUUID);
        });
        return temporaryUUID;
    }
}