-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: renamed AmqpTarget to MessageTarget #383
- Loading branch information
Ronald Holshausen
committed
May 27, 2020
1 parent
218d516
commit c04f174
Showing
6 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/target/MessageTarget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package au.com.dius.pact.provider.junit.target | ||
|
||
import au.com.dius.pact.core.model.DirectorySource | ||
import au.com.dius.pact.core.model.Interaction | ||
import au.com.dius.pact.core.model.PactBrokerSource | ||
import au.com.dius.pact.core.model.PactSource | ||
import au.com.dius.pact.provider.ConsumerInfo | ||
import au.com.dius.pact.provider.IConsumerInfo | ||
import au.com.dius.pact.provider.IProviderInfo | ||
import au.com.dius.pact.provider.IProviderVerifier | ||
import au.com.dius.pact.provider.PactVerification | ||
import au.com.dius.pact.provider.ProviderInfo | ||
import au.com.dius.pact.provider.ProviderVerifier | ||
import au.com.dius.pact.provider.VerificationResult | ||
import au.com.dius.pact.provider.junitsupport.Provider | ||
import mu.KLogging | ||
import java.net.URLClassLoader | ||
import java.util.function.Function | ||
import java.util.function.Supplier | ||
|
||
/** | ||
* Out-of-the-box implementation of [Target], that run [Interaction] against message pact and verify response | ||
* By default it will scan all packages for annotated methods, but a list of packages can be provided to reduce | ||
* the performance cost | ||
* @param packagesToScan List of JVM packages | ||
*/ | ||
open class MessageTarget @JvmOverloads constructor( | ||
private val packagesToScan: List<String> = emptyList(), | ||
private val classLoader: ClassLoader? = null | ||
) : BaseTarget() { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
override fun testInteraction( | ||
consumerName: String, | ||
interaction: Interaction, | ||
source: PactSource, | ||
context: Map<String, Any> | ||
) { | ||
val provider = getProviderInfo(source) | ||
val consumer = consumerInfo(consumerName, source) | ||
val verifier = setupVerifier(interaction, provider, consumer, source) | ||
|
||
val result = verifier.verifyResponseByInvokingProviderMethods(provider, consumer, interaction, | ||
interaction.description, mutableMapOf()) | ||
reportTestResult(result, verifier) | ||
|
||
try { | ||
if (result is VerificationResult.Failed) { | ||
verifier.displayFailures(listOf(result)) | ||
throw AssertionError(verifier.generateErrorStringFromVerificationResult(listOf(result))) | ||
} | ||
} finally { | ||
verifier.finaliseReports() | ||
} | ||
} | ||
|
||
override fun setupVerifier( | ||
interaction: Interaction, | ||
provider: IProviderInfo, | ||
consumer: IConsumerInfo, | ||
pactSource: PactSource? | ||
): IProviderVerifier { | ||
val verifier = ProviderVerifier() | ||
verifier.projectClasspath = Supplier { | ||
logger.debug { "Classloader = ${this.classLoader}" } | ||
when (this.classLoader) { | ||
is URLClassLoader -> this.classLoader.urLs.asList() | ||
else -> emptyList() | ||
} | ||
} | ||
val defaultProviderMethodInstance = verifier.providerMethodInstance | ||
verifier.providerMethodInstance = Function { m -> | ||
if (m.declaringClass == testTarget.javaClass) { | ||
testTarget | ||
} else { | ||
defaultProviderMethodInstance.apply(m) | ||
} | ||
} | ||
|
||
setupReporters(verifier) | ||
|
||
verifier.initialiseReporters(provider) | ||
verifier.reportVerificationForConsumer(consumer, provider, pactSource) | ||
|
||
if (interaction.providerStates.isNotEmpty()) { | ||
for ((name) in interaction.providerStates) { | ||
verifier.reportStateForInteraction(name.toString(), provider, consumer, true) | ||
} | ||
} | ||
|
||
verifier.reportInteractionDescription(interaction) | ||
|
||
return verifier | ||
} | ||
|
||
override fun getProviderInfo(source: PactSource): ProviderInfo { | ||
val provider = testClass.getAnnotation(Provider::class.java) | ||
val providerInfo = ProviderInfo(provider.value) | ||
providerInfo.verificationType = PactVerification.ANNOTATED_METHOD | ||
providerInfo.packagesToScan = packagesToScan | ||
|
||
if (source is PactBrokerSource<*>) { | ||
val (_, _, _, pacts) = source | ||
providerInfo.consumers = pacts.entries.flatMap { e -> | ||
e.value.map { p -> ConsumerInfo(e.key.name, p) } | ||
}.toMutableList() | ||
} else if (source is DirectorySource<*>) { | ||
val (_, pacts) = source | ||
providerInfo.consumers = pacts.entries.map { e -> ConsumerInfo(e.value.consumer.name, e.value) }.toMutableList() | ||
} | ||
|
||
return providerInfo | ||
} | ||
|
||
companion object : KLogging() | ||
} |
6 changes: 3 additions & 3 deletions
6
...om/dius/pact/provider/junit/AmqpTest.java → ...dius/pact/provider/junit/MessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...pring/src/main/kotlin/au/com/dius/pact/provider/spring/target/SpringAwareMessageTarget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package au.com.dius.pact.provider.spring.target | ||
|
||
import au.com.dius.pact.core.model.Interaction | ||
import au.com.dius.pact.core.model.PactSource | ||
import au.com.dius.pact.provider.IConsumerInfo | ||
import au.com.dius.pact.provider.IProviderInfo | ||
import au.com.dius.pact.provider.IProviderVerifier | ||
import au.com.dius.pact.provider.junit.target.MessageTarget | ||
import org.springframework.beans.factory.BeanFactory | ||
import org.springframework.beans.factory.BeanFactoryAware | ||
import java.util.function.Function | ||
|
||
/** | ||
* Target for message verification that supports a spring application context. For each annotated method, the owning | ||
* bean will be looked up from the application context | ||
*/ | ||
open class SpringAwareMessageTarget : MessageTarget(), BeanFactoryAware { | ||
private lateinit var beanFactory: BeanFactory | ||
|
||
override fun setBeanFactory(beanFactory: BeanFactory) { | ||
this.beanFactory = beanFactory | ||
} | ||
|
||
override fun setupVerifier( | ||
interaction: Interaction, | ||
provider: IProviderInfo, | ||
consumer: IConsumerInfo, | ||
pactSource: PactSource? | ||
): IProviderVerifier { | ||
val verifier = super.setupVerifier(interaction, provider, consumer, pactSource) | ||
verifier.providerMethodInstance = Function { m -> beanFactory.getBean(m.declaringClass) } | ||
return verifier | ||
} | ||
} |