[#10] Working Wiremock To Dsl Converter

This commit is contained in:
Marcin Grzejszczak
2015-02-13 15:18:28 +01:00
parent 6c60e0dc82
commit a6b1cef2e8
5 changed files with 88 additions and 54 deletions

View File

@@ -4,37 +4,64 @@ import groovy.json.JsonSlurper
import io.coderate.accurest.dsl.GroovyDsl
class WiremockToDslConverter {
static GroovyDsl fromWiremockStub(String wiremockStringStub) {
static String fromWiremockStub(String wiremockStringStub) {
return new WiremockToDslConverter().convertFromWiremockStub(wiremockStringStub)
}
private String convertFromWiremockStub(String wiremockStringStub) {
Object wiremockStub = new JsonSlurper().parseText(wiremockStringStub)
def wiremockRequest = wiremockStub.request
def wiremockResponse = wiremockStub.response
return GroovyDsl.make {
def request = wiremockStub.request
def response = wiremockStub.response
return """\
request {
wiremockRequest.method ? method(wiremockRequest.method as String) : null
wiremockRequest.url ? url(wiremockRequest.url as String) : null
wiremockRequest.urlPattern ? urlPattern(wiremockRequest.urlPattern as String) : null
wiremockRequest.urlPath ? urlPath(wiremockRequest.urlPath as String) : null
wiremockRequest.headers ? headers {
wiremockRequest.headers.each {
def assertion = it.value
String headerName = it.key as String
def entry = assertion.entrySet().first()
header(headerName)."$entry.key"(entry.value)
}
} : null
${request.method ? "method '$request.method'" : ""}
${request.url ? "url '$request.url'" : ""}
${request.urlPattern ? "urlPattern '$request.urlPattern'" : ""}
${request.urlPath ? "urlPath '$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').$entry.key('$entry.value')\n"""
}.join('')
}
}
""" : ""}
}
response {
status wiremockResponse.status ? wiremockResponse.status as Integer : null
wiremockResponse.body ? body(
wiremockResponse.body as Map
) : null
wiremockResponse.headers ? headers {
wiremockResponse.headers.each {
header([(it.key): it.value])
${response.status ? "status $response.status" : ""}
${response.body ? "body( ${response.body.entrySet().collectAll(withQuotedStringElements()).inject([:], appendToMap())})" : ""}
${response.headers ? """headers {
${response.headers.collect { "header('$it.key': '${it.value}')\n" }.join('')}
}
} : null
""" : ""}
}
}
"""
}
private Closure withQuotedStringElements() {
return { [(it.key): convert(it.value)] }
}
private Closure appendToMap() {
return { acc, el -> acc << el }
}
private Object convert(Object element) {
return element
}
private Object convert(String element) {
return """ "$element" """
}
private Object convert(List element) {
return element.collect { convert(it) }
}
private Object convert(Map element) {
return element.collectEntries { [(it.key) : convert(it.value)] }
}
static int main(String[] args) {

View File

@@ -7,7 +7,7 @@ class WiremockToDslConverterSpec extends Specification {
def 'should produce a Groovy DSL from Wiremock stub'() {
given:
String wiremockStub = '''
String wiremockStub = '''\
{
"request": {
"method": "GET",
@@ -27,7 +27,9 @@ class WiremockToDslConverterSpec extends Specification {
"response": {
"status": 200,
"body": {
"id": "123",
"id": {
"value": "132"
},
"surname": "Kowalsky",
"name": "Jan",
"created" : "2014-02-02 12:23:43"
@@ -40,32 +42,35 @@ class WiremockToDslConverterSpec extends Specification {
'''
and:
GroovyDsl expectedGroovyDsl = GroovyDsl.make {
request {
method 'GET'
urlPattern '/[0-9]{2}'
headers {
header('Accept').matches('text/.*')
header('etag').doesNotMatch('abcd.*')
header('X-Custom-Header').contains('2134')
}
}
response {
status 200
body (
id : '123',
surname : 'Kowalsky',
name: 'Jan',
created : '2014-02-02 12:23:43'
)
headers {
header 'Content-Type': 'text/plain'
request {
method 'GET'
urlPattern '/[0-9]{2}'
headers {
header('Accept').matches('text/.*')
header('etag').doesNotMatch('abcd.*')
header('X-Custom-Header').contains('2134')
}
}
response {
status 200
body (
id : [value: '132'],
surname : 'Kowalsky',
name: 'Jan',
created : '2014-02-02 12:23:43'
)
headers {
header 'Content-Type': 'text/plain'
}
}
}
}
}
}
when:
GroovyDsl groovyDsl = WiremockToDslConverter.fromWiremockStub(wiremockStub)
String groovyDsl = WiremockToDslConverter.fromWiremockStub(wiremockStub)
then:
groovyDsl == expectedGroovyDsl
new GroovyShell(this.class.classLoader).evaluate(
""" io.coderate.accurest.dsl.GroovyDsl.make {
$groovyDsl
}""") == expectedGroovyDsl
}
}

View File

@@ -12,7 +12,7 @@ abstract class BaseWiremockStubStrategy {
}
return withAssertionHeaders(headers) {
Map.Entry<String, WithValuePattern> entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)]
} << headers?.valueHeaders()
} << headers.valueHeaders()
}
protected Map buildServerHeadersSection(Headers headers) {

View File

@@ -8,7 +8,7 @@ import io.coderate.accurest.dsl.internal.Response
@TypeChecked
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false)
@ToString(includeFields = true, includePackage = false)
class GroovyDsl {
Request request
@@ -32,4 +32,5 @@ class GroovyDsl {
closure.delegate = response
closure()
}
}

View File

@@ -4,7 +4,7 @@ import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false)
@ToString(includePackage = false, includeFields = true, ignoreNulls = true)
class Headers {
private Map<String, WithValuePattern> assertionHeaders = [:]
@@ -28,4 +28,5 @@ class Headers {
Set<Map.Entry<String, WithValuePattern>> assertionEntries() {
return Collections.unmodifiableSet(assertionHeaders.entrySet())
}
}