Skip to content

Commit 2f8fbd5

Browse files
http: find,findAll updates checkLevel on single values (#813)
1 parent 082a670 commit 2f8fbd5

4 files changed

Lines changed: 64 additions & 25 deletions

File tree

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/traceable/TraceableValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2021 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,8 +20,8 @@
1920
import java.util.function.Supplier;
2021

2122
public class TraceableValue {
22-
private static ThreadLocal<Boolean> isTracingDisabled = ThreadLocal.withInitial(() -> Boolean.FALSE);
23-
private static ThreadLocal<Boolean> isAlwaysFuzzyPassedTracing = ThreadLocal.withInitial(() -> Boolean.FALSE);
23+
private static final ThreadLocal<Boolean> isTracingDisabled = ThreadLocal.withInitial(() -> Boolean.FALSE);
24+
private static final ThreadLocal<Boolean> isAlwaysFuzzyPassedTracing = ThreadLocal.withInitial(() -> Boolean.FALSE);
2425

2526
private CheckLevel checkLevel;
2627
private final Object value;

webtau-docs/znai/HTTP/data-node.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ to get values required for consequent test calls.
2323

2424
If you have a list of objects like `complexList` above, you can access all its children property value with `complexList.k2`.
2525

26-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy children key shortcut", bodyOnly: true}
26+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "children key shortcut", bodyOnly: true}
2727

2828
# Path based properties access
2929

@@ -56,34 +56,34 @@ Special values inside assertion block have convenient methods
5656

5757
`each` to iterate over a list
5858

59-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy each on simple list", bodyOnly: true, title: "List of simple values"}
59+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "each on simple list", bodyOnly: true, title: "List of simple values"}
6060

61-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy each on complex list", bodyOnly: true, title: "List of complex values"}
61+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "each on complex list", bodyOnly: true, title: "List of complex values"}
6262

6363

6464
# Find
6565

6666
`find` to find a single value
6767

68-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy find on list", bodyOnly: true}
68+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "find on list", bodyOnly: true}
6969

7070

7171
and `findAll` to find all the values matching predicate
7272

73-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy findAll on list", bodyOnly: true}
73+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "findAll on list", bodyOnly: true}
7474

7575
Note: While values inside a predicate are normal values, the result of `find` and `findAll` is still `DataNode`
7676

77-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy find on list of objects", bodyOnly: true}
77+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "find on list of objects", bodyOnly: true}
7878

7979
# Collect
8080

8181
Use `collect` to transform a collection of items
8282

83-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy transform list", bodyOnly: true}
83+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "transform list", bodyOnly: true}
8484

8585
# Combine
8686

8787
Methods `find` and `collect` can be chained
8888

89-
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "groovy findAll, collect, and sum", bodyOnly: true}
89+
:include-groovy: org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy {entry: "findAll, collect, and sum", bodyOnly: true}

webtau-http-groovy/src/main/groovy/org/testingisdocumenting/webtau/http/datanode/GroovyDataNode.groovy

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.testingisdocumenting.webtau.http.datanode
1919

20+
import org.testingisdocumenting.webtau.data.traceable.CheckLevel
2021
import org.testingisdocumenting.webtau.data.traceable.TraceableValue
2122
import org.testingisdocumenting.webtau.expectation.ActualPath
2223
import org.testingisdocumenting.webtau.http.datacoverage.DataNodeToMapOfValuesConverter
@@ -118,13 +119,26 @@ class GroovyDataNode implements DataNodeExpectations, DataNode {
118119

119120
DataNode find(Closure predicate) {
120121
def result = node.elements().find(delegateToNodeAndRemovedDataNodeFromClosure(predicate))
121-
return (result instanceof DataNode) ?
122-
new GroovyDataNode(result) :
123-
result
122+
123+
if (result instanceof DataNode) {
124+
if (result.singleValue) {
125+
result.traceableValue.updateCheckLevel(CheckLevel.FuzzyPassed)
126+
}
127+
return new GroovyDataNode(result)
128+
}
129+
130+
return result
124131
}
125132

126133
DataNode findAll(Closure predicate) {
127134
def list = node.elements().findAll(delegateToNodeAndRemovedDataNodeFromClosure(predicate))
135+
136+
list.each {
137+
if (it.isSingleValue()) {
138+
it.traceableValue.updateCheckLevel(CheckLevel.FuzzyPassed)
139+
}
140+
}
141+
128142
return wrapIntoDataNode('findAll', list)
129143
}
130144

webtau-http-groovy/src/test/groovy/org/testingisdocumenting/webtau/http/HttpGroovyTest.groovy

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,14 @@ class HttpGroovyTest extends HttpTestBase {
515515
}
516516

517517
@Test
518-
void "groovy each on simple list"() {
518+
void "each on simple list"() {
519519
http.get("/end-point") {
520520
list.each { it.shouldBe > 0 }
521521
}
522522
}
523523

524524
@Test
525-
void "groovy each on complex list"() {
525+
void "each on complex list"() {
526526
http.get("/end-point") {
527527
complexList.each { k2.shouldBe > 0 }
528528
}
@@ -541,7 +541,7 @@ class HttpGroovyTest extends HttpTestBase {
541541
}
542542

543543
@Test
544-
void "groovy find on list"() {
544+
void "find on list"() {
545545
def found = http.get("/end-point") {
546546
return list.find { it > 1 }
547547
}
@@ -551,7 +551,19 @@ class HttpGroovyTest extends HttpTestBase {
551551
}
552552

553553
@Test
554-
void "groovy find on body that is not a list"() {
554+
void "find should mark value as fuzzy checked"() {
555+
http.get("/end-point") {
556+
return list.find { it > 1 }
557+
}
558+
559+
def listElements = http.lastValidationResult.bodyNode.get("list").elements()
560+
listElements.get(0).traceableValue.checkLevel.should == CheckLevel.None
561+
listElements.get(1).traceableValue.checkLevel.should == CheckLevel.FuzzyPassed
562+
listElements.get(2).traceableValue.checkLevel.should == CheckLevel.None
563+
}
564+
565+
@Test
566+
void "find on body that is not a list"() {
555567
def found = http.get("/end-point") {
556568
return body.find { it > 1 }
557569
}
@@ -560,7 +572,7 @@ class HttpGroovyTest extends HttpTestBase {
560572
}
561573

562574
@Test
563-
void "groovy findAll on list"() {
575+
void "findAll on list"() {
564576
def found = http.get("/end-point") {
565577
return list.findAll { it > 1 }
566578
}
@@ -570,7 +582,19 @@ class HttpGroovyTest extends HttpTestBase {
570582
}
571583

572584
@Test
573-
void "groovy findAll on body that is not a list"() {
585+
void "findAll on list should mark values as fuzzy checked"() {
586+
def found = http.get("/end-point") {
587+
return list.findAll { it > 1 }
588+
}
589+
590+
def listElements = http.lastValidationResult.bodyNode.get("list").elements()
591+
listElements.get(0).traceableValue.checkLevel.should == CheckLevel.None
592+
listElements.get(1).traceableValue.checkLevel.should == CheckLevel.FuzzyPassed
593+
listElements.get(2).traceableValue.checkLevel.should == CheckLevel.FuzzyPassed
594+
}
595+
596+
@Test
597+
void "findAll on body that is not a list"() {
574598
def found = http.get("/end-point") {
575599
return body.findAll { it > 1 }
576600
}
@@ -579,7 +603,7 @@ class HttpGroovyTest extends HttpTestBase {
579603
}
580604

581605
@Test
582-
void "groovy find on list of objects"() {
606+
void "find on list of objects"() {
583607
def id = http.get("/end-point") {
584608
def found = complexList.find {
585609
assert k1.getClass() == String
@@ -594,7 +618,7 @@ class HttpGroovyTest extends HttpTestBase {
594618
}
595619

596620
@Test
597-
void "groovy transform list"() {
621+
void "transform list"() {
598622
def transformed = http.get("/end-point") {
599623
return list.collect { "world#${it}" }
600624
}
@@ -604,7 +628,7 @@ class HttpGroovyTest extends HttpTestBase {
604628
}
605629

606630
@Test
607-
void "groovy transform list by referencing node"() {
631+
void "transform list by referencing node"() {
608632
def ids = http.get("/end-point") {
609633
return complexList.collect { it.id }
610634
}
@@ -613,7 +637,7 @@ class HttpGroovyTest extends HttpTestBase {
613637
}
614638

615639
@Test
616-
void "groovy transform on body that is not a list"() {
640+
void "transform on body that is not a list"() {
617641
def transformed = http.get("/end-point") {
618642
return body.collect { "world#${it}" }
619643
}
@@ -622,7 +646,7 @@ class HttpGroovyTest extends HttpTestBase {
622646
}
623647

624648
@Test
625-
void "groovy findAll, collect, and sum"() {
649+
void "findAll, collect, and sum"() {
626650
def sum = http.get("/end-point") {
627651
return complexList
628652
.findAll { k1.startsWith('v1') }
@@ -634,7 +658,7 @@ class HttpGroovyTest extends HttpTestBase {
634658
}
635659

636660
@Test
637-
void "groovy children key shortcut"() {
661+
void "children key shortcut"() {
638662
http.get("/end-point") {
639663
complexList.k2.should == [30, 40]
640664
}

0 commit comments

Comments
 (0)