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

hygieia.transformer.JunitXmlToTestCapabilityTransformerV2 Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
package hygieia.transformer;

import com.capitalone.dashboard.model.TestCapability;
import com.capitalone.dashboard.model.TestCase;
import com.capitalone.dashboard.model.TestCaseStatus;
import com.capitalone.dashboard.model.TestSuite;
import com.capitalone.dashboard.model.TestSuiteType;
import com.capitalone.dashboard.model.quality.JunitXmlReportV2;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class JunitXmlToTestCapabilityTransformerV2 {

    private TestSuite parseFeatureAsTestSuites(JunitXmlReportV2.TestSuite testJunit) {
        TestSuite suite = new TestSuite();
        suite.setType(TestSuiteType.Unit);
        suite.setDescription(testJunit.getName());

        long duration = 0;

        int testCaseTotalCount = testJunit.getTestcase().size();
        int testCaseSkippedCount = 0, testCaseSuccessCount = 0, testCaseFailCount = 0, testCaseUnknownCount = 0;

        for (JunitXmlReportV2.TestSuite.Testcase testCase : testJunit.getTestcase()) {
            TestCase testCaseJunit = parseScenarioAsTestCase(testCase);
            if (testCase.getTime() != null) {
                duration += testCase.getTime().longValue();
            }

            switch (testCaseJunit.getStatus()) {
                case Success:
                    testCaseSuccessCount++;
                    break;
                case Failure:
                    testCaseFailCount++;
                    break;
                case Skipped:
                    testCaseSkippedCount++;
                    break;
                default:
                    testCaseUnknownCount++;
                    break;
            }

            suite.getTestCases().add(testCaseJunit);
        }
        suite.setSuccessTestCaseCount(testCaseSuccessCount);
        suite.setFailedTestCaseCount(testCaseFailCount);
        suite.setSkippedTestCaseCount(testCaseSkippedCount);
        suite.setTotalTestCaseCount(testCaseTotalCount);
        suite.setUnknownStatusCount(testCaseUnknownCount);
        suite.setDuration(duration);

        if (testCaseFailCount > 0) {
            suite.setStatus(TestCaseStatus.Failure);
        } else if (testCaseSkippedCount > 0) {
            suite.setStatus(TestCaseStatus.Skipped);
        } else if (testCaseSuccessCount > 0) {
            suite.setStatus(TestCaseStatus.Success);
        } else {
            suite.setStatus(TestCaseStatus.Unknown);
        }

        return suite;
    }


    public TestCapability convert(JunitXmlReportV2.TestSuite testJunit) {
        List testSuites = new ArrayList<>();
        testSuites.add(this.parseFeatureAsTestSuites(testJunit));
        return this.processTestSuites(testSuites);
    }

    private TestCase parseScenarioAsTestCase(JunitXmlReportV2.TestSuite.Testcase scenarioElement) {
        TestCase testCase = new TestCase();
        testCase.setDescription( scenarioElement.getName());
        // Parse each step as a TestCase
        int testStepSuccessCount = 0, testStepFailCount = 0, testStepSkippedCount = 0, testStepUnknownCount = 0;
        long testDuration=0;

        if (scenarioElement.getTime() != null) {
            testDuration += scenarioElement.getTime().longValue();
        }
        if(scenarioElement.getError() != null){
            testStepFailCount++;
        }else if (StringUtils.isNotBlank(scenarioElement.getSkipped()) && scenarioElement.getTime() != null && scenarioElement.getTime().compareTo(BigDecimal.ZERO) == 0){
            testStepSkippedCount++;
        }else if(scenarioElement.getTime() != null && scenarioElement.getTime().doubleValue() > 0){
            testStepSuccessCount++;
        }else {
            testStepUnknownCount++;
        }
        // Set Duration
        testCase.setDuration(testDuration);
        testCase.setSuccessTestStepCount(testStepSuccessCount);
        testCase.setSkippedTestStepCount(testStepSkippedCount);
        testCase.setFailedTestStepCount(testStepFailCount);
        testCase.setUnknownStatusCount(testStepUnknownCount);
        testCase.setTotalTestStepCount(testCase.getTestSteps().size());
        // Set Status
        if (testStepFailCount > 0) {
            testCase.setStatus(TestCaseStatus.Failure);
        } else if (testStepSkippedCount > 0) {
            testCase.setStatus(TestCaseStatus.Skipped);
        } else if (testStepSuccessCount > 0) {
            testCase.setStatus(TestCaseStatus.Success);
        } else {
            testCase.setStatus(TestCaseStatus.Unknown);
        }
        return testCase;
    }


    private TestCapability processTestSuites(List testSuites){
        TestCapability cap = new TestCapability();
        cap.setType(TestSuiteType.Unit);

        cap.getTestSuites().addAll(testSuites);
        //add test suites
        long duration = 0;
        int testSuiteSkippedCount = 0, testSuiteSuccessCount = 0, testSuiteFailCount = 0, testSuiteUnknownCount = 0;
        for (TestSuite t : testSuites) {
            duration += t.getDuration();
            switch (t.getStatus()) {
                case Success:
                    testSuiteSuccessCount++;
                    break;
                case Failure:
                    testSuiteFailCount++;
                    break;
                case Skipped:
                    testSuiteSkippedCount++;
                    break;
                default:
                    testSuiteUnknownCount++;
                    break;
            }
        }
        if (testSuiteFailCount > 0) {
            cap.setStatus(TestCaseStatus.Failure);
        } else if (testSuiteSkippedCount > 0) {
            cap.setStatus(TestCaseStatus.Skipped);
        } else if (testSuiteSuccessCount > 0) {
            cap.setStatus(TestCaseStatus.Success);
        } else {
            cap.setStatus(TestCaseStatus.Unknown);
        }
        cap.setFailedTestSuiteCount(testSuiteFailCount);
        cap.setSkippedTestSuiteCount(testSuiteSkippedCount);
        cap.setSuccessTestSuiteCount(testSuiteSuccessCount);
        cap.setUnknownStatusTestSuiteCount(testSuiteUnknownCount);
        cap.setTotalTestSuiteCount(testSuites.size());
        cap.setDuration(duration);
        return cap;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy