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

Commit d6f535c

Browse files
committed
resolve PR issues
1 parent 203aa96 commit d6f535c

2 files changed

Lines changed: 94 additions & 52 deletions

File tree

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

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
import com.mongodb.BasicDBList;
44
import com.mongodb.BasicDBObject;
5+
import com.mongodb.DBObject;
56
import com.mongodb.util.JSON;
67
import org.bson.BasicBSONObject;
78
import ru.sbtqa.tag.datajack.TestDataProvider;
89
import ru.sbtqa.tag.datajack.callback.CallbackData;
910
import ru.sbtqa.tag.datajack.callback.GeneratorCallback;
10-
import ru.sbtqa.tag.datajack.exceptions.*;
11-
12-
import java.util.*;
11+
import ru.sbtqa.tag.datajack.exceptions.CyclicReferencesException;
12+
import ru.sbtqa.tag.datajack.exceptions.DataException;
13+
import ru.sbtqa.tag.datajack.exceptions.FieldNotFoundException;
14+
import ru.sbtqa.tag.datajack.exceptions.GeneratorException;
15+
import ru.sbtqa.tag.datajack.exceptions.ReferenceException;
16+
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;
1323
import java.util.regex.Matcher;
1424
import java.util.regex.Pattern;
1525

@@ -21,7 +31,7 @@ public abstract class AbstractDataProvider implements TestDataProvider {
2131
protected static final String COLLECTION_TPL = "collection";
2232

2333
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.";
34+
private static final String NOT_INITIALIZED_EXCEPTION = "BasicDBObject is not initialized yet. Try to get some path first.";
2535
public static final String COLLECTION_PARSE_REGEX = "\\$([^\\{]+)";
2636
public static final String PATH_PARSE_REGEX = "(?:\\$([^\\{]+)?(\\{([^\\}]+)\\}))";
2737

@@ -30,7 +40,7 @@ public abstract class AbstractDataProvider implements TestDataProvider {
3040
protected String way;
3141
protected String path;
3242
protected Class<? extends GeneratorCallback> callback;
33-
protected BasicDBObject rootObject;
43+
private BasicDBObject rootObject;
3444

3545
private static boolean isArray(String key) {
3646
return key.matches(ARRAY_MATCHER_REGEX);
@@ -39,10 +49,10 @@ private static boolean isArray(String key) {
3949
/**
4050
* Internal use only for provider overriding purposes
4151
*
42-
* @param basicObject Current object
52+
* @param basicObject Current object
4353
* @param collectionName Name of collection
44-
* @param way Passed way
45-
* @param <T> Adaptor type
54+
* @param way Passed way
55+
* @param <T> Adaptor type
4656
* @return return Adaptor instance
4757
* @throws DataException In case provider could not be initialized
4858
*/
@@ -51,9 +61,9 @@ private static boolean isArray(String key) {
5161
/**
5262
* Internal use only for provider overriding purposes
5363
*
54-
* @param basicObject Current object
64+
* @param basicObject Current object
5565
* @param collectionName Name of collection
56-
* @param <T> Adaptor type
66+
* @param <T> Adaptor type
5767
* @return return Adaptor instance
5868
* @throws DataException In case provider c
5969
*/
@@ -63,7 +73,7 @@ private static boolean isArray(String key) {
6373
* Internal use only for provider overriding purposes
6474
*
6575
* @param collectionName Name of collection
66-
* @param <T> Adaptor type
76+
* @param <T> Adaptor type
6777
* @return Adaptor instance
6878
* @throws DataException In case provider could not be initialized
6979
*/
@@ -106,10 +116,11 @@ public Map<String, Object> toMap() throws DataException {
106116
*/
107117
@Override
108118
public Set<String> getKeySet() throws DataException {
109-
String wayTail = way== null ? null :(way.split("[.]"))[way.split("[.]").length-1];
119+
110120
if (basicObject == null) {
111121
throw new DataException(NOT_INITIALIZED_EXCEPTION);
112-
} else if (way !=null && basicObject.toMap().containsKey(wayTail) && !(basicObject.get(way) instanceof BasicDBObject)) {
122+
} else if (way != null && basicObject.toMap().containsKey(getWayTail()) &&
123+
!(basicObject.get(way) instanceof BasicDBObject)) {
113124
return new HashSet<>();
114125
}
115126
if (isReference()) {
@@ -149,9 +160,9 @@ public List<String> getStringValues() throws DataException {
149160
|| value instanceof Double
150161
|| value instanceof Boolean) {
151162
strings.add(value.toString());
152-
} else if(value == null){
163+
} else if (value == null) {
153164
strings.add("null");
154-
}else{
165+
} else {
155166
strings.add("");
156167
}
157168
}
@@ -295,43 +306,14 @@ 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.substring(way.lastIndexOf(".") + 1);
300-
}
309+
way = getWayTail();
301310

302311
if (!(basicObject.get(way) instanceof BasicDBObject)) {
303312
result = basicObject.getString(way);
304313
}
305314

306315
if (result == null) {
307-
308-
BasicDBObject dbObject = new BasicDBObject();
309-
310-
for (String s : basicObject.keySet()) {
311-
312-
BasicDBObject target = basicObject.get(s) instanceof BasicDBObject ?
313-
(BasicDBObject) basicObject.get(s) :
314-
basicObject;
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-
}
332-
333-
}
334-
result = dbObject.toString();
316+
result = resolveDbObject().toString();
335317
}
336318
}
337319
return applyCallBackData(result);
@@ -441,4 +423,54 @@ private TestDataProvider parseCollection(String collectionToParse) throws DataEx
441423
return this;
442424
}
443425
}
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+
}
444476
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@
22

33
import com.github.fakemongo.junit.FongoRule;
44
import com.mongodb.DB;
5-
import org.junit.*;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Rule;
8+
import org.junit.Test;
69
import org.junit.rules.ExpectedException;
710
import org.junit.rules.RuleChain;
811
import org.junit.rules.TestRule;
912
import ru.sbtqa.tag.datajack.TestDataProvider;
1013
import ru.sbtqa.tag.datajack.callback.SampleDataGensCallback;
11-
import ru.sbtqa.tag.datajack.exceptions.*;
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;
1219

1320
import java.io.IOException;
1421
import java.util.Map;
1522
import java.util.Set;
1623

1724
import static java.lang.String.format;
18-
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;
1929
import static org.junit.rules.ExpectedException.none;
2030
import static ru.sbtqa.tag.datajack.callback.SampleDataCache.getCache;
2131

@@ -32,8 +42,8 @@ public class MongoDataTest {
3242
private DB mongoDb;
3343

3444
@BeforeClass
35-
public static void beforeClass() {
36-
fongoRule = new FongoRule(false);
45+
public static void beforeClass() {
46+
fongoRule = new FongoRule(false);
3747
}
3848

3949
/**
@@ -313,6 +323,6 @@ public void getJsonTest() throws DataException {
313323
TestDataProvider testDataProvider = new MongoDataProvider(mongoDb, "DataBlocks");
314324
String stringJson = testDataProvider.get("Params Group 1").getValue();
315325
String expectedJson = "{ \"login\" : 123 , \"password\" : 123}";
316-
Assert.assertEquals(expectedJson, stringJson);
326+
assertEquals(expectedJson, stringJson);
317327
}
318328
}

0 commit comments

Comments
 (0)