Skip to content

Commit 9858c9e

Browse files
committed
Test WhatsApp-specific messages
1 parent 3f3d0ce commit 9858c9e

File tree

2 files changed

+149
-21
lines changed

2 files changed

+149
-21
lines changed

src/main/kotlin/com/vonage/client/kt/Messages.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ fun whatsappTemplate(init: WhatsappTemplateRequest.Builder.() -> Unit): Whatsapp
7474
return WhatsappTemplateRequest.builder().apply(init).build()
7575
}
7676

77+
fun whatsappCustom(init: WhatsappCustomRequest.Builder.() -> Unit): WhatsappCustomRequest {
78+
return WhatsappCustomRequest.builder().apply(init).build()
79+
}
80+
7781
fun messengerText(init: MessengerTextRequest.Builder.() -> Unit): MessengerTextRequest {
7882
return MessengerTextRequest.builder().apply(init).build()
7983
}

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

Lines changed: 145 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,31 @@ package com.vonage.client.kt
33
import com.fasterxml.jackson.databind.ObjectMapper
44
import com.marcinziolo.kotlin.wiremock.*
55
import com.vonage.client.messages.MessageRequest
6+
import com.vonage.client.messages.MessagesVersion
67
import com.vonage.client.messages.viber.Category
8+
import com.vonage.client.messages.whatsapp.Locale
9+
import com.vonage.client.messages.whatsapp.Policy
710
import org.junit.jupiter.api.Test
811
import org.junit.jupiter.api.assertThrows
912
import java.util.*
1013
import kotlin.test.assertEquals
1114

1215
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)
2831

2932
private fun mockResponse(expectedBodyParams: Map<String, Any>) {
3033
wiremock.post {
@@ -58,8 +61,8 @@ class MessagesTest : AbstractTest() {
5861
"channel" to channel
5962
)
6063

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
6366

6467
private fun mediaBody(channel: String, messageType: String, url: String, additionalParams: Map<String, Any>? = null): Map<String, Any> =
6568
baseBody(messageType, channel) + mapOf(messageType to mapOf("url" to url) + (additionalParams ?: mapOf()))
@@ -76,11 +79,37 @@ class MessagesTest : AbstractTest() {
7679
private fun fileBody(channel: String, additionalParams : Map<String, Any>? = null): Map<String, Any> =
7780
mediaBody(channel, "file", fileUrl, additionalParams)
7881

82+
private fun whatsappCustomBody(params: Map<String, Any>): Map<String, Any> =
83+
baseBody("custom", whatsappChannel) + mapOf("custom" to params)
7984

8085
@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`() {
82111
testSend(textBody("sms"), smsText {
83-
from(fromNumber); to(toNumber); text(text)
112+
from(fromNumber); to(toNumber); text(text);
84113
})
85114
}
86115

@@ -190,8 +219,8 @@ class MessagesTest : AbstractTest() {
190219
"ttl" to ttl
191220
)), viberVideo {
192221
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)
195224
}
196225
)
197226
}
@@ -244,4 +273,99 @@ class MessagesTest : AbstractTest() {
244273
from(fromNumber); to(toNumber); id(stickerId); url(stickerUrl)
245274
} }
246275
}
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+
}
247371
}

0 commit comments

Comments
 (0)