Skip to content

Commit

Permalink
feat: deprecate pact broker host, port and scheme in favour of a url #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Feb 20, 2021
1 parent 10e8c72 commit b782ccc
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ data class DirectorySource<I> @JvmOverloads constructor(
}

data class PactBrokerSource<I> @JvmOverloads constructor(
val host: String,
@Deprecated("Use url instead")
val host: String?,
@Deprecated("Use url instead")
val port: String?,
val scheme: String = "http",
val pacts: MutableMap<Consumer, MutableList<Pact<I>>> = mutableMapOf()
@Deprecated("Use url instead")
val scheme: String? = "http",
val pacts: MutableMap<Consumer, MutableList<Pact<I>>> = mutableMapOf(),
val url: String? = null
) : PactSource()
where I : Interaction {
override fun description() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PactBrokerLoaderSpec extends Specification {
private String host
private String port
private String protocol
private String url
private List tags
private List<VersionSelector> consumerVersionSelectors
private List consumers
Expand All @@ -47,6 +48,7 @@ class PactBrokerLoaderSpec extends Specification {
host = 'pactbroker'
port = '1234'
protocol = 'http'
url = null
tags = []
consumerVersionSelectors = []
consumers = []
Expand All @@ -65,7 +67,7 @@ class PactBrokerLoaderSpec extends Specification {
pactBrokerLoader = { boolean failIfNoPactsFound = true ->
IPactBrokerClient client = brokerClient
def loader = new PactBrokerLoader(host, port, protocol, tags, consumerVersionSelectors, consumers,
failIfNoPactsFound, null, null, valueResolver, enablePendingPacts, providerTags, includeWipPactsSince) {
failIfNoPactsFound, null, null, valueResolver, enablePendingPacts, providerTags, includeWipPactsSince, url) {
@Override
IPactBrokerClient newPactBrokerClient(URI url, ValueResolver resolver) {
client
Expand Down Expand Up @@ -173,13 +175,39 @@ class PactBrokerLoaderSpec extends Specification {
1 * brokerClient.fetchConsumersWithSelectors('test', _, _, _, _) >> new Ok([])
}

@RestoreSystemProperties
void 'Uses fallback PactBroker System Properties for URL'() {
given:
System.setProperty('pactbroker.url', 'http://my.pactbroker.host:4751')
pactBrokerLoader = {
new PactBrokerLoader(MinimalPactBrokerAnnotation.getAnnotation(PactBroker)) {
@Override
IPactBrokerClient newPactBrokerClient(URI url, ValueResolver resolver) {
assert url.host == 'my.pactbroker.host'
assert url.port == 4751
assert url.toString() == 'http://my.pactbroker.host:4751'
brokerClient
}
}
}

when:
def result = pactBrokerLoader().load('test')

then:
result == []
1 * brokerClient.fetchConsumersWithSelectors('test', _, _, _, _) >> new Ok([])
}

@RestoreSystemProperties
void 'Uses fallback PactBroker System Properties for PactSource'() {
given:
host = 'my.pactbroker.host'
port = '4711'
url = 'http://my.pactbroker.host:4711'
System.setProperty('pactbroker.host', host)
System.setProperty('pactbroker.port', port)
System.setProperty('pactbroker.url', url)

when:
def pactSource = new PactBrokerLoader(MinimalPactBrokerAnnotation.getAnnotation(PactBroker)).pactSource
Expand All @@ -189,15 +217,17 @@ class PactBrokerLoaderSpec extends Specification {

def pactBrokerSource = (PactBrokerSource) pactSource
assert pactBrokerSource.scheme == 'http'
assert pactBrokerSource.host == host
assert pactBrokerSource.port == port
assert pactBrokerSource.host == null
assert pactBrokerSource.port == null
assert pactBrokerSource.url == url
}

@RestoreSystemProperties
void 'Fails when no fallback system properties are set'() {
given:
System.clearProperty('pactbroker.host')
System.clearProperty('pactbroker.port')
System.clearProperty('pactbroker.url')
pactBrokerLoader = {
new PactBrokerLoader(MinimalPactBrokerAnnotation.getAnnotation(PactBroker)) {
@Override
Expand Down Expand Up @@ -1170,6 +1200,53 @@ class PactBrokerLoaderSpec extends Specification {
]
}

@Unroll
@SuppressWarnings('LineLength')
def 'getPactBrokerSource uses the URL if it is set'() {
given:
def valueResolver = Mock(ValueResolver)

when:
host = p_host
port = p_port
protocol = p_protocol
url = p_url
def source = pactBrokerLoader().getPactBrokerSource(valueResolver)

then:
source == expected

where:

p_host | p_port | p_protocol | p_url | expected
null | null | null | 'http://localhost' | new PactBrokerSource(null, null, 'http', [:], 'http://localhost')
'localhost' | '1234' | null | null | new PactBrokerSource('localhost', '1234', 'http', [:], null)
'localhost' | '1234' | 'https' | null | new PactBrokerSource('localhost', '1234', 'https', [:], null)
}

@Unroll
def 'brokerUrl returns the url if it is set'() {
given:
def valueResolver = Mock(ValueResolver)

when:
host = p_host
port = p_port
protocol = p_protocol
url = p_url
def result = pactBrokerLoader().brokerUrl(valueResolver).toString()

then:
result == expected

where:

p_host | p_port | p_protocol | p_url | expected
null | null | null | 'http://localhost/' | 'http://localhost/'
'localhost' | '1234' | null | null | 'http://localhost:1234'
'localhost' | '1234' | 'https' | null | 'https://localhost:1234'
}

private static VersionSelector createVersionSelector(Map args = [:]) {
new VersionSelector() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ class PactBrokerAnnotationDefaultsTest {
.filter { it.startsWith("pactbroker") }
.forEach { props.remove(it) }

@Test
fun `default url is empty`() {
assertThat(parseExpression(annotation.url, DataType.RAW)?.toString(), `is`(""))
}

@Test
fun `can set url`() {
props.setProperty("pactbroker.url", "http://myHost")
assertThat(parseExpression(annotation.url, DataType.RAW)?.toString(), `is`("http://myHost"))
}

@Test
fun `default host is empty`() {
assertThat(parseExpression(annotation.host, DataType.RAW)?.toString(), `is`(""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,59 @@
@PactSource(PactBrokerLoader.class)
@Inherited
public @interface PactBroker {
/**
* @return host of pact broker
*/
String host() default "${pactbroker.host:}";
/**
* @return URL of pact broker
*/
String url() default "${pactbroker.url:}";

/**
* @return port of pact broker
*/
String port() default "${pactbroker.port:}";
/**
* @return host of pact broker
* @deprecated Use url instead
*/
@Deprecated
String host() default "${pactbroker.host:}";

/**
* HTTP scheme, defaults to HTTP
*/
String scheme() default "${pactbroker.scheme:http}";
/**
* @return port of pact broker
* @deprecated Use url instead
*/
@Deprecated
String port() default "${pactbroker.port:}";

/**
* 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
String[] tags() default "${pactbroker.tags:}";
/**
* HTTP scheme, defaults to HTTP
* @deprecated Use url instead
*/
@Deprecated
String scheme() default "${pactbroker.scheme:http}";

/**
* 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
*/
VersionSelector[] consumerVersionSelectors() default @VersionSelector(
tag = "${pactbroker.consumerversionselectors.tags:}",
latest = "${pactbroker.consumerversionselectors.latest:}",
consumer = "${pactbroker.consumers:}"
);
/**
* 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
String[] tags() default "${pactbroker.tags:}";

/**
* 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
String[] consumers() default "${pactbroker.consumers:}";
/**
* 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
*/
VersionSelector[] consumerVersionSelectors() default @VersionSelector(
tag = "${pactbroker.consumerversionselectors.tags:}",
latest = "${pactbroker.consumerversionselectors.latest:}",
consumer = "${pactbroker.consumers:}"
);

/**
* 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
String[] consumers() default "${pactbroker.consumers:}";

/**
* Authentication to use with the pact broker, by default no authentication is used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ data class SelectorResult(
*/
@Suppress("LongParameterList", "TooManyFunctions")
open class PactBrokerLoader(
val pactBrokerHost: String,
@Deprecated("Use pactBrokerUrl")
val pactBrokerHost: String?,
@Deprecated("Use pactBrokerUrl")
val pactBrokerPort: String?,
val pactBrokerScheme: String,
@Deprecated("Use pactBrokerUrl")
val pactBrokerScheme: String?,
@Deprecated(message = "Use Consumer version selectors instead",
replaceWith = ReplaceWith("pactBrokerConsumerVersionSelectors"))
val pactBrokerTags: List<String>? = listOf("latest"),
Expand All @@ -51,7 +54,8 @@ open class PactBrokerLoader(
valueResolver: ValueResolver? = null,
val enablePendingPacts: String = "false",
val providerTags: List<String> = emptyList(),
val includeWipPactsSince: String = ""
val includeWipPactsSince: String = "",
val pactBrokerUrl: String? = null
) : OverrideablePactLoader {

private var resolver: ValueResolver? = valueResolver
Expand All @@ -73,7 +77,8 @@ open class PactBrokerLoader(
null,
pactBroker.enablePendingPacts,
pactBroker.providerTags.toList(),
pactBroker.includeWipPactsSince
pactBroker.includeWipPactsSince,
pactBroker.url
)

override fun description(): String {
Expand Down Expand Up @@ -164,12 +169,16 @@ open class PactBrokerLoader(
if (resolver != null) {
valueResolver = resolver!!
} else if (valueResolverClass != null) {
try {
valueResolver = valueResolverClass!!.java.newInstance()
} catch (e: InstantiationException) {
logger.warn(e) { "Failed to instantiate the value resolver, using the default" }
} catch (e: IllegalAccessException) {
logger.warn(e) { "Failed to instantiate the value resolver, using the default" }
if (valueResolverClass!!.objectInstance != null) {
valueResolver = valueResolverClass!!.objectInstance!!
} else {
try {
valueResolver = valueResolverClass!!.java.newInstance()
} catch (e: InstantiationException) {
logger.warn(e) { "Failed to instantiate the value resolver, using the default" }
} catch (e: IllegalAccessException) {
logger.warn(e) { "Failed to instantiate the value resolver, using the default" }
}
}
}
return valueResolver
Expand Down Expand Up @@ -230,35 +239,44 @@ open class PactBrokerLoader(
}
}

private fun brokerUrl(resolver: ValueResolver): URIBuilder {
val (host, port, scheme) = getPactBrokerSource(resolver)
fun brokerUrl(resolver: ValueResolver): URIBuilder {
val (host, port, scheme, _, url) = getPactBrokerSource(resolver)

val uriBuilder = URIBuilder().setScheme(scheme).setHost(host)
if (port.isNotEmpty()) {
uriBuilder.port = Integer.parseInt(port)
return if (url.isNullOrEmpty()) {
val uriBuilder = URIBuilder().setScheme(scheme).setHost(host)
if (port.isNotEmpty()) {
uriBuilder.port = Integer.parseInt(port)
}
uriBuilder
} else {
URIBuilder(url)
}
return uriBuilder
}

private fun getPactBrokerSource(resolver: ValueResolver): PactBrokerSource<Interaction> {
fun getPactBrokerSource(resolver: ValueResolver): PactBrokerSource<Interaction> {
val scheme = parseExpression(pactBrokerScheme, DataType.RAW, resolver)?.toString()
val host = parseExpression(pactBrokerHost, DataType.RAW, resolver)?.toString()
val port = parseExpression(pactBrokerPort, DataType.RAW, resolver)?.toString()
val url = parseExpression(pactBrokerUrl, DataType.RAW, resolver)?.toString()

if (host.isNullOrEmpty() || !host.matches(Regex("[0-9a-zA-Z\\-.]+"))) {
throw IllegalArgumentException(String.format("Invalid pact broker host specified ('%s'). " +
"Please provide a valid host or specify the system property 'pactbroker.host'.", pactBrokerHost))
}
return if (url.isNullOrEmpty()) {
if (host.isNullOrEmpty() || !host.matches(Regex("[0-9a-zA-Z\\-.]+"))) {
throw IllegalArgumentException(String.format("Invalid pact broker host specified ('%s'). " +
"Please provide a valid host or specify the system property 'pactbroker.host'.", pactBrokerHost))
}

if (port.isNotEmpty() && !port!!.matches(Regex("^[0-9]+"))) {
throw IllegalArgumentException(String.format("Invalid pact broker port specified ('%s'). " +
"Please provide a valid port number or specify the system property 'pactbroker.port'.", pactBrokerPort))
}
if (port.isNotEmpty() && !port!!.matches(Regex("^[0-9]+"))) {
throw IllegalArgumentException(String.format("Invalid pact broker port specified ('%s'). " +
"Please provide a valid port number or specify the system property 'pactbroker.port'.", pactBrokerPort))
}

return if (scheme == null) {
PactBrokerSource(host, port)
if (scheme == null) {
PactBrokerSource(host, port)
} else {
PactBrokerSource(host, port, scheme)
}
} else {
PactBrokerSource(host, port, scheme)
PactBrokerSource(null, null, url = url)
}
}

Expand Down

0 comments on commit b782ccc

Please sign in to comment.