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

com.slickqa.jupiter.SlickJunitController Maven / Gradle / Ivy

There is a newer version: 1.0.0-25
Show newest version
package com.slickqa.jupiter;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.slickqa.client.SlickClient;
import com.slickqa.client.SlickClientFactory;
import com.slickqa.client.errors.SlickError;
import com.slickqa.client.impl.JsonUtil;
import com.slickqa.client.model.*;
import com.slickqa.jupiter.annotations.SlickMetaData;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.*;
//import org.junit.runner.Description;

import java.lang.reflect.Method;
import java.util.*;

/*
 * Common class used by both the SlickTestWatcher (sends final test result after a test is run) and the
 * SchedulingExecutionCondition used for initially sending results to slick.  This class will initialize the slick
 * client, create the testrun in slick, and will hold a mapping of junit tests to slick results.  The test writer will
 * not likely have to interact with this class unless they want to customize the process by extending.
 */
public class SlickJunitController {
    protected static boolean usingSlick;
    public static ThreadLocal currentResult;
    protected SlickConfigurationSource configurationSource;
    protected SlickClient slickClient;
    protected Project project;
    protected Testrun testrun;

    protected Map results;

    protected SlickJunitController() {
        usingSlick = false;
        configurationSource = initializeConfigurationSource();
        results = new HashMap<>();
        currentResult = new ThreadLocal<>();
        initializeController();
    }

    /**
     * This method exists in case you need to override where slick get's it's configuration data from.  By default
     * it will get it from system properties.
     *
     * @return an instance of
     */
    protected SlickConfigurationSource initializeConfigurationSource() {
        return new PropertyOrEnvVariableConfigurationSource();
    }

    protected void initializeController() {
        String baseurl = configurationSource.getConfigurationEntry(ConfigurationNames.BASE_URL, null);
        String projectName = configurationSource.getConfigurationEntry(ConfigurationNames.PROJECT_NAME, null);
        if(baseurl != null && projectName != null) {
            try {

                ObjectMapper mapper = new ObjectMapper();
                mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
                mapper.disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
                mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
                mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                JsonUtil.mapper = mapper;

                if(!baseurl.endsWith("api") && !baseurl.endsWith("api/")) {
                    String add = "api/";
                    if(baseurl.endsWith("/")) {
                        baseurl = baseurl + add;
                    } else {
                        baseurl = baseurl + "/" + add;
                    }
                }
                slickClient = SlickClientFactory.getSlickClient(baseurl);
                ProjectReference projectReference = new ProjectReference();
                ReleaseReference releaseReference = null;
                BuildReference buildReference = null;
                String testplanId = null;
                String testrunName = null;
                try {
                    project = slickClient.project(projectName).get();
                } catch (SlickError e) {
                    project = new Project();
                    project.setName(projectName);
                    project = slickClient.projects().create(project);
                }
                projectReference.setName(project.getName());
                projectReference.setId(project.getId());

                String releaseName = configurationSource.getConfigurationEntry(ConfigurationNames.RELEASE_NAME, null);
                if(releaseName != null) {
                    releaseReference = new ReleaseReference();
                    releaseReference.setName(releaseName);
                }
                String buildName = configurationSource.getConfigurationEntry(ConfigurationNames.BUILD_NAME, null);
                if(buildName != null) {
                    buildReference = new BuildReference();
                    buildReference.setName(buildName);
                }

                String testplanName = configurationSource.getConfigurationEntry(ConfigurationNames.TESTPLAN_NAME, null);
                if(testplanName == null) {
                    testplanName = "Default_Testplan";
                }
                HashMap query = new HashMap<>();
                query.put("project.id", project.getId());
                query.put("name", testplanName);
                TestPlan tplan = null;
                try {
                    List tplans = slickClient.testplans(query).getList();
                    if (tplans != null && tplans.size() > 0) {
                        tplan = tplans.get(0);
                    }
                } catch (SlickError e) {
                    // don't care
                }
                if (tplan == null) {
                    tplan = new TestPlan();
                    tplan.setName(testplanName);
                    tplan.setProject(projectReference);
                    tplan = slickClient.testplans().create(tplan);
                }
                testplanId = tplan.getId();

                String testrunId = configurationSource.getConfigurationEntry(ConfigurationNames.TESTRUN_ID, null);
                if(testrunId != null) {
                    try {
                        testrun = slickClient.testrun(testrunId).get();
                    } catch (SlickError e) {
                        // doesn't matter, we'll just create it.
                    }
                }
                if(testrun == null) {
                    testrun = new Testrun();
                    testrun.setId(testrunId);
                    testrun.setName(testplanName);
                    testrun.setTestplanId(testplanId);
                    testrun.setProject(projectReference);
                    testrun.setRelease(releaseReference);
                    testrun.setBuild(buildReference);
                    testrun = slickClient.testruns().create(testrun);
                }

                usingSlick = true;
            } catch (SlickError e) {
                e.printStackTrace();
                System.err.println("!!!!!! Error occurred when initializing slick, no slick report will happen !!!!!!");
            }
        }
    }

    public static boolean isUsingSlick() {
        return usingSlick;
    }

    public Testrun getTestrun() {
        return testrun;
    }

    public SlickClient getSlickClient() {
        if(usingSlick) {
            return slickClient;
        } else {
            return null;
        }
    }

    public String getAutomationId(Method testDescription) {
        String automationId = null;
        try {
            SlickMetaData metaData = testDescription.getAnnotation(SlickMetaData.class);
            if(metaData != null && metaData.automationId() != null && !"".equals(metaData.automationId())) {
                automationId = metaData.automationId();
            }
        } catch (RuntimeException e) {
            // ignore
        }
        if(automationId == null) {
            automationId = testDescription.getDeclaringClass().getName() + ":" + testDescription.getName();
        }

        return automationId;
    }

    public void addResultFor(Method testDescription) throws SlickError {
        if (isUsingSlick()) {
            SlickMetaData metaData = testDescription.getAnnotation(SlickMetaData.class);
            boolean hasMetaData = metaData != null;

            String automationId = getAutomationId(testDescription);
            Testcase testcase = null;

            HashMap query = new HashMap<>();
            query.put("project.id", project.getId());
            query.put("automationId", automationId);
            ProjectReference projectReference = new ProjectReference();
            projectReference.setName(project.getName());
            projectReference.setId(project.getId());

            try {
                List testcases = slickClient.testcases(query).getList();
                if (testcases != null && testcases.size() > 0) {
                    testcase = testcases.get(0);
                    // update author if needed
                    if (hasMetaData) {
                        if (!metaData.author().equals("") && !metaData.author().equals(testcase.getAuthor())) {
                            testcase.setAuthor(metaData.author());
                            getSlickClient().testcase(testcase.getId()).update(testcase);
                        }
                    }
                }
            } catch (SlickError e) {
                // ignore
            }

            boolean newTestcase = false;
            if (testcase == null) {
                testcase = new Testcase();
                newTestcase = true;
            }
            if (hasMetaData) {
                testcase.setName(metaData.title());
            } else {
                if (testDescription.isAnnotationPresent(DisplayName.class)) {
                    testcase.setName(testDescription.getAnnotation(DisplayName.class).value());
                } else {
                    testcase.setName(testDescription.getName());
                }
            }
            testcase.setProject(projectReference);
            if (newTestcase) {
                testcase = slickClient.testcases().create(testcase);
            }


            testcase.setAutomated(true);
            testcase.setAutomationId(automationId);
            testcase.setAutomationTool("junit_5");
            if (hasMetaData) {
                testcase.setAutomationKey(getValueOrNullIfEmpty(metaData.automationKey()));
                testcase.setPurpose(metaData.purpose());
                ComponentReference componentReference = null;
                Component component = null;
                if (metaData.component() != null && !"".equals(metaData.component())) {
                    componentReference = new ComponentReference();
                    componentReference.setName(metaData.component());
                    if (project.getComponents() != null) {
                        for (Component possible : project.getComponents()) {
                            if (metaData.component().equals(possible.getName())) {
                                componentReference.setId(possible.getId());
                                componentReference.setCode(possible.getCode());
                                component = possible;
                                break;
                            }
                        }
                    }
                    if (componentReference.getId() == null) {
                        component = new Component();
                        component.setName(metaData.component());
                        try {
                            component = slickClient.project(project.getId()).components().create(component);
                            componentReference.setId(component.getId());
                            project = slickClient.project(project.getId()).get();
                        } catch (SlickError e) {
                            component = null;
                            componentReference = null;
                        }
                    }
                }
                testcase.setComponent(componentReference);
                FeatureReference featureReference = null;
                if (metaData.feature() != null && !"".equals(metaData.feature()) && component != null) {
                    featureReference = new FeatureReference();
                    featureReference.setName(metaData.feature());
                    Feature feature = null;
                    if (component.getFeatures() != null) {
                        for (Feature possible : component.getFeatures()) {
                            if (metaData.feature().equals(possible.getName())) {
                                featureReference.setId(possible.getId());
                                feature = possible;
                                break;
                            }
                        }
                    }
                    if (feature == null) {
                        feature = new Feature();
                        feature.setName(metaData.feature());
                        if (component.getFeatures() == null) {
                            component.setFeatures(new ArrayList(1));
                        }
                        component.getFeatures().add(feature);
                        try {
                            component = slickClient.project(project.getId()).component(component.getId()).update(component);
                            project = slickClient.project(project.getId()).get();
                            if (component.getFeatures() != null) {
                                for (Feature possible : component.getFeatures()) {
                                    if (metaData.feature().equals(possible.getName())) {
                                        featureReference.setId(feature.getId());
                                    }
                                }

                            } else {
                                // this shouldn't be possible which probably means it'll happen
                                feature = null;
                                featureReference = null;
                            }
                        } catch (SlickError e) {
                            feature = null;
                            featureReference = null;
                        }
                    }
                    testcase.setFeature(featureReference);
                    if (metaData.steps() != null && metaData.steps().length > 0) {
                        testcase.setSteps(new ArrayList(metaData.steps().length));
                        for (com.slickqa.jupiter.annotations.Step metaStep : metaData.steps()) {
                            Step slickStep = new Step();
                            slickStep.setName(metaStep.step());
                            slickStep.setExpectedResult(metaStep.expectation());
                            testcase.getSteps().add(slickStep);
                        }
                    }
                }
            }
            testcase = slickClient.testcase(testcase.getId()).update(testcase);
            TestcaseReference testReference = new TestcaseReference();
            testReference.setName(testcase.getName());
            testReference.setAutomationId(testcase.getAutomationId());
            testReference.setAutomationKey(testcase.getAutomationKey());
            testReference.setTestcaseId(testcase.getId());
            testReference.setAutomationTool(testcase.getAutomationTool());

            TestrunReference testrunReference = new TestrunReference();
            testrunReference.setName(testrun.getName());
            testrunReference.setTestrunId(testrun.getId());

            Result result = new Result();
            result.setProject(projectReference);
            result.setTestrun(testrunReference);
            result.setTestcase(testReference);
            if(testcase != null && testcase.getComponent() != null) {
                result.setComponent(testcase.getComponent());
            }
            result.setStatus("NO_RESULT");
            result.setReason("not run yet...");
            result.setRecorded(new Date());
            result = slickClient.results().create(result);
            results.put(automationId, result);
        }
    }

    public static String getValueOrNullIfEmpty(String value) {
        if (value == null || "".equals(value)) {
            return null;
        } else {
            return value;
        }
    }

    public Result getResultFor(Method testDescription) {
        String automationId = getAutomationId(testDescription);
        if(results.containsKey(automationId)) {
            return results.get(automationId);
        } else {
            return null;
        }
    }

    public Result getOrCreateResultFor(Method testDescription) {
        if(isUsingSlick()) {
            Result result = getResultFor(testDescription);
            if(result == null) {
                try {
                    addResultFor(testDescription);
                    return getResultFor(testDescription);
                } catch (SlickError e) {
                    e.printStackTrace();
                    System.err.println("!!!! ERROR creating slick result for " + testDescription.getName() + " !!!!");
                    return null;
                }
            } else {
                return result;
            }
        } else {
            return null;
        }
    }


    /*public void createSuiteResults(ArrayList children) {
        if(isUsingSlick()) {
            for (Description child : children) {
                if (child.isTest()) {
                    try {
                        addResultFor(child);
                    } catch (SlickError e) {
                        e.printStackTrace();
                        System.err.println("!!!! ERROR creating slick result for " + child.getDisplayName() + " !!!!");
                    }
                } else {
                    if (child.getChildren() != null) {
                        createSuiteResults(child.getChildren());
                    }
                }
            }
        }
    }*/
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy