Skip to content

Commit e23015b

Browse files
author
Ronald Holshausen
committedJan 26, 2020
chore: implemented can-i-deploy call on the broker client #994
1 parent 7068f20 commit e23015b

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed
 

‎core/pact-broker/src/main/kotlin/au/com/dius/pact/core/pactbroker/HalClient.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ interface IHalClient {
140140
* Upload a JSON document to the current path link, using a PUT request
141141
*/
142142
fun putJson(link: String, options: Map<String, Any>, json: String): Result<Boolean, Exception>
143+
144+
fun getJson(path: String): Result<JsonElement, Exception>
145+
fun getJson(path: String, encodePath: Boolean): Result<JsonElement, Exception>
143146
}
144147

145148
/**
@@ -260,7 +263,9 @@ open class HalClient @JvmOverloads constructor(
260263
return this
261264
}
262265

263-
private fun getJson(path: String, encodePath: Boolean = true): Result<JsonElement, Exception> {
266+
override fun getJson(path: String) = getJson(path, true)
267+
268+
override fun getJson(path: String, encodePath: Boolean): Result<JsonElement, Exception> {
264269
setupHttpClient()
265270
return Result.of {
266271
val httpGet = initialiseRequest(HttpGet(buildUrl(baseUrl, path, encodePath)))

‎core/pact-broker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt

+34-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package au.com.dius.pact.core.pactbroker
33
import au.com.dius.pact.com.github.michaelbull.result.Err
44
import au.com.dius.pact.com.github.michaelbull.result.Ok
55
import au.com.dius.pact.com.github.michaelbull.result.Result
6+
import au.com.dius.pact.core.support.Json
67
import com.github.salomonbrys.kotson.get
78
import com.github.salomonbrys.kotson.jsonArray
89
import com.github.salomonbrys.kotson.jsonObject
@@ -124,7 +125,7 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
124125
@JvmOverloads
125126
open fun uploadPactFile(pactFile: File, unescapedVersion: String, tags: List<String> = emptyList()): Any? {
126127
val pactText = pactFile.readText()
127-
val pact = JsonParser().parse(pactText)
128+
val pact = JsonParser.parseString(pactText)
128129
val halClient = newHalClient()
129130
val providerName = urlPathSegmentEscaper().escape(pact["provider"]["name"].string)
130131
val consumerName = urlPathSegmentEscaper().escape(pact["consumer"]["name"].string)
@@ -228,8 +229,8 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
228229
"metadata" -> {
229230
listOf(jsonObject(mismatch.filter { it.key != "interactionId" }
230231
.flatMap {
231-
when {
232-
it.key == "type" -> listOf("attribute" to it.value)
232+
when (it.key) {
233+
"type" -> listOf("attribute" to it.value)
233234
else -> listOf("identifier" to it.key, "description" to it.value)
234235
}
235236
}))
@@ -302,7 +303,36 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
302303
}
303304

304305
open fun canIDeploy(pacticipant: String, pacticipantVersion: String, latest: Latest, to: String?): CanIDeployResult {
305-
return CanIDeployResult(false, "", "")
306+
val halClient = newHalClient()
307+
val result = halClient.getJson("/matrix" + buildMatrixQuery(pacticipant, pacticipantVersion, latest, to),
308+
false)
309+
return when (result) {
310+
is Ok -> {
311+
val summary = result.value.asJsonObject["summary"].asJsonObject
312+
CanIDeployResult(Json.toBoolean(summary["deployable"]), "", Json.toString(summary["reason"]))
313+
}
314+
is Err -> {
315+
logger.error(result.error) { "Pact broker matrix query failed: ${result.error.message}" }
316+
CanIDeployResult(false, result.error.message.toString(), "")
317+
}
318+
}
319+
}
320+
321+
private fun buildMatrixQuery(pacticipant: String, pacticipantVersion: String, latest: Latest, to: String?): String {
322+
val escaper = urlPathSegmentEscaper()
323+
var base = "?q[][pacticipant]=${escaper.escape(pacticipant)}"
324+
base += when (latest) {
325+
is Latest.UseLatest -> if (latest.latest) {
326+
"q[][latest]=true"
327+
} else {
328+
"&q[][version]=${escaper.escape(pacticipantVersion)}"
329+
}
330+
is Latest.UseLatestTag -> "q[][tag]=${escaper.escape(latest.latestTag)}"
331+
}
332+
if (!to.isNullOrEmpty()) {
333+
base += "&latest=true&tag=${escaper.escape(to)}"
334+
}
335+
return base
306336
}
307337

308338
companion object : KLogging() {

‎core/support/src/main/kotlin/au/com/dius/pact/core/support/Json.kt

+6
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,10 @@ object Json {
111111

112112
fun exceptionToJson(exp: Exception) = jsonObject("message" to exp.message,
113113
"exceptionClass" to exp.javaClass.name)
114+
115+
fun toBoolean(jsonElement: JsonElement?) = when {
116+
jsonElement == null || jsonElement.isJsonNull -> false
117+
jsonElement.isJsonPrimitive && jsonElement.asJsonPrimitive.isBoolean -> jsonElement.asJsonPrimitive.asBoolean
118+
else -> false
119+
}
114120
}

‎core/support/src/test/groovy/au/com/dius/pact/core/support/JsonSpec.groovy

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package au.com.dius.pact.core.support
22

3+
import com.google.gson.JsonParser
34
import spock.lang.Specification
45
import spock.lang.Unroll
56

@@ -22,4 +23,23 @@ class JsonSpec extends Specification {
2223
'object' | [hello: 'world', list: [1, 2, 3]] | '{"hello":"world","list":[1,2,3]}'
2324
}
2425

26+
@Unroll
27+
def 'toBoolean - #desc'() {
28+
expect:
29+
Json.INSTANCE.toBoolean(json == null ? json : JsonParser.parseString(json)) == booleanValue
30+
31+
where:
32+
33+
desc | json | booleanValue
34+
'Null' | null | false
35+
'Json Null' | 'null' | false
36+
'Boolean True' | 'true' | true
37+
'Boolean False' | 'false' | false
38+
'integer' | '112' | false
39+
'float' | '112.66' | false
40+
'string' | '"hello"' | false
41+
'list' | '["hello", 1, true, {"a": "A"}]' | false
42+
'object' | '{"hello": "world", "list": [1, 2, 3]}' | false
43+
}
44+
2545
}

0 commit comments

Comments
 (0)
Please sign in to comment.