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

io.qameta.allure.behaviors.BehaviorsPlugin Maven / Gradle / Ivy

/*
 *  Copyright 2019 Qameta Software OÜ
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package io.qameta.allure.behaviors;

import io.qameta.allure.CommonCsvExportAggregator;
import io.qameta.allure.CommonJsonAggregator;
import io.qameta.allure.CompositeAggregator;
import io.qameta.allure.Constants;
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.csv.CsvExportBehavior;
import io.qameta.allure.entity.LabelName;
import io.qameta.allure.entity.TestResult;
import io.qameta.allure.tree.TestResultTree;
import io.qameta.allure.tree.TestResultTreeGroup;
import io.qameta.allure.tree.Tree;
import io.qameta.allure.tree.TreeWidgetData;
import io.qameta.allure.tree.TreeWidgetItem;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static io.qameta.allure.entity.LabelName.EPIC;
import static io.qameta.allure.entity.LabelName.FEATURE;
import static io.qameta.allure.entity.LabelName.STORY;
import static io.qameta.allure.entity.Statistic.comparator;
import static io.qameta.allure.entity.TestResult.comparingByTimeAsc;
import static io.qameta.allure.tree.TreeUtils.calculateStatisticByChildren;
import static io.qameta.allure.tree.TreeUtils.groupByLabels;

/**
 * The plugin adds behaviors tab to the report.
 *
 * @since 2.0
 */
@SuppressWarnings({"PMD.ExcessiveImports", "PMD.UseUtilityClass"})
public class BehaviorsPlugin extends CompositeAggregator {

    protected static final String BEHAVIORS = "behaviors";

    protected static final String JSON_FILE_NAME = "behaviors.json";

    protected static final String CSV_FILE_NAME = "behaviors.csv";

    @SuppressWarnings("PMD.DefaultPackage")
    /* default */ static final LabelName[] LABEL_NAMES = new LabelName[]{EPIC, FEATURE, STORY};

    public BehaviorsPlugin() {
        super(Arrays.asList(
                new JsonAggregator(), new CsvExportAggregator(), new WidgetAggregator()
        ));
    }

    @SuppressWarnings("PMD.DefaultPackage")
    /* default */ static Tree getData(final List launchResults) {

        // @formatter:off
        final Tree behaviors = new TestResultTree(
            BEHAVIORS,
            testResult -> groupByLabels(testResult, LABEL_NAMES)
        );
        // @formatter:on

        launchResults.stream()
                .map(LaunchResults::getResults)
                .flatMap(Collection::stream)
                .sorted(comparingByTimeAsc())
                .forEach(behaviors::add);
        return behaviors;
    }

    private static boolean isNotEmpty(final List strings) {
        return !Objects.isNull(strings) && !strings.isEmpty();
    }

    /**
     * Generates tree data.
     */
    private static class JsonAggregator extends CommonJsonAggregator {

        JsonAggregator() {
            super(JSON_FILE_NAME);
        }

        @Override
        protected Tree getData(final List launches) {
            return BehaviorsPlugin.getData(launches);
        }
    }

    /**
     * Generates export data.
     */
    private static class CsvExportAggregator extends CommonCsvExportAggregator {

        CsvExportAggregator() {
            super(CSV_FILE_NAME, CsvExportBehavior.class);
        }

        @Override
        protected List getData(final List launchesResults) {
            final List exportBehaviors = new ArrayList<>();
            launchesResults.stream().flatMap(launch -> launch.getResults().stream()).forEach(result -> {
                final Map> epicFeatureStoryMap = new HashMap<>();
                Arrays.asList(LABEL_NAMES).forEach(
                        label -> epicFeatureStoryMap.put(label, result.findAllLabels(label))
                );
                addTestResult(exportBehaviors, result, epicFeatureStoryMap);
            });
            return exportBehaviors;
        }

        private void addTestResult(final List exportBehaviors, final TestResult result,
                                   final Map> epicFeatureStoryMap) {
            if (epicFeatureStoryMap.isEmpty()) {
                addTestResultWithLabels(exportBehaviors, result, null, null, null);
            } else {
                addTestResultWithEpic(exportBehaviors, result, epicFeatureStoryMap);
            }
        }

        private void addTestResultWithEpic(final List exportBehaviors, final TestResult result,
                                           final Map> epicFeatureStoryMap) {
            if (isNotEmpty(epicFeatureStoryMap.get(EPIC))) {
                epicFeatureStoryMap.get(EPIC).forEach(
                        epic -> addTestResultWithFeature(exportBehaviors, result, epicFeatureStoryMap, epic)
                );
            } else {
                addTestResultWithFeature(exportBehaviors, result, epicFeatureStoryMap, null);
            }
        }

        private void addTestResultWithFeature(final List exportBehaviors, final TestResult result,
                                              final Map> epicFeatureStoryMap,
                                              final String epic) {
            if (isNotEmpty(epicFeatureStoryMap.get(FEATURE))) {
                epicFeatureStoryMap.get(FEATURE).forEach(
                        feature -> addTestResultWithStories(exportBehaviors, result, epicFeatureStoryMap, epic, feature)
                );
            } else {
                addTestResultWithStories(exportBehaviors, result, epicFeatureStoryMap, epic, null);
            }
        }

        private void addTestResultWithStories(final List exportBehaviors, final TestResult result,
                                              final Map> epicFeatureStoryMap,
                                              final String epic, final String feature) {
            if (isNotEmpty(epicFeatureStoryMap.get(STORY))) {
                epicFeatureStoryMap.get(STORY).forEach(
                        story -> addTestResultWithLabels(exportBehaviors, result, epic, feature, story)
                );
            } else {
                addTestResultWithLabels(exportBehaviors, result, epic, feature, null);
            }
        }

        private void addTestResultWithLabels(final List exportBehaviors, final TestResult result,
                                             final String epic, final String feature, final String story) {
            final Optional behavior = exportBehaviors.stream()
                    .filter(exportBehavior -> exportBehavior.isPassed(epic, feature, story)).findFirst();
            if (behavior.isPresent()) {
                behavior.get().addTestResult(result);
            } else {
                final CsvExportBehavior exportBehavior = new CsvExportBehavior(epic, feature, story);
                exportBehavior.addTestResult(result);
                exportBehaviors.add(exportBehavior);
            }
        }
    }

    /**
     * Generates widget data.
     */
    protected static class WidgetAggregator extends CommonJsonAggregator {

        WidgetAggregator() {
            super(Constants.WIDGETS_DIR, JSON_FILE_NAME);
        }

        @Override
        public TreeWidgetData getData(final List launches) {
            final Tree data = BehaviorsPlugin.getData(launches);
            final List items = data.getChildren().stream()
                    .filter(TestResultTreeGroup.class::isInstance)
                    .map(TestResultTreeGroup.class::cast)
                    .map(WidgetAggregator::toWidgetItem)
                    .sorted(Comparator.comparing(TreeWidgetItem::getStatistic, comparator()).reversed())
                    .limit(10)
                    .collect(Collectors.toList());
            return new TreeWidgetData().setItems(items).setTotal(data.getChildren().size());
        }

        private static TreeWidgetItem toWidgetItem(final TestResultTreeGroup group) {
            return new TreeWidgetItem()
                    .setUid(group.getUid())
                    .setName(group.getName())
                    .setStatistic(calculateStatisticByChildren(group));
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy