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

com.intuit.karate.core.FeatureResult Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
/*
 * The MIT License
 *
 * Copyright 2022 Karate Labs Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.intuit.karate.core;

import com.intuit.karate.report.ReportUtils;
import com.intuit.karate.StringUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.KarateException;
import com.intuit.karate.resource.Resource;
import com.intuit.karate.resource.ResourceUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author pthomas3
 */
public class FeatureResult {

    private final Feature feature;
    private final List scenarioResults = new ArrayList<>();

    private String resultDate;
    private String displayName; // mutable for users who want to customize

    private Map resultVariables;
    private Map callArg;
    private Config config;
    private int loopIndex = -1;
    private int callDepth;

    public FeatureResult(Feature feature) {
        this.feature = feature;
        displayName = feature.getResource().getRelativePath();
    }

    public void printStats() {
        String featureName = feature.getResource().getPrefixedPath();
        StringBuilder sb = new StringBuilder();
        sb.append("---------------------------------------------------------\n");
        sb.append("feature: ").append(featureName).append('\n');
        sb.append(String.format("scenarios: %2d | passed: %2d | failed: %2d | time: %.4f\n", getScenarioCount(), getPassedCount(), getFailedCount(), getDurationMillis() / 1000));
        sb.append("---------------------------------------------------------\n");
        System.out.println(sb);
    }

    public List getAllEmbedFiles() {
        List files = new ArrayList();
        for (ScenarioResult sr : scenarioResults) {
            for (StepResult stepResult : sr.getStepResults()) {
                if (stepResult.getEmbeds() != null) {
                    for (Embed embed : stepResult.getEmbeds()) {
                        files.add(embed.getFile());
                    }
                }
            }
        }
        return files;
    }

    public static FeatureResult fromKarateJson(File workingDir, Map map) {
        String featurePath = (String) map.get("prefixedPath");
        Resource resource = ResourceUtils.getResource(workingDir, featurePath);
        Feature feature = Feature.read(resource);
        FeatureResult fr = new FeatureResult(feature);
        fr.callArg = (Map) map.get("callArg");
        fr.loopIndex = (Integer) map.get("loopIndex");
        fr.resultDate = (String) map.get("resultDate");
        fr.callDepth = (Integer) map.get("callDepth");
        List> list = (List) map.get("scenarioResults");
        if (list != null) {
            for (Map srMap : list) {
                ScenarioResult sr = ScenarioResult.fromKarateJson(workingDir, feature, srMap);
                if (!sr.getStepResults().isEmpty()) {
                    fr.addResult(sr);
                }                
            }
        }
        return fr;
    }

    public Map toInfoJson() {
        Map map = new HashMap();
        map.put("name", feature.getName());
        map.put("description", feature.getDescription());
        map.put("prefixedPath", feature.getResource().getPrefixedPath());
        File file = feature.getResource().getFile();
        if (file != null) {
            map.put("fileName", file.getName());
            map.put("parentDir", file.getParent());
        }
        return map;
    }

    public Map toSummaryJson() {
        Map map = new HashMap();
        map.put("failed", isFailed());
        map.put("name", feature.getName());
        map.put("description", feature.getDescription());
        map.put("durationMillis", getDurationMillis());
        map.put("passedCount", getPassedCount());
        map.put("failedCount", getFailedCount());
        map.put("scenarioCount", getScenarioCount());
        map.put("packageQualifiedName", feature.getPackageQualifiedName());
        map.put("relativePath", feature.getResource().getRelativePath());
        return map;
    }

    public Map toKarateJson() {
        Map map = new HashMap();
        // these first few are only for the ease of reports
        // note that they are not involved in the reverse fromKarateJson()
        map.put("name", feature.getName());
        map.put("description", feature.getDescription());
        map.put("durationMillis", getDurationMillis());
        map.put("passedCount", getPassedCount());
        map.put("failedCount", getFailedCount());
        map.put("packageQualifiedName", feature.getPackageQualifiedName());
        map.put("relativePath", feature.getResource().getRelativePath());
        //======================================================================
        if (resultDate == null) {
            resultDate = ReportUtils.getDateString();
        }
        map.put("resultDate", resultDate);
        map.put("prefixedPath", feature.getResource().getPrefixedPath());
        List> list = new ArrayList(scenarioResults.size());
        map.put("scenarioResults", list);
        for (ScenarioResult sr : scenarioResults) {
            list.add(sr.toKarateJson());
        }
        if (callArg != null) {
            String json = JsonUtils.toJsonSafe(callArg, false);
            map.put("callArg", JsonUtils.fromJson(json));
        }
        map.put("loopIndex", loopIndex);
        map.put("callDepth", callDepth);
        return map;
    }

    public Map toCucumberJson() {
        Map map = new HashMap();
        map.put("keyword", Feature.KEYWORD);
        map.put("line", feature.getLine());
        map.put("uri", displayName);
        map.put("name", displayName);
        map.put("id", StringUtils.toIdString(feature.getName()));
        String temp = feature.getName() == null ? "" : feature.getName();
        if (feature.getDescription() != null) {
            temp = temp + "\n" + feature.getDescription();
        }
        map.put("description", temp.trim());
        if (feature.getTags() != null) {
            map.put("tags", ScenarioResult.tagsToCucumberJson(feature.getTags()));
        }
        List> list = new ArrayList(scenarioResults.size());
        map.put("elements", list);
        for (ScenarioResult sr : scenarioResults) {
            Map backgroundMap = sr.backgroundToCucumberJson();
            if (backgroundMap != null) {
                list.add(backgroundMap);
            }
            list.add(sr.toCucumberJson());
        }
        return map;
    }

    public List getAllScenarioStepResultsNotHidden() {
        List list = new ArrayList();
        for (ScenarioResult sr : scenarioResults) {
            list.addAll(sr.getStepResultsNotHidden());
        }
        return list;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public Feature getFeature() {
        return feature;
    }

    public String getDisplayName() {
        return displayName;
    }

    public KarateException getErrorMessagesCombined() {
        List errors = getErrors();
        if (errors.size() == 1) {
            return new KarateException(errors.get(0));
        }
        return new KarateException(getErrorMessages());
    }

    public String getErrorMessages() {
        return StringUtils.join(getErrors(), "\n");
    }

    public String getCallNameForReport() {
        String append = loopIndex == -1 ? "" : "[" + loopIndex + "] ";
        return append + displayName;
    }

    public String getCallArgPretty() {
        if (callArg == null) {
            return null;
        }
        try {
            return JsonUtils.toJsonSafe(callArg, true);
        } catch (Throwable t) {
            return "#error: " + t.getMessage();
        }
    }

    public void setCallDepth(int callDepth) {
        this.callDepth = callDepth;
    }

    public Map getCallArg() {
        return callArg;
    }

    public void setCallArg(Map callArg) {
        this.callArg = callArg;
    }

    public int getLoopIndex() {
        return loopIndex;
    }

    public void setLoopIndex(int loopIndex) {
        this.loopIndex = loopIndex;
    }

    public double getDurationMillis() {
        long durationNanos = 0;
        for (ScenarioResult sr : scenarioResults) {
            durationNanos += sr.getDurationNanos();
        }
        return ReportUtils.nanosToMillis(durationNanos);
    }

    public int getFailedCount() {
        return getErrors().size();
    }

    public boolean isEmpty() {
        return scenarioResults.isEmpty();
    }

    public int getScenarioCount() {
        return scenarioResults.size();
    }

    public int getPassedCount() {
        return getScenarioCount() - getFailedCount();
    }

    public boolean isFailed() {
        return getFailedCount() > 0;
    }

    public List getErrors() {
        List errors = new ArrayList();
        for (ScenarioResult sr : scenarioResults) {
            if (sr.isFailed()) {
                errors.add(sr.getErrorMessage());
            }
        }
        return errors;
    }

    public void addResult(ScenarioResult result) {
        scenarioResults.add(result);
    }

    public void setVariables(Map resultVariables) {
        this.resultVariables = resultVariables;
    }

    public Map getVariables() {
        // edge case if no scenarios were run
        return resultVariables == null ? new HashMap() : resultVariables;
    }

    public void setConfig(Config config) {
        this.config = config;
    }

    public Config getConfig() {
        return config;
    }

    public void sortScenarioResults() {
        Collections.sort(scenarioResults);
    }

    public List getScenarioResults() {
        return scenarioResults;
    }

    @Override
    public String toString() {
        return displayName;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy