Skip to content

Commit

Permalink
refactor: update Groovy pact builder to start using the Pact models
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 18, 2020
1 parent 752cdfc commit 6b0e070
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ import static au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest
@SuppressWarnings('PropertyName')
class PactBuilder extends GroovyBuilder {

Consumer consumer
Provider provider
RequestResponsePact pact = new RequestResponsePact(new Provider(), new Consumer())
Integer port = 0
String requestDescription
RequestResponseInteraction currentInteraction
List requestData = []
List responseData = []
List interactions = []
Expand All @@ -53,7 +52,7 @@ class PactBuilder extends GroovyBuilder {
* @param consumer consumer name
*/
PactBuilder serviceConsumer(String consumer) {
this.consumer = new Consumer(consumer)
this.pact.consumer = new Consumer(consumer)
this
}

Expand All @@ -62,7 +61,7 @@ class PactBuilder extends GroovyBuilder {
* @param provider provider name
*/
PactBuilder hasPactWith(String provider) {
this.provider = new Provider(provider)
this.pact.provider = new Provider(provider)
this
}

Expand Down Expand Up @@ -101,7 +100,7 @@ class PactBuilder extends GroovyBuilder {
*/
PactBuilder uponReceiving(String requestDescription) {
buildInteractions()
this.requestDescription = requestDescription
this.currentInteraction = new RequestResponseInteraction(requestDescription)
requestState = true
this
}
Expand All @@ -120,7 +119,7 @@ class PactBuilder extends GroovyBuilder {
def requestBody = requestData[i].body instanceof String ? requestData[i].body.bytes : requestData[i].body
def responseBody = responseData[i].body instanceof String ? responseData[i].body.bytes : responseData[i].body
interactions << new RequestResponseInteraction(
requestDescription,
this.currentInteraction.description,
providerStates,
new Request(requestData[i].method ?: 'get', path, query, headers,
requestData[i].containsKey(BODY) ? OptionalBody.body(requestBody, contentType(headers)) :
Expand Down Expand Up @@ -256,7 +255,7 @@ class PactBuilder extends GroovyBuilder {
' `fruits eachLike(1)` or `id = integer()`'
)
}
setupBody(body, options)
setupRequestOrResponse(body, options)
this
}

Expand All @@ -270,7 +269,7 @@ class PactBuilder extends GroovyBuilder {
PactBuilder withBody(Map options = [:], List array) {
def body = new PactBodyBuilder(mimetype: options.mimeType, prettyPrintBody: options.prettyPrint)
body.bodyRepresentation = body.build(array)
setupBody(body, options)
setupRequestOrResponse(body, options)
this
}

Expand All @@ -284,11 +283,11 @@ class PactBuilder extends GroovyBuilder {
PactBuilder withBody(Map options = [:], LikeMatcher matcher) {
def body = new PactBodyBuilder(mimetype: options.mimetype, prettyPrintBody: options.prettyPrint)
body.bodyRepresentation = body.build(matcher)
setupBody(body, options)
setupRequestOrResponse(body, options)
this
}

private setupBody(PactBodyBuilder body, Map options) {
private setupRequestOrResponse(PactBodyBuilder body, Map options) {
if (requestState) {
requestData.last().body = body.body
requestData.last().matchers.addCategory(body.matchers)
Expand Down Expand Up @@ -325,7 +324,7 @@ class PactBuilder extends GroovyBuilder {
@CompileStatic
PactVerificationResult runTest(Map options = [:], Closure closure) {
buildInteractions()
def pact = new RequestResponsePact(provider, consumer, interactions)
this.pact.interactions = interactions

def pactVersion = options.specificationVersion ?: PactSpecVersion.V3
MockProviderConfig config = MockProviderConfig.httpConfig(LOCALHOST, port ?: 0, pactVersion as PactSpecVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import kotlin.reflect.full.memberProperties
* Base Pact class
*/
abstract class BasePact<I : Interaction> @JvmOverloads constructor(
override val consumer: Consumer,
override val provider: Provider,
override var consumer: Consumer,
override var provider: Provider,
open val metadata: Map<String, Any?> = DEFAULT_METADATA,
override val source: PactSource = UnknownPactSource
) : Pact<I> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import au.com.dius.pact.core.support.jsonObject
* Pact between a consumer and a provider
*/
class RequestResponsePact @JvmOverloads constructor(
override val provider: Provider,
override val consumer: Consumer,
override var provider: Provider,
override var consumer: Consumer,
interactions: List<RequestResponseInteraction> = listOf(),
override val metadata: Map<String, Any?> = DEFAULT_METADATA,
override val source: PactSource = UnknownPactSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import java.io.File
* Pact for a sequences of messages
*/
class MessagePact @JvmOverloads constructor (
override val provider: Provider,
override val consumer: Consumer,
override var provider: Provider,
override var consumer: Consumer,
var messages: MutableList<Message> = mutableListOf(),
override val metadata: Map<String, Any?> = DEFAULT_METADATA,
override val source: PactSource = UnknownPactSource
Expand Down

0 comments on commit 6b0e070

Please sign in to comment.