Support inner map and list

This commit is contained in:
Denis Stepanov
2015-10-08 10:36:12 +02:00
parent f5da5c4278
commit cf8ee61eb3
3 changed files with 92 additions and 1 deletions

View File

@@ -7,11 +7,22 @@ import java.util.regex.Pattern
@CompileStatic
class RegexPatterns {
private static final Pattern TRUE_OR_FALSE = Pattern.compile(/(true|false)/)
private static final Pattern ONLY_ALPHA_UNICODE = Pattern.compile(/[\p{L}]*/)
private static final Pattern IP_ADDRESS = Pattern.compile('([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])');
private static final Pattern HOSTNAME_PATTERN = Pattern.compile('((http[s]?|ftp):\\/)\\/?([^:\\/\\s]+)(:[0-9]{1,5})?');
private static final Pattern EMAIL = Pattern.compile('[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}');
private static final Pattern URL = Pattern.compile('((www\\.|(http|https|ftp|news|file)+\\:\\/\\/)[_.a-z0-9-]+\\.[a-z0-9\\/_:@=.+?,##%&~-]*[^.|\\\'|\\# |!|\\(|?|,| |>|<|;|\\)])');
String onlyAlphaUnicode() {
return ONLY_ALPHA_UNICODE.pattern()
}
String anyBoolean() {
return TRUE_OR_FALSE.pattern()
}
String ipAddress() {
return IP_ADDRESS.pattern()
}

View File

@@ -20,13 +20,27 @@ class MapConverter {
return convert(json, closure)
}
} catch (Exception ignore) {
return closure(value)
}
return extractValue(value, closure);
} else if (value instanceof Map) {
return convert(value as Map, closure)
} else if (value instanceof List) {
return value.collect({ transformValues(it, closure) })
}
return transformValue(closure, value)
}
protected static Object transformValue(Closure closure, Object value) {
return extractValue(value, { Object val->
Object newValue = closure(val)
if (newValue instanceof Map || newValue instanceof List || newValue instanceof String && value) {
return transformValues(newValue, closure)
}
return newValue;
})
}
private static extractValue(Object value, Closure closure) {
try {
return closure(value)
} catch (Exception ignore) {

View File

@@ -7,6 +7,8 @@ import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll
import java.util.regex.Pattern
/**
* @author Jakub Kubrynski
*/
@@ -669,4 +671,68 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
spockTest.contains('''assertThatRejectionReasonIsNull(parsedJson.read('$.rejectionReason'))''')
}
def "should support inner map and list definitions"() {
given:
Pattern PHONE_NUMBER = Pattern.compile(/[+\w]*/)
Pattern ANYSTRING = Pattern.compile(/.*/)
Pattern NUMBERS = Pattern.compile(/[\d\.]*/)
Pattern DATETIME = ANYSTRING
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "PUT"
url "/v1/payments/e86df6f693de4b35ae648464c5b0dc09/client_data"
headers {
header('Content-Type': 'application/json')
}
body(
client: [
first_name: $(stub(regex(onlyAlphaUnicode())), test('Denis')),
last_name: $(stub(regex(onlyAlphaUnicode())), test('FakeName')),
email: $(stub(regex(email())), test('fakemail@fakegmail.com')),
fax: $(stub(PHONE_NUMBER), test('+xx001213214')),
phone: $(stub(PHONE_NUMBER), test('2223311')),
data_of_birth: $(stub(DATETIME), test('2002-10-22T00:00:00Z'))
],
client_id_card: [
id: $(stub(ANYSTRING), test('ABC12345')),
date_of_issue: $(stub(ANYSTRING), test('2002-10-02T00:00:00Z')),
address: [
street: $(stub(ANYSTRING), test('Light Street')),
city: $(stub(ANYSTRING), test('Fire')),
region: $(stub(ANYSTRING), test('Skys')),
country: $(stub(ANYSTRING), test('HG')),
zip: $(stub(NUMBERS), test('658965'))
]
],
incomes_and_expenses: [
monthly_income: $(stub(NUMBERS), test('0.0')),
monthly_loan_repayments: $(stub(NUMBERS), test('100')),
monthly_living_expenses: $(stub(NUMBERS), test('22'))
],
additional_info: [
allow_to_contact: $(stub(optional(regex(anyBoolean()))), test('true'))
]
)
}
response {
status 200
headers {
header('Content-Type': 'application/json')
}
}
}
MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
def spockTest = blockBuilder.toString()
then:
spockTest.contains '"street":"Light Street"'
!spockTest.contains("clientValue")
!spockTest.contains("cursor")
}
}