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..76570ee2b2 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,37 +16,41 @@ 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\"\"\"" : ""} - ${request.url ? "url \"\"\"$request.url\"\"\"" : ""} - ${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 { + ${request.method ? "method \"\"\"$request.method\"\"\"" : ""} + ${request.url ? "url \"\"\"$request.url\"\"\"" : ""} + ${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('') + } + } + """ : "" + } + ${bodyPatterns?.equalTo?.every { it } ? "body('''${bodyPatterns.equalTo[0]}''')" : ''} + ${bodyPatterns?.equalToJson?.every { it } ? "body('''${bodyPatterns.equalToJson[0]}''')" : ''} + ${bodyPatterns?.matches?.every { it } ? "body \$(client(regex('${escapeJava(bodyPatterns.matches[0])}')), server(''))" : ""} } - } - """ : "" - } - } - response { - ${response.status ? "status $response.status" : ""} - ${response.body ? "body( ${buildBody(response.body)})" : ""} - ${ + response { + ${response.status ? "status $response.status" : ""} + ${response.body ? "body( ${buildBody(response.body)})" : ""} + ${ response.headers ? """headers { - ${response.headers.collect { "header('$it.key': '${it.value}')\n" }.join('')} - } - """ : "" + ${response.headers.collect { "header('$it.key': '${it.value}')\n" }.join('')} + } + """ : "" } - } - """ + } + """ } private String buildHeader(String method, Object value) { @@ -155,7 +159,7 @@ class WiremockToDslConverter { static String wrapWithFactoryMethod(String dslFromWiremockStub) { return """\ ${GroovyDsl.name}.make { - $dslFromWiremockStub + $dslFromWiremockStub } """ } 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..98d74e6ca2 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 @@ -1,5 +1,6 @@ package io.codearte.accurest.wiremock +import com.github.tomakehurst.wiremock.stubbing.StubMapping import io.codearte.accurest.dsl.GroovyDsl import spock.lang.Specification @@ -9,27 +10,29 @@ class WiremockToDslConverterSpec extends Specification { given: String wiremockStub = '''\ { - "request": { - "method": "GET", - "url": "/path", - "headers" : { - "Accept": { - "matches": "text/.*" - }, - "X-Custom-Header": { - "contains": "2134" - } - } - }, - "response": { - "status": 200, - "body": "{ \\"id\\": { \\"value\\": \\"132\\" }, \\"surname\\": \\"Kowalsky\\", \\"name\\": \\"Jan\\", \\"created\\": \\"2014-02-02 12:23:43\\" }", - "headers": { - "Content-Type": "text/plain", - } - } + "request": { + "method": "GET", + "url": "/path", + "headers" : { + "Accept": { + "matches": "text/.*" + }, + "X-Custom-Header": { + "contains": "2134" + } + } + }, + "response": { + "status": 200, + "body": "{ \\"id\\": { \\"value\\": \\"132\\" }, \\"surname\\": \\"Kowalsky\\", \\"name\\": \\"Jan\\", \\"created\\": \\"2014-02-02 12:23:43\\" }", + "headers": { + "Content-Type": "text/plain" + } + } } ''' + and: + stubMappingIsValidWiremockStub(wiremockStub) and: GroovyDsl expectedGroovyDsl = GroovyDsl.make { request { @@ -66,33 +69,35 @@ class WiremockToDslConverterSpec extends Specification { then: new GroovyShell(this.class.classLoader).evaluate( """ io.codearte.accurest.dsl.GroovyDsl.make { - $groovyDsl - }""") == expectedGroovyDsl + $groovyDsl + }""") == expectedGroovyDsl } - def 'should convert Wiremock stub with body containing simple JSON'() { + def 'should convert Wiremock stub with response body containing simple JSON'() { given: String wiremockStub = '''\ { - "request": { - "method": "DELETE", - "urlPattern": "/credit-card-verification-data/[0-9]+", - "headers": { - "Content-Type": { - "equalTo": "application/vnd.mymoid-adapter.v2+json; charset=UTF-8" - } - } - }, - "response": { - "status": 200, - "body": "{\\"status\\": \\"OK\\"}", - "headers": { - "Content-Type": "application/json" - } - } + "request": { + "method": "DELETE", + "urlPattern": "/credit-card-verification-data/[0-9]+", + "headers": { + "Content-Type": { + "equalTo": "application/vnd.mymoid-adapter.v2+json; charset=UTF-8" + } + } + }, + "response": { + "status": 200, + "body": "{\\"status\\": \\"OK\\"}", + "headers": { + "Content-Type": "application/json" + } + } } ''' + and: + stubMappingIsValidWiremockStub(wiremockStub) and: GroovyDsl expectedGroovyDsl = GroovyDsl.make { request { @@ -105,7 +110,7 @@ class WiremockToDslConverterSpec extends Specification { response { status 200 body("""{ - "status": "OK" + "status": "OK" }""") headers { header 'Content-Type': 'application/json' @@ -118,32 +123,34 @@ class WiremockToDslConverterSpec extends Specification { then: new GroovyShell(this.class.classLoader).evaluate( """ io.codearte.accurest.dsl.GroovyDsl.make { - $groovyDsl - }""") == expectedGroovyDsl + $groovyDsl + }""") == expectedGroovyDsl } - def 'should convert Wiremock stub with body containing integer'() { + def 'should convert Wiremock stub with response body containing integer'() { given: String wiremockStub = '''\ { "request": { - "method": "POST", - "url": "/charge/count", - "headers": { - "Content-Type": { - "equalTo": "application/vnd.creditcard-reporter.v1+json" - } - } + "method": "POST", + "url": "/charge/count", + "headers": { + "Content-Type": { + "equalTo": "application/vnd.creditcard-reporter.v1+json" + } + } }, "response": { - "status": 200, - "body": 200, - "headers": { - "Content-Type": "application/json" - } + "status": 200, + "body": 200, + "headers": { + "Content-Type": "application/json" + } } } ''' + and: + stubMappingIsValidWiremockStub(wiremockStub) and: GroovyDsl expectedGroovyDsl = GroovyDsl.make { request { @@ -167,32 +174,34 @@ class WiremockToDslConverterSpec extends Specification { then: new GroovyShell(this.class.classLoader).evaluate( """ io.codearte.accurest.dsl.GroovyDsl.make { - $groovyDsl - }""") == expectedGroovyDsl + $groovyDsl + }""") == 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 = '''\ { "request": { - "method": "POST", - "url": "/charge/count", - "headers": { - "Content-Type": { - "equalTo": "application/vnd.creditcard-reporter.v1+json" - } - } + "method": "POST", + "url": "/charge/count", + "headers": { + "Content-Type": { + "equalTo": "application/vnd.creditcard-reporter.v1+json" + } + } }, "response": { - "status": 200, - "body": "[ {\\"a\\":1, \\"c\\":\\"3\\"}, \\"b\\", \\"a\\" ]", - "headers": { - "Content-Type": "application/json" - } + "status": 200, + "body": "[ {\\"a\\":1, \\"c\\":\\"3\\"}, \\"b\\", \\"a\\" ]", + "headers": { + "Content-Type": "application/json" + } } } ''' + and: + stubMappingIsValidWiremockStub(wiremockStub) and: GroovyDsl expectedGroovyDsl = GroovyDsl.make { request { @@ -219,30 +228,32 @@ class WiremockToDslConverterSpec extends Specification { then: new GroovyShell(this.class.classLoader).evaluate( """ io.codearte.accurest.dsl.GroovyDsl.make { - $groovyDsl - }""") == expectedGroovyDsl + $groovyDsl + }""") == expectedGroovyDsl } - 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 = '''\ { "request": { - "method": "POST", - "url": "/charge/search?pageNumber=0&size=2147483647", - "headers": { - "Content-Type": { - "equalTo": "application/vnd.creditcard-reporter.v1+json" - } - } + "method": "POST", + "url": "/charge/search?pageNumber=0&size=2147483647", + "headers": { + "Content-Type": { + "equalTo": "application/vnd.creditcard-reporter.v1+json" + } + } }, "response": { - "status": 200, - "body":"[{\\"amount\\":1.01,\\"name\\":\\"Name\\",\\"info\\":{\\"title\\":\\"title1\\",\\"payload\\":null},\\"booleanvalue\\":true,\\"user\\":null},{\\"amount\\":2.01,\\"name\\":\\"Name2\\",\\"info\\":{\\"title\\":\\"title2\\",\\"payload\\":null},\\"booleanvalue\\":true,\\"user\\":null}]" - } + "status": 200, + "body":"[{\\"amount\\":1.01,\\"name\\":\\"Name\\",\\"info\\":{\\"title\\":\\"title1\\",\\"payload\\":null},\\"booleanvalue\\":true,\\"user\\":null},{\\"amount\\":2.01,\\"name\\":\\"Name2\\",\\"info\\":{\\"title\\":\\"title2\\",\\"payload\\":null},\\"booleanvalue\\":true,\\"user\\":null}]" + } } ''' + and: + stubMappingIsValidWiremockStub(wiremockStub) and: GroovyDsl expectedGroovyDsl = GroovyDsl.make { request { @@ -255,26 +266,26 @@ class WiremockToDslConverterSpec extends Specification { response { status 200 body("""[ - { - "amount": 1.01, - "name": "Name", - "info": { - "title": "title1", - "payload": null - }, - "booleanvalue": true, - "user": null - }, - { - "amount": 2.01, - "name": "Name2", - "info": { - "title": "title2", - "payload": null - }, - "booleanvalue": true, - "user": null - } + { + "amount": 1.01, + "name": "Name", + "info": { + "title": "title1", + "payload": null + }, + "booleanvalue": true, + "user": null + }, + { + "amount": 2.01, + "name": "Name2", + "info": { + "title": "title2", + "payload": null + }, + "booleanvalue": true, + "user": null + } ]""") } } @@ -283,8 +294,213 @@ class WiremockToDslConverterSpec extends Specification { then: new GroovyShell(this.class.classLoader).evaluate( """ io.codearte.accurest.dsl.GroovyDsl.make { - $groovyDsl - }""") == expectedGroovyDsl + $groovyDsl + }""") == 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: + stubMappingIsValidWiremockStub(wiremockStub) + 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: + stubMappingIsValidWiremockStub(wiremockStub) + 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 + } + + def 'should convert Wiremock stub with request body with equalToJson'() { + given: + String wiremockStub = '''\ +{ + "request" : { + "url" : "/test", + "method" : "POST", + "bodyPatterns" : [ { + "equalToJson" : "{\\"pan\\":\\"4855141150107894\\",\\"expirationDate\\":\\"2017-01\\",\\"dcvx\\":\\"178\\"}", + "jsonCompareMode" : "LENIENT" + } ] + }, + "response" : { + "status" : 200 + } +} +''' + and: + stubMappingIsValidWiremockStub(wiremockStub) + and: + GroovyDsl expectedGroovyDsl = GroovyDsl.make { + request { + method 'POST' + url '/test' + body '''{"pan":"4855141150107894","expirationDate":"2017-01","dcvx":"178"}''' + } + 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 with equalTo'() { + given: + String wiremockStub = '''\ + { + "request" : { + "url" : "/test", + "method" : "POST", + "bodyPatterns" : [ { + "equalTo" : "{\\"pan\\":\\"4855141150107894\\",\\"expirationDate\\":\\"2017-01\\",\\"dcvx\\":\\"178\\"}" + } ] + }, + "response" : { + "status" : 200 + } + } + ''' + and: + stubMappingIsValidWiremockStub(wiremockStub) + and: + GroovyDsl expectedGroovyDsl = GroovyDsl.make { + request { + method 'POST' + url '/test' + body '''{"pan":"4855141150107894","expirationDate":"2017-01","dcvx":"178"}''' + } + 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 with matches'() { + given: + String wiremockStub = '''\ + { + "request" : { + "url" : "/test", + "method" : "POST", + "bodyPatterns" : [ { + "matches" : "[0-9]{2}" + } ] + }, + "response" : { + "status" : 200 + } + } + ''' + and: + stubMappingIsValidWiremockStub(wiremockStub) + and: + GroovyDsl expectedGroovyDsl = GroovyDsl.make { + request { + method 'POST' + url '/test' + body $(client(~/[0-9]{2}/), 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 + } + + void stubMappingIsValidWiremockStub(String mappingDefinition) { + StubMapping.buildFrom(mappingDefinition) } } 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) { diff --git a/build.gradle b/build.gradle index c2d42cfdf1..28d6b6d0a2 100644 --- a/build.gradle +++ b/build.gradle @@ -98,6 +98,7 @@ project(':accurest-converters') { compile project(':accurest-core') compile 'org.apache.commons:commons-lang3:3.3.2' compile 'commons-io:commons-io:[2.4,)' + testCompile 'com.github.tomakehurst:wiremock:1.53' } }