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

Commit 41ebab7

Browse files
authored
Merge pull request #28 from sbtqa/arraysFix
Array deep reference fix
2 parents 268f388 + cc3efdd commit 41ebab7

7 files changed

Lines changed: 90 additions & 9 deletions

File tree

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,19 @@ private Object parseArray(BasicDBObject basicO, String key) throws DataException
207207
if (!isArray(key)) {
208208
throw new DataException(String.format("%s.%s is not an array!", this.collectionName, key));
209209
}
210+
210211
String arrayKey = key.split("\\[")[0];
211212
String arrayIndex = key.split("\\[")[1].split("\\]")[0];
213+
214+
// If object passed by key is a reference to array, extract reference and replace current context by it
215+
if (basicO.get(arrayKey) instanceof BasicDBObject && isReference((BasicDBObject) basicO.get(arrayKey))) {
216+
basicO = ((AbstractDataProvider) get(arrayKey)).basicObject;
217+
arrayKey = basicO.keySet().iterator().next();
218+
way = key;
219+
} else {
220+
way += "." + key;
221+
}
222+
212223
Object listCandidate = basicO.get(arrayKey);
213224

214225
if (!(listCandidate instanceof BasicDBList)) {
@@ -230,6 +241,7 @@ private BasicDBObject parseComplexDBObject(String key) throws DataException {
230241

231242
if (isArray(partialKey)) {
232243
currentBasicObject = (BasicDBObject) parseArray(currentBasicObject, partialKey);
244+
partialBuiltPath.append(".");
233245
continue;
234246
}
235247

@@ -241,6 +253,7 @@ private BasicDBObject parseComplexDBObject(String key) throws DataException {
241253
}
242254

243255
Object currentValue = currentBasicObject.get(partialKey);
256+
this.way += "." + partialKey;
244257
if (!(currentValue instanceof BasicDBObject)) {
245258
if (null == currentValue || i < keys.length - 1) {
246259
String lastKey = keys[keys.length - 1];
@@ -352,11 +365,7 @@ public void applyGenerator(Class<? extends GeneratorCallback> callback) {
352365
*/
353366
@Override
354367
public boolean isReference() {
355-
Object value = this.basicObject.get(VALUE_TPL);
356-
if (!(value instanceof BasicDBObject)) {
357-
return false;
358-
}
359-
return ((BasicDBObject) value).containsField(COLLECTION_TPL) && ((BasicDBObject) value).containsField("path");
368+
return isReference(this.basicObject);
360369
}
361370

362371
private boolean isReference(BasicDBObject basicDBObject) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,34 @@ public void arrayReferenceTest() throws DataException {
7575
testDataProvider.get("array[3].ref").getValue());
7676
}
7777

78+
@Test
79+
public void arrayReferencedTest() throws DataException {
80+
String collectionName = "DataBlocks";
81+
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, collectionName);
82+
83+
assertEquals("a",
84+
testDataProvider.get("ref array[0]").getValue());
85+
}
86+
87+
@Test
88+
public void arrayDeepReferencedTest() throws DataException {
89+
String collectionName = "DataBlocks";
90+
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, collectionName);
91+
92+
assertEquals("1",
93+
testDataProvider.get("ref array[2].b[0].b.c").getValue());
94+
}
95+
96+
@Test
97+
public void arrayDeepReferencedToReferenceTest() throws DataException {
98+
String collectionName = "DataBlocks";
99+
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, collectionName);
100+
101+
assertEquals("123qwe",
102+
testDataProvider.get("ref array[2].b[0].b.d").getValue());
103+
}
104+
105+
78106
@Test
79107
public void arrayGeneratorTest() throws DataException {
80108
String collectionName = "Tests";

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,11 @@
6363
"nestedObjectValue": {
6464
"innerValue": "This is string from nested object"
6565
}
66+
},
67+
"ref array": {
68+
"value": {
69+
"collection": "Tests",
70+
"path": "array"
71+
}
6672
}
6773
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@
3737
"b": [
3838
{
3939
"b": {
40-
"c": 1
40+
"c": 1,
41+
"d": {
42+
"value": {
43+
"collection": "DataBlocks",
44+
"path": "Params Group 1.password"
45+
}
46+
}
4147
}
4248
}
4349
]

providers/properties-provider/src/test/java/ru/sbtqa/tag/datajack/providers/properties/PropertiesDataTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ public void arrayReferenceTest() throws DataException {
7979
dataProvider.get("array[3].ref").getValue());
8080
}
8181

82+
83+
@Test
84+
public void arrayReferencedTest() throws DataException {
85+
String collectionName = "DataBlocks";
86+
TestDataProvider testDataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName);
87+
88+
assertEquals("a",
89+
testDataProvider.get("ref array[0]").getValue());
90+
}
91+
92+
@Test
93+
public void arrayDeepReferencedTest() throws DataException {
94+
String collectionName = "DataBlocks";
95+
TestDataProvider testDataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName);
96+
97+
assertEquals("1",
98+
testDataProvider.get("ref array[2].b[0].b.c").getValue());
99+
}
100+
101+
@Test
102+
public void arrayDeepReferencedToReferenceTest() throws DataException {
103+
String collectionName = "DataBlocks";
104+
TestDataProvider testDataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName);
105+
106+
assertEquals("123qwe",
107+
testDataProvider.get("ref array[2].b[0].b.d").getValue());
108+
}
109+
82110
@Test
83111
public void arrayGeneratorTest() throws DataException {
84112
String collectionName = "Tests";

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ MapTests.doubleValue=42.01
3131
MapTests.nestedObjectValue.innerValue=This is string from nested object
3232
MapTests.ref.value.collection=DataBlocks
3333
MapTests.ref.value.path=MapTests.stringValue
34-
MapTests.ref.value.comment=Пароль пользователя
34+
MapTests.ref.value.comment=Пароль пользователя
35+
ref\ array.value.collection= Tests
36+
ref\ array.value.path = array

providers/properties-provider/src/test/resources/properties/Tests.properties

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ comment=Some comment
1212
array[0]=a
1313
array[1].b=1
1414
array[2].b[0].b.c=1
15+
array[2].b[0].b.d.value.collection=DataBlocks
16+
array[2].b[0].b.d.value.path=Params Group 1.password
1517
array[3].ref.value.collection=DataBlocks
1618
array[3].ref.value.path=Params Group 1.password
1719
array[3].genRef.value.collection=DataBlocks
1820
array[3].genRef.value.path=Common.gen gen.gendata
19-
array[3].refObj.value.collection=DataBlocks
20-
array[3].refObj.value.path=Common
21+
array[4].refObj.value.collection=DataBlocks
22+
array[4].refObj.value.path=Common

0 commit comments

Comments
 (0)