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

com.hp.octane.integrations.uft.ufttestresults.UftTestResultsUtils Maven / Gradle / Ivy

There is a newer version: 2.24.3.5
Show newest version
package com.hp.octane.integrations.uft.ufttestresults;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.hp.octane.integrations.dto.tests.TestRunResult;
import com.hp.octane.integrations.uft.ufttestresults.schema.*;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

public class UftTestResultsUtils {

    private static Set notFinalNodeType = new HashSet<>(Arrays.asList("Iteration", "Action", "Context"));

    public static List getErrorData(File file) {
        ReportResults result = fromXml(file, ReportResults.class);
        List errors = new ArrayList<>();
        List parents = new ArrayList<>();
        getErrorDataInternal(result.getReportNode(), parents, errors);
        return errors;
    }

    public static String getAggregatedErrorMessage(List errors) {
        String err = errors.stream()
                .map(e -> e.getMessage().trim() + ((e.getResult().equalsIgnoreCase("Warning")) ? " (Warning)" : ""))
                .map(msg -> msg + (msg.endsWith(".") ? "" : ". "))
                .distinct()
                .collect(Collectors.joining("\n"));
        return err;
    }

    public static List getMBTData(File file) {
        ReportResults result = fromXml(file, ReportResults.class);
        final ArrayList iterationReportNodes = new ArrayList<>();
        getMBTIterationsInternal(result.getReportNode(), new ArrayList<>(), iterationReportNodes, "Iteration", 2);
        List iterations = new ArrayList<>();
        iterationReportNodes.forEach(reportNode -> {
            List steps = new ArrayList<>();
            getMBTDataInternal(reportNode, new ArrayList<>(), steps, "Action", 3);
            iterations.add(new UftResultIterationData(steps, reportNode.getData().getDuration()));
        });
        return iterations;
    }

    private static void getErrorDataInternal(ReportNode node, List parents, List errors) {
        parents.add(node.getData().getName());
        boolean hasDescription = isNotEmpty(node.getData().getDescription());
        boolean failed = "Failed".equalsIgnoreCase(node.getData().getResult()) || "Warning".equalsIgnoreCase(node.getData().getResult());
        if (failed) {
            if (!notFinalNodeType.contains(node.getType()) && hasDescription) {
                //String parentStr = String.join("/", parents.subList(1, parents.size()));
                String error = /*parentStr + ":" +*/ (isNotEmpty(node.getData().getErrorText()) ? node.getData().getErrorText() : node.getData().getDescription());
                error = error.replace("Verify that this object's properties match an object currently displayed in your application.", "")
                        .replace("\n", "")
                        .replace(" ", " ")
                        .trim();

                //last parent name might be as error message - in this case - don't show last parent
                if (!parents.isEmpty() && error.startsWith(parents.get(parents.size() - 1))) {
                    parents.remove(parents.size() - 1);
                }
                errors.add(new UftResultStepData(parents, node.getType(), node.getData().getResult(), error, node.getData().getDuration()));
            }
            if (node.getNodes() != null) {
                node.getNodes().forEach(n -> getErrorDataInternal(n, new ArrayList<>(parents), errors));
            }
        }
    }

    private static void getMBTIterationsInternal(ReportNode node, List parents, List results, String nodeType, int targetLevel) {
        parents.add(node.getData().getName());

        if (parents.size() == targetLevel && nodeType.equals(node.getType())) {
            results.add(node);
        }

        if (node.getNodes() != null && parents.size() < targetLevel) {
            node.getNodes().forEach(n -> getMBTIterationsInternal(n, new ArrayList<>(parents), results, nodeType, targetLevel));
        }
    }

    private static void getMBTDataInternal(ReportNode node, List parents, List results, String nodeType, int targetLevel) {
        parents.add(node.getData().getName());

        boolean failed = "Failed".equalsIgnoreCase(node.getData().getResult()) || "Warning".equalsIgnoreCase(node.getData().getResult());


        if (parents.size() == targetLevel && nodeType.equals(node.getType())) {

            String errorMessage = "";
            if (failed) {

                List errors = new ArrayList<>();
                getErrorDataInternal(node, new ArrayList(parents), errors);
                errorMessage = getAggregatedErrorMessage((errors));
            }

            List inputParameters = null;
            if (node.getData().getInputParameters() != null) {
                inputParameters = node.getData().getInputParameters().stream().map(parameter -> new UftResultStepParameter(parameter.getName(), parameter.getValue(), parameter.getType())).collect(Collectors.toList());
            }
            List outputParameters = null;
            if (node.getData().getOutputParameters() != null) {
                outputParameters = node.getData().getOutputParameters().stream().map(parameter -> new UftResultStepParameter(parameter.getName(), parameter.getValue(), parameter.getType())).collect(Collectors.toList());
            }
            String result = node.getData().getResult().equalsIgnoreCase("Warning") ? TestRunResult.PASSED.value() : node.getData().getResult();
            results.add(new UftResultStepData(parents, node.getType(), result, errorMessage, node.getData().getDuration(), inputParameters, outputParameters));
        }

        if (node.getNodes() != null && parents.size() < targetLevel) {
            node.getNodes().forEach(n -> getMBTDataInternal(n, new ArrayList<>(parents), results, nodeType, targetLevel));
        }
    }

    private static boolean isNotEmpty(String str) {
        return !(str == null || str.isEmpty());
    }

    public static  T fromXml(File xml, Class clazz) {
        try {
            XmlMapper xmlMapper = new XmlMapper();
            xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            T obj = xmlMapper.readValue(xml, clazz);

            return obj;
        } catch (Exception e) {
            throw new IllegalStateException("Error while deserializing a XML file to Object of type " + clazz, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy