Merge pull request #80 from Codearte/issue/79-response-body-with-lists-exception
[#79] Fixed the bug with exception with a json and map
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user