Consumer Kotlin DSL API proposal
See original GitHub issueHi, I’d like to propose an API for consumer Kotlin DSL (in case you considered implementing one).
Flavors
There are two flavors, although they differ only slightly:
given
vs.{}
(inproviderStates
)uponReceiving
vs.request
willRespondWith
vs.response
BDD flavor
@Pact(consumer = "consumer")
fun bddPact(builder: PactDslWithProvider): RequestResponsePact = builder.toKotlinDsl()
.interaction {
providerStates {
given("sample state 1")
given("sample state 2") {
"someId" - "someValue"
"anotherId" - 20
}
}
uponReceiving("sample request") {
method = "POST"
path("/v1/sample/resources/{resourceId}") {
"resourceId" - uuid("439caf8d-c672-4596-b3c3-9d3bde5c980b")
}
query {
"someType" - enum<SomeType>("FOO")
"specialMode" - boolean(true)
}
headers {
"Content-Type" - equalTo("application/json")
"RequestId" - string("7a324886-a60b-467a-a0df-ecc5fc7a72aa")
}
body = obj {
"contextId" - uuid("439caf8d-c672-4596-b3c3-9d3bde5c980b")
"items" - array(eachLike = obj {
"name" - string("sample item")
"count" - integer(2)
})
}
}
willRespondWith {
status = 200
headers {
"Content-Type" - equalTo("application/json")
}
body = obj {
"resourceId" - uuid("d2884c7c-2231-4d18-826b-c0746b81b962")
}
}
}
.toPact()
Declarative flavor
@Pact(consumer = "consumer")
fun declarativePact(builder: PactDslWithProvider): RequestResponsePact = builder.toKotlinDsl()
.interaction {
providerStates {
"sample state 1" {}
"sample state 2" {
"someId" - "someValue"
"anotherId" - 20
}
}
request("sample request") {
method = "POST"
path("/v1/sample/resources/{resourceId}") {
"resourceId" - uuid("439caf8d-c672-4596-b3c3-9d3bde5c980b")
}
query {
"someType" - enum<SomeType>("FOO")
"specialMode" - boolean(true)
}
headers {
"Content-Type" - equalTo("application/json")
"RequestId" - string("7a324886-a60b-467a-a0df-ecc5fc7a72aa")
}
body = obj {
"contextId" - uuid("439caf8d-c672-4596-b3c3-9d3bde5c980b")
"items" - array(eachLike = obj {
"name" - string("sample item")
"count" - integer(2)
})
}
}
response {
status = 200
headers {
"Content-Type" - equalTo("application/json")
}
body = obj {
"resourceId" - uuid("d2884c7c-2231-4d18-826b-c0746b81b962")
}
}
}
.toPact()
Full list of body symbols
private val objectBody = pactBody {
obj {
"equalTo01" - equalTo(1)
"string03" - string("str03")
"stringLike04" - string("str04", like = Regex("\\w+"))
"stringWithin05" - string("str05", within = listOf("str05"))
"SampleEnum06" - enum<SampleEnum>("FOO")
"int07" - integer(7)
"long08" - integer(8L)
"decimal09" - decimal("0.09")
"boolean10" - boolean(true)
"uuid11" - uuid("11111111-c672-4596-b3c3-9d3bde5c980b")
"date12" - date("2012-12-12")
"time13" - time("13:13:13")
"dateTime14_sec" - dateTime("2014-04-14T14:14:14Z")
"dateTime14_milli" - dateTime("2014-04-14T14:14:14.141Z")
"obj15" - obj {
"obj15_string1" - string("str15.1")
"obj15_int2" - integer(152)
}
"arrayOf16" - arrayOf(
string("arrayOf16_string1"),
integer(162)
)
"eachLike17" - array(eachLike = string("eachLike17_string1"))
"eachLike18" - array(
minSize = 2, maxSize = 4, numberOfExamples = 3,
eachLike = string("eachLike18_string1")
)
"eachLike19" - array(eachLike = obj {
"eachLike19_obj_string1" - string("str19.1")
"eachLike19_obj_int2" - integer(192)
})
"eachLike20" - array(
minSize = 2, maxSize = 4, numberOfExamples = 3,
eachLike = obj {
"eachLike20_obj_string1" - string("str20.1")
"eachLike20_obj_int2" - integer(202)
}
)
"eachLike21" - array(
eachLike = arrayOf(
string("eachLike21_arrayOf_string1"),
integer(212)
)
)
"eachLike22" - array(
minSize = 2, maxSize = 4, numberOfExamples = 3,
eachLike = arrayOf(
string("eachLike22_arrayOf_string1"),
integer(222)
)
)
"map23" - map(
keyLike = "map23_key",
valueLike = string("map23_string1")
)
"map24" - map(
keyLike = "map24_key",
valueLike = obj {
"map24_obj_string1" - string("str24.1")
"map24_obj_int2" - integer(242)
}
)
"map25" - map(
keyLike = "map25_key",
valueLike = array(eachLike = obj {
"map25_eachLike_obj_string1" - string("str25.1")
"map25_eachLike_obj_int2" - integer(252)
})
)
"emptyArray26" - emptyArray()
}
}
PS. This is an API we implemented in my company (it’s a wrapper over Pact DSL). We chose the declarative flavor. Unfortunately, I can’t share the implementation without a permission from my employer. PPS. I could share corresponding Pact DSLs, if you were interested.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Kotlin DSL | TeamCity On-Premises Documentation - JetBrains
To get familiar with Kotlin API, see the online documentation on your local server, accessible via the link on the Versioned Settings project ......
Read more >api - Gradle Kotlin DSL
org.gradle.api.artifacts.repositories · org.gradle.api.artifacts.result ... The Tooling API models for the Gradle Kotlin DSL. org.gradle.language.
Read more >Spring Framework 5 Kotlin APIs, the functional way
The DSL conceptually declare a Consumer<GenericApplicationContext> via a clean declarative API which allows you to deal with profile and ...
Read more >Authoring Tasks - Gradle User Manual
If you look at the API of the tasks container you may notice that there are ... If you use the Kotlin DSL...
Read more >Chapter 11. DSL construction - Kotlin in Action
In this chapter, we'll discuss how you can design expressive and idiomatic APIs for your Kotlin classes through the use of domain-specific languages...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The
junit5spring
lib hasn’t given me any Kotlin issues 👍 idk about other provider libs 🙃Hey team,
The empty kotlin module created as part of this thread caused an empty readme page on the docs.pact.io page here 👇🏾
https://docs.pact.io/implementation_guides/jvm/consumer/kotlin
Do we want to take some of the good stuff started and share, either in that example module readme/project, or in another format?