diff --git a/docs/src/main/asciidoc/verifier_contract.adoc b/docs/src/main/asciidoc/verifier_contract.adoc index eba0500702..20577a29b4 100644 --- a/docs/src/main/asciidoc/verifier_contract.adoc +++ b/docs/src/main/asciidoc/verifier_contract.adoc @@ -449,7 +449,7 @@ the provided regular expression. The following code shows an example: [source,groovy,indent=0] ---- -include::{verifier_core_path}/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MethodBodyBuilderSpec.groovy[tags=dsl_one_side_data_generation_example,indent=0] +include::{verifier_core_path}/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SpringTestMethodBodyBuildersSpec.groovy[tags=dsl_one_side_data_generation_example,indent=0] ---- In the preceding example, the opposite side of the communication has the respective data @@ -467,7 +467,22 @@ In your contract, you can use it as shown in the following example: [source,groovy,indent=0] ---- -include::{verifier_core_path}/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MethodBodyBuilderSpec.groovy[tags=contract_with_regex,indent=0] +include::{verifier_core_path}/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SpringTestMethodBodyBuildersSpec.groovy[tags=contract_with_regex,indent=0] +---- + +To make matters even simpler you can use a set of predefined objects that will automatically assume that you want a regular expression to be passed. +All of those methods start with `any` prefix: + +[source,groovy,indent=0] +---- +include::{contract_spec_path}/src/main/groovy/org/springframework/cloud/contract/spec/internal/RegexCreatingProperty.groovy[tags=regex_creating_props,indent=0] +---- + +and this is an example of how you can reference those methods: + +[source,groovy,indent=0] +---- +include::{verifier_core_path}/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MessagingMethodBodyBuilderSpec.groovy[tags=regex_creating_props,indent=0] ---- ==== Passing Optional Parameters diff --git a/spring-cloud-contract-spec/src/main/groovy/org/springframework/cloud/contract/spec/internal/PatternValueDslProperty.groovy b/spring-cloud-contract-spec/src/main/groovy/org/springframework/cloud/contract/spec/internal/PatternValueDslProperty.groovy index 0ed877d86b..f617c2595e 100644 --- a/spring-cloud-contract-spec/src/main/groovy/org/springframework/cloud/contract/spec/internal/PatternValueDslProperty.groovy +++ b/spring-cloud-contract-spec/src/main/groovy/org/springframework/cloud/contract/spec/internal/PatternValueDslProperty.groovy @@ -26,7 +26,7 @@ import java.util.regex.Pattern */ @PackageScope @CompileStatic -abstract class PatternValueDslProperty { +abstract class PatternValueDslProperty implements RegexCreatingProperty { private final Random random = new Random() @@ -51,96 +51,117 @@ abstract class PatternValueDslProperty { * @return {@link DslProperty} wrapping a pattern and generated value */ protected abstract T createProperty(Pattern pattern, Object generatedValue) - + + @Override T anyAlphaUnicode() { return createAndValidateProperty(RegexPatterns.ONLY_ALPHA_UNICODE, RandomStringGenerator.randomString(20)) } + @Override T anyAlphaNumeric() { return createAndValidateProperty(RegexPatterns.ALPHA_NUMERIC, RandomStringUtils.randomAlphanumeric(20)) } + @Override T anyNumber() { return createAndValidateProperty(RegexPatterns.NUMBER, this.random.nextInt()) } + @Override T anyInteger() { return createAndValidateProperty(RegexPatterns.INTEGER, this.random.nextInt()) } + @Override T anyPositiveInt() { return createAndValidateProperty(RegexPatterns.POSITIVE_INT, Math.abs(this.random.nextInt() + 1)) } + @Override T anyDouble() { return createAndValidateProperty(RegexPatterns.DOUBLE, this.random.nextInt(100) + this.random.nextDouble()) } + @Override T anyHex() { return createAndValidateProperty(RegexPatterns.HEX, RandomStringUtils.random(10, "0123456789abcdef")) } + @Override T aBoolean() { return createAndValidateProperty(RegexPatterns.TRUE_OR_FALSE) } + @Override T anyIpAddress() { return createAndValidateProperty(RegexPatterns.IP_ADDRESS, "192.168.0." + this.random.nextInt(10)) } + @Override T anyHostname() { return createAndValidateProperty(RegexPatterns.HOSTNAME_PATTERN, "http://foo" + this.random.nextInt() + ".com") } + @Override T anyEmail() { return createAndValidateProperty(RegexPatterns.EMAIL, "foo@bar" + this.random.nextInt() + ".com") } + @Override T anyUrl() { return createAndValidateProperty(RegexPatterns.URL, "http://foo" + this.random.nextInt() + ".com") } + @Override T anyHttpsUrl() { return createAndValidateProperty(RegexPatterns.HTTPS_URL, "https://baz" + this.random.nextInt() + ".com") } + @Override T anyUuid() { return createAndValidateProperty(RegexPatterns.UUID, UUID.randomUUID().toString()) } + @Override T anyDate() { int d = this.random.nextInt(8) + 1 return createAndValidateProperty(RegexPatterns.ANY_DATE, "201$d-0$d-1$d") } + @Override T anyDateTime() { int d = this.random.nextInt(8) + 1 return createAndValidateProperty(RegexPatterns.ANY_DATE_TIME, "201$d-0$d-1${d}T12:23:34") } + @Override T anyTime() { int d = this.random.nextInt(9) return createAndValidateProperty(RegexPatterns.ANY_TIME, "12:2$d:3$d") } + @Override T anyIso8601WithOffset() { int d = this.random.nextInt(8) + 1 return createAndValidateProperty(RegexPatterns.ISO8601_WITH_OFFSET, "201$d-0$d-1${d}T12:23:34.123Z") } + @Override T anyNonBlankString() { return createAndValidateProperty(RegexPatterns.NON_BLANK, RandomStringGenerator.randomString(20)) } + @Override T anyNonEmptyString() { return createAndValidateProperty(RegexPatterns.NON_EMPTY, RandomStringGenerator.randomString(20)) } + @Override T anyOf(String... values){ return createAndValidateProperty(RegexPatterns.anyOf(values), values[0]) } diff --git a/spring-cloud-contract-spec/src/main/groovy/org/springframework/cloud/contract/spec/internal/RegexCreatingProperty.groovy b/spring-cloud-contract-spec/src/main/groovy/org/springframework/cloud/contract/spec/internal/RegexCreatingProperty.groovy new file mode 100644 index 0000000000..59041e479c --- /dev/null +++ b/spring-cloud-contract-spec/src/main/groovy/org/springframework/cloud/contract/spec/internal/RegexCreatingProperty.groovy @@ -0,0 +1,72 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.springframework.cloud.contract.spec.internal + + +import groovy.transform.CompileStatic +import groovy.transform.PackageScope +/** + * @author Marcin Grzejszczak + */ +@PackageScope +@CompileStatic +interface RegexCreatingProperty { + + //tag::regex_creating_props[] + T anyAlphaUnicode() + + T anyAlphaNumeric() + + T anyNumber() + + T anyInteger() + + T anyPositiveInt() + + T anyDouble() + + T anyHex() + + T aBoolean() + + T anyIpAddress() + + T anyHostname() + + T anyEmail() + + T anyUrl() + + T anyHttpsUrl() + + T anyUuid() + + T anyDate() + + T anyDateTime() + + T anyTime() + + T anyIso8601WithOffset() + + T anyNonBlankString() + + T anyNonEmptyString() + + T anyOf(String... values) + //end::regex_creating_props[] +} diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MessagingMethodBodyBuilderSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MessagingMethodBodyBuilderSpec.groovy index 311c079ff8..91f515947a 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MessagingMethodBodyBuilderSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MessagingMethodBodyBuilderSpec.groovy @@ -535,6 +535,7 @@ Contract.make { @Issue('#587') def "should allow easier way of providing dynamic values for [#methodBuilderName]"() { given: + //tag::regex_creating_props[] Contract contractDsl = Contract.make { label 'trigger_event' input { @@ -565,6 +566,7 @@ Contract.make { ]) } } + //end::regex_creating_props[] MethodBodyBuilder builder = methodBuilder(contractDsl) BlockBuilder blockBuilder = new BlockBuilder(" ") when: