[#42] Fixed wiremock stubs generation

This commit is contained in:
Marcin Grzejszczak
2015-10-04 22:37:49 +02:00
parent 9550a9ff08
commit bb73cdd4b0
3 changed files with 127 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package io.codearte.accurest.util
import groovy.json.JsonSlurper
import io.codearte.accurest.dsl.internal.DslProperty
import io.codearte.accurest.dsl.internal.Optional
/**
* @author Marcin Grzejszczak
@@ -40,6 +41,8 @@ class MapConverter {
return map.collectEntries {
key, value ->
[key, transformValues(value, closure)]
}.findAll {
!(it.value instanceof Optional)
}
}

View File

@@ -552,6 +552,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
spockTest.contains('''$[?(@.message =~ /User not found by email = \\\\[[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,4}\\\\]/)]''')
}
@Issue('42')
def "should omit an optional field from body resolution"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
@@ -591,6 +592,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
!spockTest.contains('''Optional''')
}
@Issue('42')
def "should omit an optional field from body resolution with GString"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {

View File

@@ -1293,6 +1293,128 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
stubMappingIsValidWireMockStub(wireMockStub)
}
@Issue('42')
def 'should generate stub without optional parameters with GString'() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
priority 1
request {
method 'POST'
url '/users/password'
headers {
header 'Content-Type': 'application/json'
}
body(
""" {
"email" : "${value(optional())}",
"callback_url" : "${value(client(regex(hostname())), server('http://partners.com'))}"
}
"""
)
}
response {
status 404
headers {
header 'Content-Type': 'application/json'
}
body(
""" {
"code" : "${value(optional())}",
"message" : "User not found by email = [${value(server(regex(email())), client('not.existing@user.com'))}]"
}
"""
)
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
"request" : {
"url" : "/users/password",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$[?(@.callback_url =~ /((http[s]?|ftp):\\\\/)\\\\/?([^:\\\\/\\\\s]+)(:[0-9]{1,5})?/)]"
} ],
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
}
}
},
"response" : {
"status" : 404,
"body" : "{\\"message\\":\\"User not found by email = [not.existing@user.com]\\"}",
"headers" : {
"Content-Type" : "application/json"
}
},
"priority" : 1
}
'''), wireMockStub)
and:
stubMappingIsValidWireMockStub(wireMockStub)
}
@Issue('42')
def 'should generate stub without optional parameters'() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
priority 1
request {
method 'POST'
url '/users/password'
headers {
header 'Content-Type': 'application/json'
}
body(
email: optional(),
callback_url: $(client(regex(hostname())), server('http://partners.com'))
)
}
response {
status 404
headers {
header 'Content-Type': 'application/json'
}
body(
code: optional(),
message: "User not found by email = [${value(server(regex(email())), client('not.existing@user.com'))}]"
)
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
"request" : {
"url" : "/users/password",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$[?(@.callback_url =~ /((http[s]?|ftp):\\\\/)\\\\/?([^:\\\\/\\\\s]+)(:[0-9]{1,5})?/)]"
} ],
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
}
}
},
"response" : {
"status" : 404,
"body" : "{\\"message\\":\\"User not found by email = [not.existing@user.com]\\"}",
"headers" : {
"Content-Type" : "application/json"
}
},
"priority" : 1
}
'''), wireMockStub)
and:
stubMappingIsValidWireMockStub(wireMockStub)
}
String toJsonString(value) {
new JsonBuilder(value).toPrettyString()
}