From 6dc0124224318a5e9e7e1924f99f3264339a9a45 Mon Sep 17 00:00:00 2001 From: Sina Madani Date: Tue, 2 Jul 2024 17:02:38 +0100 Subject: [PATCH] feat: Call.Builder.to* methods --- src/main/kotlin/com/vonage/client/kt/Voice.kt | 18 ++++- .../kotlin/com/vonage/client/kt/VoiceTest.kt | 67 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/vonage/client/kt/Voice.kt b/src/main/kotlin/com/vonage/client/kt/Voice.kt index 3dc9649..6599863 100644 --- a/src/main/kotlin/com/vonage/client/kt/Voice.kt +++ b/src/main/kotlin/com/vonage/client/kt/Voice.kt @@ -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 @@ -135,3 +134,20 @@ fun connectToSip(uri: String, customHeaders: Map? = 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? = 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? = 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)) diff --git a/src/test/kotlin/com/vonage/client/kt/VoiceTest.kt b/src/test/kotlin/com/vonage/client/kt/VoiceTest.kt index 9dbb70a..6858687 100644 --- a/src/test/kotlin/com/vonage/client/kt/VoiceTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/VoiceTest.kt @@ -207,6 +207,19 @@ class VoiceTest : AbstractTest() { assertEquals(conversationId, callEvent.conversationUuid) } + private fun testCreateCallToSingleEndpoint(toParams: Map, + 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 = mapOf(), ncco: Action) { val requestParams = mapOf( "random_from_number" to true, @@ -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"