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 Jul 5, 2022
1 parent f37c137 commit 471c13e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1574,32 +1574,32 @@ class PactBrokerLoaderSpec extends Specification {

static class IncorrectTypesOnSelectorMethod2 {
@au.com.dius.pact.provider.junitsupport.loader.ConsumerVersionSelectors
int consumerVersionSelectors(SelectorBuilder builder) { 0 }
int consumerVersionSelectors() { 0 }
}

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 @@ -47,7 +47,7 @@ open class PactVerificationExtension(
var testResultAccumulator: TestResultAccumulator = DefaultTestResultAccumulator

override fun getDisplayName(invocationIndex: Int): String {
var displayName = 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})"
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 @@ -27,6 +27,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 @@ -353,20 +355,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 @@ -377,9 +379,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 471c13e

Please sign in to comment.