1
1
package com.vonage.client.kt
2
2
3
+ import com.fasterxml.jackson.databind.ObjectMapper
3
4
import com.marcinziolo.kotlin.wiremock.*
4
5
import com.vonage.client.messages.MessageRequest
5
6
import org.junit.jupiter.api.Test
@@ -8,21 +9,26 @@ import kotlin.test.assertEquals
8
9
9
10
class MessagesTest : AbstractTest () {
10
11
val messagesClient = vonageClient.messages
12
+ val messageUuid = UUID .fromString(" aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab" )
13
+ val mmsChannel = " mms"
14
+ val whatsappChannel = " whatsapp"
15
+ val viberChannel = " viber_service"
16
+ val messengerChannel = " messenger"
11
17
val fromNumber = " 447700900001"
12
18
val toNumber = " 447712345689"
13
19
val text = " Hello, World!"
14
- val messageUuid = UUID .fromString(" aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab" )
20
+ val caption = " Additional text to accompany the media"
21
+ val imageUrl = " https://example.com/image.jpg"
22
+ val captionMap = mutableMapOf (" caption" to caption)
15
23
16
- private fun mockResponse (expectedBodyParams : Map <String , String >) {
24
+ private fun mockResponse (expectedBodyParams : Map <String , Any >) {
17
25
wiremock.post {
18
26
url equalTo " /v1/messages"
19
27
headers contains " User-Agent" like " vonage-java-sdk.*"
20
28
headers contains " Authorization" like " Bearer eyJ.+"
21
29
headers contains " Content-Type" equalTo " application/json"
22
30
headers contains " Accept" equalTo " application/json"
23
- for (entry in expectedBodyParams.entries) {
24
- body contains entry.key equalTo entry.value
25
- }
31
+ body equalTo ObjectMapper ().writeValueAsString(expectedBodyParams)
26
32
} returns {
27
33
header = " Content-Type" to " application/json"
28
34
statusCode = 202
@@ -34,44 +40,89 @@ class MessagesTest : AbstractTest() {
34
40
}
35
41
}
36
42
37
- private fun testSend (expectedBodyParams : Map <String , String >, req : MessageRequest ) {
43
+ private fun testSend (expectedBodyParams : Map <String , Any >, req : MessageRequest ) {
38
44
mockResponse(expectedBodyParams)
39
45
assertEquals(messageUuid, messagesClient.send(req))
40
46
}
41
47
42
- private fun expectedTextBody (channel : String ): Map <String , String > = mapOf (
43
- " message_type" to " text" ,
44
- " text" to text,
45
- " to" to toNumber,
46
- " from" to fromNumber,
47
- " channel" to channel
48
- )
48
+ private fun baseBody (messageType : String , channel : String ): Map <String , Any > =
49
+ mapOf (
50
+ " message_type" to messageType,
51
+ " to" to toNumber,
52
+ " from" to fromNumber,
53
+ " channel" to channel
54
+ )
55
+
56
+ private fun textBody (channel : String ): Map <String , Any > =
57
+ baseBody(" text" , channel) + mapOf (" text" to text)
58
+
59
+ private fun mediaBody (channel : String , messageType : String , url : String , additionalParams : Map <String , Any >? = null): Map <String , Any > =
60
+ baseBody(messageType, channel) + mapOf (messageType to mapOf (" url" to url) + (additionalParams ? : mapOf ()))
61
+
62
+ private fun imageBody (channel : String , additionalParams : Map <String , Any >? = null): Map <String , Any > =
63
+ mediaBody(channel, " image" , imageUrl, additionalParams)
49
64
50
65
@Test
51
66
fun `send SMS` () {
52
- testSend(expectedTextBody (" sms" ), smsText {
67
+ testSend(textBody (" sms" ), smsText {
53
68
from(fromNumber); to(toNumber); text(text)
54
69
})
55
70
}
56
71
57
72
@Test
58
73
fun `send WhatsApp text` () {
59
- testSend(expectedTextBody( " whatsapp " ), whatsappText {
74
+ testSend(textBody(whatsappChannel ), whatsappText {
60
75
from(fromNumber); to(toNumber); text(text)
61
76
})
62
77
}
63
78
64
79
@Test
65
80
fun `send Viber text` () {
66
- testSend(expectedTextBody( " viber_service " ), viberText {
81
+ testSend(textBody(viberChannel ), viberText {
67
82
from(fromNumber); to(toNumber); text(text)
68
83
})
69
84
}
70
85
71
86
@Test
72
87
fun `send Messenger text` () {
73
- testSend(expectedTextBody( " messenger " ), messengerText {
88
+ testSend(textBody(messengerChannel ), messengerText {
74
89
from(fromNumber); to(toNumber); text(text)
75
90
})
76
91
}
92
+
93
+ @Test
94
+ fun `send MMS vCard` () {
95
+ val vcardUrl = " https://example.com/conatact.vcf"
96
+ testSend(mediaBody(mmsChannel, " vcard" , vcardUrl, captionMap), mmsVcard {
97
+ from(fromNumber); to(toNumber); url(vcardUrl); caption(caption)
98
+ })
99
+ }
100
+
101
+ @Test
102
+ fun `send MMS image` () {
103
+ testSend(imageBody(mmsChannel, captionMap), mmsImage {
104
+ from(fromNumber); to(toNumber); url(imageUrl); caption(caption)
105
+ })
106
+ }
107
+
108
+ @Test
109
+ fun `send WhatsApp image` () {
110
+ testSend(imageBody(whatsappChannel, captionMap), whatsappImage {
111
+ from(fromNumber); to(toNumber); url(imageUrl); caption(caption)
112
+ })
113
+ }
114
+
115
+ @Test
116
+ fun `send Viber image` () {
117
+ testSend(imageBody(viberChannel), viberImage {
118
+ from(fromNumber); to(toNumber); url(imageUrl)
119
+ })
120
+ }
121
+
122
+ @Test
123
+ fun `send Messenger image` () {
124
+ testSend(imageBody(messengerChannel), messengerImage {
125
+ from(fromNumber); to(toNumber); url(imageUrl)
126
+ })
127
+ }
77
128
}
0 commit comments