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

com.slickqa.junit.testrunner.testplan.TestplanFile Maven / Gradle / Ivy

package com.slickqa.junit.testrunner.testplan;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.slickqa.junit.testrunner.Configuration;
import com.slickqa.junit.testrunner.run.TestInformationCollectingExtension;
import com.slickqa.junit.testrunner.output.TestcaseInfo;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestplanFile {
    private String name;
    private String description;
    private List> selectors;
    private List> filters;

    public TestplanFile() {
        name = "";
        description = "";
        selectors = new ArrayList<>();
        filters = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List> getSelectors() {
        return selectors;
    }

    public void setSelectors(List> selectors) {
        this.selectors = selectors;
    }

    public List> getFilters() {
        return filters;
    }

    public void setFilters(List> filters) {
        this.filters = filters;
    }

    public LauncherDiscoveryRequest toLauncherDiscoveryRequest(Configuration... entries) {
        List filters = new ArrayList<>();
        List selectors = new ArrayList<>();
        Map configurationParameters = new HashMap<>();
        for(Map selectorMap : this.getSelectors()) {
            for(Map.Entry selector : selectorMap.entrySet()) {
                selectors.add(selector.getKey().select(selector.getValue()));
            }
        }
        for(Map filterMap : this.getFilters()) {
            for(Map.Entry filter : filterMap.entrySet()) {
                filters.add(filter.getKey().filter(filter.getValue()));
            }
        }
        for(Configuration config : entries) {
            configurationParameters.put(config.getKey(), config.getValue());
        }
        return LauncherDiscoveryRequestBuilder.request()
                .selectors(selectors)
                .filters(filters.toArray(new org.junit.platform.engine.Filter[0]))
                .configurationParameters(configurationParameters)
                .build();
    }

    public static TestplanFile readFrom(File file) throws IOException {
        ObjectMapper mapper;
        if(file.getName().endsWith(".yml") || file.getName().endsWith(".yaml")) {
            mapper = new ObjectMapper(new YAMLFactory());
        } else {
            mapper = new ObjectMapper();
        }
        return mapper.readValue(file, TestplanFile.class);
    }

    public List getTests() {
        String sessionId = TestInformationCollectingExtension.createSession();
        LauncherDiscoveryRequest request = toLauncherDiscoveryRequest(
                Configuration.Value(TestInformationCollectingExtension.SESSION_ID_CONFIGURATION_NAME, sessionId),
                Configuration.Value("junit.jupiter.extensions.autodetection.enabled", "true")
        );
        Launcher launcher = LauncherFactory.create();
        launcher.execute(request);
        return TestInformationCollectingExtension.getTestsFromSession(sessionId);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy