From b5e1799468d40bf65b43f028726a7ab9990f0dbf Mon Sep 17 00:00:00 2001 From: "Jozef.Najman" Date: Thu, 11 Jun 2015 15:27:20 +0200 Subject: [PATCH] Fixed generating stubs from regex - one matcher for one regex --- .../dsl/WiremockRequestStubStrategy.groovy | 28 ++--- .../accurest/dsl/WiremockGroovyDslSpec.groovy | 113 ++++++++++++++---- .../codearte/accurest/dsl/WiremockSpec.groovy | 2 +- 3 files changed, 103 insertions(+), 40 deletions(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy index 31eb05300a..3c726f66d2 100755 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WiremockRequestStubStrategy.groovy @@ -80,26 +80,24 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { return [:] } if (clientRequest.body?.containsPattern) { - return [bodyPatterns: [[matches: escapeBodyForJson(body)]]] + return [bodyPatterns: parseMatchesBody(body)] } return [bodyPatterns: [[(getMatchType()): parseBody(body)]]] } - 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() }) + private Object parseMatchesBody(def responseBodyObject) { + def regexList = new ArrayList<>() + responseBodyObject.each { k, v -> + if (v instanceof List) { + v.each { + regexList.addAll(parseMatchesBody((Map)it)) } - )) + } else { + String regex = ".*${k}\":.?\"${v}\".*" + regexList.add([matches: regex]); + } + } + return regexList } private String getMatchType() { 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 4674441a09..452cc758ed 100755 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy @@ -161,7 +161,7 @@ class WiremockGroovyDslSpec extends WiremockSpec { } body """ { - "name": "Jan" + "name": "${value(client('Jan'), server('Honza'))}" } """ } @@ -270,22 +270,20 @@ 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: @@ -336,9 +334,8 @@ class WiremockGroovyDslSpec extends WiremockSpec { }, "url": "/fraudcheck", "bodyPatterns": [ - { - "matches": "\\\\{\\"clientPesel\\":\\"[0-9]{10}\\",\\"loanAmount\\":123.123\\\\}" - } + {"matches": ".*clientPesel\\":.?\\"[0-9]{10}\\".*"}, + {"matches": ".*loanAmount\\":.?\\"123.123\\".*"} ] }, "response": { @@ -658,10 +655,78 @@ 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) { diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockSpec.groovy index 796e603c11..ab1133c9ae 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockSpec.groovy @@ -5,7 +5,7 @@ import spock.lang.Specification import java.util.regex.Pattern -class WiremockSpec extends Specification { +abstract class WiremockSpec extends Specification { void stubMappingIsValidWiremockStub(String mappingDefinition) { StubMapping stubMapping = StubMapping.buildFrom(mappingDefinition)