From 6b0e0708c83a9efc931a9bc53acd8fd9269cb4fb Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Thu, 18 Jun 2020 11:11:00 +1000 Subject: [PATCH] refactor: update Groovy pact builder to start using the Pact models --- .../pact/consumer/groovy/PactBuilder.groovy | 23 +++++++++---------- .../au/com/dius/pact/core/model/BasePact.kt | 4 ++-- .../pact/core/model/RequestResponsePact.kt | 4 ++-- .../pact/core/model/messaging/MessagePact.kt | 4 ++-- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/consumer/groovy/src/main/groovy/au/com/dius/pact/consumer/groovy/PactBuilder.groovy b/consumer/groovy/src/main/groovy/au/com/dius/pact/consumer/groovy/PactBuilder.groovy index d3ab9c7cef..92a857aa5d 100644 --- a/consumer/groovy/src/main/groovy/au/com/dius/pact/consumer/groovy/PactBuilder.groovy +++ b/consumer/groovy/src/main/groovy/au/com/dius/pact/consumer/groovy/PactBuilder.groovy @@ -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 = [] @@ -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 } @@ -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 } @@ -101,7 +100,7 @@ class PactBuilder extends GroovyBuilder { */ PactBuilder uponReceiving(String requestDescription) { buildInteractions() - this.requestDescription = requestDescription + this.currentInteraction = new RequestResponseInteraction(requestDescription) requestState = true this } @@ -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)) : @@ -256,7 +255,7 @@ class PactBuilder extends GroovyBuilder { ' `fruits eachLike(1)` or `id = integer()`' ) } - setupBody(body, options) + setupRequestOrResponse(body, options) this } @@ -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 } @@ -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) @@ -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, diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/BasePact.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/BasePact.kt index 400112396f..b0756dcc60 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/BasePact.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/BasePact.kt @@ -14,8 +14,8 @@ import kotlin.reflect.full.memberProperties * Base Pact class */ abstract class BasePact @JvmOverloads constructor( - override val consumer: Consumer, - override val provider: Provider, + override var consumer: Consumer, + override var provider: Provider, open val metadata: Map = DEFAULT_METADATA, override val source: PactSource = UnknownPactSource ) : Pact { diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/RequestResponsePact.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/RequestResponsePact.kt index ad08b17b8f..c0ac177b52 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/RequestResponsePact.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/RequestResponsePact.kt @@ -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 = listOf(), override val metadata: Map = DEFAULT_METADATA, override val source: PactSource = UnknownPactSource diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt index ade697e5d7..c8c5dbe321 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt @@ -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 = mutableListOf(), override val metadata: Map = DEFAULT_METADATA, override val source: PactSource = UnknownPactSource