From 72b00f6ac86b508709d45612cc3fb32e46ce3996 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 12 Jun 2015 13:40:54 +0200 Subject: [PATCH 1/2] [#79] Fixed the bug with exception with a json and map --- .../builder/SpockMethodBodyBuilder.groovy | 24 ++++++++------- .../builder/SpockMethodBuilderSpec.groovy | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy index 41734b0a3a..3d44123212 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy @@ -3,6 +3,7 @@ package io.codearte.accurest.builder import groovy.json.JsonOutput import groovy.transform.PackageScope import io.codearte.accurest.dsl.GroovyDsl +import io.codearte.accurest.dsl.internal.DslProperty import io.codearte.accurest.dsl.internal.ExecutionProperty import io.codearte.accurest.dsl.internal.Header import io.codearte.accurest.dsl.internal.MatchingStrategy @@ -12,7 +13,6 @@ import io.codearte.accurest.dsl.internal.Response import io.codearte.accurest.dsl.internal.UrlPath import java.util.regex.Pattern - /** * @author Jakub Kubrynski */ @@ -102,9 +102,7 @@ class SpockMethodBodyBuilder { matchingStrategy.serverValue.toString() } - private void processBodyElement(BlockBuilder blockBuilder, String rootProperty, def element) { - def value = element.value - String property = rootProperty + "." + element.key + private void processBodyElement(BlockBuilder blockBuilder, String property, def value) { if (value instanceof String) { if (value.startsWith('$')) { value = value.substring(1).replaceAll('\\$value', "responseBody$property") @@ -114,10 +112,14 @@ class SpockMethodBodyBuilder { } } else if (value instanceof Map) { processMapElement(value, blockBuilder, property) + }else if (value instanceof Map.Entry) { + processEntryElement(blockBuilder, property, value) } else if (value instanceof List) { processArrayElements(value, property, blockBuilder) } else if (value instanceof Pattern) { blockBuilder.addLine("responseBody$property ==~ java.util.regex.Pattern.compile('${value}')") + } else if (value instanceof DslProperty) { + processBodyElement(blockBuilder, property, value.serverValue) } else if (value instanceof ExecutionProperty) { ExecutionProperty exec = (ExecutionProperty) value blockBuilder.addLine("${exec.insertValue("responseBody$property")}") @@ -127,18 +129,20 @@ class SpockMethodBodyBuilder { } private void processMapElement(def value, BlockBuilder blockBuilder, String property) { - value.each { entry -> processBodyElement(blockBuilder, property, entry) } + value.each { entry -> processEntryElement(blockBuilder, property, entry) } + } + + private def processEntryElement(BlockBuilder blockBuilder, String property, def entry) { + return processBodyElement(blockBuilder, property + "." + entry.key, entry.value) } private void processArrayElements(List responseBody, String property, BlockBuilder blockBuilder) { responseBody.eachWithIndex { listElement, listIndex -> - listElement.each { - entry -> processBodyElement(blockBuilder, property + "[$listIndex]", entry) + listElement.each { entry -> + String prop = "$property[$listIndex]" ?: '' + processBodyElement(blockBuilder, prop, entry) } } } - private void processClosure(Closure value, BlockBuilder blockBuilder, String property) { - blockBuilder.addLine() - } } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy index 0aff49357f..a2d03358a2 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy @@ -1,6 +1,7 @@ package io.codearte.accurest.builder import io.codearte.accurest.dsl.GroovyDsl +import spock.lang.Issue import spock.lang.Specification /** @@ -32,6 +33,35 @@ class SpockMethodBuilderSpec extends Specification { blockBuilder.toString().contains("responseBody.property2 == \"b\"") } + @Issue("#79") + def "should generate assertions for simple response body constructed from map"() { + given: + GroovyDsl contractDsl = GroovyDsl.make { + request { + method "GET" + url "test" + } + response { + status 200 + body ( + property1: 'a', + property2: [ + [a: 'sth'], + [b: 'sthElse'] + ] + ) + } + } + SpockMethodBodyBuilder builder = new SpockMethodBodyBuilder(contractDsl) + BlockBuilder blockBuilder = new BlockBuilder(" ") + when: + builder.appendTo(blockBuilder) + then: + blockBuilder.toString().contains("responseBody.property1 == \"a\"") + blockBuilder.toString().contains("responseBody.property2[0].a == \"sth\"") + blockBuilder.toString().contains("responseBody.property2[1].b == \"sthElse\"") + } + def "should generate assertions for array in response body"() { given: GroovyDsl contractDsl = GroovyDsl.make { From 1cabe971e477963dd8eba1331ab94c2a1d79c458 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 12 Jun 2015 14:57:25 +0200 Subject: [PATCH 2/2] [#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 {