|
| 1 | +# Special Values |
| 2 | + |
| 3 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "use groovy closure as validation", bodyOnly: true} |
| 4 | + |
| 5 | +Values that you access inside validation block are special values of `DataNode` type. When you assert them using `should` statement |
| 6 | +they act as proxies that record every assertion that you do. |
| 7 | + |
| 8 | + |
| 9 | +# Extracting Values |
| 10 | + |
| 11 | +As you have seen in [CRUD example](REST/CRUD) you can return values back from a validation block. |
| 12 | + |
| 13 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "can return simple value from get", bodyOnly: true} |
| 14 | + |
| 15 | +When you return a value from a validation block, it automatically gets converted to its correspondent primitive. |
| 16 | + |
| 17 | +Note: asserting that value after returning will not track and associated assertions with the call anymore. Use it only |
| 18 | +to get values required for consequent test calls. |
| 19 | + |
| 20 | +# Properties On Lists |
| 21 | + |
| 22 | +:include-json: objectTestResponse.json |
| 23 | + |
| 24 | +If you have a list of objects like `complexList` above, you can access all its children property value with `complexList.k2`. |
| 25 | + |
| 26 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy children key shortcut", bodyOnly: true} |
| 27 | + |
| 28 | +# Path based properties access |
| 29 | + |
| 30 | +Primarily for Java users, webtau supports the ability to query properties of a `DataNode` via a path instead of chaining |
| 31 | +`get(String name)` calls. For example, to obtain a simple property: |
| 32 | + |
| 33 | +:include-java: com/twosigma/webtau/http/HttpJavaTest.java {entry: "canQueryNodeByPath", bodyOnly: true} |
| 34 | + |
| 35 | +It is also possible to query arrays, including the ability to query for the Nth element from the end: |
| 36 | + |
| 37 | +:include-java: com/twosigma/webtau/http/HttpJavaTest.java {entry: "canQuerySpecificListElementByPath", bodyOnly: true} |
| 38 | + |
| 39 | +Similarly to the Groovy example in [Properties On Lists](REST/data-node#properties-on-lists), it is possible to access |
| 40 | +all children property values: |
| 41 | + |
| 42 | +:include-java: com/twosigma/webtau/http/HttpJavaTest.java {entry: "canQueryListByNodePath", bodyOnly: true} |
| 43 | + |
| 44 | +# If-Else Logic |
| 45 | + |
| 46 | +Even though values that you access inside validation block are special values of `DataNode` type, you can still |
| 47 | +perform simple `if-else` like logic checks on them. Accessing the values will mark them as "touched" for data coverage statistic. |
| 48 | + |
| 49 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "if-else logic", bodyOnly: true, title: "simple if-else logic"} |
| 50 | + |
| 51 | +Warning: Comparison of complex values is not properly implemented due to current Groovy API implementation details |
| 52 | + |
| 53 | +# Each |
| 54 | + |
| 55 | +Special values inside assertion block have convenient methods |
| 56 | + |
| 57 | +`each` to iterate over a list |
| 58 | + |
| 59 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy each on simple list", bodyOnly: true, title: "List of simple values"} |
| 60 | + |
| 61 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy each on complex list", bodyOnly: true, title: "List of complex values"} |
| 62 | + |
| 63 | + |
| 64 | +# Find |
| 65 | + |
| 66 | +`find` to find a single value |
| 67 | + |
| 68 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy find on list", bodyOnly: true} |
| 69 | + |
| 70 | + |
| 71 | +and `findAll` to find all the values matching predicate |
| 72 | + |
| 73 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy findAll on list", bodyOnly: true} |
| 74 | + |
| 75 | +Note: While values inside a predicate are normal values, the result of `find` and `findAll` is still `DataNode` |
| 76 | + |
| 77 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy find on list of objects", bodyOnly: true} |
| 78 | + |
| 79 | +# Collect |
| 80 | + |
| 81 | +Use `collect` to transform a collection of items |
| 82 | + |
| 83 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy transform list", bodyOnly: true} |
| 84 | + |
| 85 | +# Combine |
| 86 | + |
| 87 | +Methods `find` and `collect` can be chained |
| 88 | + |
| 89 | +:include-groovy: com/twosigma/webtau/http/HttpGroovyTest.groovy {entry: "groovy findAll, collect, and sum", bodyOnly: true} |
0 commit comments