Skip to content

Commit

Permalink
fix(regression): Upgrading au.com.dius.pact.provider:junit5 from 4.3.…
Browse files Browse the repository at this point in the history
…15 to 4.3.16 results in compilation failure #1630
  • Loading branch information
rholshausen committed Jan 3, 2023
1 parent 51c076f commit be8d57d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ open class PactRunner(private val clazz: Class<*>) : ParentRunner<InteractionRun
"Provider name should be specified by using ${Provider::class.java.simpleName} annotation"
)
logger.debug { "Found annotation $providerInfo" }
val serviceName = ep.parseExpression(providerInfo.value, DataType.STRING, valueResolver)?.toString()
val serviceName = if (providerInfo.value.isEmpty()) {
Utils.lookupEnvironmentValue("pact.provider.name")
} else {
ep.parseExpression(providerInfo.value, DataType.STRING, valueResolver)?.toString()
}
if (serviceName.isNullOrEmpty()) {
throw InitializationError(
"Provider name specified by ${Provider::class.java.simpleName} annotation is null or empty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,54 @@ class PactRunnerSpec extends Specification {
then:
!runner.children.empty
}

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

@Provider('${provider.name}')
static class ProviderWithExpression { }

@Provider
static class ProviderWithNoName { }

@Issue('#1630')
@RestoreSystemProperties
def 'lookup provider info - #clazz.simpleName'() {
given:
System.setProperty('provider.name', 'ExpectedName')
System.setProperty('pact.provider.name', 'ExpectedName')

expect:
new PactRunner(clazz).lookupProviderInfo().second == 'ExpectedName'

where:
clazz << [ ProviderWithName, ProviderWithExpression, ProviderWithNoName ]
}

@Consumer('ExpectedName')
static class ConsumerWithName { }

@Consumer('${consumer.name}')
static class ConsumerWithExpression { }

@Consumer
static class ConsumerWithNoName { }

@Issue('#1630')
@RestoreSystemProperties
def 'lookup consumer info - #clazz.simpleName'() {
given:
System.setProperty('consumer.name', 'ExpectedName')
System.setProperty('pact.consumer.name', 'ExpectedName')

expect:
new PactRunner(clazz).lookupConsumerInfo().second == name

where:

clazz | name
ConsumerWithName | 'ExpectedName'
ConsumerWithExpression | 'ExpectedName'
ConsumerWithNoName | ''
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,14 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon

private fun resolvePactSources(context: ExtensionContext): Pair<List<PactVerificationExtension>, String> {
var description = ""
val providerInfo = AnnotationSupport.findAnnotation(context.requiredTestClass, Provider::class.java)
val serviceName = if (providerInfo.isPresent && providerInfo.get().value.isNotEmpty()) {
ep.parseExpression(providerInfo.get().value, DataType.STRING)?.toString()
} else {
Utils.lookupEnvironmentValue("pact.provider.name")
}
val serviceName = lookupProviderName(context, ep)
if (serviceName.isNullOrEmpty()) {
throw UnsupportedOperationException("Provider name should be specified by using either " +
"@${Provider::class.java.name} annotation or the 'pact.provider.name' system property")
}
description += "Provider: $serviceName"

val consumerInfo = AnnotationSupport.findAnnotation(context.requiredTestClass, Consumer::class.java)
val consumerName = ep.parseExpression(consumerInfo.orElse(null)?.value, DataType.STRING)?.toString()
val consumerName = lookupConsumerName(context, ep)
if (consumerName.isNotEmpty()) {
description += "\nConsumer: $consumerName"
}
Expand Down Expand Up @@ -158,5 +152,19 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon
return AnnotationSupport.isAnnotated(context.requiredTestClass, Provider::class.java)
}

companion object : KLogging()
companion object : KLogging() {
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()
}

fun lookupProviderName(context: ExtensionContext, ep: ExpressionParser): String? {
val providerInfo = AnnotationSupport.findAnnotation(context.requiredTestClass, Provider::class.java)
return if (providerInfo.isPresent && providerInfo.get().value.isNotEmpty()) {
ep.parseExpression(providerInfo.get().value, DataType.STRING)?.toString()
} else {
Utils.lookupEnvironmentValue("pact.provider.name")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.expressions.ExpressionParser
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
import au.com.dius.pact.provider.junitsupport.Consumer
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify
Expand Down Expand Up @@ -396,4 +397,62 @@ class PactVerificationInvocationContextProviderSpec extends Specification {
noExceptionThrown()
result.empty
}
@Provider('ExpectedName')
static class ProviderWithName { }
@Provider('${provider.name}')
static class ProviderWithExpression { }
@Provider
static class ProviderWithNoName { }
@Issue('#1630')
@RestoreSystemProperties
def 'lookup provider info - #clazz.simpleName'() {
given:
System.setProperty('provider.name', 'ExpectedName')
System.setProperty('pact.provider.name', 'ExpectedName')
def context = Mock(ExtensionContext) {
getRequiredTestClass() >> clazz
}
def ep = new ExpressionParser()
expect:
PactVerificationInvocationContextProvider.Companion.newInstance().lookupProviderName(context, ep) == 'ExpectedName'
where:
clazz << [ ProviderWithName, ProviderWithExpression, ProviderWithNoName ]
}
@Consumer('ExpectedName')
static class ConsumerWithName { }
@Consumer('${consumer.name}')
static class ConsumerWithExpression { }
@Consumer
static class ConsumerWithNoName { }
@Issue('#1630')
@RestoreSystemProperties
def 'lookup consumer info - #clazz.simpleName'() {
given:
System.setProperty('consumer.name', 'ExpectedName')
System.setProperty('pact.consumer.name', 'ExpectedName')
def context = Mock(ExtensionContext) {
getRequiredTestClass() >> clazz
}
def ep = new ExpressionParser()
expect:
PactVerificationInvocationContextProvider.Companion.newInstance().lookupConsumerName(context, ep) == name
where:
clazz | name
ConsumerWithName | 'ExpectedName'
ConsumerWithExpression | 'ExpectedName'
ConsumerWithNoName | ''
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ annotation class Consumer(
/**
* @return consumer name for pact test running
*/
val value: String
val value: String = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ annotation class Provider(
/**
* @return provider name for pact test running
*/
val value: String
val value: String = ""
)

0 comments on commit be8d57d

Please sign in to comment.