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

Commit 6fe4a31

Browse files
authored
Merge pull request #33 from sbtqa/feature/32-relative-ref-collection
Add relative reference collection support
2 parents 5239dd1 + 0af052c commit 6fe4a31

19 files changed

Lines changed: 268 additions & 190 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
*.iml
77
.classpath
88
.vscode/
9-
.settings/
9+
.settings/
10+
.DS_Store
11+
**/.factorypath

datajack-api/src/main/java/ru/sbtqa/tag/datajack/providers/AbstractDataProvider.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class AbstractDataProvider implements TestDataProvider {
4040
protected String way;
4141
protected String path;
4242
protected Class<? extends GeneratorCallback> callback;
43-
private BasicDBObject rootObject;
43+
protected BasicDBObject rootObject;
4444

4545
private static boolean isArray(String key) {
4646
return key.matches(ARRAY_MATCHER_REGEX);
@@ -257,10 +257,8 @@ private BasicDBObject parseComplexDBObject(String key) throws DataException {
257257
}
258258

259259
if (isReference(currentBasicObject)) {
260-
String referenceCollection = ((BasicDBObject) currentBasicObject.get(VALUE_TPL)).getString(COLLECTION_TPL);
261-
String referencePath = ((BasicDBObject) currentBasicObject.get(VALUE_TPL)).getString("path");
262-
AbstractDataProvider dataProvider = (AbstractDataProvider) createInstance(referenceCollection).get(referencePath);
263-
currentBasicObject = dataProvider.basicObject;
260+
AbstractDataProvider dataProvider = (AbstractDataProvider) createInstance(currentBasicObject, collectionName);
261+
currentBasicObject = ((AbstractDataProvider)dataProvider.getReference()).basicObject;
264262
}
265263

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

292-
private void setRootObject(BasicDBObject rootObject, String path) {
290+
public void setRootObject(BasicDBObject rootObject, String path) {
293291
this.rootObject = rootObject;
294292
this.path = path;
295293
}
@@ -383,7 +381,7 @@ public boolean isReference() {
383381
return isReference(this.basicObject);
384382
}
385383

386-
private boolean isReference(BasicDBObject basicDBObject) {
384+
protected boolean isReference(BasicDBObject basicDBObject) {
387385
Object value = basicDBObject.get(VALUE_TPL);
388386
if (!(value instanceof BasicDBObject)) {
389387
return false;

providers/excel-provider/src/main/java/ru/sbtqa/tag/datajack/providers/excel/ExcelDataProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727
import static java.lang.String.format;
2828

29-
@SuppressWarnings("deprecation")
3029
public class ExcelDataProvider extends AbstractDataProvider {
3130

3231
private static final Logger LOG = LoggerFactory.getLogger(ExcelDataProvider.class);
3332
private static final String DEFAULT_EXTENSION = "xlsx";
33+
private static final String REF_TPL = "$ref:";
3434
private final XSSFWorkbook workBook;
3535
private final String dataFileName;
3636
private XSSFFormulaEvaluator evaluator;
@@ -212,13 +212,13 @@ private String getName(XSSFRow row) {
212212
}
213213

214214
private boolean isLink(XSSFRow row) {
215-
return getCellValue(row.getCell(2)).contains("link:");
215+
return getCellValue(row.getCell(2)).startsWith(REF_TPL);
216216
}
217217

218218
private BasicDBObject getLink(XSSFRow row) {
219-
String linkPath = getCellValue(row.getCell(2)).replace("link:", "");
219+
String linkPath = getCellValue(row.getCell(2)).replace(REF_TPL, "");
220220
BasicDBObject link = new BasicDBObject();
221-
String[] fullPathDelimited = linkPath.split("[.]", 2);
221+
String[] fullPathDelimited = linkPath.split("[:]", 2);
222222
// Link to another sheetName (sheet)
223223
link.append(COLLECTION_TPL, fullPathDelimited[0]);
224224
link.append("path", fullPathDelimited[1]);
579 Bytes
Binary file not shown.
645 Bytes
Binary file not shown.

providers/json-provider/src/main/java/ru/sbtqa/tag/datajack/providers/json/JsonDataProvider.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
public class JsonDataProvider extends AbstractDataProvider {
1616

1717
private static final String DEFAULT_EXTENSION = "json";
18+
private static final String REF_TPL = "$ref";
1819
private final String extension;
1920
private String testDataFolder;
2021

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

114+
/**
115+
* {@inheritDoc}*
116+
*/
117+
@Override
118+
public boolean isReference(BasicDBObject basicDBObject) {
119+
Object value = basicDBObject.get(REF_TPL);
120+
return value instanceof String;
121+
}
122+
123+
public TestDataProvider getReference() throws DataException {
124+
if (isReference(this.basicObject)) {
125+
if (this.rootObject == null) {
126+
this.rootObject = this.basicObject;
127+
} else {
128+
String rootJson = this.rootObject.toJson();
129+
String baseJson = this.basicObject.toJson();
130+
if (rootJson.equals(baseJson)) {
131+
throw new CyclicReferencesException("Cyclic references in database:\n" + rootJson);
132+
}
133+
}
134+
String refValue = this.basicObject.getString(REF_TPL);
135+
String referencedCollection = refValue.contains(":") ? refValue.split(":")[0] : this.collectionName;
136+
String collectionPrefix = refValue.startsWith("/") ? "" :this.collectionName.substring(0, this.collectionName.lastIndexOf("/") + 1);
137+
this.path = refValue.contains(":") ? refValue.split(":")[1] : refValue;
138+
AbstractDataProvider reference = (AbstractDataProvider) this.fromCollection(collectionPrefix + referencedCollection);
139+
reference.setRootObject(this.rootObject, collectionPrefix + referencedCollection + "." + this.path);
140+
return reference.get(this.path);
141+
} else {
142+
throw new ReferenceException(String.format("There is no reference in \"%s\". Collection \"%s\"",
143+
this.path, this.collectionName));
144+
}
145+
}
146+
147+
113148
/**
114149
* {@inheritDoc}
115150
*/
@@ -124,7 +159,8 @@ public TestDataProvider fromCollection(String collName) throws DataException {
124159

125160
private String readFile(String testDataFolder, String collectionName) throws CollectionNotFoundException {
126161
try {
127-
return readFileToString(new File(testDataFolder + separator + collectionName + "." + this.extension), "UTF-8");
162+
File targetFile = new File(testDataFolder + separator + collectionName + "." + this.extension);
163+
return readFileToString(targetFile, "UTF-8");
128164
} catch (IOException ex) {
129165
throw new CollectionNotFoundException(String.format("File %s.json not found in %s",
130166
collectionName, testDataFolder), ex);

providers/json-provider/src/test/java/ru/sbtqa/tag/datajack/providers/json/JsonDataTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ public void failWithCyclicReference() throws DataException {
234234

235235
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, collectionName);
236236

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

240239
expectDataExceptions
241240
.expect(CyclicReferencesException.class);
@@ -402,4 +401,28 @@ public void getJsonTest() throws DataException {
402401
String expectedJson = "{ \"login\" : 123 , \"password\" : 123}";
403402
Assert.assertEquals(expectedJson, stringJson);
404403
}
404+
405+
@Test
406+
public void relativeTest() throws DataException {
407+
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, "relative/relative1");
408+
String relativeValue = testDataProvider.get("relates to relative2").getValue();
409+
String expected = "123";
410+
Assert.assertEquals(expected, relativeValue);
411+
}
412+
413+
@Test
414+
public void relativeRootTest() throws DataException {
415+
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, "relative/relative1");
416+
String relativeValue = testDataProvider.get("relates to root").getValue();
417+
String expected = "20.91";
418+
Assert.assertEquals(expected, relativeValue);
419+
}
420+
421+
@Test
422+
public void relativeParentTest() throws DataException {
423+
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, "relative/relative1");
424+
String relativeValue = testDataProvider.get("relates to parent").getValue();
425+
String expected = "20.91";
426+
Assert.assertEquals(expected, relativeValue);
427+
}
405428
}

providers/json-provider/src/test/resources/json/DataBlocks.json

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
"gendata": "generate:Numeric:16"
99
},
1010
"gendata reference": {
11-
"value": {
12-
"collection": "DataBlocks",
13-
"path": "Common.gen gen.gendata"
14-
},
11+
"$ref": "DataBlocks:Common.gen gen.gendata",
1512
"comment": "Ссылается на генерированные данные"
1613
},
1714
"login": {
@@ -23,33 +20,21 @@
2320
"comment": "Пароль пользователя"
2421
},
2522
"password2": {
26-
"value": {
27-
"collection": "DataBlocks",
28-
"path": "Params Group 1.password"
29-
},
23+
"$ref": "DataBlocks:Params Group 1.password",
3024
"comment": "Пароль пользователя"
3125
},
3226
"cyclic": {
33-
"value": {
34-
"collection": "DataBlocks",
35-
"path": "Common.cyclic"
36-
},
27+
"$ref": "DataBlocks:Common.cyclic",
3728
"comment": "Cyclic"
3829
}
3930
},
4031
"Params Group 1": {
4132
"login": {
42-
"value": {
43-
"collection": "DataBlocks",
44-
"path": "Common.password"
45-
},
33+
"$ref": "Common.password",
4634
"comment": "Логин пользователя"
4735
},
4836
"password": {
49-
"value": {
50-
"collection": "DataBlocks",
51-
"path": "Common.password"
52-
},
37+
"$ref": "DataBlocks:Common.password",
5338
"comment": "Another cool password"
5439
}
5540
},
@@ -65,9 +50,6 @@
6550
}
6651
},
6752
"ref array": {
68-
"value": {
69-
"collection": "Tests",
70-
"path": "array"
71-
}
53+
"$ref": "Tests:array"
7254
}
7355
}
Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,52 @@
11
{
2-
"testId": "@234234",
3-
"Nullable": null,
4-
"Common": {
5-
"price": 20.91,
6-
"gendata": "generate:Numeric:16",
7-
"gen gen": {
8-
"gendata": "generate:Numeric:16"
9-
},
10-
"gendata reference": {
11-
"value": {
12-
"collection": "JsonP",
13-
"path": "Common.gen gen.gendata"
14-
},
15-
"comment": "Ссылается на генерированные данные"
16-
},
17-
"login": {
18-
"value": "user",
19-
"comment": "Логин пользователя"
20-
},
21-
"password": {
22-
"value": "123qwe",
23-
"comment": "Пароль пользователя"
24-
},
25-
"password2": {
26-
"value": {
27-
"collection": "JsonP",
28-
"path": "Params Group 1.password"
29-
},
30-
"comment": "Пароль пользователя"
31-
},
32-
"cyclic": {
33-
"value": {
34-
"collection": "JsonP",
35-
"path": "Common.cyclic"
36-
},
37-
"comment": "Cyclic"
38-
}
2+
"testId": "@234234",
3+
"Nullable": null,
4+
"Common": {
5+
"price": 20.91,
6+
"gendata": "generate:Numeric:16",
7+
"gen gen": {
8+
"gendata": "generate:Numeric:16"
399
},
40-
"Params Group 1": {
41-
"login": {
42-
"value": {
43-
"collection": "JsonP",
44-
"path": "Common.password"
45-
},
46-
"comment": "Логин пользователя"
47-
},
48-
"password": {
49-
"value": {
50-
"collection": "JsonP",
51-
"path": "Common.password"
52-
},
53-
"comment": "Another cool password"
54-
}
10+
"gendata reference": {
11+
"$ref": "JsonP:Common.gen gen.gendata",
12+
"comment": "Ссылается на генерированные данные"
5513
},
56-
"MapTests": {
57-
"stringValue": "this is string",
58-
"singleCharValue": "c",
59-
"booleanValue": true,
60-
"integerValue": 42,
61-
"floatValue": 42.1,
62-
"doubleValue": 42.01,
63-
"nestedObjectValue": {
64-
"innerValue": "This is string from nested object"
65-
}
14+
"login": {
15+
"value": "user",
16+
"comment": "Логин пользователя"
17+
},
18+
"password": {
19+
"value": "123qwe",
20+
"comment": "Пароль пользователя"
21+
},
22+
"password2": {
23+
"$ref": "JsonP:Params Group 1.password",
24+
"comment": "Пароль пользователя"
25+
},
26+
"cyclic": {
27+
"$ref": "JsonP:Common.cyclic",
28+
"comment": "Cyclic"
29+
}
30+
},
31+
"Params Group 1": {
32+
"login": {
33+
"$ref": "JsonP:Common.password",
34+
"comment": "Логин пользователя"
35+
},
36+
"password": {
37+
"$ref": "JsonP:Common.password",
38+
"comment": "Another cool password"
39+
}
40+
},
41+
"MapTests": {
42+
"stringValue": "this is string",
43+
"singleCharValue": "c",
44+
"booleanValue": true,
45+
"integerValue": 42,
46+
"floatValue": 42.1,
47+
"doubleValue": 42.01,
48+
"nestedObjectValue": {
49+
"innerValue": "This is string from nested object"
6650
}
51+
}
6752
}

0 commit comments

Comments
 (0)