Skip to content

Commit 5caec1e

Browse files
http: enable groovy config for http validation handlers
1 parent 9dc4bf0 commit 5caec1e

9 files changed

Lines changed: 94 additions & 5 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2021 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package scenarios.rest.handlers
18+
19+
import org.testingisdocumenting.webtau.http.validation.HttpValidationHandler
20+
import org.testingisdocumenting.webtau.http.validation.HttpValidationResult
21+
22+
class ThrowExceptionHttpValidationHandler implements HttpValidationHandler {
23+
@Override
24+
void validate(HttpValidationResult validationResult) {
25+
throw new RuntimeException("custom validation: " + validationResult.getUrl())
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2020 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package scenarios.rest
18+
19+
import static org.testingisdocumenting.webtau.WebTauGroovyDsl.*
20+
21+
scenario('validation handler') {
22+
http.get("/weather") {
23+
temperature.shouldBe < 100
24+
}
25+
}

webtau-feature-testing/examples/scenarios/rest/ping.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package scenarios.rest
1818

1919
import static org.testingisdocumenting.webtau.WebTauGroovyDsl.*
20-
import static org.testingisdocumenting.webtau.http.Http.http
2120

2221
scenario('ping') {
2322
if (!http.ping("/weather")) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package scenarios.rest
2+
3+
import scenarios.rest.handlers.ThrowExceptionHttpValidationHandler
4+
5+
httpValidationHandlers = [ThrowExceptionHttpValidationHandler]

webtau-feature-testing/src/test/groovy/org/testingisdocumenting/webtau/featuretesting/WebTauRestFeaturesTest.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class WebTauRestFeaturesTest {
7373
])
7474
}
7575

76+
@Test
77+
void "validation handler"() {
78+
runCli('httpValidationHandler.groovy', 'webtau.validation-handler.cfg.groovy', "--url=${testRunner.testServer.uri}")
79+
}
80+
7681
@Test
7782
void "schema validation"() {
7883
runCli('jsonSchema/validateSchema.groovy', 'jsonSchema/webtau.cfg.groovy', "--url=${testRunner.testServer.uri}")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"scenarioDetails" : [ {
3+
"scenario" : "validation handler",
4+
"shortContainerId" : "httpValidationHandler.groovy",
5+
"stepsSummary" : {
6+
"numberOfSuccessful" : 2,
7+
"numberOfFailed" : 1
8+
}
9+
} ],
10+
"exitCode" : 1
11+
}

webtau-groovy/src/main/groovy/org/testingisdocumenting/webtau/cfg/WebTauGroovyFileConfigHandler.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import org.testingisdocumenting.webtau.graphql.listener.GraphQLListener
2626
import org.testingisdocumenting.webtau.graphql.listener.GraphQLListeners
2727
import org.testingisdocumenting.webtau.http.listener.HttpListener
2828
import org.testingisdocumenting.webtau.http.listener.HttpListeners
29+
import org.testingisdocumenting.webtau.http.validation.HttpValidationHandler
30+
import org.testingisdocumenting.webtau.http.validation.HttpValidationHandlers
2931
import org.testingisdocumenting.webtau.report.ReportGenerator
3032
import org.testingisdocumenting.webtau.report.ReportGenerators
3133
import org.testingisdocumenting.webtau.reporter.TestListener
@@ -84,6 +86,7 @@ class WebTauGroovyFileConfigHandler implements WebTauConfigHandler {
8486
setupTestListeners(parsedConfig)
8587
setupGraphQLListeners(parsedConfig)
8688
setupHttpListeners(parsedConfig)
89+
setupHttpValidationHandlers(parsedConfig)
8790
setupDbDataSourceProviders(parsedConfig)
8891
}
8992

@@ -182,6 +185,11 @@ class WebTauGroovyFileConfigHandler implements WebTauConfigHandler {
182185
listenerInstances.each { HttpListeners.add(it) }
183186
}
184187

188+
private static void setupHttpValidationHandlers(ConfigObject config) {
189+
List<HttpValidationHandler> handlerInstances = instancesFromConfig(config, 'httpValidationHandlers')
190+
handlerInstances.each { HttpValidationHandlers.add(it) }
191+
}
192+
185193
private static void setupDbDataSourceProviders(ConfigObject config) {
186194
List<DbDataSourceProvider> providers = instancesFromConfig(config, 'dbDataSourceProviders')
187195
providers.each { DbDataSourceProviders.add(it) }

webtau-groovy/src/main/groovy/org/testingisdocumenting/webtau/cli/WebTauCliApp.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.testingisdocumenting.webtau.console.ConsoleOutputs
3131
import org.testingisdocumenting.webtau.console.ansi.AnsiConsoleOutput
3232
import org.testingisdocumenting.webtau.console.ansi.Color
3333
import org.testingisdocumenting.webtau.console.ansi.NoAnsiConsoleOutput
34+
import org.testingisdocumenting.webtau.http.validation.HttpValidationHandlers
3435
import org.testingisdocumenting.webtau.report.ReportGenerator
3536
import org.testingisdocumenting.webtau.report.ReportGenerators
3637
import org.testingisdocumenting.webtau.reporter.*
@@ -150,6 +151,7 @@ class WebTauCliApp implements TestListener, ReportGenerator {
150151
ConsoleOutputs.remove(consoleOutput)
151152
TestListeners.clearAdded()
152153
ReportGenerators.clearAdded()
154+
HttpValidationHandlers.clearAdded()
153155
GroovyConfigBasedHttpConfiguration.clear()
154156
}
155157

webtau-http/src/main/java/org/testingisdocumenting/webtau/http/validation/HttpValidationHandlers.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@
2626

2727
public class HttpValidationHandlers {
2828
private static final List<HttpValidationHandler> globalHandlers = ServiceLoaderUtils.load(HttpValidationHandler.class);
29+
private static final List<HttpValidationHandler> addedHandlers = new ArrayList<>();
30+
2931
private static final ThreadLocal<List<HttpValidationHandler>> localHandlers = ThreadLocal.withInitial(ArrayList::new);
3032
private static final ThreadLocal<Boolean> enabled = ThreadLocal.withInitial(() -> true);
3133

3234
public static void add(HttpValidationHandler handler) {
33-
globalHandlers.add(handler);
35+
addedHandlers.add(handler);
3436
}
3537

3638
public static void remove(HttpValidationHandler handler) {
37-
globalHandlers.remove(handler);
39+
addedHandlers.remove(handler);
40+
}
41+
42+
public static void clearAdded() {
43+
addedHandlers.clear();
3844
}
3945

4046
public static <R> R withDisabledHandlers(Supplier<R> code) {
@@ -57,8 +63,9 @@ public static <R> R withAdditionalHandler(HttpValidationHandler handler, Supplie
5763

5864
public static void validate(HttpValidationResult validationResult) {
5965
if (enabled.get()) {
60-
Stream.concat(localHandlers.get().stream(), globalHandlers.stream())
61-
.forEach(c -> c.validate(validationResult));
66+
Stream.concat(addedHandlers.stream(),
67+
Stream.concat(localHandlers.get().stream(), globalHandlers.stream()))
68+
.forEach(c -> c.validate(validationResult));
6269
}
6370
}
6471

0 commit comments

Comments
 (0)