Skip to content

Commit

Permalink
chore: Remove use of some deprecated items
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Dec 3, 2024
1 parent bc3815c commit 3c780af
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ open class InteractionRunner(
private val results = ConcurrentHashMap<String, Pair<VerificationResult, IProviderVerifier>>()
private val testContext = ConcurrentHashMap<String, Any>()
private val childDescriptions = ConcurrentHashMap<String, Description>()
private val descriptionGenerator = DescriptionGenerator(testClass, pact)
private val descriptionGenerator = DescriptionGenerator(testClass, pactSource, pact.consumer.name)
protected var propertyResolver: ValueResolver = SystemPropertyResolver

var testResultAccumulator: TestResultAccumulator = DefaultTestResultAccumulator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package au.com.dius.pact.provider.junit.descriptions

import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.model.PactSource
import au.com.dius.pact.provider.junitsupport.TestDescription
import org.junit.runner.Description
Expand All @@ -12,8 +11,6 @@ import org.junit.runners.model.TestClass
*/
class DescriptionGenerator(
private val testClass: TestClass,
@Deprecated("Pass the pact source and consumer name in")
private val pact: Pact?,
private val pactSource: PactSource? = null,
private val consumerName: String? = null
) {
Expand All @@ -26,7 +23,7 @@ class DescriptionGenerator(
* @param interaction the Interaction under test
*/
fun generate(interaction: Interaction): Description {
val generator = TestDescription(interaction, pactSource ?: pact?.source, consumerName, pact?.consumer)
val generator = TestDescription(interaction, pactSource, consumerName)
return Description.createTestDescription(testClass.javaClass, generator.generateDescription())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import au.com.dius.pact.provider.ProviderVerifier
import au.com.dius.pact.provider.VerificationResult
import au.com.dius.pact.provider.junit.descriptions.DescriptionGenerator
import au.com.dius.pact.provider.junitsupport.Provider
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import java.net.URLClassLoader
import java.util.function.Function
import java.util.function.Supplier

private val logger = KotlinLogging.logger {}

/**
* 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
Expand All @@ -43,13 +45,13 @@ open class MessageTarget @JvmOverloads constructor(
) {
// TODO: Require the plugin config here
val result = verifier.verifyResponseByInvokingProviderMethods(provider, consumer, interaction,
interaction.description, mutableMapOf(), false)
interaction.description, mutableMapOf(), false, emptyMap())
reportTestResult(result, verifier)

try {
if (result is VerificationResult.Failed) {
verifier.displayFailures(listOf(result))
val descriptionGenerator = DescriptionGenerator(testClass, null, source, consumerName)
val descriptionGenerator = DescriptionGenerator(testClass, source, consumerName)
val description = descriptionGenerator.generate(interaction).methodName
throw AssertionError(description + verifier.generateErrorStringFromVerificationResult(listOf(result)))
}
Expand Down Expand Up @@ -116,6 +118,4 @@ open class MessageTarget @JvmOverloads constructor(
}

override fun validForInteraction(interaction: Interaction) = interaction.isAsynchronousMessage()

companion object : KLogging()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package au.com.dius.pact.provider.junit.descriptions

import au.com.dius.pact.core.model.RequestResponseInteraction
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.BrokerUrlSource
import au.com.dius.pact.core.model.DirectorySource
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.Response
import au.com.dius.pact.core.model.Provider
import au.com.dius.pact.core.model.Consumer
import au.com.dius.pact.core.model.ProviderState
import au.com.dius.pact.core.pactbroker.PactBrokerResult
import au.com.dius.pact.provider.junit.target.HttpTarget
Expand All @@ -34,16 +31,10 @@ class DescriptionGeneratorTest extends Specification {
def 'when BrokerUrlSource tests description includes tag if present'() {
def interaction = new RequestResponseInteraction('Interaction 1',
[ new ProviderState('Test State') ], new Request(), new Response())
def pact = new RequestResponsePact(
new Provider(),
new Consumer('the-consumer-name'),
[ interaction ],
[:],
new BrokerUrlSource('url', 'url', [:], [:], tag)
)
def source = new BrokerUrlSource('url', 'url', [:], [:], tag)

expect:
def generator = new DescriptionGenerator(clazz, pact, null, null)
def generator = new DescriptionGenerator(clazz, source, 'the-consumer-name')
description == generator.generate(interaction).methodName

where:
Expand All @@ -56,15 +47,10 @@ class DescriptionGeneratorTest extends Specification {
def 'when non broker pact source tests name are built correctly'() {
def interaction = new RequestResponseInteraction('Interaction 1',
[ new ProviderState('Test State') ], new Request(), new Response())
def pact = new RequestResponsePact(new Provider(),
new Consumer(),
[ interaction ],
[:],
new DirectorySource(Mock(File))
)
def source = new DirectorySource(Mock(File))

expect:
def generator = new DescriptionGenerator(clazz, pact, null, null)
def generator = new DescriptionGenerator(clazz, source, 'consumer')
'consumer - Upon Interaction 1 ' == generator.generate(interaction).methodName
}

Expand All @@ -75,9 +61,7 @@ class DescriptionGeneratorTest extends Specification {
[ new ProviderState('Test State') ], new Request(), new Response())
def pactSource = new BrokerUrlSource('url', 'url', [:], [:], 'master',
new PactBrokerResult('test', 'test', 'test', [], [], pending == 'enabled', null, false, true))
def pact = new RequestResponsePact(new Provider(), new Consumer('the-consumer-name'), [ interaction ],
[:], pactSource)
def generator = new DescriptionGenerator(clazz, pact, null, null)
def generator = new DescriptionGenerator(clazz, pactSource, 'the-consumer-name')

expect:
description == generator.generate(interaction).methodName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import au.com.dius.pact.provider.junitsupport.StateChangeAction
import com.github.restdriver.clientdriver.ClientDriverRule
import com.github.restdriver.clientdriver.RestClientDriver.giveEmptyResponse
import com.github.restdriver.clientdriver.RestClientDriver.onRequestTo
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.equalTo
Expand All @@ -19,6 +19,8 @@ import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.runner.RunWith

private val logger = KotlinLogging.logger {}

@RunWith(PactRunner::class)
@Provider("providerMultipleStates")
@PactFolder("pacts")
Expand All @@ -40,71 +42,71 @@ class MultipleStatesContractTest {
fun state1() {
// Prepare service before interaction that require "default" state
// ...
logger.info("Now service in state1")
logger.info { { "Now service in state1" } }
executedStates.add("state 1")
}

@State("state 2")
fun toSecondState(params: Map<*, *>) {
// Prepare service before interaction that require "state 2" state
// ...
logger.info("Now service in 'state 2' state: $params")
logger.info { "Now service in 'state 2' state: $params" }
executedStates.add("state 2")
}

@State("a gateway account with external id exists")
fun gatewayAccount(params: Map<*, *>) {
logger.info("Now service in 'gateway account' state: $params")
logger.info { "Now service in 'gateway account' state: $params" }
executedStates.add("a gateway account with external id exists")
}

@State("a confirmed mandate exists")
fun confirmedMandate(params: Map<*, *>) {
logger.info("Now service in 'confirmed mandate' state: $params")
logger.info { "Now service in 'confirmed mandate' state: $params" }
executedStates.add("a confirmed mandate exists")
}

@State("something else exists")
fun somethingElse() {
logger.info("Now service in 'somethingElse' state")
logger.info { "Now service in 'somethingElse' state" }
executedStates.add("something else exists")
}

@State("state 1", action = StateChangeAction.TEARDOWN)
fun state1Teardown() {
// Prepare service before interaction that require "default" state
// ...
logger.info("Now service in state1 Teardown")
logger.info { "Now service in state1 Teardown" }
executedStates.add("state 1 Teardown")
}

@State("state 2", action = StateChangeAction.TEARDOWN)
fun toSecondStateTeardown(params: Map<*, *>) {
// Prepare service before interaction that require "state 2" state
// ...
logger.info("Now service in 'state 2' Teardown state: $params")
logger.info { "Now service in 'state 2' Teardown state: $params" }
executedStates.add("state 2 Teardown")
}

@State("a gateway account with external id exists", action = StateChangeAction.TEARDOWN)
fun gatewayAccountTeardown(params: Map<*, *>) {
logger.info("Now service in 'gateway account' Teardown state: $params")
logger.info { "Now service in 'gateway account' Teardown state: $params" }
executedStates.add("a gateway account with external id exists Teardown")
}

@State("a confirmed mandate exists", action = StateChangeAction.TEARDOWN)
fun confirmedMandateTeardown(params: Map<*, *>) {
logger.info("Now service in 'confirmed mandate' Teardown state: $params")
logger.info { "Now service in 'confirmed mandate' Teardown state: $params" }
executedStates.add("a confirmed mandate exists Teardown")
}

@State("something else exists", action = StateChangeAction.TEARDOWN)
fun somethingElseTeardown() {
logger.info("Now service in 'somethingElse' Teardown state")
logger.info { "Now service in 'somethingElse' Teardown state" }
executedStates.add("something else exists Teardown")
}

companion object : KLogging() {
companion object {
@ClassRule
@JvmField
val embeddedService = ClientDriverRule(8332)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import au.com.dius.pact.provider.junitsupport.Provider
import au.com.dius.pact.provider.junitsupport.State
import au.com.dius.pact.provider.junitsupport.loader.NoPactsFoundException
import au.com.dius.pact.provider.junitsupport.loader.PactLoader
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.TestTemplateInvocationContext
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider
Expand All @@ -31,6 +31,7 @@ import java.io.IOException
import java.util.stream.Stream

val namespace: ExtensionContext.Namespace = ExtensionContext.Namespace.create("pact-jvm")
private val logger = KotlinLogging.logger {}

/**
* Main TestTemplateInvocationContextProvider for JUnit 5 Pact verification tests. This class needs to be applied to
Expand Down Expand Up @@ -152,7 +153,7 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon
return AnnotationSupport.isAnnotated(context.requiredTestClass, Provider::class.java)
}

companion object : KLogging() {
companion object {
fun lookupConsumerName(context: ExtensionContext, ep: ExpressionParser): String? {
val consumerInfo = AnnotationSupport.findAnnotation(context.requiredTestClass, Consumer::class.java)
return ep.parseExpression(consumerInfo.orElse(null)?.value, DataType.STRING)?.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ data class PactVerificationContext @JvmOverloads constructor(
} else {
UnknownPactSource
}
val description = TestDescription(interaction, source, null, consumer.toPactConsumer())
val description = TestDescription(interaction, source, consumer.name)
throw AssertionError(description.generateDescription() +
verifier!!.generateErrorStringFromVerificationResult(testExecutionResult))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import au.com.dius.pact.provider.VerificationFailureType
import au.com.dius.pact.provider.VerificationResult
import au.com.dius.pact.provider.junitsupport.VerificationReports
import au.com.dius.pact.provider.reporters.ReporterManager
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import org.apache.hc.core5.http.ClassicHttpRequest
import org.apache.hc.core5.http.HttpRequest
import org.junit.jupiter.api.extension.AfterTestExecutionCallback
Expand All @@ -35,6 +35,8 @@ import org.junit.jupiter.api.extension.TestTemplateInvocationContext
import org.junit.platform.commons.support.AnnotationSupport
import java.io.File

private val logger = KotlinLogging.logger {}

/**
* JUnit 5 test extension class used to inject parameters and execute the test for a Pact interaction.
*/
Expand Down Expand Up @@ -243,6 +245,4 @@ open class PactVerificationExtension(
}
}
}

companion object : KLogging()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import au.com.dius.pact.provider.junitsupport.IgnoreMissingStateChange
import au.com.dius.pact.provider.junitsupport.MissingStateChangeMethod
import au.com.dius.pact.provider.junitsupport.State
import au.com.dius.pact.provider.junitsupport.StateChangeAction
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import org.junit.jupiter.api.extension.AfterTestExecutionCallback
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback
import org.junit.jupiter.api.extension.ExtensionContext
Expand All @@ -22,6 +22,8 @@ import org.junit.platform.commons.support.HierarchyTraversalMode
import org.junit.platform.commons.support.ReflectionSupport
import java.lang.reflect.Method

private val logger = KotlinLogging.logger {}

/**
* JUnit 5 test extension class for executing state change callbacks
*/
Expand Down Expand Up @@ -151,6 +153,4 @@ class PactVerificationStateChangeExtension(
private fun ignoreMissingStateChangeMethod(testClass: Class<*>): Boolean {
return ProviderUtils.findAnnotation(testClass, IgnoreMissingStateChange::class.java) != null
}

companion object : KLogging()
}
Loading

0 comments on commit 3c780af

Please sign in to comment.