Special Values

Values that you access inside the http call validation block are special values of DataNode type. DataNode has methods to assert and traverse the response.All assertions made on DataNode are tracked and are available as part of the generated report. Groovy http.get("/end-point") { price.should == 100 assert price instanceof DataNode } Check HTTP/import-and-dependencies Import And Dependencies for prerequisites. Java http.get("/end-point", ((header, body) -> { DataNode price = body.get("price"); price.should(equal(100)); })); Check HTTP/import-and-dependencies Import And Dependencies for prerequisites.

Extracting Values

As you have seen in HTTP/CRUD-example CRUD example you can return values back from a validation block. Groovy def id = http.get("/end-point") { return id } id.should == 10 id.getClass().should == Integer List<Map<String, ?>> complexList = http.get("/end-point") { return complexList } complexList[0].k2.should == 30 Java Integer id = http.get("/end-point", ((header, body) -> { return body.get("id"); })); actual(id).should(equal(10)); List<Map<String, ?>> complexList = http.get("/end-point", ((header, body) -> { return body.get("complexList"); })); Number k2 = (Number) complexList.get(0).get("k2"); actual(k2).should(equal(30)); WebTau automatically converts leaf value to its correspondent primitive. It converts List and object to java.util.List and java.util.Map .Note: WebTau will not be able to associate assertions on returned values with a specific call. Avoid using returned values for validation. Make assertions on DataNode instance to generate useful report information such as data coverage.

Properties On Lists

If you have a list of objects like complexList below, you can access all its children property value with complexList.k2 . Groovy http.get("/end-point") { complexList.k2.should == [30, 40] } Java http.get("/end-point", ((header, body) -> { body.get("complexList").get("k2").should(equal(list(30, 40))); }));

Path based properties access

Primarily for Java users, WebTau supports the ability to query properties of a DataNode via a path instead of chaining get(String name) calls. For example, to obtain a simple property: http.get("/end-point", (header, body) -> { body.get("object.k1").should(equal("v1")); }); It is also possible to query arrays, including the ability to query for the Nth element from the end: http.get("/end-point", (header, body) -> { body.get("complexList[0].k1").should(equal("v1")); body.get("complexList[-1].k1").should(equal("v11")); }); Similarly to the Groovy example in HTTP/data-node#properties-on-lists Properties On Lists, it is possible to access all children property values: http.get("/end-point", (header, body) -> { body.get("complexList.k1").should(equal(list("v1", "v11"))); });

If-Else Logic

Even though values that you access inside validation block are special values of DataNode type, you can still perform simple if-else like logic checks on them.In case of Groovy, accessing a value during if-else will mark the value as accessed for reporting.In case of Java, use .get() to access underlying value. No marking of the value will during comparison. Groovy def zipCode = http.get("/address") { return addressType == "complex" ? address.zipCode : "NA" } Warning: Comparison of complex values is not properly implemented due to current Groovy API implementation details Java String zipCode = http.get("/address", ((header, body) -> { return body.get("addressType").get().equals("complex") ? body.get("address.zipCode") : "NA"; }));

Iteration

DataNode have convenient methods to iterate over values and make assertions. Groovy http.get("/end-point") { list.each { it.shouldBe > 0 } } http.get("/end-point") { complexList.each { k2.shouldBe > 0 } } Java http.get("/end-point", (header, body) -> { body.get("list").forEach(node -> node.shouldBe(greaterThan(0))); }); http.get("/end-point", (header, body) -> { body.get("complexList").forEach(node -> node.get("k2").shouldBe(greaterThan(0))); });

Find

Use find to find a DataNode based on a provided predicate Groovy http.get("/end-point") { def found = complexList.find { it.id.get() == "id1" } found.k1.should == "v1" found.k2.should == 30 } http.doc.capture("find-on-list-and-assert") // doc-exclude def found = http.get("/end-point") { return complexList.find { it.id == "id1" } } Java http.get("/end-point", ((header, body) -> { DataNode found = body.get("complexList").find(node -> node.get("id").get().equals("id1")); found.get("k1").should(equal("v1")); found.get("k2").should(equal(30)); })); Map<String, ?> found = http.get("/end-point", ((header, body) -> { return body.get("complexList").find(node -> node.get("id").get().equals("id1")); })); Note: WebTau will not track assertion on returned values and it will not show in reports and will not participate in data coverage. Use returned values for further test logic.

Find All

Use findAll to find a list of DataNode s based on a provided predicate Groovy http.get("/end-point") { def found = complexList.findAll { it.k2 > 20 } found.k1.should containAll("v1", "v11") } Java http.get("/end-point", ((header, body) -> { DataNode found = body.get("complexList").findAll(node -> { int k2 = node.get("k2").get(); return k2 > 20; }); found.get("k1").should(containAll("v1", "v11")); })); Note: The result of findAll is of type DataNode and you can leverage #properties-on-lists Properties On Lists

Collect

Use collect to transform a collection of items def transformed = http.get("/end-point") { return list.collect { "world#${it}" } } assert transformed == ["world#1", "world#2", "world#3"] assert transformed[0] instanceof GString

Combine

Methods find and collect can be chained def sum = http.get("/end-point") { return complexList .findAll { k1.get().startsWith("v1") } .collect { k2.get() } .sum() } assert sum == 70