Added javadoc

This commit is contained in:
Tim Ysewyn
2019-08-13 15:44:18 +02:00
parent ed83b824bb
commit 787083be08
14 changed files with 256 additions and 21 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.cloud.contract.spec.internal.ResponseDsl
/**
* @author Tim Ysewyn
* @since 2.2.0
*/
@ContractDslMarker
class ContractDsl {
@@ -36,31 +37,96 @@ class ContractDsl {
fun contract(dsl: ContractDsl.() -> Unit): Contract = ContractDsl().apply(dsl).get()
}
/**
* You can set the level of priority of this contract. If there are two contracts
* mapped for example to the same endpoint, then the one with greater priority should
* take precedence. A priority of 1 is highest and takes precedence over a priority of
* 2.
*/
var priority: Int? = null
/**
* The label by which you'll reference the contract on the message consumer side.
*/
var label: String? = null
/**
* Description of a contract. May be used in the documentation generation.
*/
var description: String? = null
/**
* Name of the generated test / stub. If not provided then the file name will be used.
* If you have multiple contracts in a single file and you don't provide this value
* then a prefix will be added to the file with the index number while iterating over
* the collection of contracts.
*
* Remember to have a unique name for every single contract. Otherwise you might
* generate tests that have two identical methods or you will override the stubs.
*/
var name: String? = null
/**
* Whether the contract should be ignored or not.
*/
var ignored: Boolean = false
/**
* Whether the contract is in progress. It's not ignored, but the feature is not yet
* finished. Used together with the {@code generateStubs} option.
*/
var inProgress: Boolean = false
/**
* The HTTP request part of the contract.
*/
var request: Request? = null
/**
* The HTTP response part of the contract.
*/
var response: Response? = null
/**
* The input side of a messaging contract.
*/
var input: Input? = null
/**
* The output side of a messaging contract.
*/
var outputMessage: OutputMessage? = null
fun request(request: RequestDsl.() -> Unit) {
this.request = RequestDsl().apply(request).get()
/**
* The HTTP request part of the contract.
* @param configurer lambda to configure the HTTP request
*/
fun request(configurer: RequestDsl.() -> Unit) {
request = RequestDsl().apply(configurer).get()
}
fun response(response: ResponseDsl.() -> Unit) {
this.response = ResponseDsl().apply(response).get()
/**
* The HTTP response part of the contract.
* @param configurer lambda to configure the HTTP response
*/
fun response(configurer: ResponseDsl.() -> Unit) {
response = ResponseDsl().apply(configurer).get()
}
fun input(input: InputDsl.() -> Unit) {
this.input = InputDsl().apply(input).get()
/**
* The input part of the contract.
* @param configurer lambda to configure the input message
*/
fun input(configurer: InputDsl.() -> Unit) {
input = InputDsl().apply(configurer).get()
}
fun outputMessage(outputMessage: OutputMessageDsl.() -> Unit) {
this.outputMessage = OutputMessageDsl().apply(outputMessage).get()
/**
* The output part of the contract.
* @param configurer lambda to configure the output message
*/
fun outputMessage(configurer: OutputMessageDsl.() -> Unit) {
outputMessage = OutputMessageDsl().apply(configurer).get()
}
private fun get(): Contract {

View File

@@ -24,7 +24,9 @@ import java.lang.IllegalStateException
import java.util.stream.Collectors
/**
* Class extension functions which make our lives easier.
* @author Tim Ysewyn
* @since 2.2.0
*/
infix fun Url.withQueryParameters(parameters: QueryParameters.() -> Unit) = apply {
queryParameters = QueryParameters().apply(parameters)

View File

@@ -19,7 +19,10 @@ package org.springframework.cloud.contract.spec.internal
import java.util.regex.Pattern
/**
* Matching strategy of dynamic parts of the body.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
open class BodyMatchersDsl {

View File

@@ -22,7 +22,10 @@ import java.nio.charset.Charset
import java.util.regex.Pattern
/**
* Composed out of common variables and funtions.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
open class CommonDsl {
@@ -145,8 +148,18 @@ open class CommonDsl {
*/
fun test(serverValue: Any?) = ServerDslProperty(serverValue)
/**
* Helper method to indicate an optional property.
* @param value the optional property value
* @return optional dsl property
*/
fun optional(value: Any?) = OptionalProperty(value)
/**
* Helper method to indicate that a method needs to be executed during testing.
* @param commandToExecute the name of the method to execute
* @return execution dsl property
*/
fun execute(commandToExecute: String) = ExecutionProperty(commandToExecute)
/**
@@ -165,16 +178,33 @@ open class CommonDsl {
fun file(relativePath: String, charset: Charset) = FromFileProperty(fileLocation(relativePath), String::class.java, charset)
/**
* Read file contents as bytes[].
* Read file contents as String with the given Charset.
* @param relativePath of the file to read
* @param charset to use for converting the bytes to String
* @return String file contents
*/
fun fileAsBytes(relativePath: String) = FromFileProperty(fileLocation(relativePath), ByteArray::class.java)
/**
* Read file contents as String using a default charset (mostly UTF-8).
* @param relativePath of the file to read
* @return Body DSL property with the content of the file as String
*/
fun bodyFromFile(relativePath: String) = bodyFromFile(relativePath, Charset.defaultCharset())
/**
* Read file contents as String using the provided file encoding.
* @param relativePath of the file to read
* @param charset of the file's encoding
* @return Body the body with the String as its content
*/
fun bodyFromFile(relativePath: String, charset: Charset) = Body(FromFileProperty(fileLocation(relativePath), String::class.java, charset))
/**
* Read file contents as bytes[].
* @param relativePath of the file to read
* @return Body the body with the byte array as its content
*/
fun bodyFromFileAsBytes(relativePath: String) = Body(FromFileProperty(fileLocation(relativePath), ByteArray::class.java))
/**

View File

@@ -17,7 +17,10 @@
package org.springframework.cloud.contract.spec.internal
/**
* Represents the cookies when sending/receiving HTTP traffic.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
open class CookiesDsl : CommonDsl() {

View File

@@ -19,7 +19,10 @@ package org.springframework.cloud.contract.spec.internal
import org.springframework.cloud.contract.spec.ContractTemplate
/**
* Helper class to reference the request body parameters.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
class FromRequestDsl {
@@ -52,7 +55,7 @@ class FromRequestDsl {
fun path() = FromRequestDslProperty(template.escapedPath())
/**
* nth value of a URL path (zero indexed) e.g. {{{ request.path.[2] }}}*.
* nth value of a URL path (zero indexed) e.g. {{{ request.path.[2] }}}.
* @param index path index
* @return dsl property
*/
@@ -74,7 +77,7 @@ class FromRequestDsl {
fun header(key: String, index: Int) = FromRequestDslProperty(template.escapedHeader(key, index))
/**
* Retruns the tempalte for retrieving the first value of a cookie with certain key.
* Returns the template for retrieving the first value of a cookie with certain key.
* @param key cookie key
* @return dsl property
*/
@@ -100,7 +103,7 @@ class FromRequestDsl {
fun rawUrl() = FromRequestDslProperty(template.url())
/**
* Unescaped First value of a query parameter e.g. request.query.search.
* Unescaped first value of a query parameter e.g. request.query.search.
* @param key query key
* @return dsl property
*/
@@ -122,14 +125,14 @@ class FromRequestDsl {
fun rawPath() = FromRequestDslProperty(template.path())
/**
* Unescaped nth value of a URL path (zero indexed) e.g. {{{ request.path.[2]. }}}*
* Unescaped nth value of a URL path (zero indexed) e.g. {{{ request.path.[2]. }}}.
* @param index path index
* @return dsl property
*/
fun rawPath(index: Int) = FromRequestDslProperty(template.path(index))
/**
* Unescaped First value of a request header e.g. request.headers.X-Request-Id.
* Unescaped first value of a request header e.g. request.headers.X-Request-Id.
* @param key header key
* @return dsl property
*/

View File

@@ -17,7 +17,10 @@
package org.springframework.cloud.contract.spec.internal
/**
* Represents the headers when sending/receiving HTTP traffic.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
open class HeadersDsl: CommonDsl() {

View File

@@ -19,18 +19,46 @@ package org.springframework.cloud.contract.spec.internal
import org.springframework.cloud.contract.spec.toDslProperty
/**
* Represents an input for messaging.
* The input can be a message or some action inside the application.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
@ContractDslMarker
class InputDsl : CommonDsl() {
private val delegate = Input()
/**
* Name of a destination from which message would come to trigger action in the
* system.
*/
var messageFrom: DslProperty<String>? = null
/**
* Function that needs to be executed to trigger action in the system.
*/
var triggeredBy: String? = null
/**
* The message headers part of the contract.
*/
var headers: Headers? = null
/**
* The contents of the incoming message.
*/
var messageBody: Input.BodyType? = null
/**
* Function that needs to be executed after the message has been received/processed by the system.
*/
var assertThat: String? = null
/**
* The body matchers part of the contract.
*/
var bodyMatchers: BodyMatchers? = null
fun messageFrom(messageFrom: String) = messageFrom.toDslProperty()

View File

@@ -23,7 +23,10 @@ import java.net.URLClassLoader.newInstance
import javax.script.ScriptEngineManager
/**
* Converter that will convert the Kotlin DSL to Java DSL.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
class KotlinContractConverter: ContractConverter<List<Contract>> {

View File

@@ -19,7 +19,10 @@ package org.springframework.cloud.contract.spec.internal
import org.springframework.cloud.contract.spec.toDslProperty
/**
* Represents the content when sending multipart HTTP requests.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
class MultipartDsl {

View File

@@ -19,17 +19,41 @@ package org.springframework.cloud.contract.spec.internal
import org.springframework.cloud.contract.spec.toDslProperty
/**
* Represents an output for messaging.
* Used for verifying the body and headers that are sent.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
@ContractDslMarker
class OutputMessageDsl : CommonDsl() {
private val delegate = OutputMessage()
/**
* Name of a destination to which a message should be sent.
*/
var sentTo: DslProperty<String>? = null
/**
* The message headers part of the contract.
*/
var headers: Headers? = null
/**
* Name of a destination from which message would come to trigger action in the
* system.
*/
var body: DslProperty<Any>? = null
/**
* Function that needs to be executed after the message has been sent out of the system.
*/
var assertThat: String? = null
/**
* The body matchers part of the contract.
*/
var bodyMatchers: ResponseBodyMatchers? = null
fun sentTo(sentTo: String) = sentTo.toDslProperty()

View File

@@ -22,20 +22,54 @@ import org.springframework.cloud.contract.spec.util.RegexpUtils
import java.util.regex.Pattern
/**
* Represents the request side of the HTTP communication.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
@ContractDslMarker
open class RequestDsl : CommonDsl() {
private val delegate = Request()
/**
* The HTTP method.
*/
var method: DslProperty<*>? = null
/**
* The URL to which the request will be sent.
*/
var url: Url? = null
/**
* The URL to which the request will be sent.
*/
var urlPath: UrlPath? = null
/**
* The HTTP headers which should be sent with the request.
*/
var headers: Headers? = null
/**
* The HTTP cookies which should be sent with the request.
*/
var cookies: Cookies? = null
/**
* The HTTP request body which should be sent.
*/
var body: Body? = null
/**
* The content that needs to be sent with a multipart HTTP request.
*/
var multipart: Multipart? = null
/**
* The HTTP request body matchers.
*/
var bodyMatchers: BodyMatchers? = null
fun method(method: String) = method.toDslProperty()

View File

@@ -17,7 +17,10 @@
package org.springframework.cloud.contract.spec.internal
/**
* Additional matchers for the response or output message part.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
class ResponseBodyMatchersDsl: BodyMatchersDsl() {

View File

@@ -22,20 +22,50 @@ import org.springframework.cloud.contract.spec.util.RegexpUtils
import java.util.regex.Pattern
/**
* Represents the response side of the HTTP communication.
*
* @author Tim Ysewyn
* @since 2.2.0
*/
@ContractDslMarker
class ResponseDsl : CommonDsl() {
private val delegate = Response()
var status: DslProperty<Any>? = null
var delay: DslProperty<Any>? = null
var headers: Headers? = null
var cookies: Cookies? = null
var body: Body? = null
var async: Boolean = false
var bodyMatchers: ResponseBodyMatchers? = null
/**
* The HTTP response status.
*/
var status: DslProperty<Any>? = null
/**
* The HTTP response delay in milliseconds.
*/
var delay: DslProperty<Any>? = null
/**
* The HTTP response headers.
*/
var headers: Headers? = null
/**
* The HTTP response cookies.
*/
var cookies: Cookies? = null
/**
* The HTTP response body.
*/
var body: Body? = null
/**
* Indicates asynchronous communication.
*/
var async: Boolean = false
/**
* The HTTP response body matchers.
*/
var bodyMatchers: ResponseBodyMatchers? = null
fun code(code: Int): DslProperty<Any> = code.toDslProperty()