Skip to content

Commit

Permalink
feat(JUnit): allow pact loader to setup from the test class instead o…
Browse files Browse the repository at this point in the history
…f just annotations
  • Loading branch information
uglyog committed Aug 2, 2022
1 parent afeca40 commit f54ee01
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
61 changes: 61 additions & 0 deletions docs/system-properties.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion provider/gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {
testImplementation "junit:junit:${project.junitVersion}"
testImplementation 'org.mockito:mockito-core:2.28.2'
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:${project.junit5Version}"
testImplementation('org.spockframework:spock-core:2.0-groovy-3.0') {
testImplementation('org.spockframework:spock-core:2.0-groovy-2.5') {
exclude group: 'org.codehaus.groovy'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import au.com.dius.pact.provider.ProviderVerifier
import org.gradle.api.GradleScriptException
import org.gradle.api.Task
import org.gradle.api.tasks.GradleBuild
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction

/**
* Task to verify a pact against a provider
*/
class PactVerificationTask extends PactVerificationBaseTask {
@Internal
IProviderVerifier verifier = new ProviderVerifier()
@Internal
GradleProviderInfo providerToVerify

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ open class GradleProviderInfo(override var name: String, val project: Project) :
return try {
provider.hasPactsFromPactBrokerWithSelectorsV2(options, pactBrokerUrl, selectors)
} catch (e: Exception) {
val verifyTaskName = PACT_VERIFY.lowercase()
if (project.gradle.startParameter.taskNames.any { it.lowercase().contains(verifyTaskName) }) {
val verifyTaskName = PACT_VERIFY.toLowerCase()
if (project.gradle.startParameter.taskNames.any { it.toLowerCase().contains(verifyTaskName) }) {
logger.error(e) { "Failed to access Pact Broker" }
throw e
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package au.com.dius.pact.provider.junit5
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.pactbroker.NotFoundHalResponse
import au.com.dius.pact.core.support.Utils
import au.com.dius.pact.core.support.expressions.DataType
import au.com.dius.pact.core.support.expressions.ExpressionParser.parseExpression
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
Expand Down Expand Up @@ -55,7 +56,7 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon
val serviceName = if (providerInfo.isPresent && providerInfo.get().value.isNotEmpty()) {
parseExpression(providerInfo.get().value, DataType.STRING)?.toString()
} else {
System.getProperty("pact.provider.name")
Utils.lookupEnvironmentValue("pact.provider.name")
}
if (serviceName.isNullOrEmpty()) {
throw UnsupportedOperationException("Provider name should be specified by using either " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@
* Tags to use to fetch pacts for, defaults to `latest`
* If you set the tags through the `pactbroker.tags` system property, separate the tags by commas
*
* @deprecated Use {@link #consumerVersionSelectors} instead
* @deprecated Use consumerVersionSelectors method or pactbroker.consumerversionselectors property instead
*/
@Deprecated
String[] tags() default "${pactbroker.tags:}";

/**
* Consumer version selectors to fetch pacts for, defaults to latest version
* If you set the version selector tags or latest fields through system properties, separate values by commas
* @deprecated Use consumerVersionSelectors method or pactbroker.consumerversionselectors property instead
*/
@Deprecated
VersionSelector[] consumerVersionSelectors() default @VersionSelector(
tag = "${pactbroker.consumerversionselectors.tags:}",
latest = "${pactbroker.consumerversionselectors.latest:}",
Expand All @@ -69,7 +71,7 @@ VersionSelector[] consumerVersionSelectors() default @VersionSelector(
* Consumers to fetch pacts for, defaults to all consumers
* If you set the consumers through the `pactbroker.consumers` system property, separate the consumers by commas
*
* @deprecated Use {@link #consumerVersionSelectors} instead
* @deprecated Use consumerVersionSelectors method or pactbroker.consumerversionselectors property instead
*/
@Deprecated
String[] consumers() default "${pactbroker.consumers:}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface PactLoader {
PactSource getPactSource();

/**
* Sets the value resolver to use to resolve property expressions. By default a system property resolver will be used.
* Sets the value resolver to use to resolve property expressions. By default, a system property resolver will be used.
*
* @param valueResolver Value Resolver
*/
Expand All @@ -40,4 +40,9 @@ default void setValueResolver(ValueResolver valueResolver) { }
* Enables pending pact feature
*/
default void enablePendingPacts(boolean flag) { };

/**
* Supports additional initialisation using the test class
*/
default void initLoader(Class testClass) { };
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ object ProviderUtils : KLogging() {
return result
}

fun instantiatePactLoader(pactSource: PactSource, clazz: Class<*>, annotation: Annotation?): PactLoader {
fun instantiatePactLoader(pactSource: PactSource, testClass: Class<*>, annotation: Annotation?): PactLoader {
val pactLoaderClass = pactSource.value
return try {
val pactLoader = try {
// Checks if there is a constructor with one argument of type Class.
val constructorWithClass = pactLoaderClass.java.getDeclaredConstructor(Class::class.java)
constructorWithClass.isAccessible = true
constructorWithClass.newInstance(clazz)
constructorWithClass.newInstance(testClass)
} catch (e: NoSuchMethodException) {
logger.debug { "Pact source does not have a constructor with one argument of type Class" }
if (annotation != null) {
Expand Down Expand Up @@ -169,5 +169,7 @@ object ProviderUtils : KLogging() {
pactLoaderClass.createInstance()
}
}
pactLoader.initLoader(testClass)
return pactLoader
}
}

0 comments on commit f54ee01

Please sign in to comment.