@@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
4
4
import com.marcinziolo.kotlin.wiremock.*
5
5
import com.vonage.client.messages.Channel
6
6
import com.vonage.client.messages.MessageRequest
7
- import com.vonage.client.messages.MessageResponse
7
+ import com.vonage.client.messages.MessageResponseException
8
8
import com.vonage.client.messages.MessageType
9
9
import com.vonage.client.messages.MessagesVersion
10
10
import com.vonage.client.messages.viber.Category
@@ -35,15 +35,20 @@ class MessagesTest : AbstractTest() {
35
35
private val fileUrl = " https://example.com/file.pdf"
36
36
private val captionMap = mapOf (" caption" to caption)
37
37
38
- private fun mockResponse (expectedBodyParams : Map <String , Any >) {
38
+ private fun baseMockRequest (expectedBodyParams : Map <String , Any >? = null) =
39
39
wiremock.post {
40
40
url equalTo " /v1/messages"
41
41
headers contains " User-Agent" like " vonage-java-sdk.*"
42
42
headers contains " Authorization" like " Bearer eyJ.+"
43
43
headers contains " Content-Type" equalTo " application/json"
44
44
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 {
47
52
header = " Content-Type" to " application/json"
48
53
statusCode = 202
49
54
body = """
@@ -55,7 +60,7 @@ class MessagesTest : AbstractTest() {
55
60
}
56
61
57
62
private fun testSend (expectedBodyParams : Map <String , Any >, req : MessageRequest ) {
58
- mockResponse (expectedBodyParams)
63
+ mock202Response (expectedBodyParams)
59
64
assertEquals(messageUuid, messagesClient.send(req))
60
65
}
61
66
@@ -70,7 +75,8 @@ class MessagesTest : AbstractTest() {
70
75
private fun textBody (channel : String , additionalParams : Map <String , Any > = mapOf()): Map <String , Any > =
71
76
baseBody(" text" , channel) + mapOf (" text" to text) + additionalParams
72
77
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 > =
74
80
baseBody(messageType, channel) + mapOf (messageType to mapOf (" url" to url) + (additionalParams ? : mapOf ()))
75
81
76
82
private fun imageBody (channel : String , additionalParams : Map <String , Any >? = null): Map <String , Any > =
@@ -88,6 +94,39 @@ class MessagesTest : AbstractTest() {
88
94
private fun whatsappCustomBody (params : Map <String , Any >): Map <String , Any > =
89
95
baseBody(" custom" , whatsappChannel) + mapOf (" custom" to params)
90
96
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
+
91
130
@Test
92
131
fun `send SMS text all parameters` () {
93
132
val clientRef = " My reference"
@@ -115,7 +154,7 @@ class MessagesTest : AbstractTest() {
115
154
@Test
116
155
fun `send SMS text required parameters` () {
117
156
testSend(textBody(" sms" ), smsText {
118
- from(fromNumber); to(toNumber); text(text);
157
+ from(fromNumber); to(toNumber); text(text)
119
158
})
120
159
}
121
160
0 commit comments