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

net.thucydides.core.releases.ReleaseManager Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
package net.thucydides.core.releases;

import com.google.common.base.Splitter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.serenitybdd.core.collect.NewList;
import net.thucydides.core.ThucydidesSystemProperty;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.model.Release;
import net.thucydides.core.model.ReportType;
import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.model.TestTag;
import net.thucydides.core.reports.TestOutcomes;
import net.thucydides.core.reports.html.ReportNameProvider;
import net.thucydides.core.requirements.RequirementsService;
import net.thucydides.core.requirements.RequirementsTagProvider;
import net.thucydides.core.requirements.reports.RequirementOutcome;
import net.thucydides.core.util.EnvironmentVariables;
import org.hamcrest.Matcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import static net.thucydides.core.reports.html.ReportNameProvider.NO_CONTEXT;
import static org.apache.commons.lang3.StringUtils.capitalize;
import static org.apache.commons.lang3.StringUtils.lowerCase;
import static org.hamcrest.Matchers.containsString;

public class ReleaseManager {

    private final static String DEFAULT_RELEASE_TYPES = "Release,Iteration:Sprint";
    private List releaseTypes;
    private ReportNameProvider reportNameProvider;
    private RequirementsService requirementsService;
    private static final Logger LOGGER = LoggerFactory.getLogger(RequirementsTagProvider.class);

    public ReleaseManager(EnvironmentVariables environmentVariables, ReportNameProvider reportNameProvider) {
        this(environmentVariables, reportNameProvider, Injectors.getInjector().getInstance(RequirementsService.class));
    }

    public ReleaseManager(EnvironmentVariables environmentVariables, ReportNameProvider reportNameProvider, RequirementsService requirementsService) {
        this.reportNameProvider = reportNameProvider;
        this.requirementsService = requirementsService;
        String typeValues = ThucydidesSystemProperty.THUCYDIDES_RELEASE_TYPES.from(environmentVariables, DEFAULT_RELEASE_TYPES);
        releaseTypes = Splitter.on(",").trimResults().splitToList(typeValues);
    }

    public String getJSONReleasesFrom(TestOutcomes testOutcomes) {
        List releases = getReleasesFrom(testOutcomes);
        GsonBuilder builder = new GsonBuilder();
        builder.setPrettyPrinting();
        Gson gson = builder.create();
        return gson.toJson(releases);

    }

    public String getJSONReleasesFrom(Release release) {
        GsonBuilder builder = new GsonBuilder();
        builder.setPrettyPrinting();
        Gson gson = builder.create();
        return gson.toJson(NewList.of(release));

    }

    public List getFlattenedReleasesFrom(TestOutcomes testOutcomes) {
        return flattened(getReleasesFrom(testOutcomes));
    }

    private List flattened(List releases) {
        List flattenedReleases = new ArrayList<>();
        for (Release release : releases) {
            flattenedReleases.add(release);
            flattenedReleases.addAll(flattened(release.getChildren()));
        }
        return NewList.copyOf(flattenedReleases);
    }

    List NO_PARENTS = new ArrayList();

    public List getReleasesFrom(TestOutcomes testOutcomes) {

        List releases = requirementsService.getReleasesFromRequirements();
        LOGGER.debug("Loaded Releases: " + releases);

        enrichOutcomesWithReleaseTags(testOutcomes.getOutcomes());

        if (releases.isEmpty()) {
            releases = extractReleasesFromTestOutcomeAnnotations(testOutcomes);
        }
        return NewList.copyOf(releases);
    }

    private List extractReleasesFromTestOutcomeAnnotations(TestOutcomes testOutcomes) {
        List releases = new ArrayList<>();
        List releaseTags = testOutcomes.findMatchingTags()
                .withType("version")
                .withNameIn(matchingNames(releaseTypes.get(0)))
                .list();
        for (TestTag tag : releaseTags) {
            releases.add(extractReleaseFor(tag, testOutcomes.withTag(tag), 1, NO_PARENTS));
        }
        return releases;
    }

    private List> matchingNames(String possibleNames) {
        List> matchers = new ArrayList<>();
        List nameCandidates = Splitter.on(":").trimResults().splitToList(possibleNames.toLowerCase());
        for(String nameCandidate : nameCandidates) {
            matchers.add((containsString(nameCandidate)));
            matchers.add((containsString(capitalize(nameCandidate))));
            matchers.add((containsString(lowerCase(nameCandidate))));
        }
        return matchers;
    }

    public List enrichRequirementsOutcomesWithReleaseTags(List outcomes) {
        List requirementOutcomes = new ArrayList<>();
        for (RequirementOutcome outcome : outcomes) {
            List enrichedOutcomes = enrichOutcomesWithReleaseTags(outcome.getTestOutcomes().getOutcomes());
            requirementOutcomes.add(outcome.withTestOutcomes(TestOutcomes.of(enrichedOutcomes)));
        }
        return requirementOutcomes;
    }

    public List enrichOutcomesWithReleaseTags(List outcomes) {
        List enrichedOutcomes = new ArrayList<>();
        for (TestOutcome outcome : outcomes) {
            List releaseVersions = requirementsService.getReleaseVersionsFor(outcome);
            outcome.addTags(releaseTagsFrom(releaseVersions));
            outcome.addVersions(releaseVersions);
            enrichedOutcomes.add(outcome);
        }
        return enrichedOutcomes;
    }

    private List releaseTagsFrom(List releaseVersions) {
        List tags = new ArrayList<>();
        for (String releaseVersion : releaseVersions) {
            tags.add(TestTag.withName(releaseVersion).andType("version"));
        }
        return tags;
    }

    private Release extractReleaseFor(TestTag releaseTag, TestOutcomes testOutcomes,
                                      int level, List parents) {
        Release release = new Release(releaseTag);
        String reportName = reportNameProvider.forRelease(release);
        release = release.withReport(reportName).withParents(parents);

        List ancestors = ancestorsFor(release);

        if (level < releaseTypes.size()) {
            String childReleaseType = releaseTypes.get(level);
            List childReleaseTags = testOutcomes.findMatchingTags()
                    .withType("version")
                    .withNameIn(matchingNames(childReleaseType))
                    .list();
            List children = new ArrayList<>();
            for (TestTag tag : childReleaseTags) {
                children.add(extractReleaseFor(tag, testOutcomes.withTag(tag), level + 1, ancestors));
            }
            release = release.withChildren(children);
        }

        return release;
    }

    private List ancestorsFor(Release release) {
        List ancestors = new ArrayList(release.getParents());
        ancestors.add(release);
        return ancestors;
    }

    public List extractReleasesFrom(List> releaseVersions) {
        return extractReleasesOfTypeLevel(releaseVersions);
    }

    private ReportNameProvider defaultNameProvider;

    private ReportNameProvider getReportNameProvider() {
        if (defaultNameProvider == null) {
            defaultNameProvider = new ReportNameProvider(NO_CONTEXT, ReportType.HTML, requirementsService);
        }
        return defaultNameProvider;
    }

    private List extractReleasesOfTypeLevel(List> releaseVersions) {
        List releases = new ArrayList<>();
        Set distinctReleases = getDistinct(releaseVersions);
        for (String release : distinctReleases) {
            if (releaseIsOfType(release, releaseTypes.get(0))) {
                List subReleases = extractSubReleasesOfLevel(releaseVersions,
                                                                      release,
                                                                      1,
                                                                      Collections.EMPTY_LIST);
                String reportName = getReportNameProvider().forRelease(release);
                addUniqueRelease(releases,
                                 Release.ofVersion(release)
                                        .withChildren(subReleases)
                                        .withReport(reportName));
            }
        }
        Collections.sort(releases);
        return releases;
    }

    private boolean releaseIsOfType(String release, String releaseTypes) {
        for(Matcher matcher : matchingNames(releaseTypes)) {
            if (matcher.matches(release.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    private void addUniqueRelease(List releases, Release release) {
        if (!containsReleaseWithName(releases,release.getName()))
            releases.add(release);
        }

    private boolean containsReleaseWithName(List releases, String name) {
        for(Release release : releases) {
            if (release.getName().equalsIgnoreCase(name)) {
                return true;
            }
        }
        return false;
    }


    private List extractSubReleasesOfLevel(List> releaseVersions,
                                                    String release,
                                                    int level,
                                                    List parents) {
        List subReleases = new ArrayList<>();
        if (level < releaseTypes.size()) {
            for (List releaseVersionSet : releaseVersions) {
                if (releaseVersionSet.contains(release)) {
                    for (String subRelease : releasesOfType(releaseTypes.get(level), releaseVersionSet)) {
                        List ancestors = new ArrayList<>();
                        ancestors.add(lightweightReleaseNamed(release));
                        List subSubReleases = extractSubReleasesOfLevel(releaseVersions,
                                                                                 subRelease,
                                                                                 level + 1,
                                                                                 ancestors);
                        String reportName = getReportNameProvider().forRelease(subRelease);
                        addUniqueRelease(subReleases, Release.ofVersion(subRelease).withChildren(subSubReleases)
                                                                                   .withReport(reportName)
                                                                                   .withParents(ancestors));
                    }
                }
            }
        }
        Collections.sort(subReleases);
        return subReleases;
    }

    private Release lightweightReleaseNamed(String release) {
        return Release.ofVersion(release).withReport(reportNameProvider.forRelease(release));
    }

    private List releasesOfType(String type, List releaseVersionSet) {
        List matchingReleases = new ArrayList<>();
        for (String release : releaseVersionSet) {
            if (releaseIsOfType(release, type)) {
                matchingReleases.add(release);
            }
        }
        return matchingReleases;
    }


    private Set getDistinct(List> releaseVersions) {
        Set distinctVersions = new HashSet();
        for (List releaseVersionSets : releaseVersions) {
            distinctVersions.addAll(releaseVersionSets);
        }
        return distinctVersions;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy