From cf8ee61eb3a41499e344a7ea3e067c0538969073 Mon Sep 17 00:00:00 2001 From: Denis Stepanov Date: Thu, 8 Oct 2015 10:36:12 +0200 Subject: [PATCH] Support inner map and list --- .../dsl/internal/RegexPatterns.groovy | 11 ++++ .../accurest/util/MapConverter.groovy | 16 ++++- .../MockMvcSpockMethodBuilderSpec.groovy | 66 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy index 432dee266d..0f530bbf8f 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy @@ -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() } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/MapConverter.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/util/MapConverter.groovy index 22896d8308..7087f9c1d7 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/util/MapConverter.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/MapConverter.groovy @@ -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) { diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy index 44cb4e235f..8d6bdbf343 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy @@ -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") + } + }