22
33import com .mongodb .BasicDBList ;
44import com .mongodb .BasicDBObject ;
5+ import com .mongodb .DBObject ;
56import com .mongodb .util .JSON ;
67import org .bson .BasicBSONObject ;
78import ru .sbtqa .tag .datajack .TestDataProvider ;
89import ru .sbtqa .tag .datajack .callback .CallbackData ;
910import 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 ;
1323import java .util .regex .Matcher ;
1424import 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}
0 commit comments