Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@Data
@DataFile(fileName = "selcukes.yaml", streamLoader = true)
public class Environment {
public class Config {
private String projectName;
private String env;
private String baseUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
@UtilityClass
public class ConfigFactory {
private static final String DEFAULT_LOG_BACK_FILE = "selcukes-logback.yaml";
private static final SingletonContext<Environment> ENVIRONMENT_CONTEXT = SingletonContext
.with(() -> DataMapper.parse(Environment.class));
private static final SingletonContext<Config> CONFIG_CONTEXT = SingletonContext
.with(() -> DataMapper.parse(Config.class));

public static Environment getConfig() {
return ENVIRONMENT_CONTEXT.get();
public static Config getConfig() {
return CONFIG_CONTEXT.get();
}

public static void cleanupConfig() {
ENVIRONMENT_CONTEXT.remove();
CONFIG_CONTEXT.remove();
}

public void loadLoggerProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@
import static io.github.selcukes.commons.properties.SelcukesTestProperties.TIMESTAMP_REPORT;
import static io.github.selcukes.commons.properties.SelcukesTestProperties.setSystemProperty;
import static io.github.selcukes.databind.utils.Clocks.DATE_TIME_FILE_FORMAT;
import static io.github.selcukes.databind.utils.StringHelper.isNullOrEmpty;
import static java.util.Optional.ofNullable;
import static io.github.selcukes.databind.utils.StringHelper.isNonEmpty;

@CustomLog
@UtilityClass
public class SelcukesRuntime {
public void loadOptions() {
try {
SelcukesTestProperties properties = new SelcukesTestProperties();
String features = ofNullable(properties.getSubstitutedConfigProperty(FEATURES)).orElse("");
String glue = ofNullable(properties.getCucumberProperty(GLUE)).orElse("");
String tag = ofNullable(properties.getCucumberProperty(TAGS)).orElse("");
var properties = new SelcukesTestProperties();
String features = properties.getSubstitutedConfigProperty(FEATURES);
String glue = properties.getCucumberProperty(GLUE);
String tag = properties.getCucumberProperty(TAGS);
String additionalPlugin = properties.getCucumberProperty(PLUGIN);
String reportsPath = ofNullable(properties.getReportsProperty(REPORTS_PATH)).orElse("target");
String timestampReport = properties.getReportsProperty(TIMESTAMP_REPORT);
String emailReport = properties.getReportsProperty(EMAIL_REPORT);
String reportsFile = ofNullable(properties.getReportsProperty(REPORTS_FILE)).orElse("TestReport");
String reportsPath = properties.getReportsProperty(REPORTS_PATH).orElse("target");
String timestampReport = properties.getReportsProperty(TIMESTAMP_REPORT).orElse("");
String emailReport = properties.getReportsProperty(EMAIL_REPORT).orElse("");
String reportsFile = properties.getReportsProperty(REPORTS_FILE).orElse("TestReport");

String cucumberReportPath = reportsPath + "/cucumber-reports";
String extentReportPath = reportsPath + "/extent-reports";
Expand All @@ -59,10 +58,10 @@ public void loadOptions() {
String plugin = String.format("html:%s/%s%s.html, json:%s/cucumber%s.json",
cucumberReportPath, reportsFile, timestamp, cucumberReportPath, timestamp);

if (!isNullOrEmpty(additionalPlugin)) {
if (isNonEmpty(additionalPlugin)) {
plugin += "," + additionalPlugin;
}
if (!isNullOrEmpty(emailReport) && !emailReport.equalsIgnoreCase("false")) {
if (isNonEmpty(emailReport) && !emailReport.equalsIgnoreCase("false")) {
setSystemProperty("extent.reporter.spark.start", "true");
setSystemProperty("extent.reporter.spark.out",
String.format("%s/%s.html", extentReportPath, reportsFile));
Expand All @@ -71,7 +70,7 @@ public void loadOptions() {
"dashboard,test,category,exception,author,device,log");
setSystemProperty("systeminfo.Platform", Platform.getPlatform().getOsName());
setSystemProperty("systeminfo.Environment", ConfigFactory.getConfig().getEnv());
plugin += "," + "io.github.selcukes.extent.report.SelcukesExtentAdapter:";
plugin += "," + "io.github.selcukes.extent.report.SelcukesExtentAdapter";
}
setSystemProperty("cucumber.plugin", plugin);
setSystemProperty("cucumber.features", features);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

package io.github.selcukes.commons.properties;

import io.github.selcukes.commons.config.Config;
import io.github.selcukes.commons.config.ConfigFactory;
import io.github.selcukes.databind.utils.StringHelper;
import lombok.CustomLog;

import static io.github.selcukes.commons.config.ConfigFactory.getConfig;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import static java.util.Optional.ofNullable;

@CustomLog
public class SelcukesTestProperties {
Expand All @@ -39,37 +45,33 @@ public class SelcukesTestProperties {
public static final String CRYPTO_KEY = "selcukes.crypto.key";

public static void setSystemProperty(String key, String value) {
if (!StringHelper.isNullOrEmpty(value)) {
if (StringHelper.isNonEmpty(value)) {
System.setProperty(key, value);
}
}

private String getProperty(String propertyKey, Function<Config, Map<String, String>> mapGetter) {
return ofNullable(System.getProperty(propertyKey))
.orElseGet(() -> {
String key = propertyKey.substring(propertyKey.lastIndexOf(".") + 1);
return mapGetter.apply(ConfigFactory.getConfig()).get(key);
});
}

public String getExcelProperty(String propertyKey) {
if (System.getProperty(propertyKey) != null) {
return System.getProperty(propertyKey);
}
String key = propertyKey.substring(propertyKey.lastIndexOf(".") + 1);
return getConfig().getExcel().getOrDefault(key, "");
return ofNullable(getProperty(propertyKey, Config::getExcel)).orElse("");
}

public String getCucumberProperty(String propertyKey) {
if (System.getProperty(propertyKey) != null) {
return System.getProperty(propertyKey);
}
String key = propertyKey.substring(propertyKey.lastIndexOf(".") + 1);
return getConfig().getCucumber().getOrDefault(key, "");
return ofNullable(getProperty(propertyKey, Config::getCucumber)).orElse("");
}

public String getReportsProperty(String propertyKey) {
if (System.getProperty(propertyKey) != null) {
return System.getProperty(propertyKey);
}
String key = propertyKey.substring(propertyKey.lastIndexOf(".") + 1);
return getConfig().getReports().getOrDefault(key, "");
public Optional<String> getReportsProperty(String propertyKey) {
return ofNullable(getProperty(propertyKey, Config::getReports));
}

public String getSubstitutedConfigProperty(String propertyKey) {
return StringHelper.interpolate(getCucumberProperty(propertyKey),
this::getCucumberProperty);
return ofNullable(StringHelper.interpolate(getCucumberProperty(propertyKey),
this::getCucumberProperty)).orElse("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
import static org.testng.Assert.assertTrue;

public class PropertiesTest {
private final static String PROPS_FILE = "target/temp.properties";
private final static Path PROPS_FILE = Path.of("target/temp.properties");
private final Logger logger = LoggerFactory.getLogger(getClass());

@Test
public void createPropertyFileTest() {
Map<String, String> data = Map.of("userName", "QA",
"Time", Clocks.timeStamp());
Path filePath = Path.of(PROPS_FILE);
PropertiesMapper.write(filePath.toAbsolutePath().toString(), data);
assertTrue(filePath.toFile().exists());

PropertiesMapper.write(PROPS_FILE, data);
assertTrue(PROPS_FILE.toFile().exists());
}

@Test(dependsOnMethods = "createPropertyFileTest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
import io.github.selcukes.databind.collections.Maps;
import io.github.selcukes.databind.exception.DataMapperException;
import io.github.selcukes.databind.utils.DataFileHelper;
import io.github.selcukes.databind.utils.Resources;
import lombok.experimental.UtilityClass;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;

/**
* The type Properties mapper.
* The PropertiesMapper class provides utilities for parsing, writing and
* updating
* <p>
* property files and maps.
*/
@UtilityClass
public class PropertiesMapper {
Expand Down Expand Up @@ -56,63 +58,63 @@ public <T> T parse(final Class<T> entityClass) {
/**
* It takes a property file and returns a map of the properties
*
* @param propertyFile The name of the property file to parse.
* @return A map of the properties in the file.
* @param filePath The path of the property file to parse.
* @return A map of the properties in the file.
*/
public static Map<String, String> parse(final String propertyFile) {
return Maps.of(PropertiesLoader.getProperties(Path.of(propertyFile)));
public static Map<String, String> parse(final Path filePath) {
return Maps.of(PropertiesLoader.getProperties(filePath));
}

/**
* > This function writes the dataMap to the propertyFile
*
* @param propertyFile The path to the property file.
* @param dataMap A map of key-value pairs to be written to the
* property file.
* @param filePath The path to the property file.
* @param dataMap A map of key-value pairs to be written to the property
* file.
*/
public static void write(final String propertyFile, final Map<String, String> dataMap) {
write(propertyFile, new Properties(), dataMap);
public static void write(final Path filePath, final Map<String, String> dataMap) {
write(filePath, new Properties(), dataMap);
}

private static void write(
final String propertyFile, final Properties linkedProperties, final Map<String, String> dataMap
final Path filePath, final Properties properties, final Map<String, String> dataMap
) {
dataMap.forEach(linkedProperties::setProperty);
write(propertyFile, linkedProperties);
dataMap.forEach(properties::setProperty);
write(filePath, properties);
}

private static void write(final String propertyFile, final Properties properties) {
try (OutputStream output = new FileOutputStream(propertyFile)) {
private static void write(final Path filePath, final Properties properties) {
try (var output = Resources.newOutputStream(filePath)) {
properties.store(output, null);
} catch (Exception e) {
throw new DataMapperException("Could not write property file '" + propertyFile + "'", e);
throw new DataMapperException("Could not write property file '" + filePath + "'", e);
}
}

/**
* It loads the properties file, sets the property, and writes the file back
* to disk
*
* @param propertyFile The path to the properties file.
* @param key the key of the property you want to update
* @param value the value to be written to the property file
* @param filePath The path to the properties file.
* @param key the key of the property you want to update
* @param value the value to be written to the property file
*/
public static void updateProperty(final String propertyFile, final String key, final String value) {
Properties properties = PropertiesLoader.getProperties(Path.of(propertyFile));
public static void updateProperty(final Path filePath, final String key, final String value) {
Properties properties = PropertiesLoader.getProperties(filePath);
properties.setProperty(key, value);
write(propertyFile, properties);
write(filePath, properties);
}

/**
* It takes a property file and a map of key-value pairs, and updates the
* property file with the key-value pairs
*
* @param propertyFile The path to the properties file.
* @param dataMap A map of key-value pairs that you want to update in
* the properties file.
* @param filePath The path to the properties file.
* @param dataMap A map of key-value pairs that you want to update in the
* properties file.
*/
public static void updateProperties(final String propertyFile, final Map<String, String> dataMap) {
write(propertyFile, PropertiesLoader.getProperties(Path.of(propertyFile)), dataMap);
public static void updateProperties(final Path filePath, final Map<String, String> dataMap) {
write(filePath, PropertiesLoader.getProperties(filePath), dataMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package io.github.selcukes.databind.utils;

import io.github.selcukes.databind.exception.DataMapperException;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
Expand Down Expand Up @@ -192,4 +194,16 @@ public static InputStream newFileStream(String filePath) {
}
return null;
}

/**
* Returns a new output stream that writes to the file with the specified
* file path.
*
* @param filePath the path of the file to write to
* @return a new output stream that writes to the specified file
*/
@SneakyThrows
public static OutputStream newOutputStream(Path filePath) {
return Files.newOutputStream(filePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ExcelTestRunner extends SelcukesTestNGRunner {
public void setUpExcel(ITestContext context) {
var testProperties = new SelcukesTestProperties();
SingleExcelData.init();
if (!testProperties.getExcelProperty(EXCEL_RUNNER).equalsIgnoreCase("false")) {
if (testProperties.getExcelProperty(EXCEL_RUNNER).equalsIgnoreCase("true")) {
runScenarios = SingleExcelData.getScenariosToRun();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ExcelTestRunner2 extends SelcukesTestNGRunner {
public void setUpExcel(ITestContext context) {
var testProperties = new SelcukesTestProperties();
MultiExcelData.init();
if (!testProperties.getExcelProperty(EXCEL_RUNNER).equalsIgnoreCase("false")) {
if (testProperties.getExcelProperty(EXCEL_RUNNER).equalsIgnoreCase("true")) {
runScenarios = MultiExcelData.getScenariosToRun();
}
}
Expand Down
Loading