Added contract converter
This commit is contained in:
@@ -31,6 +31,26 @@
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-compiler</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-scripting-compiler</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-script-runtime</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-script-util</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test</artifactId>
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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
|
||||
*
|
||||
* https://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 org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.spec.ContractConverter
|
||||
import java.io.File
|
||||
import javax.script.ScriptEngineManager
|
||||
|
||||
/**
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
class KotlinContractConverter: ContractConverter<List<Contract>> {
|
||||
|
||||
private val ext = "kts"
|
||||
|
||||
constructor() {
|
||||
// Sets an {@code idea.use.native.fs.for.win} system property to {@code false}
|
||||
// to disable a native engine discovery for Windows: may be resolved in the future Kotlin versions.
|
||||
System.setProperty("idea.use.native.fs.for.win", "false")
|
||||
}
|
||||
|
||||
override fun isAccepted(file: File): Boolean {
|
||||
return ext == file.extension
|
||||
}
|
||||
|
||||
override fun convertFrom(file: File): Collection<Contract> {
|
||||
val eval = file.reader().use {
|
||||
// Get a new engine every time we need to process a file.
|
||||
// Reusing the script engine could leak context and will fail subsequent evals
|
||||
ScriptEngineManager().getEngineByExtension(ext).eval(it)
|
||||
}
|
||||
return when (eval) {
|
||||
is Contract -> listOf(eval)
|
||||
is Iterable<*> -> eval.filterIsInstance(Contract::class.java)
|
||||
is Array<*> -> eval.filterIsInstance(Contract::class.java)
|
||||
else -> emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
override fun convertTo(contract: Collection<Contract>) = contract.toList()
|
||||
}
|
||||
@@ -44,20 +44,20 @@ open class RequestDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
|
||||
this.method(method.toString())
|
||||
}
|
||||
|
||||
fun url(url: String): Url {
|
||||
return Url(url)
|
||||
fun url(url: String) {
|
||||
this.url = Url(url)
|
||||
}
|
||||
|
||||
fun url(url: DslProperty<Any>): Url {
|
||||
return Url(url)
|
||||
fun url(url: DslProperty<Any>) {
|
||||
this.url = Url(url)
|
||||
}
|
||||
|
||||
fun urlPath(url: String): UrlPath {
|
||||
return UrlPath(url)
|
||||
fun urlPath(url: String) {
|
||||
this.urlPath = UrlPath(url)
|
||||
}
|
||||
|
||||
fun urlPath(url: DslProperty<Any>): UrlPath {
|
||||
return UrlPath(url)
|
||||
fun urlPath(url: DslProperty<Any>) {
|
||||
this.urlPath = UrlPath(url)
|
||||
}
|
||||
|
||||
fun headers(headers: Headers.() -> Unit) {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.cloud.contract.spec.ContractConverter=\
|
||||
org.springframework.cloud.contract.spec.internal.KotlinContractConverter
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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
|
||||
*
|
||||
* https://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 org.junit.Test
|
||||
import java.io.File
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class KotlinContractConverterTest {
|
||||
|
||||
@Test
|
||||
fun `accept kts files`() {
|
||||
assertTrue(KotlinContractConverter().isAccepted(file("contracts/singleDefinition.kts")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should convert single contract definition`() {
|
||||
val converter = KotlinContractConverter()
|
||||
val contracts = converter.convertFrom(file("contracts/singleDefinition.kts"))
|
||||
assertEquals(1, contracts.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should convert multiple contract definitions`() {
|
||||
val converter = KotlinContractConverter()
|
||||
val contracts = converter.convertFrom(file("contracts/multipleDefinitions.kts"))
|
||||
assertEquals(2, contracts.size)
|
||||
}
|
||||
|
||||
private fun file(filename: String) = File(javaClass.classLoader.getResource(filename)!!.toURI())
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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
|
||||
*
|
||||
* https://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 contracts
|
||||
|
||||
import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
|
||||
|
||||
arrayOf(
|
||||
contract {
|
||||
request {
|
||||
method("GET")
|
||||
url("/frauds")
|
||||
}
|
||||
response {
|
||||
status(200)
|
||||
body("count" to 200)
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
}
|
||||
}
|
||||
},
|
||||
contract {
|
||||
request {
|
||||
method("GET")
|
||||
url("/drunks")
|
||||
}
|
||||
response {
|
||||
status(200)
|
||||
body("count" to 100)
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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
|
||||
*
|
||||
* https://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 contracts
|
||||
|
||||
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")),
|
||||
"loanAmount" to 123.123
|
||||
)
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
}
|
||||
|
||||
}
|
||||
response {
|
||||
status(200)
|
||||
body(
|
||||
"fraudCheckStatus" to "OK",
|
||||
"rejectionReason" to listOf(value(consumer(null), producer("assertThatRejectionReasonIsNull(\$it)")))
|
||||
)
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user