Reverted generating stubs from regex - one matcher for one regex

This commit is contained in:
Jozef.Najman
2015-06-11 17:14:12 +02:00
parent b5e1799468
commit 254a67cdb5
2 changed files with 39 additions and 102 deletions

View File

@@ -80,24 +80,26 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy {
return [:]
}
if (clientRequest.body?.containsPattern) {
return [bodyPatterns: parseMatchesBody(body)]
return [bodyPatterns: [[matches: escapeBodyForJson(body)]]]
}
return [bodyPatterns: [[(getMatchType()): parseBody(body)]]]
}
private Object parseMatchesBody(def responseBodyObject) {
def regexList = new ArrayList<>()
responseBodyObject.each { k, v ->
if (v instanceof List) {
v.each {
regexList.addAll(parseMatchesBody((Map<String, Object>)it))
private Object escapeBodyForJson(Object body) {
return parseBody(convertJsonStructureToObjectUnderstandingStructure(body,
{ it instanceof Pattern },
{ String json -> json.collect {
switch(it) {
case ('{'): return '\\{'
case ('}'): return '\\}'
default: return it
}
} .join('')
},
{ LinkedList list, String json ->
return json.replaceAll(TEMPORARY_PATTERN_HOLDER, { String a, String[] b -> list.pop() })
}
} else {
String regex = ".*${k}\":.?\"${v}\".*"
regexList.add([matches: regex]);
}
}
return regexList
))
}
private String getMatchType() {

View File

@@ -161,7 +161,7 @@ class WiremockGroovyDslSpec extends WiremockSpec {
}
body """
{
"name": "${value(client('Jan'), server('Honza'))}"
"name": "Jan"
}
"""
}
@@ -270,20 +270,22 @@ class WiremockGroovyDslSpec extends WiremockSpec {
then:
new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText('''
{
"request": {
"method": "GET",
"urlPattern": "/[0-9]{2}",
"bodyPatterns": [
{"matches": ".*personalId\\":.?\\"^[0-9]{11}$\\".*"}
]
},
"response": {
"status": 200,
"body": "{\\"name\\":\\"Jan\\"}",
"headers": {
"Content-Type": "text/plain"
}
}
"request": {
"method": "GET",
"urlPattern": "/[0-9]{2}",
"bodyPatterns": [
{
"matches":"\\\\{\\"personalId\\":\\"^[0-9]{11}$\\"\\\\}"
}
]
},
"response": {
"status": 200,
"body": "{\\"name\\":\\"Jan\\"}",
"headers": {
"Content-Type": "text/plain"
}
}
}
''')
and:
@@ -334,8 +336,9 @@ class WiremockGroovyDslSpec extends WiremockSpec {
},
"url": "/fraudcheck",
"bodyPatterns": [
{"matches": ".*clientPesel\\":.?\\"[0-9]{10}\\".*"},
{"matches": ".*loanAmount\\":.?\\"123.123\\".*"}
{
"matches": "\\\\{\\"clientPesel\\":\\"[0-9]{10}\\",\\"loanAmount\\":123.123\\\\}"
}
]
},
"response": {
@@ -655,78 +658,10 @@ class WiremockGroovyDslSpec extends WiremockSpec {
},
"X-Custom-Header": {
"matches": "^.*2134.*$"
}
}
}
''')
}
def 'should convert groovy dsl stub with rich tree Body as String to wiremock stub for the client side'() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
request {
method('GET')
url $(client(~/\/[0-9]{2}/), server('/12'))
body """\
{
"personalId": "${value(client(regex('[0-9]{11}')), server('57593728525'))}",
"firstName": "${value(client(regex('.*')), server('Bruce'))}",
"lastName": "${value(client(regex('.*')), server('Lee'))}",
"birthDate": "${value(client(regex('[0-9]{4}-[0-9]{2}-[0-9]{2}')), server('1985-12-12'))}",
"errors": [
{
"propertyName": "${value(client(regex('[0-9]{2}')), server('04'))}",
"providerValue": "Test"
},
{
"propertyName": "${value(client(regex('[0-9]{2}')), server('08'))}",
"providerValue": "Test"
}
]
}
"""
}
response {
status 200
body("""\
{
"name": "Jan"
}
"""
)
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}",
"bodyPatterns": [
{"matches": ".*birthDate\\":.?\\"[0-9]{4}-[0-9]{2}-[0-9]{2}\\".*"},
{"matches": ".*propertyName\\":.?\\"[0-9]{2}\\".*"},
{"matches": ".*providerValue\\":.?\\"Test\\".*"},
{"matches": ".*propertyName\\":.?\\"[0-9]{2}\\".*"},
{"matches": ".*providerValue\\":.?\\"Test\\".*"},
{"matches": ".*firstName\\":.?\\".*\\".*"},
{"matches": ".*lastName\\":.?\\".*\\".*"},
{"matches": ".*personalId\\":.?\\"[0-9]{11}\\".*"}
]
},
"response": {
"status": 200,
"body": "{\\"name\\":\\"Jan\\"}",
"headers": {
"Content-Type": "text/plain"
}
}
}
''')
}
}
}
''')
}
String toJsonString(value) {