Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #33 from sbtqa/feature/32-relative-ref-collection
Browse files Browse the repository at this point in the history
Add relative reference collection support
  • Loading branch information
clicman authored Apr 8, 2019
2 parents 5239dd1 + 0af052c commit 6fe4a31
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 190 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
*.iml
.classpath
.vscode/
.settings/
.settings/
.DS_Store
**/.factorypath
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class AbstractDataProvider implements TestDataProvider {
protected String way;
protected String path;
protected Class<? extends GeneratorCallback> callback;
private BasicDBObject rootObject;
protected BasicDBObject rootObject;

private static boolean isArray(String key) {
return key.matches(ARRAY_MATCHER_REGEX);
Expand Down Expand Up @@ -257,10 +257,8 @@ private BasicDBObject parseComplexDBObject(String key) throws DataException {
}

if (isReference(currentBasicObject)) {
String referenceCollection = ((BasicDBObject) currentBasicObject.get(VALUE_TPL)).getString(COLLECTION_TPL);
String referencePath = ((BasicDBObject) currentBasicObject.get(VALUE_TPL)).getString("path");
AbstractDataProvider dataProvider = (AbstractDataProvider) createInstance(referenceCollection).get(referencePath);
currentBasicObject = dataProvider.basicObject;
AbstractDataProvider dataProvider = (AbstractDataProvider) createInstance(currentBasicObject, collectionName);
currentBasicObject = ((AbstractDataProvider)dataProvider.getReference()).basicObject;
}

Object currentValue = currentBasicObject.get(partialKey);
Expand Down Expand Up @@ -289,7 +287,7 @@ public String toString() {
return this.basicObject == null ? "" : this.basicObject.toString();
}

private void setRootObject(BasicDBObject rootObject, String path) {
public void setRootObject(BasicDBObject rootObject, String path) {
this.rootObject = rootObject;
this.path = path;
}
Expand Down Expand Up @@ -383,7 +381,7 @@ public boolean isReference() {
return isReference(this.basicObject);
}

private boolean isReference(BasicDBObject basicDBObject) {
protected boolean isReference(BasicDBObject basicDBObject) {
Object value = basicDBObject.get(VALUE_TPL);
if (!(value instanceof BasicDBObject)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

import static java.lang.String.format;

@SuppressWarnings("deprecation")
public class ExcelDataProvider extends AbstractDataProvider {

private static final Logger LOG = LoggerFactory.getLogger(ExcelDataProvider.class);
private static final String DEFAULT_EXTENSION = "xlsx";
private static final String REF_TPL = "$ref:";
private final XSSFWorkbook workBook;
private final String dataFileName;
private XSSFFormulaEvaluator evaluator;
Expand Down Expand Up @@ -212,13 +212,13 @@ private String getName(XSSFRow row) {
}

private boolean isLink(XSSFRow row) {
return getCellValue(row.getCell(2)).contains("link:");
return getCellValue(row.getCell(2)).startsWith(REF_TPL);
}

private BasicDBObject getLink(XSSFRow row) {
String linkPath = getCellValue(row.getCell(2)).replace("link:", "");
String linkPath = getCellValue(row.getCell(2)).replace(REF_TPL, "");
BasicDBObject link = new BasicDBObject();
String[] fullPathDelimited = linkPath.split("[.]", 2);
String[] fullPathDelimited = linkPath.split("[:]", 2);
// Link to another sheetName (sheet)
link.append(COLLECTION_TPL, fullPathDelimited[0]);
link.append("path", fullPathDelimited[1]);
Expand Down
Binary file modified providers/excel-provider/src/test/resources/excell/TestData.xlsx
Binary file not shown.
Binary file modified providers/excel-provider/src/test/resources/excell/TestDataNew.xlsx
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class JsonDataProvider extends AbstractDataProvider {

private static final String DEFAULT_EXTENSION = "json";
private static final String REF_TPL = "$ref";
private final String extension;
private String testDataFolder;

Expand Down Expand Up @@ -110,6 +111,40 @@ protected JsonDataProvider createInstance(BasicDBObject obj, String collectionNa
return new JsonDataProvider(testDataFolder, obj, collectionName, extension);
}

/**
* {@inheritDoc}*
*/
@Override
public boolean isReference(BasicDBObject basicDBObject) {
Object value = basicDBObject.get(REF_TPL);
return value instanceof String;
}

public TestDataProvider getReference() throws DataException {
if (isReference(this.basicObject)) {
if (this.rootObject == null) {
this.rootObject = this.basicObject;
} else {
String rootJson = this.rootObject.toJson();
String baseJson = this.basicObject.toJson();
if (rootJson.equals(baseJson)) {
throw new CyclicReferencesException("Cyclic references in database:\n" + rootJson);
}
}
String refValue = this.basicObject.getString(REF_TPL);
String referencedCollection = refValue.contains(":") ? refValue.split(":")[0] : this.collectionName;
String collectionPrefix = refValue.startsWith("/") ? "" :this.collectionName.substring(0, this.collectionName.lastIndexOf("/") + 1);
this.path = refValue.contains(":") ? refValue.split(":")[1] : refValue;
AbstractDataProvider reference = (AbstractDataProvider) this.fromCollection(collectionPrefix + referencedCollection);
reference.setRootObject(this.rootObject, collectionPrefix + referencedCollection + "." + this.path);
return reference.get(this.path);
} else {
throw new ReferenceException(String.format("There is no reference in \"%s\". Collection \"%s\"",
this.path, this.collectionName));
}
}


/**
* {@inheritDoc}
*/
Expand All @@ -124,7 +159,8 @@ public TestDataProvider fromCollection(String collName) throws DataException {

private String readFile(String testDataFolder, String collectionName) throws CollectionNotFoundException {
try {
return readFileToString(new File(testDataFolder + separator + collectionName + "." + this.extension), "UTF-8");
File targetFile = new File(testDataFolder + separator + collectionName + "." + this.extension);
return readFileToString(targetFile, "UTF-8");
} catch (IOException ex) {
throw new CollectionNotFoundException(String.format("File %s.json not found in %s",
collectionName, testDataFolder), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ public void failWithCyclicReference() throws DataException {

TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, collectionName);

String cyclicObject = format("{ \"value\" : { \"collection\" : \"%s\", "
+ "\"path\" : \"Common.cyclic\" }, \"comment\" : \"Cyclic\"", collectionName);
String cyclicObject = format("{ \"$ref\" : \"%s:Common.cyclic\", \"comment\" : \"Cyclic\"", collectionName);

expectDataExceptions
.expect(CyclicReferencesException.class);
Expand Down Expand Up @@ -402,4 +401,28 @@ public void getJsonTest() throws DataException {
String expectedJson = "{ \"login\" : 123 , \"password\" : 123}";
Assert.assertEquals(expectedJson, stringJson);
}

@Test
public void relativeTest() throws DataException {
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, "relative/relative1");
String relativeValue = testDataProvider.get("relates to relative2").getValue();
String expected = "123";
Assert.assertEquals(expected, relativeValue);
}

@Test
public void relativeRootTest() throws DataException {
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, "relative/relative1");
String relativeValue = testDataProvider.get("relates to root").getValue();
String expected = "20.91";
Assert.assertEquals(expected, relativeValue);
}

@Test
public void relativeParentTest() throws DataException {
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, "relative/relative1");
String relativeValue = testDataProvider.get("relates to parent").getValue();
String expected = "20.91";
Assert.assertEquals(expected, relativeValue);
}
}
30 changes: 6 additions & 24 deletions providers/json-provider/src/test/resources/json/DataBlocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
"gendata": "generate:Numeric:16"
},
"gendata reference": {
"value": {
"collection": "DataBlocks",
"path": "Common.gen gen.gendata"
},
"$ref": "DataBlocks:Common.gen gen.gendata",
"comment": "Ссылается на генерированные данные"
},
"login": {
Expand All @@ -23,33 +20,21 @@
"comment": "Пароль пользователя"
},
"password2": {
"value": {
"collection": "DataBlocks",
"path": "Params Group 1.password"
},
"$ref": "DataBlocks:Params Group 1.password",
"comment": "Пароль пользователя"
},
"cyclic": {
"value": {
"collection": "DataBlocks",
"path": "Common.cyclic"
},
"$ref": "DataBlocks:Common.cyclic",
"comment": "Cyclic"
}
},
"Params Group 1": {
"login": {
"value": {
"collection": "DataBlocks",
"path": "Common.password"
},
"$ref": "Common.password",
"comment": "Логин пользователя"
},
"password": {
"value": {
"collection": "DataBlocks",
"path": "Common.password"
},
"$ref": "DataBlocks:Common.password",
"comment": "Another cool password"
}
},
Expand All @@ -65,9 +50,6 @@
}
},
"ref array": {
"value": {
"collection": "Tests",
"path": "array"
}
"$ref": "Tests:array"
}
}
109 changes: 47 additions & 62 deletions providers/json-provider/src/test/resources/json/JsonP.jsonp
Original file line number Diff line number Diff line change
@@ -1,67 +1,52 @@
{
"testId": "@234234",
"Nullable": null,
"Common": {
"price": 20.91,
"gendata": "generate:Numeric:16",
"gen gen": {
"gendata": "generate:Numeric:16"
},
"gendata reference": {
"value": {
"collection": "JsonP",
"path": "Common.gen gen.gendata"
},
"comment": "Ссылается на генерированные данные"
},
"login": {
"value": "user",
"comment": "Логин пользователя"
},
"password": {
"value": "123qwe",
"comment": "Пароль пользователя"
},
"password2": {
"value": {
"collection": "JsonP",
"path": "Params Group 1.password"
},
"comment": "Пароль пользователя"
},
"cyclic": {
"value": {
"collection": "JsonP",
"path": "Common.cyclic"
},
"comment": "Cyclic"
}
"testId": "@234234",
"Nullable": null,
"Common": {
"price": 20.91,
"gendata": "generate:Numeric:16",
"gen gen": {
"gendata": "generate:Numeric:16"
},
"Params Group 1": {
"login": {
"value": {
"collection": "JsonP",
"path": "Common.password"
},
"comment": "Логин пользователя"
},
"password": {
"value": {
"collection": "JsonP",
"path": "Common.password"
},
"comment": "Another cool password"
}
"gendata reference": {
"$ref": "JsonP:Common.gen gen.gendata",
"comment": "Ссылается на генерированные данные"
},
"MapTests": {
"stringValue": "this is string",
"singleCharValue": "c",
"booleanValue": true,
"integerValue": 42,
"floatValue": 42.1,
"doubleValue": 42.01,
"nestedObjectValue": {
"innerValue": "This is string from nested object"
}
"login": {
"value": "user",
"comment": "Логин пользователя"
},
"password": {
"value": "123qwe",
"comment": "Пароль пользователя"
},
"password2": {
"$ref": "JsonP:Params Group 1.password",
"comment": "Пароль пользователя"
},
"cyclic": {
"$ref": "JsonP:Common.cyclic",
"comment": "Cyclic"
}
},
"Params Group 1": {
"login": {
"$ref": "JsonP:Common.password",
"comment": "Логин пользователя"
},
"password": {
"$ref": "JsonP:Common.password",
"comment": "Another cool password"
}
},
"MapTests": {
"stringValue": "this is string",
"singleCharValue": "c",
"booleanValue": true,
"integerValue": 42,
"floatValue": 42.1,
"doubleValue": 42.01,
"nestedObjectValue": {
"innerValue": "This is string from nested object"
}
}
}
Loading

0 comments on commit 6fe4a31

Please sign in to comment.