diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy index 1a3b331bb3..502e18a2ca 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy @@ -1,7 +1,9 @@ package io.coderate.accurest.dsl.internal +import groovy.json.JsonSlurper import groovy.transform.EqualsAndHashCode import groovy.transform.ToString +import org.codehaus.groovy.runtime.GStringImpl @ToString(includePackage = false, includeFields = true) @EqualsAndHashCode(includeFields = true) @@ -27,6 +29,22 @@ class Body { this.bodyAsValue = new DslProperty(bodyAsValue) } + Body(GString bodyAsValue) { + this.bodyAsValue = new DslProperty(getClientValue(bodyAsValue), getServerValue(bodyAsValue)) + } + + private Map getClientValue(GString bodyAsValue) { + GString clientGString = new GStringImpl(bodyAsValue.values.clone(), bodyAsValue.strings.clone()) + Object[] clientValues = bodyAsValue.values.collect { it instanceof DslProperty ? it.clientValue : it } as Object[] + return new JsonSlurper().parseText(new GStringImpl(clientValues, clientGString.strings).toString()) + } + + private Map getServerValue(GString bodyAsValue) { + GString clientGString = new GStringImpl(bodyAsValue.values.clone(), bodyAsValue.strings.clone()) + Object[] serverValues = bodyAsValue.values.collect { it instanceof DslProperty ? it.serverValue: it } as Object[] + return new JsonSlurper().parseText(new GStringImpl(serverValues, clientGString.strings).toString()) + } + Body(DslProperty bodyAsValue) { this.bodyAsValue = bodyAsValue } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy index 3ee9cd7f11..9740c6f40e 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy @@ -60,6 +60,102 @@ class WiremockGroovyDslSpec extends Specification { ''') } + def 'should convert groovy dsl stub with Body as String to wiremock stub for the client side'() { + given: + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method('GET') + urlPattern $(client('/[0-9]{2}'), server('/12')) + } + response { + status 200 + body ("""\ + { + "id": "${value(client('123'),server('321'))}", + "surname": "${value(client('Kowalsky'),server('Lewandowski'))}", + "name": "Jan", + "created" : "${$(client('2014-02-02 12:23:43'), server('2999-09-09 01:23:45'))}" + } + """ + ) + headers { + header 'Content-Type': 'text/plain' + } + } + } + when: + String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' +{ + "request": { + "method": "GET", + "urlPattern": "/[0-9]{2}" + }, + "response": { + "status": 200, + "body": { + "id": "123", + "surname": "Kowalsky", + "name": "Jan", + "created" : "2014-02-02 12:23:43" + }, + "headers": { + "Content-Type": "text/plain" + } + } +} +''') + } + + def 'should convert groovy dsl stub with Body as String to wiremock stub for the server side'() { + given: + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method('GET') + urlPattern $(client('/[0-9]{2}'), server('/12')) + } + response { + status 200 + body ("""\ + { + "id": "${value(client('123'),server('321'))}", + "surname": "${value(client('Kowalsky'),server('Lewandowski'))}", + "name": "Jan", + "created" : "${$(client('2014-02-02 12:23:43'), server('2999-09-09 01:23:45'))}" + } + """ + ) + headers { + header 'Content-Type': 'text/plain' + } + } + } + when: + String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockServerStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' +{ + "request": { + "method": "GET", + "urlPattern": "/12" + }, + "response": { + "status": 200, + "body": { + "id": "321", + "surname": "Lewandowski", + "name": "Jan", + "created" : "2999-09-09 01:23:45" + }, + "headers": { + "Content-Type": "text/plain" + } + } +} +''') + } + def 'should convert groovy dsl stub to wiremock stub for the server side'() { given: GroovyDsl groovyDsl = GroovyDsl.make {