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

org.catools.common.testng.utils.CXmlSuiteUtils Maven / Gradle / Ivy

The newest version!
package org.catools.common.testng.utils;

import lombok.experimental.UtilityClass;
import org.catools.common.collections.CHashMap;
import org.catools.common.collections.CList;
import org.catools.common.collections.CSet;
import org.catools.common.testng.CTestNGConfigs;
import org.catools.common.utils.CObjectUtil;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import javax.annotation.Nullable;
import java.util.function.Consumer;

@UtilityClass
public class CXmlSuiteUtils {
  public static XmlTest buildTestForIssueKeys(CSet issueIds, String testName, boolean filterTestsWhichWillSkipInRun) {
    CSet classNameForIssueKeys = CTestClassUtil.getClassNameForIssueKeys(issueIds, filterTestsWhichWillSkipInRun);
    return buildTestForClasses(classNameForIssueKeys, testName);
  }

  public static XmlSuite buildTestSuiteForClasses(CHashMap> testClasses, String suiteName, @Nullable Consumer xmlSuiteAdjuster) {
    CList tests = testClasses.asSet().mapToList(e -> buildTestForClasses(e.getValue(), e.getKey()));
    return buildTestSuiteForTests(tests, suiteName, xmlSuiteAdjuster);
  }

  public static XmlTest buildTestForClasses(CSet testClasses, String testName) {
    XmlTest xmlTest = new XmlTest();
    xmlTest.setJUnit(false);
    xmlTest.setName(testName);

    // Sort list
    CList listForSorting = testClasses.toList();
    listForSorting.sort(String::compareTo);
    xmlTest.setXmlClasses(listForSorting.mapToList(XmlClass::new));
    return xmlTest;
  }

  public static XmlSuite buildTestSuiteForTests(CList tests, String suiteName, @Nullable Consumer xmlSuiteAdjuster) {
    XmlSuite xmlSuite = new XmlSuite();
    xmlSuite.setName(suiteName);
    xmlSuite.setJUnit(false);
    xmlSuite.setAllowReturnValues(true);
    if (!XmlSuite.ParallelMode.NONE.equals(CTestNGConfigs.getSuiteLevelParallel())) {
      xmlSuite.setParallel(CTestNGConfigs.getSuiteLevelParallel());
    }

    if (CTestNGConfigs.getSuiteLevelThreadCount() > 0) {
      xmlSuite.setThreadCount(CTestNGConfigs.getSuiteLevelThreadCount());
    }

    tests.forEach(test -> {
      if (!XmlSuite.ParallelMode.NONE.equals(CTestNGConfigs.getTestLevelParallel())) {
        test.setParallel(CTestNGConfigs.getTestLevelParallel());
      }

      if (CTestNGConfigs.getTestLevelThreadCount() > 0) {
        test.setThreadCount(CTestNGConfigs.getTestLevelThreadCount());
      }

      test.setSuite(xmlSuite);
      xmlSuite.addTest(test);
    });

    if (xmlSuiteAdjuster != null) {
      xmlSuiteAdjuster.accept(xmlSuite);
    }
    return xmlSuite;
  }

  public static XmlSuite copy(XmlSuite xmlSuite, String suiteNamePostfix) {
    XmlSuite suite = CObjectUtil.clone(xmlSuite);
    suite.setName(suite.getName() + suiteNamePostfix);
    return suite;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy