Skip to content

Commit 8029893

Browse files
committed
Test Messages 402 response
1 parent 40c1791 commit 8029893

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/test/kotlin/com/vonage/client/kt/MessagesTest.kt

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
44
import com.marcinziolo.kotlin.wiremock.*
55
import com.vonage.client.messages.Channel
66
import com.vonage.client.messages.MessageRequest
7-
import com.vonage.client.messages.MessageResponse
7+
import com.vonage.client.messages.MessageResponseException
88
import com.vonage.client.messages.MessageType
99
import com.vonage.client.messages.MessagesVersion
1010
import com.vonage.client.messages.viber.Category
@@ -35,15 +35,20 @@ class MessagesTest : AbstractTest() {
3535
private val fileUrl = "https://example.com/file.pdf"
3636
private val captionMap = mapOf("caption" to caption)
3737

38-
private fun mockResponse(expectedBodyParams: Map<String, Any>) {
38+
private fun baseMockRequest(expectedBodyParams: Map<String, Any>? = null) =
3939
wiremock.post {
4040
url equalTo "/v1/messages"
4141
headers contains "User-Agent" like "vonage-java-sdk.*"
4242
headers contains "Authorization" like "Bearer eyJ.+"
4343
headers contains "Content-Type" equalTo "application/json"
4444
headers contains "Accept" equalTo "application/json"
45-
body equalTo ObjectMapper().writeValueAsString(expectedBodyParams)
46-
} returns {
45+
if (expectedBodyParams != null) {
46+
body equalTo ObjectMapper().writeValueAsString(expectedBodyParams)
47+
}
48+
}
49+
50+
private fun mock202Response(expectedBodyParams: Map<String, Any>) {
51+
baseMockRequest(expectedBodyParams) returns {
4752
header = "Content-Type" to "application/json"
4853
statusCode = 202
4954
body = """
@@ -55,7 +60,7 @@ class MessagesTest : AbstractTest() {
5560
}
5661

5762
private fun testSend(expectedBodyParams: Map<String, Any>, req: MessageRequest) {
58-
mockResponse(expectedBodyParams)
63+
mock202Response(expectedBodyParams)
5964
assertEquals(messageUuid, messagesClient.send(req))
6065
}
6166

@@ -70,7 +75,8 @@ class MessagesTest : AbstractTest() {
7075
private fun textBody(channel: String, additionalParams: Map<String, Any> = mapOf()): Map<String, Any> =
7176
baseBody("text", channel) + mapOf("text" to text) + additionalParams
7277

73-
private fun mediaBody(channel: String, messageType: String, url: String, additionalParams: Map<String, Any>? = null): Map<String, Any> =
78+
private fun mediaBody(channel: String, messageType: String, url: String,
79+
additionalParams: Map<String, Any>? = null): Map<String, Any> =
7480
baseBody(messageType, channel) + mapOf(messageType to mapOf("url" to url) + (additionalParams ?: mapOf()))
7581

7682
private fun imageBody(channel: String, additionalParams : Map<String, Any>? = null): Map<String, Any> =
@@ -88,6 +94,39 @@ class MessagesTest : AbstractTest() {
8894
private fun whatsappCustomBody(params: Map<String, Any>): Map<String, Any> =
8995
baseBody("custom", whatsappChannel) + mapOf("custom" to params)
9096

97+
@Test
98+
fun `send message 402 response`() {
99+
val errorType = "https://developer.nexmo.com/api-errors/#low-balance"
100+
val title = "Low balance"
101+
val detail = "This request could not be performed due to your account balance being low."
102+
val instance = "bf0ca0bf927b3b52e3cb03217e1a1ddf"
103+
104+
baseMockRequest() returns {
105+
header = "Content-Type" to "application/json"
106+
statusCode = 402
107+
body = """
108+
{
109+
"type": "$errorType",
110+
"title": "$title",
111+
"detail": "$detail",
112+
"instance": "$instance"
113+
}
114+
"""
115+
}
116+
117+
val exception = assertThrows<MessageResponseException> {
118+
messagesClient.send(smsText {
119+
from(fromNumber); to(toNumber); text(text)
120+
})
121+
}
122+
123+
assertEquals(402, exception.statusCode)
124+
assertEquals(URI.create(errorType), exception.type)
125+
assertEquals(title, exception.title)
126+
assertEquals(instance, exception.instance)
127+
assertEquals(detail, exception.detail)
128+
}
129+
91130
@Test
92131
fun `send SMS text all parameters`() {
93132
val clientRef = "My reference"
@@ -115,7 +154,7 @@ class MessagesTest : AbstractTest() {
115154
@Test
116155
fun `send SMS text required parameters`() {
117156
testSend(textBody("sms"), smsText {
118-
from(fromNumber); to(toNumber); text(text);
157+
from(fromNumber); to(toNumber); text(text)
119158
})
120159
}
121160

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.vonage.client.kt
2+
3+
class VerifyTest : AbstractTest() {
4+
5+
}

0 commit comments

Comments
 (0)