|
| 1 | +package au.com.dius.pact.provider.junit.loader |
| 2 | + |
| 3 | +import au.com.dius.pact.core.model.DefaultPactReader |
| 4 | +import au.com.dius.pact.core.model.Interaction |
| 5 | +import au.com.dius.pact.core.model.Pact |
| 6 | +import au.com.dius.pact.core.model.PactBrokerSource |
| 7 | +import au.com.dius.pact.core.model.PactReader |
| 8 | +import au.com.dius.pact.core.model.UrlSource |
| 9 | +import au.com.dius.pact.core.pactbroker.PactBrokerClient |
| 10 | +import au.com.dius.pact.core.support.expressions.ExpressionParser.parseExpression |
| 11 | +import au.com.dius.pact.core.support.expressions.ExpressionParser.parseListExpression |
| 12 | +import au.com.dius.pact.core.support.expressions.SystemPropertyResolver |
| 13 | +import au.com.dius.pact.core.support.expressions.ValueResolver |
| 14 | +import au.com.dius.pact.core.support.isNotEmpty |
| 15 | +import au.com.dius.pact.provider.ConsumerInfo |
| 16 | +import mu.KLogging |
| 17 | +import org.apache.http.client.utils.URIBuilder |
| 18 | +import java.io.IOException |
| 19 | +import java.net.URI |
| 20 | +import java.net.URISyntaxException |
| 21 | +import kotlin.reflect.KClass |
| 22 | + |
| 23 | +/** |
| 24 | + * Out-of-the-box implementation of {@link PactLoader} that downloads pacts from Pact broker |
| 25 | + */ |
| 26 | +open class PactBrokerLoader( |
| 27 | + val pactBrokerHost: String, |
| 28 | + val pactBrokerPort: String?, |
| 29 | + val pactBrokerScheme: String, |
| 30 | + val pactBrokerTags: List<String>? = listOf("latest"), |
| 31 | + val pactBrokerConsumers: List<String> = emptyList(), |
| 32 | + var failIfNoPactsFound: Boolean = true, |
| 33 | + var authentication: PactBrokerAuth?, |
| 34 | + var valueResolverClass: KClass<out ValueResolver>?, |
| 35 | + valueResolver: ValueResolver? = null |
| 36 | +) : OverrideablePactLoader { |
| 37 | + |
| 38 | + private var _pactSource: PactBrokerSource<Interaction> = PactBrokerSource( |
| 39 | + pactBrokerHost, pactBrokerPort, pactBrokerScheme) |
| 40 | + private var resolver: ValueResolver? = valueResolver |
| 41 | + private var overriddenPactUrl: String? = null |
| 42 | + private var overriddenConsumer: String? = null |
| 43 | + |
| 44 | + var pactReader: PactReader = DefaultPactReader |
| 45 | + |
| 46 | + constructor(pactBroker: PactBroker) : this( |
| 47 | + pactBroker.host, |
| 48 | + pactBroker.port, |
| 49 | + pactBroker.scheme, |
| 50 | + pactBroker.tags.toList(), |
| 51 | + pactBroker.consumers.toList(), |
| 52 | + true, |
| 53 | + pactBroker.authentication, |
| 54 | + pactBroker.valueResolver |
| 55 | + ) |
| 56 | + |
| 57 | + override fun overridePactUrl(pactUrl: String, consumer: String) { |
| 58 | + overriddenPactUrl = pactUrl |
| 59 | + overriddenConsumer = consumer |
| 60 | + } |
| 61 | + |
| 62 | + override fun load(providerName: String): List<Pact<Interaction>> { |
| 63 | + val resolver = setupValueResolver() |
| 64 | + val pacts = when { |
| 65 | + overriddenPactUrl.isNotEmpty() -> { |
| 66 | + val pactBrokerClient = newPactBrokerClient(brokerUrl(resolver).build(), resolver) |
| 67 | + val pactSource = UrlSource<Interaction>(overriddenPactUrl!!) |
| 68 | + pactSource.encodePath = false |
| 69 | + listOf(loadPact(ConsumerInfo(name = overriddenConsumer!!, pactSource = pactSource), |
| 70 | + pactBrokerClient.options)) |
| 71 | + } |
| 72 | + pactBrokerTags.isNullOrEmpty() -> loadPactsForProvider(providerName, null, resolver) |
| 73 | + else -> { |
| 74 | + pactBrokerTags.flatMap { parseListExpression(it, resolver) }.flatMap { |
| 75 | + try { |
| 76 | + loadPactsForProvider(providerName, it, resolver) |
| 77 | + } catch (e: NoPactsFoundException) { |
| 78 | + // Ignoring exception at this point, it will be handled at a higher level |
| 79 | + emptyList<Pact<Interaction>>() |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return pacts |
| 85 | + } |
| 86 | + |
| 87 | + private fun setupValueResolver(): ValueResolver { |
| 88 | + var valueResolver: ValueResolver = SystemPropertyResolver() |
| 89 | + if (resolver != null) { |
| 90 | + valueResolver = resolver!! |
| 91 | + } else if (valueResolverClass != null) { |
| 92 | + try { |
| 93 | + valueResolver = valueResolverClass!!.java.newInstance() |
| 94 | + } catch (e: InstantiationException) { |
| 95 | + logger.warn(e) { "Failed to instantiate the value resolver, using the default" } |
| 96 | + } catch (e: IllegalAccessException) { |
| 97 | + logger.warn(e) { "Failed to instantiate the value resolver, using the default" } |
| 98 | + } |
| 99 | + } |
| 100 | + return valueResolver |
| 101 | + } |
| 102 | + |
| 103 | + override fun getPactSource() = _pactSource |
| 104 | + |
| 105 | + override fun setValueResolver(valueResolver: ValueResolver) { |
| 106 | + this.resolver = valueResolver |
| 107 | + } |
| 108 | + |
| 109 | + @Throws(IOException::class, IllegalArgumentException::class) |
| 110 | + private fun loadPactsForProvider( |
| 111 | + providerName: String, |
| 112 | + tag: String?, |
| 113 | + resolver: ValueResolver |
| 114 | + ): List<Pact<Interaction>> { |
| 115 | + logger.debug { "Loading pacts from pact broker for provider $providerName and tag $tag" } |
| 116 | + val uriBuilder = brokerUrl(resolver) |
| 117 | + try { |
| 118 | + var consumers: List<ConsumerInfo> |
| 119 | + val pactBrokerClient = newPactBrokerClient(uriBuilder.build(), resolver) |
| 120 | + consumers = if (tag.isNullOrEmpty() || tag == "latest") { |
| 121 | + pactBrokerClient.fetchConsumers(providerName).map { ConsumerInfo.from(it) } |
| 122 | + } else { |
| 123 | + pactBrokerClient.fetchConsumersWithTag(providerName, tag).map { ConsumerInfo.from(it) } |
| 124 | + } |
| 125 | + |
| 126 | + if (failIfNoPactsFound && consumers.isEmpty()) { |
| 127 | + throw NoPactsFoundException("No consumer pacts were found for provider '" + providerName + "' and tag '" + |
| 128 | + tag + "'. (URL " + getUrlForProvider(providerName, tag.orEmpty(), pactBrokerClient) + ")") |
| 129 | + } |
| 130 | + |
| 131 | + if (pactBrokerConsumers.isNotEmpty()) { |
| 132 | + val consumerInclusions = pactBrokerConsumers.flatMap { parseListExpression(it, resolver) } |
| 133 | + consumers = consumers.filter { consumerInclusions.isEmpty() || consumerInclusions.contains(it.name) } |
| 134 | + } |
| 135 | + |
| 136 | + return consumers.map { loadPact(it, pactBrokerClient.options) } |
| 137 | + } catch (e: URISyntaxException) { |
| 138 | + throw IOException("Was not able load pacts from broker as the broker URL was invalid", e) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private fun brokerUrl(resolver: ValueResolver): URIBuilder { |
| 143 | + val scheme = parseExpression(pactBrokerScheme, resolver) |
| 144 | + val host = parseExpression(pactBrokerHost, resolver) |
| 145 | + val port = parseExpression(pactBrokerPort, resolver) |
| 146 | + |
| 147 | + if (host.isNullOrEmpty()) { |
| 148 | + throw IllegalArgumentException(String.format("Invalid pact broker host specified ('%s'). " + |
| 149 | + "Please provide a valid host or specify the system property 'pactbroker.host'.", pactBrokerHost)) |
| 150 | + } |
| 151 | + |
| 152 | + if (port.isNotEmpty() && !port!!.matches(Regex("^[0-9]+"))) { |
| 153 | + throw IllegalArgumentException(String.format("Invalid pact broker port specified ('%s'). " + |
| 154 | + "Please provide a valid port number or specify the system property 'pactbroker.port'.", pactBrokerPort)) |
| 155 | + } |
| 156 | + |
| 157 | + val uriBuilder = URIBuilder().setScheme(scheme).setHost(host) |
| 158 | + if (port.isNotEmpty()) { |
| 159 | + uriBuilder.port = Integer.parseInt(port) |
| 160 | + } |
| 161 | + return uriBuilder |
| 162 | + } |
| 163 | + |
| 164 | + private fun getUrlForProvider(providerName: String, tag: String, pactBrokerClient: PactBrokerClient): String { |
| 165 | + return try { |
| 166 | + pactBrokerClient.getUrlForProvider(providerName, tag)!! |
| 167 | + } catch (e: Exception) { |
| 168 | + logger.debug(e) { "Failed to get provider URL from the pact broker" } |
| 169 | + "Unknown" |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + open fun loadPact(consumer: ConsumerInfo, options: Map<String, Any>): Pact<Interaction> { |
| 174 | + val pact = pactReader.loadPact(consumer.pactSource!!, options) as Pact<Interaction> |
| 175 | + val pacts = this.pactSource.pacts |
| 176 | + val pactConsumer = consumer.toPactConsumer() |
| 177 | + val pactList = pacts.getOrDefault(pactConsumer, mutableListOf()) |
| 178 | + pactList.add(pact) |
| 179 | + pacts[pactConsumer] = pactList |
| 180 | + return pact |
| 181 | + } |
| 182 | + |
| 183 | + open fun newPactBrokerClient(url: URI, resolver: ValueResolver): PactBrokerClient { |
| 184 | + if (authentication == null || authentication!!.scheme.equals("none", ignoreCase = true)) { |
| 185 | + logger.debug { "Authentication: None" } |
| 186 | + return PactBrokerClient(url.toString(), emptyMap()) |
| 187 | + } |
| 188 | + |
| 189 | + val scheme = parseExpression(authentication!!.scheme, resolver) |
| 190 | + if (scheme.isNotEmpty()) { |
| 191 | + // Legacy behavior (before support for bearer token was added): |
| 192 | + // If scheme was not explicitly set, basic was always used. |
| 193 | + // If it was explicitly set, the given value was used together with username and password |
| 194 | + val schemeToUse = if (scheme.equals("legacy")) "basic" else scheme |
| 195 | + logger.debug { "Authentication: $schemeToUse" } |
| 196 | + val options = mapOf("authentication" to listOf(schemeToUse, |
| 197 | + parseExpression(authentication!!.username, resolver), |
| 198 | + parseExpression(authentication!!.password, resolver))) |
| 199 | + return PactBrokerClient(url.toString(), options) |
| 200 | + } |
| 201 | + |
| 202 | + // Check if username is set. If yes, use basic auth. |
| 203 | + val username = parseExpression(authentication!!.username, resolver) |
| 204 | + if (username.isNotEmpty()) { |
| 205 | + logger.debug { "Authentication: Basic" } |
| 206 | + val options = mapOf("authentication" to listOf("basic", username, |
| 207 | + parseExpression(authentication!!.password, resolver))) |
| 208 | + return PactBrokerClient(url.toString(), options) |
| 209 | + } |
| 210 | + |
| 211 | + // Check if token is set. If yes, use bearer auth. |
| 212 | + val token = parseExpression(authentication!!.token, resolver) |
| 213 | + if (token.isNotEmpty()) { |
| 214 | + logger.debug { "Authentication: Bearer" } |
| 215 | + val options = mapOf("authentication" to listOf("bearer", token)) |
| 216 | + return PactBrokerClient(url.toString(), options) |
| 217 | + } |
| 218 | + |
| 219 | + throw IllegalArgumentException("Invalid pact authentication specified. Either username or token must be set.") |
| 220 | + } |
| 221 | + |
| 222 | + companion object : KLogging() |
| 223 | +} |
0 commit comments