Skip to content

Commit

Permalink
fix: Update RestPactRunner and MessagePactRunner to support V4 Pacts #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jun 27, 2023
1 parent 302013d commit 3af6b08
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 4 deletions.
21 changes: 21 additions & 0 deletions core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ sealed class V4Interaction(
}
}

/**
* returns true if the interaction is of the required type
*/
abstract fun isInteractionType(interactionType: V4InteractionType): Boolean

open class SynchronousHttp @JvmOverloads constructor(
key: String?,
description: String,
Expand Down Expand Up @@ -233,6 +238,9 @@ sealed class V4Interaction(
this.response.updateProperties(responseConfig)
}

override fun isInteractionType(interactionType: V4InteractionType) =
interactionType == V4InteractionType.SynchronousHTTP

override fun toMap(pactSpecVersion: PactSpecVersion): Map<String, *> {
val map = mutableMapOf(
"type" to V4InteractionType.SynchronousHTTP.toString(),
Expand Down Expand Up @@ -411,6 +419,9 @@ sealed class V4Interaction(
contents = contents.copy(metadata = metadata.toMutableMap())
return this
}

override fun isInteractionType(interactionType: V4InteractionType) =
interactionType == V4InteractionType.AsynchronousMessages
}

open class SynchronousMessages @Suppress("LongParameterList") @JvmOverloads constructor(
Expand Down Expand Up @@ -516,6 +527,9 @@ sealed class V4Interaction(
override fun isSynchronousMessages() = true

override fun asSynchronousMessages() = this

override fun isInteractionType(interactionType: V4InteractionType) =
interactionType == V4InteractionType.SynchronousMessages
}

companion object : KLogging() {
Expand Down Expand Up @@ -683,4 +697,11 @@ open class V4Pact @JvmOverloads constructor(
val pluginData = metadata["plugins"]
return pluginData is List<*> && pluginData.isNotEmpty()
}

/**
* Returns true if the Pact has interactions of the given type
*/
fun hasInteractionsOfType(interactionType: V4InteractionType): Boolean {
return interactions.any { it.asV4Interaction().isInteractionType(interactionType) }
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package au.com.dius.pact.provider.junit

import au.com.dius.pact.core.model.FilteredPact
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.core.model.messaging.MessagePact
import au.com.dius.pact.core.model.v4.V4InteractionType

/**
* Pact runner that only verifies message pacts
*/
open class MessagePactRunner(clazz: Class<*>) : PactRunner(clazz) {
override fun filterPacts(pacts: List<Pact>): List<Pact> {
return super.filterPacts(pacts).filter { pact ->
pact is MessagePact || (pact is FilteredPact && pact.pact is MessagePact)
isMessagePact(pact) || (pact is FilteredPact && isMessagePact(pact.pact))
}
}

private fun isMessagePact(pact: Pact) = pact is MessagePact ||
(pact is V4Pact && pact.hasInteractionsOfType(V4InteractionType.AsynchronousMessages))
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package au.com.dius.pact.provider.junit

import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.support.Utils
import au.com.dius.pact.core.support.expressions.DataType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package au.com.dius.pact.provider.junit
import au.com.dius.pact.core.model.FilteredPact
import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.core.model.v4.V4InteractionType

open class RestPactRunner(clazz: Class<*>) : PactRunner(clazz) {
override fun filterPacts(pacts: List<Pact>): List<Pact> {
return super.filterPacts(pacts).filter { pact ->
pact is RequestResponsePact || (pact is FilteredPact && pact.pact is RequestResponsePact)
isHttpPact(pact) || (pact is FilteredPact && isHttpPact(pact.pact))
}
}

private fun isHttpPact(pact: Pact) = pact is RequestResponsePact ||
(pact is V4Pact && pact.hasInteractionsOfType(V4InteractionType.SynchronousHTTP))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.RequestResponseInteraction
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.Response
import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.core.model.messaging.Message
import au.com.dius.pact.core.model.messaging.MessagePact
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify
Expand All @@ -16,6 +17,7 @@ import au.com.dius.pact.provider.junitsupport.loader.PactFilter
import au.com.dius.pact.provider.junitsupport.loader.PactFolder
import au.com.dius.pact.provider.junitsupport.target.Target
import au.com.dius.pact.provider.junitsupport.target.TestTarget
import spock.lang.Issue
import spock.lang.Specification

class MessagePactRunnerSpec extends Specification {
Expand Down Expand Up @@ -44,6 +46,13 @@ class MessagePactRunnerSpec extends Specification {
Target target
}

@Provider('test_provider_combined')
@PactFolder('pacts')
class V4TestClass {
@TestTarget
Target target
}

def setup() {
consumer = new au.com.dius.pact.core.model.Consumer('Consumer 1')
consumer2 = new au.com.dius.pact.core.model.Consumer('Consumer 2')
Expand Down Expand Up @@ -96,4 +105,17 @@ class MessagePactRunnerSpec extends Specification {
result.size() == 1
}

@Issue('#1692')
def 'supports V4 Pacts'() {
given:
MessagePactRunner pactRunner = new MessagePactRunner(V4TestClass)
def pactLoader = pactRunner.getPactSource(new org.junit.runners.model.TestClass(V4TestClass), null)

when:
def result = pactRunner.filterPacts(pactLoader.load('test_provider_combined'))

then:
result.size() == 1
result.first() instanceof V4Pact
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class PactRunnerSpec extends Specification {

}

@Provider('test_provider_combined')
@PactFolder('pacts')
class V4TestClass {
@TestTarget
Target target
}

static class PactLoaderWithConstructorParameter implements PactLoader {

private final Class clazz
Expand Down Expand Up @@ -265,6 +272,15 @@ class PactRunnerSpec extends Specification {
!runner.children.empty
}

@Issue('#1692')
def 'PactRunner supports V4 Pacts'() {
when:
new PactRunner(V4TestClass).run(new RunNotifier())

then:
notThrown(InitializationError)
}

@Provider('ExpectedName')
static class ProviderWithName { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.RequestResponseInteraction
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.Response
import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.core.model.messaging.Message
import au.com.dius.pact.core.model.messaging.MessagePact
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify
Expand All @@ -16,6 +17,7 @@ import au.com.dius.pact.provider.junitsupport.loader.PactFilter
import au.com.dius.pact.provider.junitsupport.loader.PactFolder
import au.com.dius.pact.provider.junitsupport.target.Target
import au.com.dius.pact.provider.junitsupport.target.TestTarget
import spock.lang.Issue
import spock.lang.Specification

class RestPactRunnerSpec extends Specification {
Expand Down Expand Up @@ -43,6 +45,13 @@ class RestPactRunnerSpec extends Specification {
Target target
}

@Provider('test_provider_combined')
@PactFolder('pacts')
class V4TestClass {
@TestTarget
Target target
}

def setup() {
consumer = new au.com.dius.pact.core.model.Consumer('Consumer 1')
consumer2 = new au.com.dius.pact.core.model.Consumer('Consumer 2')
Expand Down Expand Up @@ -95,4 +104,17 @@ class RestPactRunnerSpec extends Specification {
result.size() == 1
}

@Issue('#1692')
def 'supports V4 Pacts'() {
given:
RestPactRunner pactRunner = new RestPactRunner(V4TestClass)
def pactLoader = pactRunner.getPactSource(new org.junit.runners.model.TestClass(V4TestClass), null)

when:
def result = pactRunner.filterPacts(pactLoader.load('test_provider_combined'))

then:
result.size() == 1
result.first() instanceof V4Pact
}
}

0 comments on commit 3af6b08

Please sign in to comment.