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

Commit 203aa96

Browse files
committed
Add test
Fix getKeyset
1 parent 804260e commit 203aa96

5 files changed

Lines changed: 76 additions & 26 deletions

File tree

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
import com.mongodb.BasicDBList;
44
import com.mongodb.BasicDBObject;
5+
import com.mongodb.util.JSON;
56
import org.bson.BasicBSONObject;
67
import ru.sbtqa.tag.datajack.TestDataProvider;
78
import ru.sbtqa.tag.datajack.callback.CallbackData;
89
import ru.sbtqa.tag.datajack.callback.GeneratorCallback;
9-
import ru.sbtqa.tag.datajack.exceptions.CyclicReferencesException;
10-
import ru.sbtqa.tag.datajack.exceptions.DataException;
11-
import ru.sbtqa.tag.datajack.exceptions.FieldNotFoundException;
12-
import ru.sbtqa.tag.datajack.exceptions.GeneratorException;
13-
import ru.sbtqa.tag.datajack.exceptions.ReferenceException;
10+
import ru.sbtqa.tag.datajack.exceptions.*;
1411

1512
import java.util.*;
1613
import java.util.regex.Matcher;
@@ -23,8 +20,8 @@ public abstract class AbstractDataProvider implements TestDataProvider {
2320
protected static final String VALUE_TPL = "value";
2421
protected static final String COLLECTION_TPL = "collection";
2522

26-
private static final String NOT_INITIALIZED_EXCEPTION = "BasicDBObject is not initialized yet. Try to get some path first.";
2723
private static final String ARRAY_MATCHER_REGEX = "(.+\\[\\d+\\])";
24+
public static final String NOT_INITIALIZED_EXCEPTION = "BasicDBObject is not initialized yet. Try to get some path first.";
2825
public static final String COLLECTION_PARSE_REGEX = "\\$([^\\{]+)";
2926
public static final String PATH_PARSE_REGEX = "(?:\\$([^\\{]+)?(\\{([^\\}]+)\\}))";
3027

@@ -33,7 +30,7 @@ public abstract class AbstractDataProvider implements TestDataProvider {
3330
protected String way;
3431
protected String path;
3532
protected Class<? extends GeneratorCallback> callback;
36-
private BasicDBObject rootObject;
33+
protected BasicDBObject rootObject;
3734

3835
private static boolean isArray(String key) {
3936
return key.matches(ARRAY_MATCHER_REGEX);
@@ -109,13 +106,14 @@ public Map<String, Object> toMap() throws DataException {
109106
*/
110107
@Override
111108
public Set<String> getKeySet() throws DataException {
109+
String wayTail = way== null ? null :(way.split("[.]"))[way.split("[.]").length-1];
112110
if (basicObject == null) {
113111
throw new DataException(NOT_INITIALIZED_EXCEPTION);
114-
} else if (getValue() != null) {
112+
} else if (way !=null && basicObject.toMap().containsKey(wayTail) && !(basicObject.get(way) instanceof BasicDBObject)) {
115113
return new HashSet<>();
116114
}
117115
if (isReference()) {
118-
this.rootObject = null;
116+
rootObject = null;
119117
return getReference().toMap().keySet();
120118
} else {
121119
return basicObject.keySet();
@@ -151,7 +149,9 @@ public List<String> getStringValues() throws DataException {
151149
|| value instanceof Double
152150
|| value instanceof Boolean) {
153151
strings.add(value.toString());
154-
} else {
152+
} else if(value == null){
153+
strings.add("null");
154+
}else{
155155
strings.add("");
156156
}
157157
}
@@ -307,17 +307,28 @@ public String getValue() throws DataException {
307307

308308
BasicDBObject dbObject = new BasicDBObject();
309309

310-
for (String s : basicObject.keySet()){
310+
for (String s : basicObject.keySet()) {
311311

312-
BasicDBObject target = (basicObject.get(s) instanceof BasicDBObject) ?
312+
BasicDBObject target = basicObject.get(s) instanceof BasicDBObject ?
313313
(BasicDBObject) basicObject.get(s) :
314314
basicObject;
315-
316-
AbstractDataProvider instance = createInstance(target, collectionName, way + "." + s);
317-
instance.applyGenerator(callback);
318-
dbObject.put(s, instance.getValue());
319-
320-
315+
AbstractDataProvider instance = createInstance(target, collectionName, way + "." + s);
316+
instance.applyGenerator(callback);
317+
318+
if (basicObject.get(s) instanceof BasicDBObject) {
319+
try {
320+
dbObject.put(s, JSON.parse(instance.getValue()));
321+
} catch (Exception e) {
322+
dbObject.put(s, instance.getValue());
323+
}
324+
} else {
325+
if (instance.basicObject.keySet().contains(s) && instance
326+
.basicObject.get(s) == null) {
327+
dbObject.put(s, null);
328+
} else {
329+
dbObject.put(s, instance.getValue());
330+
}
331+
}
321332

322333
}
323334
result = dbObject.toString();

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,12 @@ public void getByPathArrayTest() throws DataException {
163163
TestDataProvider testDataProvider = new JsonDataProvider(JSON_DATA_PATH, collectionName);
164164
testDataProvider.applyGenerator(SampleDataGensCallback.class);
165165

166-
String value = testDataProvider.getByPath("$Tests{Common}").getValue();
167-
Assert.assertNotNull(value);
168-
System.out.println(value);
166+
String value = testDataProvider.getByPath("$Tests{array[0]}").getValue();
167+
String valueArrayObject = testDataProvider.getByPath("${array[1].b}").getValue();
168+
169+
assertEquals("a", value);
170+
assertEquals("1", valueArrayObject);
171+
assertEquals(testDataProvider.get("array").toString(), testDataProvider.getByPath("$Tests{array}").toString());
169172
}
170173

171174
@Test
@@ -382,10 +385,21 @@ public void getStringValuesTest() throws DataException {
382385
while (resultIterator.hasNext()) {
383386
Object currentResValue = resultIterator.next();
384387
Object currentOrigValue = originalIterator.next();
388+
if (currentOrigValue == null) {
389+
currentOrigValue = "null";
390+
}
385391

386392
if (!(currentOrigValue instanceof BasicDBObject)) {
387393
assertEquals("Unexpected value transformation", currentOrigValue.toString(), currentResValue.toString());
388394
}
389395
}
390396
}
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+
}
391405
}

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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
import com.github.fakemongo.junit.FongoRule;
44
import com.mongodb.DB;
5-
import org.junit.Before;
6-
import org.junit.Rule;
7-
import org.junit.Test;
5+
import org.junit.*;
86
import org.junit.rules.ExpectedException;
97
import org.junit.rules.RuleChain;
108
import org.junit.rules.TestRule;
119
import ru.sbtqa.tag.datajack.TestDataProvider;
1210
import ru.sbtqa.tag.datajack.callback.SampleDataGensCallback;
1311
import ru.sbtqa.tag.datajack.exceptions.*;
14-
import ru.sbtqa.tag.datajack.providers.mongo.MongoDataProvider;
1512

1613
import java.io.IOException;
1714
import java.util.Map;
@@ -24,7 +21,7 @@
2421

2522
public class MongoDataTest {
2623

27-
private final FongoRule fongoRule = new FongoRule(false);
24+
private static FongoRule fongoRule;
2825

2926
private final ExpectedException exception = ExpectedException.none();
3027

@@ -34,6 +31,11 @@ public class MongoDataTest {
3431
public ExpectedException expectDataExceptions = none();
3532
private DB mongoDb;
3633

34+
@BeforeClass
35+
public static void beforeClass() {
36+
fongoRule = new FongoRule(false);
37+
}
38+
3739
/**
3840
* @throws IOException if no json found
3941
*/
@@ -305,4 +307,12 @@ public void getEmptySelfTest() throws DataException {
305307

306308
assertEquals("Objects are not same", origin, self);
307309
}
310+
311+
@Test
312+
public void getJsonTest() throws DataException {
313+
TestDataProvider testDataProvider = new MongoDataProvider(mongoDb, "DataBlocks");
314+
String stringJson = testDataProvider.get("Params Group 1").getValue();
315+
String expectedJson = "{ \"login\" : 123 , \"password\" : 123}";
316+
Assert.assertEquals(expectedJson, stringJson);
317+
}
308318
}

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)