From 1165f154a62c6ee1249fd8dfaf7467cbd4f219a9 Mon Sep 17 00:00:00 2001 From: Tim Ysewyn Date: Mon, 18 Nov 2019 11:13:19 +0100 Subject: [PATCH] Enhanced Kotlin DSL --- .../cloud/contract/spec/internal/InputDsl.kt | 25 +++++++++++ .../spec/internal/OutputMessageDsl.kt | 28 +++++++++++++ .../cloud/contract/spec/ContractTests.kt | 42 ++++++++++++++++--- 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/InputDsl.kt b/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/InputDsl.kt index 03d24bdad7..bffb723c32 100644 --- a/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/InputDsl.kt +++ b/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/InputDsl.kt @@ -17,6 +17,7 @@ package org.springframework.cloud.contract.spec.internal import org.springframework.cloud.contract.spec.toDslProperty +import java.util.regex.Pattern /** * Represents an input for messaging. @@ -141,6 +142,30 @@ class InputDsl : CommonDsl() { /* HELPER FUNCTIONS */ + fun value(value: DslProperty): DslProperty = delegate.value(value) + + fun v(value: DslProperty): DslProperty = delegate.value(value) + + fun value(value: Pattern): DslProperty = delegate.value(value) + + fun v(value: Pattern): DslProperty = delegate.value(value) + + fun value(value: RegexProperty): DslProperty = delegate.value(value) + + fun v(value: RegexProperty): DslProperty = delegate.value(value) + + fun value(value: Any?): DslProperty = delegate.value(value) + + fun v(value: Any?): DslProperty = delegate.value(value) + + fun value(client: ClientDslProperty, server: ServerDslProperty): DslProperty = delegate.value(client, server) + + fun v(client: ClientDslProperty, server: ServerDslProperty): DslProperty = delegate.value(client, server) + + fun value(server: ServerDslProperty, client: ClientDslProperty): DslProperty = delegate.value(client, server) + + fun v(server: ServerDslProperty, client: ClientDslProperty): DslProperty = delegate.value(client, server) + fun anyOf(vararg values: String?) = delegate.anyOf(*values) internal fun get(): Input { diff --git a/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/OutputMessageDsl.kt b/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/OutputMessageDsl.kt index f2babfb262..3f4fa46681 100644 --- a/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/OutputMessageDsl.kt +++ b/specs/spring-cloud-contract-spec-kotlin/src/main/kotlin/org/springframework/cloud/contract/spec/internal/OutputMessageDsl.kt @@ -16,7 +16,9 @@ package org.springframework.cloud.contract.spec.internal +import org.springframework.cloud.contract.spec.toDslProperties import org.springframework.cloud.contract.spec.toDslProperty +import java.util.regex.Pattern /** * Represents an output for messaging. @@ -64,6 +66,8 @@ class OutputMessageDsl : CommonDsl() { fun body(body: Any) = body.toDslProperty() + fun body(vararg body: Pair) = Body(body.toMap().toDslProperties()) + fun bodyMatchers(configurer: ResponseBodyMatchersDsl.() -> Unit) { this.bodyMatchers = ResponseBodyMatchersDsl().apply(configurer).get() } @@ -132,6 +136,30 @@ class OutputMessageDsl : CommonDsl() { /* HELPER FUNCTIONS */ + fun value(value: DslProperty): DslProperty = delegate.value(value) + + fun v(value: DslProperty): DslProperty = delegate.value(value) + + fun value(value: Pattern): DslProperty = delegate.value(value) + + fun v(value: Pattern): DslProperty = delegate.value(value) + + fun value(value: RegexProperty): DslProperty = delegate.value(value) + + fun v(value: RegexProperty): DslProperty = delegate.value(value) + + fun value(value: Any?): DslProperty = delegate.value(value) + + fun v(value: Any?): DslProperty = delegate.value(value) + + fun value(client: ClientDslProperty, server: ServerDslProperty): DslProperty = delegate.value(client, server) + + fun v(client: ClientDslProperty, server: ServerDslProperty): DslProperty = delegate.value(client, server) + + fun value(server: ServerDslProperty, client: ClientDslProperty): DslProperty = delegate.value(client, server) + + fun v(server: ServerDslProperty, client: ClientDslProperty): DslProperty = delegate.value(client, server) + fun anyOf(vararg values: String?) = delegate.anyOf(*values) internal fun get(): OutputMessage { diff --git a/specs/spring-cloud-contract-spec-kotlin/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt b/specs/spring-cloud-contract-spec-kotlin/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt index c6488c73ac..70ea5c6429 100644 --- a/specs/spring-cloud-contract-spec-kotlin/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt +++ b/specs/spring-cloud-contract-spec-kotlin/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt @@ -150,13 +150,15 @@ class ContractTests { messageBody = messageBody("foo" to "bar") headers { header("foo", "bar") + header("X-Custom-Header", value(consumer(regex("^.*2134.*\$")), producer("121345"))) } } outputMessage { sentTo = sentTo("output") - body = body("foo2" to "bar") + body = body("foo" to "bar", "foo2" to "bar2") headers { header("foo2", "bar") + header("X-Custom-Header", value(consumer("121345"), producer(regex("^.*2134.*\$")))) } } } @@ -170,21 +172,33 @@ class ContractTests { assertThat(input.messageBody.clientValue).isEqualTo(mapOf("foo" to "bar")) assertThat(input.messageBody.serverValue).isEqualTo(mapOf("foo" to "bar")) val headers = input.messageHeaders.entries - assertThat(headers).hasSize(1) + assertThat(headers).hasSize(2) assertThat(headers.elementAt(0).name).isEqualTo("foo") assertThat(headers.elementAt(0).clientValue).isEqualTo("bar") assertThat(headers.elementAt(0).serverValue).isEqualTo("bar") + assertThat(headers.elementAt(1).name).isEqualTo("X-Custom-Header") + assertThat(headers.elementAt(1).clientValue).isInstanceOf(RegexProperty::class.java) + assertThat((headers.elementAt(1).clientValue as RegexProperty).pattern()).isEqualTo("^.*2134.*\$") + assertThat(headers.elementAt(1).serverValue).isEqualTo("121345") }.also { val output = contract.outputMessage assertThat(output.sentTo.clientValue).isEqualTo("output") assertThat(output.sentTo.serverValue).isEqualTo("output") - assertThat(output.body.clientValue).isEqualTo("foo2" to "bar") - assertThat(output.body.serverValue).isEqualTo("foo2" to "bar") + assertThat(output.body.clientValue).isEqualTo(mapOf("foo" to "bar", + "foo2" to "bar2" + )) + assertThat(output.body.serverValue).isEqualTo(mapOf("foo" to "bar", + "foo2" to "bar2" + )) val headers = output.headers.entries - assertThat(headers).hasSize(1) + assertThat(headers).hasSize(2) assertThat(headers.elementAt(0).name).isEqualTo("foo2") assertThat(headers.elementAt(0).clientValue).isEqualTo("bar") assertThat(headers.elementAt(0).serverValue).isEqualTo("bar") + assertThat(headers.elementAt(1).name).isEqualTo("X-Custom-Header") + assertThat(headers.elementAt(1).clientValue).isEqualTo("121345") + assertThat(headers.elementAt(1).serverValue).isInstanceOf(RegexProperty::class.java) + assertThat((headers.elementAt(1).serverValue as RegexProperty).pattern()).isEqualTo("^.*2134.*\$") } } @@ -440,6 +454,12 @@ then: ) headers { header("Content-Type", "text/plain") + header("X-Custom-Header", + value( + stub("121345"), + test(regex("^.*2134.*$")) + ) + ) } } } @@ -471,6 +491,12 @@ then: ) headers { header("Content-Type", "text/plain") + header("X-Custom-Header", + value( + stub("121345"), + test(regex("^.*2134.*$")) + ) + ) } } } @@ -500,10 +526,14 @@ then: assertThat(response.status.clientValue).isEqualTo(200) assertThat(response.status.serverValue).isEqualTo(200) val headers = response.headers.entries - assertThat(headers).hasSize(1) + assertThat(headers).hasSize(2) assertThat(headers.elementAt(0).name).isEqualTo("Content-Type") assertThat(headers.elementAt(0).clientValue).isEqualTo("text/plain") assertThat(headers.elementAt(0).serverValue).isEqualTo("text/plain") + assertThat(headers.elementAt(1).name).isEqualTo("X-Custom-Header") + assertThat(headers.elementAt(1).clientValue).isEqualTo("121345") + assertThat(headers.elementAt(1).serverValue).isInstanceOf(RegexProperty::class.java) + assertThat((headers.elementAt(1).serverValue as RegexProperty).pattern()).isEqualTo("^.*2134.*\$") assertThat(response.body.clientValue).isEqualTo(mapOf("id" to mapOf("value" to "132"), "surname" to "Kowalsky", "name" to "Jan",