Skip to content

Commit cb74166

Browse files
make findAll to return DataNode
1 parent 83aecb3 commit cb74166

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

webtau-rest-groovy/src/main/groovy/com/twosigma/webtau/http/datanode/GroovyDataNode.groovy

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ class GroovyDataNode implements DataNodeExpectations, DataNode {
103103
result
104104
}
105105

106-
List<DataNode> findAll(Closure predicate) {
107-
return node.elements().findAll(removedDataNodeFromClosure(predicate))
106+
DataNode findAll(Closure predicate) {
107+
def list = node.elements().findAll(removedDataNodeFromClosure(predicate))
108+
return wrapIntoDataNode('findAll', list)
108109
}
109110

110111
List collect(Closure transformation) {
@@ -121,6 +122,10 @@ class GroovyDataNode implements DataNodeExpectations, DataNode {
121122
return node.actualPath()
122123
}
123124

125+
private DataNode wrapIntoDataNode(String operationId, List list) {
126+
return new GroovyDataNode(new StructuredDataNode(node.id().child(operationId), list))
127+
}
128+
124129
private static Closure removedDataNodeFromClosure(Closure closure) {
125130
def newClosure = { dataNode ->
126131
def converter = new DataNodeToMapOfValuesConverter({ id, traceableValue ->

webtau-rest-groovy/src/test/groovy/com/twosigma/webtau/http/HttpTest.groovy

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ class HttpTest {
124124
http.get("/end-point") {
125125
complexList.should == ["k1" | "k2"] {
126126
__________________
127-
"v1" | "v2"
128-
"v11" | "v22" }
127+
"v1" | 30
128+
"v11" | 40 }
129129
}
130130
}
131131

@@ -195,7 +195,7 @@ class HttpTest {
195195
return complexList
196196
}
197197

198-
assert complexList == [[id: 'id1', k1: 'v1', k2: 'v2'], [id: 'id2', k1: 'v11', k2: 'v22']]
198+
assert complexList == [[id: 'id1', k1: 'v1', k2: 30], [id: 'id2', k1: 'v11', k2: 40]]
199199
assert complexList.getClass() == ArrayList
200200
assert complexList[0].getClass() == LinkedHashMap
201201
}
@@ -381,6 +381,18 @@ class HttpTest {
381381
assert found[0].getClass() == Integer
382382
}
383383

384+
@Test
385+
void "groovy findAlll, collect, and sum"() {
386+
def sum = http.get("/end-point") {
387+
return complexList
388+
.findAll { it.k1.startsWith('v1') }
389+
.collect { it.k2 }
390+
.sum()
391+
}
392+
393+
assert sum == 70
394+
}
395+
384396
@Test
385397
void "groovy findAll on body that is not a list"() {
386398
def found = http.get("/end-point") {
@@ -415,6 +427,15 @@ class HttpTest {
415427
assert transformed[0] instanceof GString
416428
}
417429

430+
@Test
431+
void "groovy transform list by referencing node"() {
432+
def ids = http.get("/end-point") {
433+
return complexList.collect { it.id }
434+
}
435+
436+
assert ids == ['id1', 'id2']
437+
}
438+
418439
@Test
419440
void "groovy transform on body that is not a list"() {
420441
def transformed = http.get("/end-point") {
@@ -610,8 +631,8 @@ class HttpTest {
610631

611632
complexList.should == ["k1" | "k2"] { // matching only specified fields, but number of entries must be exact
612633
________________
613-
"v1" | "v2"
614-
"v11" | "v22" }
634+
"v1" | 30
635+
"v11" | 40 }
615636
}
616637

617638
http.doc.capture("end-point-object-equality-matchers")

webtau-rest-groovy/src/test/resources/objectTestResponse.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
{
1717
"id": "id1",
1818
"k1": "v1",
19-
"k2": "v2"
19+
"k2": 30
2020
},
2121
{
2222
"id": "id2",
2323
"k1": "v11",
24-
"k2": "v22"
24+
"k2": 40
2525
}
2626
]
2727
}

0 commit comments

Comments
 (0)