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

com.liferay.jenkins.results.parser.test.suite.RelevantTestSuite Maven / Gradle / Ivy

There is a newer version: 1.0.1492
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2024 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.suite;

import com.liferay.jenkins.results.parser.JenkinsResultsParserUtil;
import com.liferay.jenkins.results.parser.PortalAcceptancePullRequestJob;
import com.liferay.jenkins.results.parser.PortalGitWorkingDirectory;
import com.liferay.jenkins.results.parser.job.property.JobProperty;
import com.liferay.jenkins.results.parser.test.batch.TestBatch;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Kenji Heigel
 */
public class RelevantTestSuite {

	public RelevantTestSuite(
		PortalAcceptancePullRequestJob portalAcceptancePullRequestJob) {

		PortalGitWorkingDirectory portalGitWorkingDirectory =
			portalAcceptancePullRequestJob.getPortalGitWorkingDirectory();

		_modifiedFiles = portalGitWorkingDirectory.getModifiedFilesList();
		_portalGitWorkingDirectory = portalGitWorkingDirectory;

		_relevantRuleEngine = RelevantRuleEngine.getInstance(
			portalAcceptancePullRequestJob);
	}

	public List getTestBatches() {
		File baseTestPropertiesFile = new File(
			_relevantRuleEngine.getBaseDir(), "test.properties");

		String testBatchNamesPropertyValue =
			JenkinsResultsParserUtil.getProperty(
				JenkinsResultsParserUtil.getProperties(baseTestPropertiesFile),
				"test.batch.names[relevant]");

		if (testBatchNamesPropertyValue == null) {
			throw new RuntimeException(
				"Please set test.batch.names[relevant] in " +
					baseTestPropertiesFile);
		}

		try {
			RelevantRuleValidation.validate(
				_portalGitWorkingDirectory.getGitRepositoryName(),
				_portalGitWorkingDirectory.getUpstreamBranchName());
		}
		catch (IOException ioException) {
			throw new RuntimeException(ioException);
		}

		List validTestBatchNames = Arrays.asList(
			testBatchNamesPropertyValue.split(","));

		List testBatches = new ArrayList<>();

		List relevantRules =
			_relevantRuleEngine.getMatchingRelevantRules(_modifiedFiles);

		Collections.sort(relevantRules);

		System.out.println(
			"There are " + relevantRules.size() + " matching relevant rules: " +
				_relevantRuleEngine.getRelevantRuleNames(relevantRules));

		for (RelevantRule relevantRule : relevantRules) {
			for (TestBatch testBatch : relevantRule.getTestBatches()) {
				if (testBatches.contains(testBatch)) {
					TestBatch existingTestBatch = testBatches.get(
						testBatches.indexOf(testBatch));

					existingTestBatch.merge(testBatch);

					continue;
				}

				if (!validTestBatchNames.isEmpty() &&
					validTestBatchNames.contains(testBatch.getName())) {

					testBatches.add(testBatch);
				}
			}

			_testBatchNamesJobProperties.addAll(
				relevantRule.getTestBatchNamesJobProperties());
		}

		Collections.sort(testBatches);

		return testBatches;
	}

	public Set getTestBatchNamesJobProperties() {
		return _testBatchNamesJobProperties;
	}

	public void setModifiedFiles(List modifiedFiles) {
		_modifiedFiles = modifiedFiles;
	}

	private List _modifiedFiles;
	private final PortalGitWorkingDirectory _portalGitWorkingDirectory;
	private final RelevantRuleEngine _relevantRuleEngine;
	private final Set _testBatchNamesJobProperties =
		new HashSet<>();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy