Skip to content

Commit 01396e6

Browse files
author
Ronald Holshausen
authoredFeb 17, 2020
Merge pull request #1015 from asteffey/verbose-pact-sources
fix: Use concise Pact Source descriptions
2 parents 3d4bc07 + ec77d2d commit 01396e6

File tree

5 files changed

+88
-10
lines changed

5 files changed

+88
-10
lines changed
 

‎core/model/src/main/kotlin/au/com/dius/pact/core/model/PactSource.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ data class DirectorySource<I> @JvmOverloads constructor(
2222
val dir: File,
2323
val pacts: MutableMap<File, Pact<I>> = mutableMapOf()
2424
) : PactSource()
25-
where I : Interaction
25+
where I : Interaction {
26+
override fun description() = "Directory $dir"
27+
}
2628

2729
data class PactBrokerSource<I> @JvmOverloads constructor(
2830
val host: String,
2931
val port: String?,
3032
val scheme: String = "http",
3133
val pacts: MutableMap<Consumer, MutableList<Pact<I>>> = mutableMapOf()
3234
) : PactSource()
33-
where I : Interaction
35+
where I : Interaction {
36+
override fun description() =
37+
if (port == null) {
38+
"Pact Broker $scheme://$host"
39+
} else {
40+
"Pact Broker $scheme://$host:$port"
41+
}
42+
}
3443

3544
data class FileSource<I> @JvmOverloads constructor(val file: File, val pact: Pact<I>? = null) :
3645
PactSource() where I : Interaction {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package au.com.dius.pact.core.model
2+
3+
import spock.lang.Specification
4+
import spock.lang.Unroll
5+
6+
class DirectorySourceSpec extends Specification {
7+
8+
@Unroll
9+
def 'description includes the directory Pacts are contained in'() {
10+
when:
11+
def path = new File('/target/pacts')
12+
def source = new DirectorySource(path)
13+
14+
then:
15+
source.description() == "Directory ${path}"
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package au.com.dius.pact.core.model
2+
3+
import spock.lang.Specification
4+
import spock.lang.Unroll
5+
6+
class PactBrokerSourceSpec extends Specification {
7+
8+
@Unroll
9+
def 'description includes the URL for the Broker'() {
10+
expect:
11+
source.description() == description
12+
13+
where:
14+
source | description
15+
new PactBrokerSource('localhost', '80', 'http') | 'Pact Broker http://localhost:80'
16+
new PactBrokerSource('www.example.com', '443', 'https') | 'Pact Broker https://www.example.com:443'
17+
}
18+
}

‎provider/pact-jvm-provider-junit/src/test/groovy/au/com/dius/pact/provider/junit/loader/PactBrokerLoaderSpec.groovy

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package au.com.dius.pact.provider.junit.loader
22

33
import au.com.dius.pact.core.model.Pact
4+
import au.com.dius.pact.core.model.PactBrokerSource
45
import au.com.dius.pact.core.model.UrlSource
56
import au.com.dius.pact.core.pactbroker.IHalClient
67
import au.com.dius.pact.core.pactbroker.InvalidHalResponse
@@ -138,6 +139,26 @@ class PactBrokerLoaderSpec extends Specification {
138139
1 * brokerClient.fetchConsumers('test') >> []
139140
}
140141

142+
@RestoreSystemProperties
143+
void 'Uses fallback PactBroker System Properties for PactSource'() {
144+
given:
145+
host = 'my.pactbroker.host'
146+
port = '4711'
147+
System.setProperty('pactbroker.host', host)
148+
System.setProperty('pactbroker.port', port)
149+
150+
when:
151+
def pactSource = new PactBrokerLoader(MinimalPactBrokerAnnotation.getAnnotation(PactBroker)).pactSource
152+
153+
then:
154+
assert pactSource instanceof PactBrokerSource
155+
156+
def pactBrokerSource = (PactBrokerSource) pactSource
157+
assert pactBrokerSource.scheme == 'http'
158+
assert pactBrokerSource.host == host
159+
assert pactBrokerSource.port == port
160+
}
161+
141162
@RestoreSystemProperties
142163
void 'Fails when no fallback system properties are set'() {
143164
given:

‎provider/pact-jvm-provider/src/main/kotlin/au/com/dius/pact/provider/junit/loader/PactBrokerLoader.kt

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package au.com.dius.pact.provider.junit.loader
22

3+
import au.com.dius.pact.core.model.Consumer
34
import au.com.dius.pact.core.model.DefaultPactReader
45
import au.com.dius.pact.core.model.Interaction
56
import au.com.dius.pact.core.model.Pact
67
import au.com.dius.pact.core.model.PactBrokerSource
78
import au.com.dius.pact.core.model.PactReader
89
import au.com.dius.pact.core.model.UrlSource
10+
import au.com.dius.pact.core.model.PactSource
911
import au.com.dius.pact.core.pactbroker.PactBrokerClient
1012
import au.com.dius.pact.core.support.expressions.ExpressionParser.parseExpression
1113
import au.com.dius.pact.core.support.expressions.ExpressionParser.parseListExpression
@@ -35,8 +37,7 @@ open class PactBrokerLoader(
3537
valueResolver: ValueResolver? = null
3638
) : OverrideablePactLoader {
3739

38-
private var _pactSource: PactBrokerSource<Interaction> = PactBrokerSource(
39-
pactBrokerHost, pactBrokerPort, pactBrokerScheme)
40+
private var pacts: MutableMap<Consumer, MutableList<Pact<Interaction>>> = mutableMapOf()
4041
private var resolver: ValueResolver? = valueResolver
4142
private var overriddenPactUrl: String? = null
4243
private var overriddenConsumer: String? = null
@@ -100,7 +101,10 @@ open class PactBrokerLoader(
100101
return valueResolver
101102
}
102103

103-
override fun getPactSource() = _pactSource
104+
override fun getPactSource(): PactSource? {
105+
val resolver = setupValueResolver()
106+
return getPactBrokerSource(resolver)
107+
}
104108

105109
override fun setValueResolver(valueResolver: ValueResolver) {
106110
this.resolver = valueResolver
@@ -140,6 +144,16 @@ open class PactBrokerLoader(
140144
}
141145

142146
private fun brokerUrl(resolver: ValueResolver): URIBuilder {
147+
val (host, port, scheme) = getPactBrokerSource(resolver)
148+
149+
val uriBuilder = URIBuilder().setScheme(scheme).setHost(host)
150+
if (port.isNotEmpty()) {
151+
uriBuilder.port = Integer.parseInt(port)
152+
}
153+
return uriBuilder
154+
}
155+
156+
private fun getPactBrokerSource(resolver: ValueResolver): PactBrokerSource<Interaction> {
143157
val scheme = parseExpression(pactBrokerScheme, resolver)
144158
val host = parseExpression(pactBrokerHost, resolver)
145159
val port = parseExpression(pactBrokerPort, resolver)
@@ -154,11 +168,11 @@ open class PactBrokerLoader(
154168
"Please provide a valid port number or specify the system property 'pactbroker.port'.", pactBrokerPort))
155169
}
156170

157-
val uriBuilder = URIBuilder().setScheme(scheme).setHost(host)
158-
if (port.isNotEmpty()) {
159-
uriBuilder.port = Integer.parseInt(port)
171+
return if (scheme == null) {
172+
PactBrokerSource(host, port, pacts = pacts)
173+
} else {
174+
PactBrokerSource(host, port, scheme, pacts)
160175
}
161-
return uriBuilder
162176
}
163177

164178
private fun getUrlForProvider(providerName: String, tag: String, pactBrokerClient: PactBrokerClient): String {
@@ -172,7 +186,6 @@ open class PactBrokerLoader(
172186

173187
open fun loadPact(consumer: ConsumerInfo, options: Map<String, Any>): Pact<Interaction> {
174188
val pact = pactReader.loadPact(consumer.pactSource!!, options) as Pact<Interaction>
175-
val pacts = this.pactSource.pacts
176189
val pactConsumer = consumer.toPactConsumer()
177190
val pactList = pacts.getOrDefault(pactConsumer, mutableListOf())
178191
pactList.add(pact)

0 commit comments

Comments
 (0)
Please sign in to comment.