diff --git a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt index e229e0b0a..ff812f1bf 100644 --- a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt +++ b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt @@ -1037,13 +1037,18 @@ open class PactBrokerClient( when (val result = halClient.getJson(path, false)) { is Result.Ok -> { val summary: JsonValue.Object = result.value["summary"].downcast() - val verificationResultUrl = result.value["matrix"].asArray() - ?.get(0)?.asObject() - ?.get("verificationResult")?.asObject() - ?.get("_links")?.asObject() - ?.get("self")?.asObject() - ?.get("href") - ?.let{ url -> Json.toString(url) } + val matrix = result.value["matrix"] + val verificationResultUrl = if (matrix.isArray && matrix.size() > 0) { + result.value["matrix"].asArray() + ?.get(0)?.asObject() + ?.get("verificationResult")?.asObject() + ?.get("_links")?.asObject() + ?.get("self")?.asObject() + ?.get("href") + ?.let{ url -> Json.toString(url) } + } else { + null + } CanIDeployResult(Json.toBoolean(summary["deployable"]), "", Json.toString(summary["reason"]), Json.toInteger(summary["unknown"]), verificationResultUrl) } diff --git a/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy b/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy index 1f304a20c..662113746 100644 --- a/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy +++ b/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy @@ -886,4 +886,39 @@ class PactBrokerClientSpec extends Specification { 1 * halClient.postJson('pb:provider-pacts-for-verification', [provider: 'provider'], expectedJson) >> new Result.Ok(jsonResult) result instanceof Result.Ok } + + @Issue('#1814') + def 'can-i-deploy - handles an empty matrix response'() { + given: + def halClient = Mock(IHalClient) + def config = new PactBrokerClientConfig(10, 0) + PactBrokerClient client = Spy(PactBrokerClient, constructorArgs: ['baseUrl', [:], config]) { + newHalClient() >> halClient + } + def json = JsonParser.parseString(''' + |{ + | "summary": { + | "deployable": true, + | "reason": "There are no missing dependencies", + | "success": 0, + | "failed": 0, + | "unknown": 0 + | }, + | "notices": [ + | { + | "type": "success", + | "text": "There are no missing dependencies" + | } + | ], + | "matrix": [] + |}'''.stripMargin()) + + when: + def result = client.canIDeploy('test', '1.2.3', new Latest.UseLatest(true), null) + + then: + 1 * halClient.getJson(_, _) >> new Result.Ok(json) + result.ok + !result.verificationResultUrl + } }