@@ -3,28 +3,31 @@ package com.vonage.client.kt
3
3
import com.fasterxml.jackson.databind.ObjectMapper
4
4
import com.marcinziolo.kotlin.wiremock.*
5
5
import com.vonage.client.messages.MessageRequest
6
+ import com.vonage.client.messages.MessagesVersion
6
7
import com.vonage.client.messages.viber.Category
8
+ import com.vonage.client.messages.whatsapp.Locale
9
+ import com.vonage.client.messages.whatsapp.Policy
7
10
import org.junit.jupiter.api.Test
8
11
import org.junit.jupiter.api.assertThrows
9
12
import java.util.*
10
13
import kotlin.test.assertEquals
11
14
12
15
class MessagesTest : AbstractTest () {
13
- val messagesClient = vonageClient.messages
14
- val messageUuid = UUID .fromString(" aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab" )
15
- val mmsChannel = " mms"
16
- val whatsappChannel = " whatsapp"
17
- val viberChannel = " viber_service"
18
- val messengerChannel = " messenger"
19
- val fromNumber = " 447700900001"
20
- val toNumber = " 447712345689"
21
- val text = " Hello, World!"
22
- val caption = " Additional text to accompany the media"
23
- val imageUrl = " https://example.com/image.jpg"
24
- val audioUrl = " https://example.com/audio.mp3"
25
- val videoUrl = " https://example.com/video.mp4"
26
- val fileUrl = " https://example.com/file.pdf"
27
- val captionMap = mapOf (" caption" to caption)
16
+ private val messagesClient = vonageClient.messages
17
+ private val messageUuid = UUID .fromString(" aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab" )
18
+ private val mmsChannel = " mms"
19
+ private val whatsappChannel = " whatsapp"
20
+ private val viberChannel = " viber_service"
21
+ private val messengerChannel = " messenger"
22
+ private val fromNumber = " 447700900001"
23
+ private val toNumber = " 447712345689"
24
+ private val text = " Hello, World!"
25
+ private val caption = " Additional text to accompany the media"
26
+ private val imageUrl = " https://example.com/image.jpg"
27
+ private val audioUrl = " https://example.com/audio.mp3"
28
+ private val videoUrl = " https://example.com/video.mp4"
29
+ private val fileUrl = " https://example.com/file.pdf"
30
+ private val captionMap = mapOf (" caption" to caption)
28
31
29
32
private fun mockResponse (expectedBodyParams : Map <String , Any >) {
30
33
wiremock.post {
@@ -58,8 +61,8 @@ class MessagesTest : AbstractTest() {
58
61
" channel" to channel
59
62
)
60
63
61
- private fun textBody (channel : String ): Map <String , Any > =
62
- baseBody(" text" , channel) + mapOf (" text" to text)
64
+ private fun textBody (channel : String , additionalParams : Map < String , Any > = mapOf() ): Map <String , Any > =
65
+ baseBody(" text" , channel) + mapOf (" text" to text) + additionalParams
63
66
64
67
private fun mediaBody (channel : String , messageType : String , url : String , additionalParams : Map <String , Any >? = null): Map <String , Any > =
65
68
baseBody(messageType, channel) + mapOf (messageType to mapOf (" url" to url) + (additionalParams ? : mapOf ()))
@@ -76,11 +79,37 @@ class MessagesTest : AbstractTest() {
76
79
private fun fileBody (channel : String , additionalParams : Map <String , Any >? = null): Map <String , Any > =
77
80
mediaBody(channel, " file" , fileUrl, additionalParams)
78
81
82
+ private fun whatsappCustomBody (params : Map <String , Any >): Map <String , Any > =
83
+ baseBody(" custom" , whatsappChannel) + mapOf (" custom" to params)
79
84
80
85
@Test
81
- fun `send SMS text` () {
86
+ fun `send SMS text all parameters` () {
87
+ val clientRef = " My reference"
88
+ val webhookUrl = " https://example.com/status"
89
+ val ttl = 9000
90
+ val contentId = " 1107457532145798767"
91
+ val entityId = " 1101456324675322134"
92
+
93
+ testSend(textBody(" sms" , mapOf (
94
+ " client_ref" to clientRef,
95
+ " ttl" to ttl,
96
+ " webhook_url" to webhookUrl,
97
+ " webhook_version" to " v0.1" ,
98
+ " sms" to mapOf (
99
+ " content_id" to contentId,
100
+ " entity_id" to entityId
101
+ )
102
+ )), smsText {
103
+ from(fromNumber); to(toNumber); text(text); ttl(ttl);
104
+ clientRef(clientRef); contentId(contentId); entityId(entityId)
105
+ webhookUrl(webhookUrl); webhookVersion(MessagesVersion .V0_1 )
106
+ })
107
+ }
108
+
109
+ @Test
110
+ fun `send SMS text required parameters` () {
82
111
testSend(textBody(" sms" ), smsText {
83
- from(fromNumber); to(toNumber); text(text)
112
+ from(fromNumber); to(toNumber); text(text);
84
113
})
85
114
}
86
115
@@ -190,8 +219,8 @@ class MessagesTest : AbstractTest() {
190
219
" ttl" to ttl
191
220
)), viberVideo {
192
221
from(fromNumber); to(toNumber); url(videoUrl); caption(caption);
193
- category(Category .TRANSACTION )
194
- duration(duration); ttl(ttl); fileSize(fileSize); thumbUrl(thumbUrl)
222
+ category(Category .TRANSACTION ); duration(duration); ttl(ttl);
223
+ fileSize(fileSize); thumbUrl(thumbUrl)
195
224
}
196
225
)
197
226
}
@@ -244,4 +273,99 @@ class MessagesTest : AbstractTest() {
244
273
from(fromNumber); to(toNumber); id(stickerId); url(stickerUrl)
245
274
} }
246
275
}
276
+
277
+ @Test
278
+ fun `send WhatsApp template` () {
279
+ val messageContext = UUID .randomUUID().toString()
280
+ val name = " 9b6b4fcb_da19_4a26_8fe8_78074a91b584:verify"
281
+ val templateParams = listOf (" Un" , " Deux" , " Trois" )
282
+
283
+ val expectedBodyParams = baseBody(" template" , whatsappChannel) + mapOf (
284
+ " webhook_version" to " v1" ,
285
+ " context" to mapOf (" message_uuid" to messageContext),
286
+ " whatsapp" to mapOf (
287
+ " policy" to " deterministic" ,
288
+ " locale" to " fa"
289
+ ),
290
+ " template" to mapOf (
291
+ " name" to name,
292
+ " parameters" to templateParams
293
+ )
294
+ )
295
+
296
+ val request = whatsappTemplate {
297
+ from(fromNumber); to(toNumber); webhookVersion(MessagesVersion .V1 )
298
+ policy(Policy .DETERMINISTIC ); locale(Locale .PERSIAN )
299
+ contextMessageId(messageContext); name(name); parameters(templateParams)
300
+ }
301
+
302
+ testSend(expectedBodyParams, request)
303
+ }
304
+
305
+ @Test
306
+ fun `send WhatsApp custom` () {
307
+ val customParams = mapOf (
308
+ " type" to " contacts" ,
309
+ " contacts" to listOf (
310
+ mapOf (
311
+ " addresses" to listOf (mapOf (
312
+ " city" to " Birmingham"
313
+ ))
314
+ )
315
+ )
316
+ )
317
+
318
+ testSend(whatsappCustomBody(customParams), whatsappCustom {
319
+ from(fromNumber); to(toNumber); custom(customParams)
320
+ })
321
+ }
322
+
323
+ @Test
324
+ fun `send WhatsApp location` () {
325
+ val latitude = 51.5356396 ; val longitude = - 0.1077174
326
+ val name = " Business Design Centre"
327
+ val address = " 52 Upper St, London N1 0QH"
328
+
329
+ val params = whatsappCustomBody(mapOf (
330
+ " type" to " location" ,
331
+ " location" to mapOf (
332
+ " lat" to latitude, " long" to longitude,
333
+ " name" to name, " address" to address
334
+ )
335
+ )
336
+ )
337
+
338
+ testSend(params, whatsappLocation {
339
+ from(fromNumber); to(toNumber)
340
+ name(name); address(address)
341
+ latitude(latitude); longitude(longitude)
342
+ })
343
+ }
344
+
345
+ @Test
346
+ fun `send WhatsApp single product` () {
347
+ val bodyText = " Check this out:"
348
+ val footerText = " Hurry! While stocks last."
349
+ val catalogId = " Cat 1"
350
+ val productId = " prod_746"
351
+
352
+ val params = whatsappCustomBody(mapOf (
353
+ " type" to " interactive" ,
354
+ " interactive" to mapOf (
355
+ " type" to " product" ,
356
+ " body" to mapOf (" text" to bodyText),
357
+ " footer" to mapOf (" text" to footerText),
358
+ " action" to mapOf (
359
+ " catalog_id" to catalogId,
360
+ " product_retailer_id" to productId
361
+ )
362
+ )
363
+ ))
364
+
365
+ testSend(params, whatsappSingleProduct {
366
+ from(fromNumber); to(toNumber)
367
+ bodyText(bodyText); footerText(footerText)
368
+ catalogId(catalogId); productRetailerId(productId)
369
+ })
370
+ }
247
371
}
0 commit comments