[#5] Added possibility of providing a GString as Body with Client / Server values

This commit is contained in:
Marcin Grzejszczak
2015-02-16 10:24:06 +01:00
parent e0f72ab6ab
commit ca4cceaa5b
2 changed files with 114 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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 {