From 7d2f01aad2fd166f5216d11811f71af5310c1222 Mon Sep 17 00:00:00 2001 From: Matt Reynolds Date: Mon, 16 May 2016 16:06:37 -0600 Subject: [PATCH] fix for issue #47 - add async options on request when new async flag set (#277) * fix for issue #47 - add async options on request when new async flag set * don't add async to initial request declaration and switch spaces to tabs for all changes. --- .../builder/JUnitMethodBodyBuilder.groovy | 6 +++- ...kMethodRequestProcessingBodyBuilder.groovy | 6 +++- .../accurest/dsl/internal/Response.groovy | 7 ++++- .../MockMvcMethodBodyBuilderSpec.groovy | 31 +++++++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/JUnitMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/JUnitMethodBodyBuilder.groovy index 02f8ad854f..543e8c41cf 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/JUnitMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/JUnitMethodBodyBuilder.groovy @@ -88,7 +88,11 @@ abstract class JUnitMethodBodyBuilder extends RequestProcessingMethodBodyBuilder @Override protected String getInputString(Request request) { - return 'ResponseOptions response = given().spec(request)' + def inputString = 'ResponseOptions response = given().spec(request)' + if (response.async){ + inputString = inputString + '.when().async()' + } + return inputString } @Override diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodRequestProcessingBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodRequestProcessingBodyBuilder.groovy index b9d1aedf86..c149c13bb8 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodRequestProcessingBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodRequestProcessingBodyBuilder.groovy @@ -71,7 +71,11 @@ abstract class SpockMethodRequestProcessingBodyBuilder extends RequestProcessing @Override protected String getInputString(Request request) { - return 'def response = given().spec(request)' + def inputString = 'def response = given().spec(request)' + if (response.async){ + inputString = inputString + '.when().async()' + } + return inputString } @Override diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Response.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Response.groovy index 612a092ef1..9078add18f 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Response.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Response.groovy @@ -14,6 +14,7 @@ class Response extends Common { DslProperty delay Headers headers Body body + boolean async Response() { } @@ -54,6 +55,10 @@ class Response extends Common { this.delay = toDslProperty(timeInMilliseconds) } + public void async() { + this.async = true + } + void assertThatSidesMatch(OptionalProperty stubSide, Object testSide) { throw new IllegalStateException("Optional can be used only in the test side of the response!") } @@ -75,4 +80,4 @@ class ClientResponse extends Response { ClientResponse(Response request) { super(request) } -} \ No newline at end of file +} diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy index ce1f0db1a0..533eaf7a9a 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcMethodBodyBuilderSpec.groovy @@ -1263,6 +1263,33 @@ World.'''""" "MockMvcSpockMethodBuilder" | { GroovyDsl dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl) } "MockMvcJUnitMethodBuilder" | { GroovyDsl dsl -> new MockMvcJUnitMethodBodyBuilder(dsl) } } + @Issue('47') + def "should generate async body when async flag set in response"() { + given: + GroovyDsl contractDsl = GroovyDsl.make { + request { + method 'GET' + url '/test' + } + response { + status 200 + async() + } + } + MethodBodyBuilder builder = methodBuilder(contractDsl) + BlockBuilder blockBuilder = new BlockBuilder(" ") + when: + builder.appendTo(blockBuilder) + def test = blockBuilder.toString() + then: + test.contains(bodyDefinitionString) + and: + stubMappingIsValidWireMockStub(contractDsl) + where: + methodBuilderName | methodBuilder | bodyDefinitionString + "MockMvcSpockMethodBuilder" | { GroovyDsl dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl) } | '.when().async()' + "MockMvcJUnitMethodBuilder" | { GroovyDsl dsl -> new MockMvcJUnitMethodBodyBuilder(dsl) } | '.when().async()' + } def "should generate proper test code with array of primitives using #methodBuilderName"() { given: @@ -1280,7 +1307,7 @@ World.'''""" } ] } - ''') + ''') } } MethodBodyBuilder builder = methodBuilder(contractDsl) @@ -1343,4 +1370,4 @@ World.'''""" } } // end::dsl_example[] -} \ No newline at end of file +}