[#10] Added conversion from Wiremock Stub to Groovy DSL
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import groovy.io.FileType
|
||||
import groovy.json.JsonOutput
|
||||
import groovy.json.JsonSlurper
|
||||
import groovy.xml.XmlUtil
|
||||
|
||||
import static org.apache.commons.lang3.StringEscapeUtils.escapeJava
|
||||
|
||||
class WiremockToDslConverter {
|
||||
static String fromWiremockStub(String wiremockStringStub) {
|
||||
@@ -14,16 +17,16 @@ class WiremockToDslConverter {
|
||||
def response = wiremockStub.response
|
||||
return """\
|
||||
request {
|
||||
${request.method ? "method '''$request.method'''" : ""}
|
||||
${request.url ? "url '''$request.url'''" : ""}
|
||||
${request.urlPattern ? "urlPattern '''$request.urlPattern'''" : ""}
|
||||
${request.urlPath ? "urlPath '''$request.urlPath'''" : ""}
|
||||
${request.method ? "method \"\"\"$request.method\"\"\"" : ""}
|
||||
${request.url ? "url \"\"\"$request.url\"\"\"" : ""}
|
||||
${request.urlPattern ? "urlPattern \"\"\"${escapeJava(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"""
|
||||
"""header(\"\"\"$headerName\"\"\").$entry.key(\"\"\"${escapeJava(entry.value)}\"\"\")\n"""
|
||||
}.join('')
|
||||
}
|
||||
}
|
||||
@@ -54,12 +57,22 @@ class WiremockToDslConverter {
|
||||
|
||||
private Object buildBody(String responseBody) {
|
||||
try {
|
||||
return buildBody(new JsonSlurper().parseText(responseBody))
|
||||
} catch (Exception e) {
|
||||
return """'''$responseBody'''"""
|
||||
def json = new JsonSlurper().parseText(responseBody)
|
||||
return wrapWithMultilineGString(JsonOutput.prettyPrint(responseBody))
|
||||
} catch (Exception jsonException) {
|
||||
try {
|
||||
def xml = new XmlSlurper().parseText(responseBody)
|
||||
return wrapWithMultilineGString(XmlUtil.serialize(responseBody))
|
||||
} catch (Exception xmlException) {
|
||||
return wrapWithMultilineGString(responseBody)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String wrapWithMultilineGString(String string) {
|
||||
return """\"\"\"$string\"\"\""""
|
||||
}
|
||||
|
||||
private Closure withQuotedMapStringElements() {
|
||||
return {
|
||||
[(it.key): convert(it.value)]
|
||||
@@ -90,7 +103,7 @@ class WiremockToDslConverter {
|
||||
if (element =~ /^".*"$/) {
|
||||
return element
|
||||
}
|
||||
return """'''$element'''"""
|
||||
return """\"\"\"${escapeJava(element)}\"\"\""""
|
||||
}
|
||||
|
||||
private Object convert(List element) {
|
||||
|
||||
@@ -90,7 +90,7 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"body": "{\"status\": \"OK\"}",
|
||||
"body": "{\\"status\\": \\"OK\\"}",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
@@ -108,9 +108,9 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
body (
|
||||
status : "OK"
|
||||
)
|
||||
body ("""{
|
||||
"status": "OK"
|
||||
}""")
|
||||
headers {
|
||||
header 'Content-Type': 'application/json'
|
||||
|
||||
@@ -215,7 +215,7 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
body ([
|
||||
[a: 1, c: '3'],
|
||||
'b',
|
||||
'c'
|
||||
'a'
|
||||
])
|
||||
headers {
|
||||
header 'Content-Type': 'application/json'
|
||||
@@ -263,20 +263,28 @@ 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]
|
||||
])
|
||||
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
|
||||
}
|
||||
]""")
|
||||
}
|
||||
}
|
||||
when:
|
||||
|
||||
@@ -47,6 +47,7 @@ project(':accurest-core') {
|
||||
project(':accurest-converters') {
|
||||
dependencies {
|
||||
compile project(':accurest-core')
|
||||
compile 'org.apache.commons:commons-lang3:3.3.2'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user