Simplified part of the DSL

This commit is contained in:
Tim Ysewyn
2019-08-08 19:45:44 +02:00
parent 4603b54428
commit ed288c2f36
7 changed files with 196 additions and 52 deletions

View File

@@ -39,12 +39,12 @@ open class RequestDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
fun method(method: String) = method.toDslProperty()
fun method(method: HttpMethods.HttpMethod) = this.method(method.toString())
fun url(url: String) = Url(url)
fun url(url: DslProperty<Any>) = Url(url)
fun url(client: ClientDslProperty, server: ServerDslProperty) = Url(value(client, server))
fun path(path: String) = UrlPath(path)
fun path(path: DslProperty<Any>) = UrlPath(path)
@@ -81,6 +81,26 @@ open class RequestDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
bodyMatchers = BodyMatchers().apply(block)
}
/* HELPER VARIABLES */
/* HTTP METHODS */
val GET = method(HttpMethods.HttpMethod.GET.name)
val HEAD = method(HttpMethods.HttpMethod.HEAD.name)
val POST = method(HttpMethods.HttpMethod.POST.name)
val PUT = method(HttpMethods.HttpMethod.PUT.name)
val PATCH = method(HttpMethods.HttpMethod.PATCH.name)
val DELETE = method(HttpMethods.HttpMethod.DELETE.name)
val OPTIONS = method(HttpMethods.HttpMethod.OPTIONS.name)
val TRACE = method(HttpMethods.HttpMethod.TRACE.name)
/* HELPER FUNCTIONS */
/**

View File

@@ -64,6 +64,128 @@ class ResponseDsl : CommonDsl(), RegexCreatingProperty<ServerDslProperty> {
this.bodyMatchers = ResponseBodyMatchers().apply(bodyMatchers)
}
/* HELPER VARIABLES */
/* HTTP STATUS CODES */
val CONTINUE = code(HttpStatus.CONTINUE())
val SWITCHING_PROTOCOLS = code(HttpStatus.SWITCHING_PROTOCOLS())
val PROCESSING = code(HttpStatus.PROCESSING())
val CHECKPOINT = code(HttpStatus.CHECKPOINT())
val OK = code(HttpStatus.OK())
val CREATED = code(HttpStatus.CREATED())
val ACCEPTED = code(HttpStatus.ACCEPTED())
val NON_AUTHORITATIVE_INFORMATION = code(HttpStatus.NON_AUTHORITATIVE_INFORMATION())
val NO_CONTENT = code(HttpStatus.NO_CONTENT())
val RESET_CONTENT = code(HttpStatus.RESET_CONTENT())
val PARTIAL_CONTENT = code(HttpStatus.PARTIAL_CONTENT())
val MULTI_STATUS = code(HttpStatus.MULTI_STATUS())
val ALREADY_REPORTED = code(HttpStatus.ALREADY_REPORTED())
val IM_USED = code(HttpStatus.IM_USED())
val MULTIPLE_CHOICES = code(HttpStatus.MULTIPLE_CHOICES())
val MOVED_PERMANENTLY = code(HttpStatus.MOVED_PERMANENTLY())
val FOUND = code(HttpStatus.FOUND())
val SEE_OTHER = code(HttpStatus.SEE_OTHER())
val NOT_MODIFIED = code(HttpStatus.NOT_MODIFIED())
val TEMPORARY_REDIRECT = code(HttpStatus.TEMPORARY_REDIRECT())
val PERMANENT_REDIRECT = code(HttpStatus.PERMANENT_REDIRECT())
val BAD_REQUEST = code(HttpStatus.BAD_REQUEST())
val UNAUTHORIZED = code(HttpStatus.UNAUTHORIZED())
val PAYMENT_REQUIRED = code(HttpStatus.PAYMENT_REQUIRED())
val FORBIDDEN = code(HttpStatus.FORBIDDEN())
val NOT_FOUND = code(HttpStatus.NOT_FOUND())
val METHOD_NOT_ALLOWED = code(HttpStatus.METHOD_NOT_ALLOWED())
val NOT_ACCEPTABLE = code(HttpStatus.NOT_ACCEPTABLE())
val PROXY_AUTHENTICATION_REQUIRED = code(HttpStatus.PROXY_AUTHENTICATION_REQUIRED())
val REQUEST_TIMEOUT = code(HttpStatus.REQUEST_TIMEOUT())
val CONFLICT = code(HttpStatus.CONFLICT())
val GONE = code(HttpStatus.GONE())
val LENGTH_REQUIRED = code(HttpStatus.LENGTH_REQUIRED())
val PRECONDITION_FAILED = code(HttpStatus.PRECONDITION_FAILED())
val PAYLOAD_TOO_LARGE = code(HttpStatus.PAYLOAD_TOO_LARGE())
val UNSUPPORTED_MEDIA_TYPE = code(HttpStatus.UNSUPPORTED_MEDIA_TYPE())
val REQUESTED_RANGE_NOT_SATISFIABLE = code(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE())
val EXPECTATION_FAILED = code(HttpStatus.EXPECTATION_FAILED())
val I_AM_A_TEAPOT = code(HttpStatus.I_AM_A_TEAPOT())
val UNPROCESSABLE_ENTITY = code(HttpStatus.UNPROCESSABLE_ENTITY())
val LOCKED = code(HttpStatus.LOCKED())
val FAILED_DEPENDENCY = code(HttpStatus.FAILED_DEPENDENCY())
val UPGRADE_REQUIRED = code(HttpStatus.UPGRADE_REQUIRED())
val PRECONDITION_REQUIRED = code(HttpStatus.PRECONDITION_REQUIRED())
val TOO_MANY_REQUESTS = code(HttpStatus.TOO_MANY_REQUESTS())
val REQUEST_HEADER_FIELDS_TOO_LARGE = code(HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE())
val UNAVAILABLE_FOR_LEGAL_REASONS = code(HttpStatus.UNAVAILABLE_FOR_LEGAL_REASONS())
val INTERNAL_SERVER_ERROR = code(HttpStatus.INTERNAL_SERVER_ERROR())
val NOT_IMPLEMENTED = code(HttpStatus.NOT_IMPLEMENTED())
val BAD_GATEWAY = code(HttpStatus.BAD_GATEWAY())
val SERVICE_UNAVAILABLE = code(HttpStatus.SERVICE_UNAVAILABLE())
val GATEWAY_TIMEOUT = code(HttpStatus.GATEWAY_TIMEOUT())
val HTTP_VERSION_NOT_SUPPORTED = code(HttpStatus.HTTP_VERSION_NOT_SUPPORTED())
val VARIANT_ALSO_NEGOTIATES = code(HttpStatus.VARIANT_ALSO_NEGOTIATES())
val INSUFFICIENT_STORAGE = code(HttpStatus.INSUFFICIENT_STORAGE())
val LOOP_DETECTED = code(HttpStatus.LOOP_DETECTED())
val BANDWIDTH_LIMIT_EXCEEDED = code(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED())
val NOT_EXTENDED = code(HttpStatus.NOT_EXTENDED())
val NETWORK_AUTHENTICATION_REQUIRED = code(HttpStatus.NETWORK_AUTHENTICATION_REQUIRED())
/* HELPER FUNCTIONS */
fun value(value: ClientDslProperty) = delegate.value(value)

View File

@@ -37,14 +37,14 @@ class ContractTests {
val contract = contract {
request {
url = url("/foo")
method = method(HttpMethods.HttpMethod.PUT)
method = PUT
headers {
header("foo", "bar")
}
body = body("foo" to "bar")
}
response {
status = code(200)
status = OK
headers {
header("foo2", "bar")
}
@@ -88,7 +88,7 @@ class ContractTests {
url = url("/foo")
}
response {
status = code(200)
status = OK
}
}
@@ -103,10 +103,10 @@ class ContractTests {
fun `should fail when no url is present`() {
val contract = contract {
request {
method = method("GET")
method = GET
}
response {
status = code(200)
status = OK
}
}
@@ -122,7 +122,7 @@ class ContractTests {
val contract = contract {
request {
url = url("/foo")
method = method("GET")
method = GET
}
response {
}
@@ -275,13 +275,13 @@ then:
fun `should make equals and hashcode work properly for URL`() {
val a: Contract = contract {
request {
method = method("GET")
method = GET
url = url("/1")
}
}
val b: Contract = contract {
request {
method = method("GET")
method = GET
url = url("/1")
}
}
@@ -298,14 +298,14 @@ then:
fun `should make equals and hashcode work properly for URL with consumer producer`() {
val a: Contract = contract {
request {
method = method("GET")
url = url(value(c("/1"), p("/1")))
method = GET
url = url(c("/1"), p("/1"))
}
}
val b: Contract = contract {
request {
method = method("GET")
url = url(value(c("/1"), p("/1")))
method = GET
url = url(c("/1"), p("/1"))
}
}
@@ -322,26 +322,26 @@ then:
val index = 1
val a: Contract = contract {
request {
method = method(HttpMethods.HttpMethod.PUT)
method = PUT
headers {
contentType(applicationJson())
}
url = url("/$index")
}
response {
status = code(HttpStatus.OK())
status = OK
}
}
val b: Contract = contract {
request {
method = method(HttpMethods.HttpMethod.PUT)
method = PUT
headers {
contentType(applicationJson())
}
url = url("/$index")
}
response {
status = code(HttpStatus.OK())
status = OK
}
}
@@ -358,27 +358,27 @@ then:
var index = 1
val a: Contract = contract {
request {
method = method(HttpMethods.HttpMethod.PUT)
method = PUT
headers {
contentType(applicationJson())
}
url = url("/$index")
}
response {
status = code(HttpStatus.OK())
status = OK
}
}
index = 2
val b: Contract = contract {
request {
method = method(HttpMethods.HttpMethod.PUT)
method = PUT
headers {
contentType(applicationJson())
}
url = url("/$index")
}
response {
status = code(HttpStatus.OK())
status = OK
}
}
@@ -394,7 +394,7 @@ then:
fun `should return true when comparing two equal complex contracts`() {
val a: Contract = contract {
request {
method = method(HttpMethods.HttpMethod.GET)
method = GET
url = url("/path")
headers {
header("Accept", value(
@@ -408,7 +408,7 @@ then:
}
}
response {
status = code(HttpStatus.OK())
status = OK
body = body("id" to mapOf("value" to "132"),
"surname" to "Kowalsky",
"name" to "Jan",
@@ -421,7 +421,7 @@ then:
}
val b: Contract = contract {
request {
method = method(HttpMethods.HttpMethod.GET)
method = GET
url = url("/path")
headers {
header("Accept", value(
@@ -435,7 +435,7 @@ then:
}
}
response {
status = code(HttpStatus.OK())
status = OK
body = body("id" to mapOf("value" to "132"),
"surname" to "Kowalsky",
"name" to "Jan",
@@ -493,16 +493,16 @@ then:
fun `should support bodyMatchers`() {
val contract = contract {
request {
method = method(HttpMethods.HttpMethod.GET)
method = GET
url = url("/path")
body("id" to mapOf("value" to "132"))
body = body("id" to mapOf("value" to "132"))
bodyMatchers {
jsonPath("$.id.value", byRegex(anInteger()))
}
}
response {
status = code(HttpStatus.OK())
body("id" to mapOf("value" to "132"),
status = OK
body = body("id" to mapOf("value" to "132"),
"surname" to "Kowalsky",
"name" to "Jan",
"created" to "2014-02-02 12:23:43"
@@ -537,13 +537,13 @@ then:
fun `should support query parameters for url`() {
val contract = contract {
request {
method = method(HttpMethods.HttpMethod.GET)
method = GET
url = url("/path") withQueryParameters {
parameter("foo", "bar")
}
}
response {
status = code(HttpStatus.OK())
status = OK
}
}
@@ -570,13 +570,13 @@ then:
fun `should support query parameters for url path`() {
val contract = contract {
request {
method = method(HttpMethods.HttpMethod.GET)
method = GET
urlPath = path("/path") withQueryParameters {
parameter("foo", "bar")
}
}
response {
status = code(HttpStatus.OK())
status = OK
}
}
@@ -603,12 +603,12 @@ then:
fun `should work with list as body`() {
val contract = contract {
request {
method = method(HttpMethods.HttpMethod.PUT)
method = PUT
url = url("/path")
body = body(listOf("foo", "bar"))
}
response {
status = code(HttpStatus.OK())
status = OK
body = body(listOf("foo2", "bar2"))
}
}

View File

@@ -34,6 +34,7 @@ class KotlinContractConverterTest {
val converter = KotlinContractConverter()
val contracts = converter.convertFrom(file("contracts/singleDefinition.kts"))
assertEquals(1, contracts.size)
contracts.forEach(Contract::assertContract)
}
@Test
@@ -41,6 +42,7 @@ class KotlinContractConverterTest {
val converter = KotlinContractConverter()
val contracts = converter.convertFrom(file("contracts/multipleDefinitions.kts"))
assertEquals(2, contracts.size)
contracts.forEach(Contract::assertContract)
}
@Test
@@ -48,7 +50,7 @@ class KotlinContractConverterTest {
val converter = KotlinContractConverter()
val contracts = converter.convertFrom(file("contracts/shouldWorkWithBinaryPayload.kts"))
assertEquals(1, contracts.size)
Contract.assertContract(contracts.elementAt(0))
contracts.forEach(Contract::assertContract)
}
private fun file(filename: String) = File(javaClass.classLoader.getResource(filename)!!.toURI())

View File

@@ -21,12 +21,12 @@ import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
arrayOf(
contract {
request {
method("GET")
url("/frauds")
method = GET
url = url("/frauds")
}
response {
code(200)
body("count" to 200)
status = OK
body = body("count" to 200)
headers {
contentType("application/vnd.fraud.v1+json")
}
@@ -34,12 +34,12 @@ arrayOf(
},
contract {
request {
method("GET")
url("/drunks")
method = GET
url = url("/drunks")
}
response {
code(200)
body("count" to 100)
status = OK
body = body("count" to 100)
headers {
contentType("application/vnd.fraud.v1+json")
}

View File

@@ -22,14 +22,14 @@ import org.springframework.cloud.contract.spec.internal.HttpMethods
contract {
request {
url = url("/1")
method = method(HttpMethods.PUT())
method = PUT
headers {
contentType(applicationOctetStream())
}
body = bodyFromFileAsBytes("contracts/request.pdf")
}
response {
status = code(200)
status = OK
body = bodyFromFileAsBytes("contracts/response.pdf")
headers {
contentType(applicationOctetStream())

View File

@@ -20,9 +20,9 @@ import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
contract {
request {
method("PUT")
url("/fraudcheck")
body("clientId" to value(consumer(regex("[0-9]{10}")), producer("1234567890")),
method = PUT
url = url("/fraudcheck")
body = body("clientId" to value(consumer(regex("[0-9]{10}")), producer("1234567890")),
"loanAmount" to 123.123
)
headers {
@@ -31,8 +31,8 @@ contract {
}
response {
code(200)
body(
status = OK
body = body(
"fraudCheckStatus" to "OK",
"rejectionReason" to listOf(value(consumer(null), producer("assertThatRejectionReasonIsNull(\$it)")))
)