Skip to content

Commit

Permalink
Send image tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 20, 2024
1 parent e796c2f commit 0f45369
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 20 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,4 @@ jobs:
java-version: ${{ matrix.java }}
cache: maven
- name: Compile with Maven
run: mvn --batch-mode clean verify
- name: Run Codecov
uses: codecov/codecov-action@v4
run: mvn --batch-mode clean verify
85 changes: 68 additions & 17 deletions src/test/kotlin/com/vonage/client/kt/MessagesTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vonage.client.kt

import com.fasterxml.jackson.databind.ObjectMapper
import com.marcinziolo.kotlin.wiremock.*
import com.vonage.client.messages.MessageRequest
import org.junit.jupiter.api.Test
Expand All @@ -8,21 +9,26 @@ import kotlin.test.assertEquals

class MessagesTest : AbstractTest() {
val messagesClient = vonageClient.messages
val messageUuid = UUID.fromString("aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab")
val mmsChannel = "mms"
val whatsappChannel = "whatsapp"
val viberChannel = "viber_service"
val messengerChannel = "messenger"
val fromNumber = "447700900001"
val toNumber = "447712345689"
val text = "Hello, World!"
val messageUuid = UUID.fromString("aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab")
val caption = "Additional text to accompany the media"
val imageUrl = "https://example.com/image.jpg"
val captionMap = mutableMapOf("caption" to caption)

private fun mockResponse(expectedBodyParams: Map<String, String>) {
private fun mockResponse(expectedBodyParams: Map<String, Any>) {
wiremock.post {
url equalTo "/v1/messages"
headers contains "User-Agent" like "vonage-java-sdk.*"
headers contains "Authorization" like "Bearer eyJ.+"
headers contains "Content-Type" equalTo "application/json"
headers contains "Accept" equalTo "application/json"
for (entry in expectedBodyParams.entries) {
body contains entry.key equalTo entry.value
}
body equalTo ObjectMapper().writeValueAsString(expectedBodyParams)
} returns {
header = "Content-Type" to "application/json"
statusCode = 202
Expand All @@ -34,44 +40,89 @@ class MessagesTest : AbstractTest() {
}
}

private fun testSend(expectedBodyParams: Map<String, String>, req: MessageRequest) {
private fun testSend(expectedBodyParams: Map<String, Any>, req: MessageRequest) {
mockResponse(expectedBodyParams)
assertEquals(messageUuid, messagesClient.send(req))
}

private fun expectedTextBody(channel: String): Map<String, String> = mapOf(
"message_type" to "text",
"text" to text,
"to" to toNumber,
"from" to fromNumber,
"channel" to channel
)
private fun baseBody(messageType: String, channel: String): Map<String, Any> =
mapOf(
"message_type" to messageType,
"to" to toNumber,
"from" to fromNumber,
"channel" to channel
)

private fun textBody(channel: String): Map<String, Any> =
baseBody("text", channel) + mapOf("text" to text)

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

private fun imageBody(channel: String, additionalParams : Map<String, Any>? = null): Map<String, Any> =
mediaBody(channel, "image", imageUrl, additionalParams)

@Test
fun `send SMS`() {
testSend(expectedTextBody("sms"), smsText {
testSend(textBody("sms"), smsText {
from(fromNumber); to(toNumber); text(text)
})
}

@Test
fun `send WhatsApp text`() {
testSend(expectedTextBody("whatsapp"), whatsappText {
testSend(textBody(whatsappChannel), whatsappText {
from(fromNumber); to(toNumber); text(text)
})
}

@Test
fun `send Viber text`() {
testSend(expectedTextBody("viber_service"), viberText {
testSend(textBody(viberChannel), viberText {
from(fromNumber); to(toNumber); text(text)
})
}

@Test
fun `send Messenger text`() {
testSend(expectedTextBody("messenger"), messengerText {
testSend(textBody(messengerChannel), messengerText {
from(fromNumber); to(toNumber); text(text)
})
}

@Test
fun `send MMS vCard`() {
val vcardUrl = "https://example.com/conatact.vcf"
testSend(mediaBody(mmsChannel, "vcard", vcardUrl, captionMap), mmsVcard {
from(fromNumber); to(toNumber); url(vcardUrl); caption(caption)
})
}

@Test
fun `send MMS image`() {
testSend(imageBody(mmsChannel, captionMap), mmsImage {
from(fromNumber); to(toNumber); url(imageUrl); caption(caption)
})
}

@Test
fun `send WhatsApp image`() {
testSend(imageBody(whatsappChannel, captionMap), whatsappImage {
from(fromNumber); to(toNumber); url(imageUrl); caption(caption)
})
}

@Test
fun `send Viber image`() {
testSend(imageBody(viberChannel), viberImage {
from(fromNumber); to(toNumber); url(imageUrl)
})
}

@Test
fun `send Messenger image`() {
testSend(imageBody(messengerChannel), messengerImage {
from(fromNumber); to(toNumber); url(imageUrl)
})
}
}

0 comments on commit 0f45369

Please sign in to comment.