Skip to content

Commit

Permalink
feat: Call.Builder.to* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jul 2, 2024
1 parent 40291d1 commit 6dc0124
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/kotlin/com/vonage/client/kt/Voice.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vonage.client.kt

import com.vonage.client.users.channels.Websocket.ContentType
import com.vonage.client.voice.*
import com.vonage.client.voice.ncco.*
import java.net.URI
Expand Down Expand Up @@ -135,3 +134,20 @@ fun connectToSip(uri: String, customHeaders: Map<String, Any>? = null, userToUse
}
return connectAction(builder.build(), properties)
}

fun Call.Builder.toPstn(number: String, dtmfAnswer: String? = null): Call.Builder =
to(com.vonage.client.voice.PhoneEndpoint(number, dtmfAnswer))

fun Call.Builder.toSip(uri: String, customHeaders: Map<String, Any>? = null,
userToUserHeader: String? = null): Call.Builder =
to(com.vonage.client.voice.SipEndpoint(uri, customHeaders, userToUserHeader))

fun Call.Builder.toWebSocket(uri: String, contentType: String? = null,
headers: Map<String, Any>? = null): Call.Builder =
to(com.vonage.client.voice.WebSocketEndpoint(uri, contentType, headers))

fun Call.Builder.toVbc(extension: String): Call.Builder =
to(com.vonage.client.voice.VbcEndpoint(extension))

fun Call.Builder.toApp(user: String): Call.Builder =
to(com.vonage.client.voice.AppEndpoint(user))
67 changes: 67 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/VoiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ class VoiceTest : AbstractTest() {
assertEquals(conversationId, callEvent.conversationUuid)
}

private fun testCreateCallToSingleEndpoint(toParams: Map<String, Any>,
toBuilder: Call.Builder.() -> Call.Builder) {
testCreateCall(mapOf(
"random_from_number" to true,
"answer_url" to listOf(onAnswerUrl),
"answer_method" to "GET",
"to" to listOf(toParams)
)) {
fromRandomNumber(true); answerUrl(onAnswerUrl)
toBuilder()
}
}

private fun testSingleNcco(additionalParams: Map<String, Any> = mapOf(), ncco: Action) {
val requestParams = mapOf(
"random_from_number" to true,
Expand Down Expand Up @@ -392,6 +405,60 @@ class VoiceTest : AbstractTest() {
}
}

@Test
fun `create call to PSTN`() {
val baseMap = mapOf("type" to phoneType, "number" to toNumber)
testCreateCallToSingleEndpoint(baseMap) {
toPstn(toNumber)
}

testCreateCallToSingleEndpoint(baseMap + mapOf("dtmfAnswer" to dtmf)) {
toPstn(toNumber, dtmf)
}
}

@Test
fun `create call to App`() {
testCreateCallToSingleEndpoint(mapOf("type" to "app", "user" to user)) {
toApp(user)
}
}

@Test
fun `create call to VBC`() {
testCreateCallToSingleEndpoint(mapOf("type" to "vbc", "extension" to vbcExt)) {
toVbc(vbcExt)
}
}

@Test
fun `create call to SIP`() {
val baseMap = mapOf("type" to "sip", "uri" to sipUri)
testCreateCallToSingleEndpoint(baseMap) {
toSip(sipUri)
}

testCreateCallToSingleEndpoint(baseMap + mapOf(
"headers" to customHeaders, "standard_headers" to mapOf("User-to-User" to userToUserHeader)
)) {
toSip(sipUri, customHeaders, userToUserHeader)
}
}

@Test
fun `create call to WebSocket`() {
val baseMap = mapOf("type" to "websocket", "uri" to websocketUri)
testCreateCallToSingleEndpoint(baseMap) {
toWebSocket(websocketUri)
}

testCreateCallToSingleEndpoint(baseMap + mapOf(
"content-type" to wsContentType, "headers" to customHeaders
)) {
toWebSocket(websocketUri, wsContentType, customHeaders)
}
}

@Test
fun `create call to all endpoint types with all fields and answer url`() {
val answerUrl = "https://example.com/answer"
Expand Down

0 comments on commit 6dc0124

Please sign in to comment.