Merge pull request #95 from dstepanov/absentMatching

Added "absent" matching strategy for query parameters
This commit is contained in:
Marcin Grzejszczak
2015-06-25 13:13:22 +02:00
8 changed files with 108 additions and 3 deletions

View File

@@ -120,12 +120,26 @@ class SpockMethodBodyBuilder {
}
private String buildUrlFromUrlPath(UrlPath urlPath) {
String params = urlPath.queryParameters.parameters.inject([]) { result, param ->
String params = urlPath.queryParameters.parameters
.findAll(this.&allowedQueryParameter)
.inject([]) { result, param ->
result << "${param.name}=${resolveParamValue(param).toString()}"
}.join('&')
return "$urlPath.serverValue?$params"
}
private boolean allowedQueryParameter(QueryParameter param) {
return allowedQueryParameter(param.serverValue)
}
private boolean allowedQueryParameter(MatchingStrategy matchingStrategy) {
return matchingStrategy.type != MatchingStrategy.Type.ABSENT
}
private boolean allowedQueryParameter(Object o) {
return true
}
private String resolveParamValue(QueryParameter param) {
resolveParamValue(param.serverValue)
}

View File

@@ -53,6 +53,10 @@ abstract class BaseWiremockStubStrategy {
return parseBody(value.toString(), contentType)
}
public Boolean parseBody(Boolean value, ContentType contentType) {
return value
}
public String parseBody(Map map, ContentType contentType) {
def transformedMap = transformValues(map, transform)
return parseBody(toJson(transformedMap), contentType)

View File

@@ -88,6 +88,28 @@ class Common {
assert firstSide ==~ secondSide
}
void assertThatSidesMatch(MatchingStrategy firstSide, MatchingStrategy secondSide) {
if (firstSide.type == MatchingStrategy.Type.ABSENT && secondSide != MatchingStrategy.Type.ABSENT) {
throwAbsentError()
}
}
void assertThatSidesMatch(MatchingStrategy firstSide, Object secondSide) {
if (firstSide.type == MatchingStrategy.Type.ABSENT) {
throwAbsentError()
}
}
void assertThatSidesMatch(Object firstSide, MatchingStrategy secondSide) {
if (secondSide.type == MatchingStrategy.Type.ABSENT) {
throwAbsentError()
}
}
private void throwAbsentError() {
throw new IllegalStateException("Absent cannot only be used only on one side")
}
void assertThatSidesMatch(Object firstSide, Object secondSide) {
// do nothing
}

View File

@@ -35,7 +35,7 @@ class MatchingStrategy extends DslProperty {
enum Type {
EQUAL_TO("equalTo"), CONTAINS("contains"), MATCHING("matches"), NOT_MATCHING("doesNotMatch"),
EQUAL_TO_JSON("equalToJson"), EQUAL_TO_XML("equalToXml")
EQUAL_TO_JSON("equalToJson"), EQUAL_TO_XML("equalToXml"), ABSENT("absent")
final String name

View File

@@ -125,6 +125,10 @@ class Request extends Common {
return new MatchingStrategy(value, MatchingStrategy.Type.EQUAL_TO_JSON)
}
MatchingStrategy absent() {
return new MatchingStrategy(true, MatchingStrategy.Type.ABSENT)
}
}
@CompileStatic

View File

@@ -6,6 +6,9 @@ import io.codearte.accurest.dsl.internal.MatchingStrategy
import java.util.regex.Pattern
import static io.codearte.accurest.dsl.internal.MatchingStrategy.Type.ABSENT
import static io.codearte.accurest.dsl.internal.MatchingStrategy.Type.EQUAL_TO
@TypeChecked
class ValidateUtils {
@@ -23,8 +26,10 @@ class ValidateUtils {
throw new IllegalStateException("$msg can't be a pattern for the server side")
}
static List ALLOWED_MATCHING_TYPES_ON_SERVER_SIDE = [EQUAL_TO, ABSENT]
static void validateServerValue(MatchingStrategy matchingStrategy, String msg) {
if (matchingStrategy.type != MatchingStrategy.Type.EQUAL_TO) {
if (!ALLOWED_MATCHING_TYPES_ON_SERVER_SIDE.contains(matchingStrategy.type)) {
throw new IllegalStateException("$msg can't be of a matching type: $matchingStrategy.type for the server side")
}
validateServerValue(matchingStrategy.serverValue, msg)

View File

@@ -260,6 +260,8 @@ class SpockMethodBuilderSpec extends Specification {
parameter 'age': $(client(notMatching("^\\w*\$")), server("99"))
parameter 'name': $(client(matching("Denis.*")), server("Denis.Stepanov"))
parameter 'email': "bob@email.com"
parameter 'hello': $(client(matching("Denis.*")), server(absent()))
parameter 'hello': absent()
}
}
}

View File

@@ -694,6 +694,7 @@ class WiremockGroovyDslSpec extends WiremockSpec {
parameter 'search': $(client(notMatching(~/^\/[0-9]{2}$/)), server("10"))
parameter 'age': $(client(notMatching("^\\w*\$")), server(10))
parameter 'name': $(client(matching("Denis.*")), server("Denis"))
parameter 'credit': absent()
}
}
}
@@ -730,6 +731,9 @@ class WiremockGroovyDslSpec extends WiremockSpec {
},
"name": {
"matches": "Denis.*"
},
"credit": {
"absent": true
}
}
},
@@ -862,6 +866,56 @@ class WiremockGroovyDslSpec extends WiremockSpec {
e.message.contains "Query parameter 'age' can't be of a matching type: NOT_MATCHING for the server side"
}
def "should not allow query parameter with a different absent variation for server/client"() {
when:
GroovyDsl.make dsl
then:
def e = thrown(IllegalStateException)
e.message.contains "Absent cannot only be used only on one side"
where:
dsl << [
{
request {
method 'GET'
urlPath("users") {
queryParameters {
parameter 'name': $(client(absent()), server(""))
}
}
}
response {
status 200
}
},
{
request {
method 'GET'
urlPath("users") {
queryParameters {
parameter 'name': $(client(""), server(absent()))
}
}
}
response {
status 200
}
},
{
request {
method 'GET'
urlPath("users") {
queryParameters {
parameter 'name': $(client(absent()), server(matching("abc")))
}
}
}
response {
status 200
}
}
]
}
def "should generate request with url and queryParameters for client side"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {