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

Commit 400f0aa

Browse files
authored
Merge pull request #31 from sbtqa/getobj
Now get value returs json if points to not final value
2 parents 9864cc2 + d6f535c commit 400f0aa

6 files changed

Lines changed: 124 additions & 12 deletions

File tree

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

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.mongodb.BasicDBList;
44
import com.mongodb.BasicDBObject;
5+
import com.mongodb.DBObject;
6+
import com.mongodb.util.JSON;
57
import org.bson.BasicBSONObject;
68
import ru.sbtqa.tag.datajack.TestDataProvider;
79
import ru.sbtqa.tag.datajack.callback.CallbackData;
@@ -12,7 +14,12 @@
1214
import ru.sbtqa.tag.datajack.exceptions.GeneratorException;
1315
import ru.sbtqa.tag.datajack.exceptions.ReferenceException;
1416

15-
import java.util.*;
17+
import java.util.ArrayList;
18+
import java.util.Collection;
19+
import java.util.HashSet;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Set;
1623
import java.util.regex.Matcher;
1724
import java.util.regex.Pattern;
1825

@@ -23,8 +30,8 @@ public abstract class AbstractDataProvider implements TestDataProvider {
2330
protected static final String VALUE_TPL = "value";
2431
protected static final String COLLECTION_TPL = "collection";
2532

26-
private static final String NOT_INITIALIZED_EXCEPTION = "BasicDBObject is not initialized yet. Try to get some path first.";
2733
private static final String ARRAY_MATCHER_REGEX = "(.+\\[\\d+\\])";
34+
private static final String NOT_INITIALIZED_EXCEPTION = "BasicDBObject is not initialized yet. Try to get some path first.";
2835
public static final String COLLECTION_PARSE_REGEX = "\\$([^\\{]+)";
2936
public static final String PATH_PARSE_REGEX = "(?:\\$([^\\{]+)?(\\{([^\\}]+)\\}))";
3037

@@ -109,13 +116,15 @@ public Map<String, Object> toMap() throws DataException {
109116
*/
110117
@Override
111118
public Set<String> getKeySet() throws DataException {
119+
112120
if (basicObject == null) {
113121
throw new DataException(NOT_INITIALIZED_EXCEPTION);
114-
} else if (getValue() != null) {
122+
} else if (way != null && basicObject.toMap().containsKey(getWayTail()) &&
123+
!(basicObject.get(way) instanceof BasicDBObject)) {
115124
return new HashSet<>();
116125
}
117126
if (isReference()) {
118-
this.rootObject = null;
127+
rootObject = null;
119128
return getReference().toMap().keySet();
120129
} else {
121130
return basicObject.keySet();
@@ -151,6 +160,8 @@ public List<String> getStringValues() throws DataException {
151160
|| value instanceof Double
152161
|| value instanceof Boolean) {
153162
strings.add(value.toString());
163+
} else if (value == null) {
164+
strings.add("null");
154165
} else {
155166
strings.add("");
156167
}
@@ -295,11 +306,15 @@ public String getValue() throws DataException {
295306
String result = this.basicObject.getString(VALUE_TPL);
296307

297308
if (result == null) {
298-
if (this.way != null && this.way.contains(".")) {
299-
this.way = this.way.split("[.]")[this.way.split("[.]").length - 1];
309+
way = getWayTail();
310+
311+
if (!(basicObject.get(way) instanceof BasicDBObject)) {
312+
result = basicObject.getString(way);
300313
}
301314

302-
result = this.basicObject.getString(this.way);
315+
if (result == null) {
316+
result = resolveDbObject().toString();
317+
}
303318
}
304319
return applyCallBackData(result);
305320
}
@@ -408,4 +423,54 @@ private TestDataProvider parseCollection(String collectionToParse) throws DataEx
408423
return this;
409424
}
410425
}
426+
427+
428+
/**
429+
* Get after last dot string value
430+
*
431+
* @return tail or null
432+
*/
433+
private String getWayTail() {
434+
if (way == null) {
435+
return null;
436+
} else {
437+
String[] wayArray = way.split("[.]");
438+
return wayArray[wayArray.length - 1];
439+
}
440+
}
441+
442+
/**
443+
* Walk into current {@link DBObject} and resolve all generators and references
444+
*
445+
* @return resolved {@link DBObject}
446+
* @throws DataException =
447+
*/
448+
private DBObject resolveDbObject() throws DataException {
449+
BasicDBObject resolvedDbObject = new BasicDBObject();
450+
451+
for (String key : basicObject.keySet()) {
452+
BasicDBObject target = basicObject.get(key) instanceof BasicDBObject ?
453+
(BasicDBObject) basicObject.get(key) :
454+
basicObject;
455+
AbstractDataProvider instance = createInstance(target, collectionName, way + "." + key);
456+
instance.applyGenerator(callback);
457+
458+
if (basicObject.get(key) instanceof BasicDBObject) {
459+
try {
460+
resolvedDbObject.put(key, JSON.parse(instance.getValue()));
461+
} catch (Exception e) {
462+
resolvedDbObject.put(key, instance.getValue());
463+
}
464+
} else {
465+
if (instance.basicObject.keySet().contains(key) && instance
466+
.basicObject.get(key) == null) {
467+
resolvedDbObject.put(key, null);
468+
} else {
469+
resolvedDbObject.put(key, instance.getValue());
470+
}
471+
}
472+
473+
}
474+
return resolvedDbObject;
475+
}
411476
}

datajack-api/src/test/java/ru/sbtqa/tag/datajack/callback/SampleDataGensCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Object call(CallbackData callbackData) {
2323
String cacheKey = callbackData.getPath();
2424
String result = callbackData.getResult();
2525

26-
if (result.startsWith("generate:")) {
26+
if (result != null && result.startsWith("generate:")) {
2727
if (!cache.containsKey("testDataCache")) {
2828
cache.put("testDataCache", new HashMap<>());
2929
} else if (((Map) cache.get("testDataCache")).containsKey(cacheKey)) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.sbtqa.tag.datajack.providers.json;
22

33
import com.mongodb.BasicDBObject;
4+
import org.junit.Assert;
45
import org.junit.Before;
56
import org.junit.Rule;
67
import org.junit.Test;
@@ -384,10 +385,21 @@ public void getStringValuesTest() throws DataException {
384385
while (resultIterator.hasNext()) {
385386
Object currentResValue = resultIterator.next();
386387
Object currentOrigValue = originalIterator.next();
388+
if (currentOrigValue == null) {
389+
currentOrigValue = "null";
390+
}
387391

388392
if (!(currentOrigValue instanceof BasicDBObject)) {
389393
assertEquals("Unexpected value transformation", currentOrigValue.toString(), currentResValue.toString());
390394
}
391395
}
392396
}
397+
398+
@Test
399+
public void getJsonTest() throws DataException {
400+
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, "DataBlocks");
401+
String stringJson = testDataProvider.get("Params Group 1").getValue();
402+
String expectedJson = "{ \"login\" : 123 , \"password\" : 123}";
403+
Assert.assertEquals(expectedJson, stringJson);
404+
}
393405
}

providers/mongo-provider/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,11 @@
3535
<version>${fakemongo.fongo.version}</version>
3636
<scope>test</scope>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-log4j12</artifactId>
41+
<version>1.7.25</version>
42+
<scope>test</scope>
43+
</dependency>
3844
</dependencies>
3945
</project>

providers/mongo-provider/src/test/java/ru/sbtqa/tag/datajack/providers/mongo/MongoDataTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,35 @@
33
import com.github.fakemongo.junit.FongoRule;
44
import com.mongodb.DB;
55
import org.junit.Before;
6+
import org.junit.BeforeClass;
67
import org.junit.Rule;
78
import org.junit.Test;
89
import org.junit.rules.ExpectedException;
910
import org.junit.rules.RuleChain;
1011
import org.junit.rules.TestRule;
1112
import ru.sbtqa.tag.datajack.TestDataProvider;
1213
import ru.sbtqa.tag.datajack.callback.SampleDataGensCallback;
13-
import ru.sbtqa.tag.datajack.exceptions.*;
14-
import ru.sbtqa.tag.datajack.providers.mongo.MongoDataProvider;
14+
import ru.sbtqa.tag.datajack.exceptions.CollectionNotFoundException;
15+
import ru.sbtqa.tag.datajack.exceptions.CyclicReferencesException;
16+
import ru.sbtqa.tag.datajack.exceptions.DataException;
17+
import ru.sbtqa.tag.datajack.exceptions.FieldNotFoundException;
18+
import ru.sbtqa.tag.datajack.exceptions.ReferenceException;
1519

1620
import java.io.IOException;
1721
import java.util.Map;
1822
import java.util.Set;
1923

2024
import static java.lang.String.format;
21-
import static org.junit.Assert.*;
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertTrue;
2229
import static org.junit.rules.ExpectedException.none;
2330
import static ru.sbtqa.tag.datajack.callback.SampleDataCache.getCache;
2431

2532
public class MongoDataTest {
2633

27-
private final FongoRule fongoRule = new FongoRule(false);
34+
private static FongoRule fongoRule;
2835

2936
private final ExpectedException exception = ExpectedException.none();
3037

@@ -34,6 +41,11 @@ public class MongoDataTest {
3441
public ExpectedException expectDataExceptions = none();
3542
private DB mongoDb;
3643

44+
@BeforeClass
45+
public static void beforeClass() {
46+
fongoRule = new FongoRule(false);
47+
}
48+
3749
/**
3850
* @throws IOException if no json found
3951
*/
@@ -305,4 +317,12 @@ public void getEmptySelfTest() throws DataException {
305317

306318
assertEquals("Objects are not same", origin, self);
307319
}
320+
321+
@Test
322+
public void getJsonTest() throws DataException {
323+
TestDataProvider testDataProvider = new MongoDataProvider(mongoDb, "DataBlocks");
324+
String stringJson = testDataProvider.get("Params Group 1").getValue();
325+
String expectedJson = "{ \"login\" : 123 , \"password\" : 123}";
326+
assertEquals(expectedJson, stringJson);
327+
}
308328
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.sbtqa.tag.datajack.providers.properties;
22

33
import com.mongodb.BasicDBObject;
4+
import org.junit.Assert;
45
import org.junit.Before;
56
import org.junit.Rule;
67
import org.junit.Test;
@@ -389,4 +390,12 @@ public void getStringValuesTest() throws DataException {
389390
}
390391
}
391392
}
393+
394+
@Test
395+
public void getJsonTest() throws DataException {
396+
TestDataProvider testDataProvider = new PropertiesDataProvider(propertiesDataPath, "DataBlocks");
397+
String stringJson = testDataProvider.get("Params Group 1").getValue();
398+
String expectedJson = "{ \"password\" : 123 , \"login\" : 123}";
399+
Assert.assertEquals(expectedJson, stringJson);
400+
}
392401
}

0 commit comments

Comments
 (0)