Skip to content

Commit

Permalink
refactor: Consumer Version Selectors method does not need a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
uglyog committed Aug 2, 2022
1 parent 743ac4b commit 17e51e3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1573,33 +1573,33 @@ class PactBrokerLoaderSpec extends Specification {
@SuppressWarnings('UnusedMethodParameter')
static class IncorrectTypesOnSelectorMethod2 {
@au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors
int consumerVersionSelectors(SelectorBuilder builder) { 0 }
int consumerVersionSelectors() { 0 }
}

@SuppressWarnings(['UnusedPrivateMethod', 'UnusedPrivateMethodParameter'])
static class IncorrectScopeOnSelectorMethod {
@au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors
private SelectorBuilder consumerVersionSelectors(SelectorBuilder builder) { null }
private SelectorBuilder consumerVersionSelectors() { null }
}

static class CorrectSelectorMethod implements IConsumerVersionSelectors {
@au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors
SelectorBuilder consumerVersionSelectors(SelectorBuilder builder) {
builder.environment('CorrectSelectorMethod')
SelectorBuilder consumerVersionSelectors() {
new SelectorBuilder().environment('CorrectSelectorMethod')
}
}

static class CorrectSelectorMethod2 {
@au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors
List<ConsumerVersionSelectors> consumerVersionSelectors(SelectorBuilder builder) {
builder.environment('CorrectSelectorMethod2').build()
List<ConsumerVersionSelectors> consumerVersionSelectors() {
new SelectorBuilder().environment('CorrectSelectorMethod2').build()
}
}

static class CorrectSelectorMethod3 {
@au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors
static List<ConsumerVersionSelectors> consumerVersionSelectors(SelectorBuilder builder) {
builder.environment('CorrectSelectorMethod3').build()
static List<ConsumerVersionSelectors> consumerVersionSelectors() {
new SelectorBuilder().environment('CorrectSelectorMethod3').build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ open class PactVerificationExtension(
var testResultAccumulator: TestResultAccumulator = DefaultTestResultAccumulator

override fun getDisplayName(invocationIndex: Int): String {
return when {
val displayName = when {
pactSource is BrokerUrlSource && pactSource.result != null -> {
var displayName = pactSource.result!!.name + " - ${interaction.description}"
if (pactSource.tag.isNotEmpty()) displayName += " (tag ${pactSource.tag})"
if (pactSource.result!!.pending) {
"$displayName [PENDING]"
} else {
displayName
}
displayName
}
pactSource is BrokerUrlSource && pactSource.tag.isNotEmpty() -> "${pact.consumer.name} - ${interaction.description} (tag ${pactSource.tag})"
pactSource is BrokerUrlSource && pactSource.tag.isNotEmpty() ->
"${pact.consumer.name} - ${interaction.description} (tag ${pactSource.tag})"
else -> "${pact.consumer.name} - ${interaction.description}"
}
return when {
pactSource is BrokerUrlSource && pactSource.result?.pending == true -> "$displayName [PENDING]"
else -> displayName
}
}

override fun getAdditionalExtensions(): MutableList<Extension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IConsumerVersionSelectors {
* Return the consumer version selectors to use in the test
*/
@au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors
SelectorBuilder consumerVersionSelectors(SelectorBuilder builder);
SelectorBuilder consumerVersionSelectors();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import java.net.URI
import java.net.URISyntaxException
import kotlin.reflect.KCallable
import kotlin.reflect.KClass
import kotlin.reflect.KParameter
import kotlin.reflect.KType
import kotlin.reflect.KVisibility
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.isSubtypeOf
Expand Down Expand Up @@ -351,20 +353,20 @@ open class PactBrokerLoader(
fun invokeSelectorsMethod(testInstance: Any?, selectorsMethod: KCallable<*>): List<ConsumerVersionSelectors> {
val projectedType = SelectorBuilder::class.starProjectedType
return when (selectorsMethod.parameters.size) {
1 -> if (selectorsMethod.returnType.isSubtypeOf(projectedType)) {
val builder = selectorsMethod.call(SelectorBuilder()) as SelectorBuilder
0 -> if (selectorsMethod.returnType.isSubtypeOf(projectedType)) {
val builder = selectorsMethod.call() as SelectorBuilder
builder.build()
} else {
selectorsMethod.call(SelectorBuilder()) as List<ConsumerVersionSelectors>
selectorsMethod.call() as List<ConsumerVersionSelectors>
}
2 -> if (selectorsMethod.returnType.isSubtypeOf(projectedType)) {
val builder = selectorsMethod.call(testInstance, SelectorBuilder()) as SelectorBuilder
1 -> if (selectorsMethod.returnType.isSubtypeOf(projectedType)) {
val builder = selectorsMethod.call(testInstance) as SelectorBuilder
builder.build()
} else {
selectorsMethod.call(testInstance, SelectorBuilder()) as List<ConsumerVersionSelectors>
selectorsMethod.call(testInstance) as List<ConsumerVersionSelectors>
}
else -> throw java.lang.IllegalArgumentException(
"Consumer version selector method should take one parameter of type SelectorBuilder")
"Consumer version selector method should not take any parameters and return an instance of SelectorBuilder")
}
}

Expand All @@ -375,9 +377,9 @@ open class PactBrokerLoader(
method.findAnnotation<au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors>() != null
&& (
// static method
(method.parameters.size == 1 && method.parameters[0].type.isSubtypeOf(projectedType))
method.parameters.isEmpty()
// instance method
|| (method.parameters.size == 2 && method.parameters[1].type.isSubtypeOf(projectedType))
|| (method.parameters.size == 1 && method.parameters[0].kind == KParameter.Kind.INSTANCE)
)
&& method.visibility == KVisibility.PUBLIC
&& (method.returnType.isSubtypeOf(projectedType) || method.returnType.isSubtypeOf(List::class.starProjectedType))
Expand Down

0 comments on commit 17e51e3

Please sign in to comment.