Merge pull request #188 from mchmielarz/master

Fixe for #187
This commit is contained in:
Marcin Grzejszczak
2015-12-24 00:27:07 +01:00
4 changed files with 88 additions and 2 deletions

View File

@@ -142,7 +142,19 @@ class JsonToJsonPathsConverter {
}
protected static String potentiallyWrappedWithQuotesValue(Object value) {
return value instanceof Number ? value : "'$value'"
return isNumber(value) || isBoolean(value) || isNull(value) ? value : "'$value'"
}
private static boolean isNull(value) {
return value == null
}
private static boolean isBoolean(value) {
return value instanceof Boolean
}
private static boolean isNumber(value) {
return value instanceof Number
}
}

View File

@@ -34,6 +34,35 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
}
@Issue("#187")
def "should generate assertions for null and boolean values"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "GET"
url "test"
}
response {
status 200
body """{
"property1": "true",
"property2": null,
"property3": false
}"""
}
}
JaxRsClientSpockMethodBodyBuilder builder = new JaxRsClientSpockMethodBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
then:
blockBuilder.toString().contains("\$[?(@.property1 == 'true')]")
blockBuilder.toString().contains("\$[?(@.property2 == null)]")
blockBuilder.toString().contains("\$[?(@.property3 == false)]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
}
@Issue("#79")
def "should generate assertions for simple response body constructed from map with a list"() {
given:

View File

@@ -40,6 +40,35 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
}
@Issue("#187")
def "should generate assertions for null and boolean values"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "GET"
url "test"
}
response {
status 200
body """{
"property1": "true",
"property2": null,
"property3": false
}"""
}
}
MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
then:
blockBuilder.toString().contains("\$[?(@.property1 == 'true')]")
blockBuilder.toString().contains("\$[?(@.property2 == null)]")
blockBuilder.toString().contains("\$[?(@.property3 == false)]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
}
@Issue("#79")
def "should generate assertions for simple response body constructed from map with a list"() {
given:

View File

@@ -119,6 +119,21 @@ class JsonToJsonPathsConverterSpec extends Specification {
assertThatJsonPathsInMapAreValid(json, pathAndValues)
}
def 'should convert a json with null and boolean values'() {
given:
String json = '''
{
"property1" : null,
"property2" : true
}
'''
when:
JsonPaths pathAndValues = JsonToJsonPathsConverter.transformToJsonPathWithTestsSideValues(new JsonSlurper().parseText(json))
then:
pathAndValues['''$[?(@.property1 == null)]'''] == null
pathAndValues['''$[?(@.property2 == true)]'''] == true
}
def "should convert numbers map"() {
given:
String json = ''' {
@@ -207,7 +222,8 @@ class JsonToJsonPathsConverterSpec extends Specification {
private void assertThatJsonPathsInMapAreValid(String json, JsonPaths pathAndValues) {
DocumentContext parsedJson = JsonPath.using(Configuration.builder().options(Option.ALWAYS_RETURN_LIST).build()).parse(json);
pathAndValues.each {
assert parsedJson.read(it.jsonPath, JSONArray).getAt(it.optionalSuffix ?: 0) == it.optionalSuffix ? [it.value] : it.value
def at = parsedJson.read(it.jsonPath, JSONArray).getAt(it.optionalSuffix ?: 0)
assert at == it.optionalSuffix ? [it.value] : it.value
}
}