From 3932b24569150fc9be779029ee93cdf95ce0bfc7 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Wed, 29 Jun 2022 13:55:05 +1000 Subject: [PATCH] feat(JUnit): allow pact loader to setup from the test class instead of just annotations --- docs/system-properties.md | 2 +- .../provider/junit5/PactJUnit5VerificationProvider.kt | 3 ++- .../pact/provider/junitsupport/loader/PactBroker.java | 6 ++++-- .../pact/provider/junitsupport/loader/PactLoader.java | 7 ++++++- .../kotlin/au/com/dius/pact/provider/ProviderUtils.kt | 9 +++++---- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/docs/system-properties.md b/docs/system-properties.md index 5fa5e0ae45..49510d8ba4 100644 --- a/docs/system-properties.md +++ b/docs/system-properties.md @@ -44,7 +44,7 @@ Theses are all the system properties used by Pact-JVM | pact.verification.reportDir | Verification (JUnit, JUnit 5) | Directory name | n | y | n | Sets the directory to write any configured verification reports to. | | pactfolder.path | Verification (JUnit, JUnit 5) | Directory name | n | y | n | Directory to fetch pacts from when using the folder loader. | | pact.verification.ignoreIoErrors | Verification (JUnit, JUnit 5) | true, false | n | y | n | When a test is annotated with @IgnoreNoPactsToVerify, any IO errors that occur while fetching the pacts will also be ignored . | -| pact.provider.name | Verification (JUnit 5) | string value | n | n | n | Sets the provider name to use when running the Pact verification tests. | +| pact.provider.name | Verification (JUnit 5) | string value | n | y | y | Sets the provider name to use when running the Pact verification tests. | | pact.provider.version | Verification | string value | n | n | n | Sets the provider version to use when publishing verification results. | | pact.provider.version.trimSnapshot | Verification | true, false | n | n | n | Enabling this will trim the Maven snapshot suffix off the Provider version. | | pact.provider.tag | Verification | Provider names | y | y | n | List of provider tags to use to tag the verification results with when published back to the Pact Broker. | diff --git a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt index 3459c55ea1..6d0ed05ebc 100644 --- a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt +++ b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt @@ -2,6 +2,7 @@ package au.com.dius.pact.provider.junit5 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 import au.com.dius.pact.core.support.expressions.SystemPropertyResolver @@ -56,7 +57,7 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon val serviceName = if (providerInfo.isPresent && providerInfo.get().value.isNotEmpty()) { ep.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 " + diff --git a/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactBroker.java b/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactBroker.java index c7f7c4b2a1..e8425b6613 100644 --- a/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactBroker.java +++ b/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactBroker.java @@ -50,7 +50,7 @@ * 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:}"; @@ -58,7 +58,9 @@ /** * 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:}", @@ -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:}"; diff --git a/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactLoader.java b/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactLoader.java index 0945b706ff..45b0c334de 100644 --- a/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactLoader.java +++ b/provider/src/main/java/au/com/dius/pact/provider/junitsupport/loader/PactLoader.java @@ -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 */ @@ -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) { }; } diff --git a/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderUtils.kt b/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderUtils.kt index 1b2e2d0571..8a4021168b 100644 --- a/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderUtils.kt +++ b/provider/src/main/kotlin/au/com/dius/pact/provider/ProviderUtils.kt @@ -2,7 +2,6 @@ package au.com.dius.pact.provider import au.com.dius.pact.core.model.DefaultPactReader import au.com.dius.pact.core.model.FileSource -import au.com.dius.pact.core.model.Interaction import au.com.dius.pact.provider.junitsupport.loader.PactLoader import au.com.dius.pact.provider.junitsupport.loader.PactSource import mu.KLogging @@ -134,13 +133,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) { @@ -169,5 +168,7 @@ object ProviderUtils : KLogging() { pactLoaderClass.createInstance() } } + pactLoader.initLoader(testClass) + return pactLoader } }