From ce42dd5e85c3aa91ae8e4884893bb265ca897cd0 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 11 May 2015 18:17:06 +0200 Subject: [PATCH 1/3] [#55] Fixed regex processing on the server side --- .../builder/SpockMethodBodyBuilder.groovy | 7 +++++++ .../accurest/dsl/internal/Common.groovy | 4 ++++ .../dsl/internal/MethodProperty.groovy | 13 ++++++++++++ .../pairId/moreComplexVersion.groovy | 20 +++++++++++++++++++ .../ofg/twitter/place/PairIdController.groovy | 7 ++++++- 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy create mode 100644 accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy index 25ebd02b99..babee4264c 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy @@ -5,6 +5,8 @@ import groovy.transform.PackageScope import io.codearte.accurest.dsl.GroovyDsl import io.codearte.accurest.dsl.internal.Header +import java.util.regex.Pattern + /** * @author Jakub Kubrynski */ @@ -73,6 +75,8 @@ class SpockMethodBodyBuilder { processMapElement(value, blockBuilder, property) } else if (value instanceof List) { processArrayElements(value, property, blockBuilder) + } else if (value instanceof Pattern) { + blockBuilder.addLine("responseBody$property ==~ java.util.regex.Pattern.compile('${value}')") } else { blockBuilder.addLine("responseBody$property == ${value}") } @@ -90,4 +94,7 @@ class SpockMethodBodyBuilder { } } } + private void processClosure(Closure value, BlockBuilder blockBuilder, String property) { + blockBuilder.addLine() + } } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy index f6eb65ab9e..864c52efe4 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy @@ -66,6 +66,10 @@ class Common { return Pattern.compile(regex) } + MethodProperty execute(String closureDefinition) { + return new MethodProperty(closureDefinition) + } + ClientDslProperty client(Object clientValue) { return new ClientDslProperty(clientValue) } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy new file mode 100644 index 0000000000..b707a8a7c1 --- /dev/null +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy @@ -0,0 +1,13 @@ +package io.codearte.accurest.dsl.internal + +import groovy.transform.CompileStatic + +@CompileStatic +class MethodProperty { + + final String closureDefinition + + MethodProperty(String closureDefinition) { + this.closureDefinition = closureDefinition + } +} diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy new file mode 100644 index 0000000000..9937b43721 --- /dev/null +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy @@ -0,0 +1,20 @@ +io.codearte.accurest.dsl.GroovyDsl.make { + request { + method 'PUT' + url $(client(regex('^/api/[0-9]{2}$')), server('/api/12')) + headers { + header 'Content-Type': 'application/json' + } + body '''\ + [{ + "text": "Gonna see you at Warsaw" + }] +''' + } + response { + body ( + path: $(client('/api/12'), server(regex('^/api/[0-9]{2}$'))) + ) + status 200 + } +} \ No newline at end of file diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy index 8a4e1e2e63..ee02469454 100644 --- a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy @@ -21,10 +21,15 @@ class PairIdController { method = PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - void getPlacesFromTweets(@PathVariable long pairId, @RequestBody List tweets) { + String getPlacesFromTweets(@PathVariable long pairId, @RequestBody List tweets) { log.info("Inside PairIdController, doing very important logic") if (tweets?.text != ["Gonna see you at Warsaw"]) { throw new IllegalArgumentException("Wrong text in tweet: ${tweets?.text}") } + return """ + { + "path" : "/api/$pairId" + } + """ } } From efc8e9ec72b62f86dd3af6c4b8aec4369f77a3d8 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 11 May 2015 18:40:06 +0200 Subject: [PATCH 2/3] Revert "[#55] Fixed regex processing on the server side" This reverts commit ce42dd5e85c3aa91ae8e4884893bb265ca897cd0. --- .../builder/SpockMethodBodyBuilder.groovy | 7 ------- .../accurest/dsl/internal/Common.groovy | 4 ---- .../dsl/internal/MethodProperty.groovy | 13 ------------ .../pairId/moreComplexVersion.groovy | 20 ------------------- .../ofg/twitter/place/PairIdController.groovy | 7 +------ 5 files changed, 1 insertion(+), 50 deletions(-) delete mode 100644 accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy delete mode 100644 accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy index babee4264c..25ebd02b99 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy @@ -5,8 +5,6 @@ import groovy.transform.PackageScope import io.codearte.accurest.dsl.GroovyDsl import io.codearte.accurest.dsl.internal.Header -import java.util.regex.Pattern - /** * @author Jakub Kubrynski */ @@ -75,8 +73,6 @@ class SpockMethodBodyBuilder { processMapElement(value, blockBuilder, property) } else if (value instanceof List) { processArrayElements(value, property, blockBuilder) - } else if (value instanceof Pattern) { - blockBuilder.addLine("responseBody$property ==~ java.util.regex.Pattern.compile('${value}')") } else { blockBuilder.addLine("responseBody$property == ${value}") } @@ -94,7 +90,4 @@ class SpockMethodBodyBuilder { } } } - private void processClosure(Closure value, BlockBuilder blockBuilder, String property) { - blockBuilder.addLine() - } } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy index 864c52efe4..f6eb65ab9e 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy @@ -66,10 +66,6 @@ class Common { return Pattern.compile(regex) } - MethodProperty execute(String closureDefinition) { - return new MethodProperty(closureDefinition) - } - ClientDslProperty client(Object clientValue) { return new ClientDslProperty(clientValue) } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy deleted file mode 100644 index b707a8a7c1..0000000000 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/MethodProperty.groovy +++ /dev/null @@ -1,13 +0,0 @@ -package io.codearte.accurest.dsl.internal - -import groovy.transform.CompileStatic - -@CompileStatic -class MethodProperty { - - final String closureDefinition - - MethodProperty(String closureDefinition) { - this.closureDefinition = closureDefinition - } -} diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy deleted file mode 100644 index 9937b43721..0000000000 --- a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy +++ /dev/null @@ -1,20 +0,0 @@ -io.codearte.accurest.dsl.GroovyDsl.make { - request { - method 'PUT' - url $(client(regex('^/api/[0-9]{2}$')), server('/api/12')) - headers { - header 'Content-Type': 'application/json' - } - body '''\ - [{ - "text": "Gonna see you at Warsaw" - }] -''' - } - response { - body ( - path: $(client('/api/12'), server(regex('^/api/[0-9]{2}$'))) - ) - status 200 - } -} \ No newline at end of file diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy index ee02469454..8a4e1e2e63 100644 --- a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy @@ -21,15 +21,10 @@ class PairIdController { method = PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - String getPlacesFromTweets(@PathVariable long pairId, @RequestBody List tweets) { + void getPlacesFromTweets(@PathVariable long pairId, @RequestBody List tweets) { log.info("Inside PairIdController, doing very important logic") if (tweets?.text != ["Gonna see you at Warsaw"]) { throw new IllegalArgumentException("Wrong text in tweet: ${tweets?.text}") } - return """ - { - "path" : "/api/$pairId" - } - """ } } From bd6a4c4b4c4609a2fe8c2c2d1d268a7b0b99ecec Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 12 May 2015 00:02:04 +0200 Subject: [PATCH 3/3] [#49] Added missing request body conversion of Wiremock to DSL --- .../wiremock/WiremockToDslConverter.groovy | 27 +++--- .../WiremockToDslConverterSpec.groovy | 84 ++++++++++++++++++- .../io/codearte/accurest/dsl/GroovyDsl.groovy | 2 +- .../accurest/dsl/internal/Request.groovy | 6 +- .../accurest/dsl/internal/Response.groovy | 23 ++--- 5 files changed, 105 insertions(+), 37 deletions(-) diff --git a/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy b/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy index bfc58cc17d..beb15bfd2b 100644 --- a/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy +++ b/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy @@ -16,6 +16,7 @@ class WiremockToDslConverter { Object wiremockStub = new JsonSlurper().parseText(wiremockStringStub) def request = wiremockStub.request def response = wiremockStub.response + def bodyPatterns = request.bodyPatterns return """\ request { ${request.method ? "method \"\"\"$request.method\"\"\"" : ""} @@ -23,18 +24,20 @@ class WiremockToDslConverter { ${request.urlPattern ? "url \$(client(regex('${escapeJava(request.urlPattern)}')), server(''))" : ""} ${request.urlPath ? "url \"\"\"$request.urlPath\"\"\"" : ""} ${ - request.headers ? """headers { - ${ - request.headers.collect { - def assertion = it.value - String headerName = it.key as String - def entry = assertion.entrySet().first() - """header(\"\"\"$headerName\"\"\", ${buildHeader(entry.key, entry.value)})\n""" - }.join('') - } - } - """ : "" - } + request.headers ? """headers { + ${ + request.headers.collect { + def assertion = it.value + String headerName = it.key as String + def entry = assertion.entrySet().first() + """header(\"\"\"$headerName\"\"\", ${buildHeader(entry.key, entry.value)})\n""" + }.join('') + } + } + """ : "" + } + ${bodyPatterns?.equalTo ? "body('''${bodyPatterns.equalTo}''')" : '' } + ${bodyPatterns?.matches ? "body \$(client(regex('${escapeJava(bodyPatterns.matches)}')), server(''))" : ""} } response { ${response.status ? "status $response.status" : ""} diff --git a/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy b/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy index 7466b2e7f5..a88c05681e 100755 --- a/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy +++ b/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy @@ -71,7 +71,7 @@ class WiremockToDslConverterSpec extends Specification { } - def 'should convert Wiremock stub with body containing simple JSON'() { + def 'should convert Wiremock stub with response body containing simple JSON'() { given: String wiremockStub = '''\ { @@ -122,7 +122,7 @@ class WiremockToDslConverterSpec extends Specification { }""") == expectedGroovyDsl } - def 'should convert Wiremock stub with body containing integer'() { + def 'should convert Wiremock stub with response body containing integer'() { given: String wiremockStub = '''\ { @@ -171,7 +171,7 @@ class WiremockToDslConverterSpec extends Specification { }""") == expectedGroovyDsl } - def 'should convert Wiremock stub with body as a list'() { + def 'should convert Wiremock stub with response body as a list'() { given: String wiremockStub = '''\ { @@ -224,7 +224,7 @@ class WiremockToDslConverterSpec extends Specification { } - def 'should convert Wiremock stub with body containing a nested list'() { + def 'should convert Wiremock stub with response body containing a nested list'() { given: String wiremockStub = '''\ { @@ -287,4 +287,80 @@ class WiremockToDslConverterSpec extends Specification { }""") == expectedGroovyDsl } + def 'should convert Wiremock stub with request body checking equality to Json'() { + given: + String wiremockStub = '''\ +{ + "request": { + "method": "POST", + "url": "/test", + "bodyPatterns": { + "equalTo": "{\\"property1\\":\\"abc\\",\\"property2\\":\\"2017-01\\",\\"property3\\":\\"666\\",\\"property4\\":1428566412}" + } + }, + "response": { + "status": 200 + } +} +''' + and: + GroovyDsl expectedGroovyDsl = GroovyDsl.make { + request { + method 'POST' + url '/test' + body ('''{"property1":"abc","property2":"2017-01","property3":"666","property4":1428566412}''') + } + response { + status 200 + } + } + when: + String groovyDsl = WiremockToDslConverter.fromWiremockStub(wiremockStub) + then: + GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + """ io.codearte.accurest.dsl.GroovyDsl.make { + $groovyDsl + }""") + and: + evaluatedGroovyDsl == expectedGroovyDsl + } + + def 'should convert Wiremock stub with request body checking matching to Json'() { + given: + String wiremockStub = '''\ +{ + "request": { + "method": "POST", + "url": "/test", + "bodyPatterns": { + "matches": "[0-9]{5}" + } + }, + "response": { + "status": 200 + } +} +''' + and: + GroovyDsl expectedGroovyDsl = GroovyDsl.make { + request { + method 'POST' + url '/test' + body $(client(~/[0-9]{5}/), server('')) + } + response { + status 200 + } + } + when: + String groovyDsl = WiremockToDslConverter.fromWiremockStub(wiremockStub) + then: + GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + """ io.codearte.accurest.dsl.GroovyDsl.make { + $groovyDsl + }""") + and: + evaluatedGroovyDsl == expectedGroovyDsl + } + } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/GroovyDsl.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/GroovyDsl.groovy index 717047341d..1007e14cec 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/GroovyDsl.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/GroovyDsl.groovy @@ -7,7 +7,7 @@ import io.codearte.accurest.dsl.internal.Request import io.codearte.accurest.dsl.internal.Response @TypeChecked -@EqualsAndHashCode(includeFields = true) +@EqualsAndHashCode @ToString(includeFields = true, includePackage = false, includeNames = true) class GroovyDsl { diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Request.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Request.groovy index 77652a9396..50bd42910c 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Request.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Request.groovy @@ -5,7 +5,7 @@ import groovy.transform.ToString import groovy.transform.TypeChecked @TypeChecked -@EqualsAndHashCode(includeFields = true) +@EqualsAndHashCode @ToString(includePackage = false, includeNames = true) class Request extends Common { @@ -64,7 +64,7 @@ class Request extends Common { } @CompileStatic -@EqualsAndHashCode(includeFields = true) +@EqualsAndHashCode @ToString(includePackage = false) class ServerRequest extends Request { ServerRequest(Request request) { @@ -73,7 +73,7 @@ class ServerRequest extends Request { } @CompileStatic -@EqualsAndHashCode(includeFields = true) +@EqualsAndHashCode @ToString(includePackage = false) class ClientRequest extends Request { ClientRequest(Request request) { 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 ce8a7b3b7d..3715f0c243 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 @@ -6,13 +6,13 @@ import groovy.transform.ToString import groovy.transform.TypeChecked @TypeChecked -@EqualsAndHashCode(includeFields = true) +@EqualsAndHashCode @ToString(includePackage = false, includeFields = true) class Response extends Common { - private DslProperty status - private Headers headers - private Body body + DslProperty status + Headers headers + Body body Response() { } @@ -49,21 +49,10 @@ class Response extends Common { this.body = new Body(bodyAsValue) } - Body getBody() { - return body - } - - DslProperty getStatus() { - return status - } - - Headers getHeaders() { - return headers - } } @CompileStatic -@EqualsAndHashCode(includeFields = true) +@EqualsAndHashCode @ToString(includePackage = false) class ServerResponse extends Response { ServerResponse(Response request) { @@ -72,7 +61,7 @@ class ServerResponse extends Response { } @CompileStatic -@EqualsAndHashCode(includeFields = true) +@EqualsAndHashCode @ToString(includePackage = false) class ClientResponse extends Response { ClientResponse(Response request) {