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

com.liferay.jenkins.results.parser.test.clazz.group.QAWebsitesFunctionalBatchTestClassGroup Maven / Gradle / Ivy

The newest version!
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.jenkins.results.parser.test.clazz.group;

import com.google.common.collect.Lists;

import com.liferay.jenkins.results.parser.GitWorkingDirectory;
import com.liferay.jenkins.results.parser.JenkinsResultsParserUtil;
import com.liferay.jenkins.results.parser.PortalTestClassJob;
import com.liferay.jenkins.results.parser.QAWebsitesGitRepositoryJob;
import com.liferay.jenkins.results.parser.job.property.JobProperty;
import com.liferay.jenkins.results.parser.test.clazz.TestClass;
import com.liferay.poshi.core.PoshiContext;
import com.liferay.poshi.core.util.GetterUtil;
import com.liferay.poshi.core.util.MathUtil;
import com.liferay.poshi.core.util.PropsUtil;

import java.io.File;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.json.JSONObject;

/**
 * @author Michael Hashimoto
 */
public class QAWebsitesFunctionalBatchTestClassGroup
	extends FunctionalBatchTestClassGroup {

	@Override
	public List getTestBaseDirs() {
		List testBaseDirs = new ArrayList<>();

		GitWorkingDirectory qaWebsitesGitWorkingDirectory =
			_getQAWebsitesGitWorkingDirectory();

		QAWebsitesGitRepositoryJob qaWebsitesGitRepositoryJob =
			_getQAWebsitesGitRepositoryJob();

		for (String projectName :
				qaWebsitesGitRepositoryJob.getProjectNames()) {

			testBaseDirs.add(
				new File(
					qaWebsitesGitWorkingDirectory.getWorkingDirectory(),
					projectName));
		}

		return testBaseDirs;
	}

	protected QAWebsitesFunctionalBatchTestClassGroup(
		JSONObject jsonObject, PortalTestClassJob portalTestClassJob) {

		super(jsonObject, portalTestClassJob);
	}

	protected QAWebsitesFunctionalBatchTestClassGroup(
		String batchName, PortalTestClassJob portalTestClassJob) {

		super(batchName, portalTestClassJob);
	}

	protected int getAxisMaxSize(File testBaseDir) {
		JobProperty jobProperty = getJobProperty(
			"test.batch.axis.max.size", testSuiteName, testBaseDir,
			JobProperty.Type.QA_WEBSITES_TEST_DIR);

		String jobPropertyValue = jobProperty.getValue();

		if (JenkinsResultsParserUtil.isInteger(jobPropertyValue)) {
			recordJobProperty(jobProperty);

			return Integer.parseInt(jobPropertyValue);
		}

		return AXES_SIZE_MAX_DEFAULT;
	}

	@Override
	protected String getDefaultTestBatchRunPropertyQuery(
		File testBaseDir, String testSuiteName) {

		String query = System.getenv("TEST_QA_WEBSITES_PROPERTY_QUERY");

		if (JenkinsResultsParserUtil.isNullOrEmpty(query)) {
			query = getBuildStartProperty("TEST_QA_WEBSITES_PROPERTY_QUERY");
		}

		if (!JenkinsResultsParserUtil.isNullOrEmpty(query)) {
			return query;
		}

		JobProperty jobProperty = getJobProperty(
			"test.batch.property.query", testSuiteName, testBaseDir,
			JobProperty.Type.QA_WEBSITES_TEST_DIR);

		recordJobProperty(jobProperty);

		return jobProperty.getValue();
	}

	@Override
	protected List> getPoshiTestClassGroups(File testBaseDir) {
		String query = getTestBatchRunPropertyQuery(testBaseDir);

		if (JenkinsResultsParserUtil.isNullOrEmpty(query)) {
			return new ArrayList<>();
		}

		synchronized (portalTestClassJob) {
			String testBaseDirPath = null;

			if ((testBaseDir != null) && testBaseDir.exists()) {
				testBaseDirPath = JenkinsResultsParserUtil.getCanonicalPath(
					testBaseDir);
			}

			Properties properties = JenkinsResultsParserUtil.getProperties(
				new File(testBaseDir.getParentFile(), "test.properties"),
				new File(testBaseDir, "poshi-ext.properties"),
				new File(testBaseDir, "poshi.properties"),
				new File(testBaseDir, "test.properties"));

			if (!JenkinsResultsParserUtil.isNullOrEmpty(testBaseDirPath)) {
				properties.setProperty("test.base.dir.name", testBaseDirPath);
			}

			PropsUtil.clear();

			PropsUtil.setProperties(properties);

			try {
				PoshiContext.clear();

				PoshiContext.readFiles();

				JobProperty jobProperty = getJobProperty(
					"test.batch.axis.count", testSuiteName, testBaseDir,
					JobProperty.Type.QA_WEBSITES_TEST_DIR);

				if (jobProperty.getValue() != null) {
					return getTestClassGroups(
						_getTestBatchGroupsByAxisCount(
							query,
							GetterUtil.getInteger(jobProperty.getValue())));
				}

				return getTestClassGroups(
					PoshiContext.getTestBatchGroups(
						query, getAxisMaxSize(testBaseDir)));
			}
			catch (Exception exception) {
				throw new RuntimeException(exception);
			}
		}
	}

	private QAWebsitesGitRepositoryJob _getQAWebsitesGitRepositoryJob() {
		if (!(portalTestClassJob instanceof QAWebsitesGitRepositoryJob)) {
			throw new RuntimeException(
				"Invalid job type " + portalTestClassJob);
		}

		return (QAWebsitesGitRepositoryJob)portalTestClassJob;
	}

	private GitWorkingDirectory _getQAWebsitesGitWorkingDirectory() {
		QAWebsitesGitRepositoryJob qaWebsitesGitRepositoryJob =
			_getQAWebsitesGitRepositoryJob();

		return qaWebsitesGitRepositoryJob.getGitWorkingDirectory();
	}

	private List> _getTestBatchGroupsByAxisCount(
			String pqlQuery, long axisCount)
		throws Exception {

		List classCommandNames = PoshiContext.executePQLQuery(
			pqlQuery, false);

		long testCount = classCommandNames.size();

		long groupSize = MathUtil.quotient(testCount, axisCount, true);

		return Lists.partition(
			classCommandNames, GetterUtil.getInteger(groupSize));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy