More type safe

This commit is contained in:
Tim Ysewyn
2019-08-10 15:09:49 +02:00
parent d0f578de5f
commit ff22880d1e
7 changed files with 32 additions and 46 deletions

View File

@@ -65,16 +65,16 @@ class ContractDsl {
private fun get(): Contract {
val contract = Contract()
priority?.also { contract.priority = priority!! }
label?.also { contract.label = label!! }
description?.also { contract.description = description!! }
name?.also { contract.name = name!! }
priority?.also { contract.priority = priority }
label?.also { contract.label = label }
description?.also { contract.description = description }
name?.also { contract.name = name }
contract.ignored = ignored
contract.inProgress = inProgress
request?.also { contract.request = request!! }
response?.also { contract.response = response!! }
input?.also { contract.input = input!! }
outputMessage?.also { contract.outputMessage = outputMessage!! }
request?.also { contract.request = request }
response?.also { contract.response = response }
input?.also { contract.input = input }
outputMessage?.also { contract.outputMessage = outputMessage }
return contract
}

View File

@@ -34,9 +34,7 @@ infix fun UrlPath.withQueryParameters(parameters: QueryParameters.() -> Unit) =
queryParameters = QueryParameters().apply(parameters)
}
fun Any.toDslProperty(): DslProperty<Any> {
return DslProperty(this)
}
fun <T: Any> T.toDslProperty(): DslProperty<T> = DslProperty(this)
fun Map<String, Any>.toDslProperties(): Map<String, DslProperty<Any>> {
return entries.stream().collect(Collectors.toMap(

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.contract.spec.internal
import org.springframework.cloud.contract.spec.toDslProperty
/**
* @author Tim Ysewyn
*/
@@ -31,25 +33,17 @@ class InputDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
var assertThat: String? = null
var bodyMatchers: BodyMatchers? = null
fun messageFrom(messageFrom: String) {
this.messageFrom = DslProperty(messageFrom)
}
fun messageFrom(messageFrom: String) = messageFrom.toDslProperty()
fun headers(headers: HeadersDsl.() -> Unit) {
this.headers = HeadersDsl().apply(headers).get()
}
fun messageBody(vararg pairs: Pair<String, Any>) {
this.messageBody = Input.BodyType(pairs.toMap())
}
fun messageBody(vararg pairs: Pair<String, Any>) = Input.BodyType(pairs.toMap())
fun messageBody(pair: Pair<String, Any>) {
this.messageBody = Input.BodyType(mapOf(pair))
}
fun messageBody(pair: Pair<String, Any>) = Input.BodyType(mapOf(pair))
fun messageBody(value: String) {
this.messageBody = Input.BodyType(value)
}
fun messageBody(value: String) = Input.BodyType(value)
fun bodyMatchers(configurer: BodyMatchersDsl.() -> Unit) {
this.bodyMatchers = BodyMatchersDsl().apply(configurer).get()

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.contract.spec.internal
import org.springframework.cloud.contract.spec.toDslProperty
/**
* @author Tim Ysewyn
*/
@@ -30,17 +32,13 @@ class OutputMessageDsl : CommonDsl(), RegexCreatingProperty<ServerDslProperty> {
var assertThat: String? = null
var bodyMatchers: ResponseBodyMatchers? = null
fun sentTo(sentTo: String) {
this.sentTo = DslProperty(sentTo)
}
fun sentTo(sentTo: String) = sentTo.toDslProperty()
fun headers(headers: HeadersDsl.() -> Unit) {
this.headers = HeadersDsl().apply(headers).get()
}
fun body(body: Any) {
this.body = DslProperty(body)
}
fun body(body: Any) = body.toDslProperty()
fun bodyMatchers(configurer: ResponseBodyMatchersDsl.() -> Unit) {
this.bodyMatchers = ResponseBodyMatchersDsl().apply(configurer).get()

View File

@@ -29,7 +29,7 @@ open class RequestDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
private val delegate = Request()
var method: DslProperty<Any>? = null
var method: DslProperty<*>? = null
var url: Url? = null
var urlPath: UrlPath? = null
var headers: Headers? = null
@@ -66,8 +66,6 @@ open class RequestDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
fun body(body: List<Any>) = Body(body.toDslProperties())
fun body(body: DslProperty<Any>) = Body(body)
fun body(body: Any) = Body(body)
fun multipart(multipart: Map<String, Any>) = Multipart(multipart.toDslProperties())
@@ -238,14 +236,14 @@ open class RequestDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
internal fun get(): Request {
val request = Request()
method?.also { request.method = method!! }
url?.also { request.url = url!! }
urlPath?.also { request.urlPath = urlPath!! }
headers?.also { request.headers = headers!! }
cookies?.also { request.cookies = cookies!! }
body?.also { request.body = body!! }
multipart?.also { request.multipart = multipart!! }
bodyMatchers?.also { request.bodyMatchers = bodyMatchers!! }
method?.also { request.method = method }
url?.also { request.url = url }
urlPath?.also { request.urlPath = urlPath }
headers?.also { request.headers = headers }
cookies?.also { request.cookies = cookies }
body?.also { request.body = body }
multipart?.also { request.multipart = multipart }
bodyMatchers?.also { request.bodyMatchers = bodyMatchers }
return request
}

View File

@@ -57,8 +57,6 @@ class ResponseDsl : CommonDsl(), RegexCreatingProperty<ServerDslProperty> {
fun body(body: List<Any>) = Body(body.toDslProperties())
fun body(body: DslProperty<Any>) = Body(body.toDslProperty())
fun body(body: Any) = Body(body)
fun bodyMatchers(configurer: ResponseBodyMatchersDsl.() -> Unit) {

View File

@@ -145,8 +145,8 @@ class ContractTests {
fun `should work for messaging`() {
val contract = contract {
input {
messageFrom("input")
messageBody("foo" to "bar")
messageFrom = messageFrom("input")
messageBody = messageBody("foo" to "bar")
headers {
header {
name = "foo"
@@ -155,8 +155,8 @@ class ContractTests {
}
}
outputMessage {
sentTo("output")
body("foo2" to "bar")
sentTo = sentTo("output")
body = body("foo2" to "bar")
headers {
header {
name = "foo2"