From 1cabe971e477963dd8eba1331ab94c2a1d79c458 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 12 Jun 2015 14:57:25 +0200 Subject: [PATCH] [#79] Fixed the way wiremock stubs are built --- .../dsl/BaseWiremockStubStrategy.groovy | 10 +++- .../accurest/dsl/WiremockGroovyDslSpec.groovy | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/BaseWiremockStubStrategy.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/BaseWiremockStubStrategy.groovy index 77866f1437..32bc831f1b 100755 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/BaseWiremockStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/BaseWiremockStubStrategy.groovy @@ -3,8 +3,10 @@ import groovy.json.JsonOutput import groovy.json.JsonSlurper import groovy.transform.TypeChecked import groovy.xml.XmlUtil +import io.codearte.accurest.dsl.internal.DslProperty import io.codearte.accurest.dsl.internal.Header import io.codearte.accurest.dsl.internal.Headers +import io.codearte.accurest.util.JsonConverter import java.util.regex.Pattern @@ -12,6 +14,11 @@ import static groovy.json.StringEscapeUtils.escapeJava @TypeChecked abstract class BaseWiremockStubStrategy { + + private static Closure transform = { + it instanceof DslProperty ? JsonConverter.transformValues(it.clientValue, transform) : it + } + protected Map buildClientRequestHeadersSection(Headers headers) { if (!headers) { return null @@ -62,6 +69,7 @@ abstract class BaseWiremockStubStrategy { } protected String parseBody(Map body) { - return JsonOutput.toJson(body) + def transformedMap = JsonConverter.transformValues(body, transform) + return JsonOutput.toJson(transformedMap) } } 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 7d8e5f1d49..c8767551b4 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 @@ -2,6 +2,7 @@ package io.codearte.accurest.dsl import groovy.json.JsonBuilder import groovy.json.JsonSlurper +import spock.lang.Issue class WiremockGroovyDslSpec extends WiremockSpec { @@ -53,6 +54,53 @@ class WiremockGroovyDslSpec extends WiremockSpec { stubMappingIsValidWiremockStub(wiremockStub) } + @Issue("#79") + def 'should convert groovy dsl stub to wiremock stub for the client side with a body containing a map'() { + given: + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method 'GET' + url '/ingredients' + headers { + header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json' + } + } + response { + status 200 + body( + ingredients: [ + [type: 'MALT', quantity: 100], + [type: 'WATER', quantity: 200], + [type: 'HOP', quantity: 300], + [type: 'YIEST', quantity: 400] + ] + ) + } + } + when: + String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' +{ + "request": { + "method": "GET", + "headers": { + "Content-Type": { + "equalTo": "application/vnd.pl.devoxx.aggregatr.v1+json" + } + }, + "url": "/ingredients" + }, + "response": { + "status": 200, + "body": "{\\"ingredients\\":[{\\"type\\":\\"MALT\\",\\"quantity\\":100},{\\"type\\":\\"WATER\\",\\"quantity\\":200},{\\"type\\":\\"HOP\\",\\"quantity\\":300},{\\"type\\":\\"YIEST\\",\\"quantity\\":400}]}" + } +} +''') + and: + stubMappingIsValidWiremockStub(wiremockStub) + } + def 'should convert groovy dsl stub with Body as String to wiremock stub for the client side'() { given: GroovyDsl groovyDsl = GroovyDsl.make {