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

com.teamscale.test_impacted.engine.options.TestEngineOptionUtils Maven / Gradle / Ivy

Go to download

A JUnit 5 engine that handles retrieving impacted tests from Teamscale and organizes their execution

There is a newer version: 34.2.0
Show newest version
package com.teamscale.test_impacted.engine.options;

import com.teamscale.client.CommitDescriptor;
import com.teamscale.client.StringUtils;
import org.junit.platform.engine.ConfigurationParameters;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;

/** Utility class for {@link TestEngineOptions}. */
public class TestEngineOptionUtils {

	/** Returns the {@link TestEngineOptions} configured in the {@link Properties}. */
	public static TestEngineOptions getEngineOptions(ConfigurationParameters configurationParameters) {
		PrefixingPropertyReader propertyReader = new PrefixingPropertyReader("teamscale.test.impacted.",
				configurationParameters);
		ServerOptions serverOptions = null;
		Boolean runImpacted = propertyReader.getBoolean("runImpacted", true);
		if (runImpacted) {
			serverOptions = ServerOptions.builder()
					.url(propertyReader.getString("server.url"))
					.project(propertyReader.getString("server.project"))
					.userName(propertyReader.getString("server.userName"))
					.userAccessToken(propertyReader.getString("server.userAccessToken"))
					.build();
		}

		return TestEngineOptions.builder()
				.serverOptions(serverOptions)
				.partition(propertyReader.getString("partition"))
				.runImpacted(runImpacted)
				.runAllTests(propertyReader.getBoolean("runAllTests", false))
				.includeAddedTests(propertyReader.getBoolean("includeAddedTests", true))
				.includeFailedAndSkipped(propertyReader.getBoolean("includeFailedAndSkipped", true))
				.endCommit(propertyReader.getCommitDescriptor("endCommit"))
				.endRevision(propertyReader.getString("endRevision"))
				.baseline(propertyReader.getString("baseline"))
				.baselineRevision(propertyReader.getString("baselineRevision"))
				.repository(propertyReader.getString("repository"))
				.agentUrls(propertyReader.getStringList("agentsUrls"))
				.includedTestEngineIds(propertyReader.getStringList("includedEngines"))
				.excludedTestEngineIds(propertyReader.getStringList("excludedEngines"))
				.reportDirectory(propertyReader.getString("reportDirectory"))
				.build();
	}

	/**
	 * Throws an {@link AssertionError} if the given value is blank.
	 */
	static void assertNotBlank(String value, String message) {
		if (StringUtils.isBlank(value)) {
			throw new AssertionError(message);
		}
	}

	/**
	 * Throws an {@link AssertionError} if the given value is null.
	 */
	static void assertNotNull(Object value, String message) {
		if (value == null) {
			throw new AssertionError(message);
		}
	}

	private static class PrefixingPropertyReader {

		private final ConfigurationParameters configurationParameters;

		private final String prefix;

		private PrefixingPropertyReader(String prefix, ConfigurationParameters configurationParameters) {
			this.prefix = prefix;
			this.configurationParameters = configurationParameters;
		}

		private  T getOrNull(String propertyName, Function mapper) {
			return get(propertyName, mapper, null);
		}

		private  T get(String propertyName, Function mapper, T defaultValue) {
			return configurationParameters.get(prefix + propertyName).map(mapper).orElse(defaultValue);
		}

		private String getString(String propertyName) {
			return getOrNull(propertyName, Function.identity());
		}

		private Boolean getBoolean(String propertyName, boolean defaultValue) {
			return get(propertyName, Boolean::valueOf, defaultValue);
		}

		private CommitDescriptor getCommitDescriptor(String propertyName) {
			return getOrNull(propertyName, CommitDescriptor::parse);
		}

		private List getStringList(String propertyName) {
			return get(propertyName, listAsString -> {
				if (StringUtils.isEmpty(listAsString)) {
					return Collections.emptyList();
				}

				return Arrays.asList(listAsString.split(","));
			}, Collections.emptyList());
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy