diff --git a/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt b/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt index cc3ca15..f5aa4a9 100644 --- a/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.marcinziolo.kotlin.wiremock.* import com.vonage.client.messages.Channel import com.vonage.client.messages.MessageRequest -import com.vonage.client.messages.MessageResponse +import com.vonage.client.messages.MessageResponseException import com.vonage.client.messages.MessageType import com.vonage.client.messages.MessagesVersion import com.vonage.client.messages.viber.Category @@ -35,15 +35,20 @@ class MessagesTest : AbstractTest() { private val fileUrl = "https://example.com/file.pdf" private val captionMap = mapOf("caption" to caption) - private fun mockResponse(expectedBodyParams: Map) { + private fun baseMockRequest(expectedBodyParams: Map? = null) = wiremock.post { url equalTo "/v1/messages" headers contains "User-Agent" like "vonage-java-sdk.*" headers contains "Authorization" like "Bearer eyJ.+" headers contains "Content-Type" equalTo "application/json" headers contains "Accept" equalTo "application/json" - body equalTo ObjectMapper().writeValueAsString(expectedBodyParams) - } returns { + if (expectedBodyParams != null) { + body equalTo ObjectMapper().writeValueAsString(expectedBodyParams) + } + } + + private fun mock202Response(expectedBodyParams: Map) { + baseMockRequest(expectedBodyParams) returns { header = "Content-Type" to "application/json" statusCode = 202 body = """ @@ -55,7 +60,7 @@ class MessagesTest : AbstractTest() { } private fun testSend(expectedBodyParams: Map, req: MessageRequest) { - mockResponse(expectedBodyParams) + mock202Response(expectedBodyParams) assertEquals(messageUuid, messagesClient.send(req)) } @@ -70,7 +75,8 @@ class MessagesTest : AbstractTest() { private fun textBody(channel: String, additionalParams: Map = mapOf()): Map = baseBody("text", channel) + mapOf("text" to text) + additionalParams - private fun mediaBody(channel: String, messageType: String, url: String, additionalParams: Map? = null): Map = + private fun mediaBody(channel: String, messageType: String, url: String, + additionalParams: Map? = null): Map = baseBody(messageType, channel) + mapOf(messageType to mapOf("url" to url) + (additionalParams ?: mapOf())) private fun imageBody(channel: String, additionalParams : Map? = null): Map = @@ -88,6 +94,39 @@ class MessagesTest : AbstractTest() { private fun whatsappCustomBody(params: Map): Map = baseBody("custom", whatsappChannel) + mapOf("custom" to params) + @Test + fun `send message 402 response`() { + val errorType = "https://developer.nexmo.com/api-errors/#low-balance" + val title = "Low balance" + val detail = "This request could not be performed due to your account balance being low." + val instance = "bf0ca0bf927b3b52e3cb03217e1a1ddf" + + baseMockRequest() returns { + header = "Content-Type" to "application/json" + statusCode = 402 + body = """ + { + "type": "$errorType", + "title": "$title", + "detail": "$detail", + "instance": "$instance" + } + """ + } + + val exception = assertThrows { + messagesClient.send(smsText { + from(fromNumber); to(toNumber); text(text) + }) + } + + assertEquals(402, exception.statusCode) + assertEquals(URI.create(errorType), exception.type) + assertEquals(title, exception.title) + assertEquals(instance, exception.instance) + assertEquals(detail, exception.detail) + } + @Test fun `send SMS text all parameters`() { val clientRef = "My reference" @@ -115,7 +154,7 @@ class MessagesTest : AbstractTest() { @Test fun `send SMS text required parameters`() { testSend(textBody("sms"), smsText { - from(fromNumber); to(toNumber); text(text); + from(fromNumber); to(toNumber); text(text) }) } diff --git a/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt b/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt new file mode 100644 index 0000000..fb595ba --- /dev/null +++ b/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt @@ -0,0 +1,5 @@ +package com.vonage.client.kt + +class VerifyTest : AbstractTest() { + +} \ No newline at end of file