Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #145 from sbtqa/datajack-4
Browse files Browse the repository at this point in the history
Update to jatajack-4.0.1
  • Loading branch information
clicman authored Aug 31, 2018
2 parents 7384b6b + bbaf69a commit 2b5059e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 93 deletions.
61 changes: 32 additions & 29 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.sbtqa.tag</groupId>
<artifactId>page-factory</artifactId>
Expand Down Expand Up @@ -43,7 +44,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.cucumber>2.4.0</version.cucumber>
<cucumber.version>2.4.0</cucumber.version>
<datajack.version>4.0.1</datajack.version>
</properties>
<distributionManagement>
<snapshotRepository>
Expand All @@ -62,30 +64,6 @@
<version>1.3.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag</groupId>
<artifactId>datajack</artifactId>
<version>1.4.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack.adaptors</groupId>
<artifactId>datajack-json-adaptor</artifactId>
<version>1.2.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack.adaptors</groupId>
<artifactId>datajack-properties-adaptor</artifactId>
<version>1.1.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack.adaptors</groupId>
<artifactId>datajack-excel-adaptor</artifactId>
<version>1.1.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag</groupId>
<artifactId>allure-helper</artifactId>
Expand All @@ -95,17 +73,17 @@
<groupId>ru.sbtqa.tag</groupId>
<artifactId>video-recorder</artifactId>
<version>1.0.6</version>
<type>jar</type>
<type>jar</type>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${version.cucumber}</version>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${version.cucumber}</version>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>ru.sbtqa.htmlelements</groupId>
Expand Down Expand Up @@ -194,6 +172,31 @@
<artifactId>webdrivermanager</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack</groupId>
<artifactId>datajack-api</artifactId>
<version>${datajack.version}</version>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack.providers</groupId>
<artifactId>json-provider</artifactId>
<version>${datajack.version}</version>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack.providers</groupId>
<artifactId>properties-provider</artifactId>
<version>${datajack.version}</version>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack.providers</groupId>
<artifactId>excel-provider</artifactId>
<version>${datajack.version}</version>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.datajack.providers</groupId>
<artifactId>mongo-provider</artifactId>
<version>${datajack.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
package ru.sbtqa.tag.pagefactory.support.data;

import ru.sbtqa.tag.datajack.TestDataObject;
import ru.sbtqa.tag.datajack.adaptors.ExcelDataObjectAdaptor;
import ru.sbtqa.tag.datajack.adaptors.json.JsonDataObjectAdaptor;
import ru.sbtqa.tag.datajack.adaptors.properties.PropertiesDataObjectAdaptor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import ru.sbtqa.tag.datajack.TestDataProvider;
import ru.sbtqa.tag.datajack.exceptions.DataException;
import ru.sbtqa.tag.datajack.providers.ExcelDataProvider;
import ru.sbtqa.tag.datajack.providers.MongoDataProvider;
import ru.sbtqa.tag.datajack.providers.json.JsonDataProvider;
import ru.sbtqa.tag.datajack.providers.properties.PropertiesDataProvider;
import ru.sbtqa.tag.qautils.properties.Props;

public class DataFactory {

private static TestDataObject dataContainer;
private static TestDataProvider testDataProvider;
private static String configCollection;

public static TestDataObject getInstance() throws DataException {
if (dataContainer == null) {
public static TestDataProvider getDataProvider() throws DataException {
if (testDataProvider == null) {
configCollection = Props.get("data.initial.collection", null);
String dataType = Props.get("data.type", "stash");

switch (dataType) {
case "json":
dataContainer = new JsonDataObjectAdaptor(
testDataProvider = new JsonDataProvider(
Props.get("data.folder"),
Props.get("data.initial.collection"),
Props.get("data.extension", "json")
);
break;
case "properties":
dataContainer = new PropertiesDataObjectAdaptor(
testDataProvider = new PropertiesDataProvider(
Props.get("data.folder"),
Props.get("data.initial.collection"),
Props.get("data.extension", "properties")
);
break;
case "excel":
dataContainer = new ExcelDataObjectAdaptor(
testDataProvider = new ExcelDataProvider(
Props.get("data.folder"),
Props.get("data.initial.collection")
);
break;
case "mongo":
testDataProvider = new MongoDataProvider(
new MongoClient(new MongoClientURI(Props.get("data.uri"))).getDB("data.db"),
Props.get("data.initial.collection")
);
break;
default:
throw new DataException(String.format("Data adaptor %s isn't supported", dataType));
}
}
return dataContainer;
return testDataProvider;
}

public static void updateCollection(TestDataObject newObject) {
dataContainer = newObject;
public static void updateCollection(TestDataProvider newObject) {
testDataProvider = newObject;
}

public static String getConfigCollection() {
Expand Down
68 changes: 17 additions & 51 deletions src/main/java/ru/sbtqa/tag/pagefactory/support/data/DataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.reflect.FieldUtils;
import ru.sbtqa.tag.datajack.TestDataObject;
import ru.sbtqa.tag.datajack.exceptions.DataException;
import static ru.sbtqa.tag.datajack.providers.AbstractDataProvider.PATH_PARSE_REGEX;

public class DataParser {

private static final String STEP_PARSE_REGEX = "(:?\\$([\\w|-|\\s|_\\d]+)?(\\{[^\\}|^\\$|^\\{]+\\}))+";
private static final String TAG_PARSE_REGEX = "(?:\\$([^\\{]+)(\\{([^\\}]+)\\})?)";

private String featureDataTag;
private String currentScenarioTag;
private String featureDataTagValue;
private String currentScenarioDataTagValue;

public void replaceDataPlaceholders(List<CucumberFeature> cucumberFeatures) throws DataException, IllegalAccessException {

Expand Down Expand Up @@ -83,63 +80,36 @@ private Node replaceArgumentPlaceholders(Node argument) throws DataException {
}

private String replaceDataPlaceholders(String raw) throws DataException {
Pattern stepDataPattern = Pattern.compile(STEP_PARSE_REGEX);
Pattern stepDataPattern = Pattern.compile(PATH_PARSE_REGEX);
Matcher stepDataMatcher = stepDataPattern.matcher(raw);
StringBuffer replacedStep = new StringBuffer(raw);
// Offet to handle diff between placeholder length and value length
int offset = 0;

while (stepDataMatcher.find()) {
String collection = stepDataMatcher.group(2);
String value = stepDataMatcher.group(3);

if (value == null) {
continue;
}
if (collection != null) {
DataFactory.updateCollection(DataFactory.getInstance().fromCollection(collection.replace("$", "")));
String collection = stepDataMatcher.group(1);
String value = stepDataMatcher.group(2);

replacedStep = replacedStep.replace(stepDataMatcher.start(2) + offset, stepDataMatcher.end(2) + offset, "");
offset -= collection.length();
} else {
String tag = currentScenarioTag != null ? currentScenarioTag : featureDataTag;

if (tag != null) {
parseTestDataObject(tag);
}
if (collection == null) {
parseDataTagValue(currentScenarioDataTagValue != null ? currentScenarioDataTagValue : featureDataTagValue);
}

String dataPath = normalizeValue(value);
String parsedValue = DataFactory.getInstance().get(dataPath).getValue();
replacedStep = replacedStep.replace(stepDataMatcher.start(3) - 1 + offset, stepDataMatcher.end(3) + offset, parsedValue);
offset += parsedValue.length() - 1 - value.length();
String builtPath = collection == null ? "$" + value : "$" + collection + value;
String parsedValue = DataFactory.getDataProvider().getByPath(builtPath).getValue();
replacedStep = replacedStep.replace(stepDataMatcher.start(), stepDataMatcher.end(), parsedValue);
stepDataMatcher = stepDataPattern.matcher(replacedStep);
}
return replacedStep.toString();
}

private void parseTestDataObject(String tag) throws DataException {
Pattern tagPattern = Pattern.compile(TAG_PARSE_REGEX);
Matcher tagMatcher = tagPattern.matcher(tag.trim());

if (tagMatcher.matches()) {
String collection = tagMatcher.group(1);
String value = tagMatcher.group(2);
TestDataObject tdo = DataFactory.getInstance().fromCollection(collection);

if (value != null) {
tdo = tdo.get(normalizeValue(value));
}

DataFactory.updateCollection(tdo);
}
private void parseDataTagValue(String tag) throws DataException {
DataFactory.updateCollection(DataFactory.getDataProvider().getByPath(tag));
}

private void setFeatureDataTag(String featureDataTag) {
this.featureDataTag = featureDataTag;
this.featureDataTagValue = featureDataTag;
}

private void setCurrentScenarioTag(String currentScenarioTag) {
this.currentScenarioTag = currentScenarioTag;
private void setCurrentScenarioTag(String currentScenarioDataTag) {
this.currentScenarioDataTagValue = currentScenarioDataTag;
}

private Node replaceDataTablePlaceholders(DataTable dataTable) throws DataException {
Expand Down Expand Up @@ -176,8 +146,4 @@ private List<TableRow> replaceTableRows(List<TableRow> tableRows) throws DataExc
}
return resultTableRows;
}

private String normalizeValue(String value) {
return value.replace("$", "").replace("{", "").replace("}", "");
}
}

0 comments on commit 2b5059e

Please sign in to comment.