Skip to content

Commit

Permalink
test: More comprehensive NCCO testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jul 2, 2024
1 parent 77d404f commit 059bfc4
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/vonage/client/kt/Voice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fun CallsFilter.Builder.dateEnd(dateEnd: String): CallsFilter.Builder =
fun Call.Builder.advancedMachineDetection(amd: AdvancedMachineDetection.Builder.() -> Unit = {}): Call.Builder =
advancedMachineDetection(AdvancedMachineDetection.builder().apply(amd).build())

fun InputAction.Builder.speech(settings: SpeechSettings.Builder.() -> Unit): InputAction.Builder =
fun InputAction.Builder.speech(settings: SpeechSettings.Builder.() -> Unit = {}): InputAction.Builder =
speech(SpeechSettings.builder().apply(settings).build())

fun InputAction.Builder.dtmf(timeout: Int? = null, maxDigits: Int? = null, submitOnHash: Boolean? = null): InputAction.Builder {
Expand Down Expand Up @@ -91,7 +91,7 @@ fun talkAction(text: String, properties: TalkAction.Builder.() -> Unit = {}): Ta
fun streamAction(streamUrl: String, properties: StreamAction.Builder.() -> Unit = {}): StreamAction =
StreamAction.builder(streamUrl).apply(properties).build()

fun notifyAction(eventUrl: String, payload: Map<String, Any>? = null, eventMethod: EventMethod? = null): NotifyAction =
fun notifyAction(eventUrl: String, payload: Map<String, Any>, eventMethod: EventMethod? = null): NotifyAction =
NotifyAction.builder(payload, eventUrl).eventMethod(eventMethod).build()

fun inputAction(properties: InputAction.Builder.() -> Unit = {}): InputAction =
Expand Down
137 changes: 133 additions & 4 deletions src/test/kotlin/com/vonage/client/kt/VoiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.vonage.client.kt

import com.vonage.client.common.HttpMethod
import com.vonage.client.voice.*
import com.vonage.client.voice.PhoneEndpoint
import com.vonage.client.voice.ncco.*
import java.net.URI
import java.time.Instant
Expand Down Expand Up @@ -32,6 +31,7 @@ class VoiceTest : AbstractTest() {
private val streamUrl = "https://example.com/waiting.mp3"
private val websocketUri = "wss://example.com/socket"
private val sipUri = "sip:[email protected]"
private val conversationName = "selective-audio Demo"
private val customHeaders = mapOf(
"customer_id" to "abc123",
"purchases" to 19,
Expand Down Expand Up @@ -203,6 +203,24 @@ class VoiceTest : AbstractTest() {
assertEquals(conversationId, callEvent.conversationUuid)
}

private fun testSingleNcco(additionalParams: Map<String, Any> = mapOf(), ncco: Action) {
val requestParams = mapOf(
"random_from_number" to true,
"to" to listOf(mapOf(
"type" to "websocket",
"uri" to websocketUri
)),
"ncco" to listOf(
mapOf("action" to ncco.action) + additionalParams
)
)

testCreateCall(requestParams) {
ncco(ncco); fromRandomNumber(true);
to(com.vonage.client.voice.WebSocketEndpoint(websocketUri, null, null))
}
}

@Test
fun `terminate call`() {
testModifyCall("hangup", callObj::hangup)
Expand Down Expand Up @@ -361,8 +379,8 @@ class VoiceTest : AbstractTest() {
"text" to ssmlText
))
)) {
fromRandomNumber(true); to(PhoneEndpoint(toNumber))
ncco(talkAction(ssmlText))
fromRandomNumber(true); ncco(talkAction(ssmlText))
to(com.vonage.client.voice.PhoneEndpoint(toNumber))
}
}

Expand Down Expand Up @@ -441,14 +459,125 @@ class VoiceTest : AbstractTest() {
}
}

@Test
fun `create call with talk action required parameters only`() {
testSingleNcco(mapOf("text" to text), talkAction(text))
}

@Test
fun `create call with stream action required parameters only`() {
testSingleNcco(mapOf("streamUrl" to listOf(streamUrl)), streamAction(streamUrl))
}

@Test
fun `create call with conversation action required parameters only`() {
testSingleNcco(mapOf("name" to conversationName), conversationAction(conversationName))
}

@Test
fun `create call with input action required parameters only`() {
testSingleNcco(ncco = inputAction())
}

@Test
fun `create call with notify action required parameters only`() {
testSingleNcco(
mapOf("eventUrl" to listOf(eventUrl), "payload" to mapOf<String, Any>()),
notifyAction(eventUrl, mapOf())
)
}

@Test
fun `create call with record action required parameters only`() {
testSingleNcco(ncco = recordAction())
}

@Test
fun `create call with connect action required parameters only`() {
testSingleNcco(
mapOf("endpoint" to listOf(mapOf("type" to "vbc", "extension" to vbcExt))),
connectAction(com.vonage.client.voice.ncco.VbcEndpoint.builder(vbcExt).build())
)
}

@Test
fun `create call with all NCCO actions required parameters and empty builder properties`() {
val emptyMap = mapOf<String, Any>()

testCreateCall(mapOf(
"random_from_number" to true,
"to" to listOf(mapOf(
"type" to "vbc",
"extension" to vbcExt
)),
"advanced_machine_detection" to emptyMap,
"ncco" to listOf(
mapOf(
"action" to "talk",
"text" to text
),
mapOf(
"action" to "stream",
"streamUrl" to listOf(streamUrl)
),
mapOf(
"action" to "conversation",
"name" to conversationName,
"record" to true,
"transcription" to emptyMap
),
mapOf(
"action" to "input",
"speech" to emptyMap,
"dtmf" to emptyMap
),
mapOf(
"action" to "notify",
"eventUrl" to listOf(eventUrl),
"payload" to emptyMap
),
mapOf(
"action" to "record",
"transcription" to emptyMap
),
mapOf(
"action" to "connect",
"endpoint" to listOf(mapOf(
"type" to "sip",
"uri" to sipUri
)),
"advancedMachineDetection" to emptyMap
)
)
)) {
fromRandomNumber(true); to(com.vonage.client.voice.VbcEndpoint(vbcExt))
advancedMachineDetection(); ncco(
talkAction(text),
streamAction(streamUrl),
conversationAction(conversationName) {
transcription(); record(true)
},
inputAction {
speech(); dtmf()
},
notifyAction(eventUrl, mapOf()),
recordAction {
transcription()
},
connectAction(com.vonage.client.voice.ncco.SipEndpoint.builder(sipUri).build()) {
advancedMachineDetection()
}
)
}
}

@Test
fun `create call with all NCCO actions and all parameters of those actions`() {
val bargeIn = false
val premium = true
val loop = 2
val style = 1
val level = -0.5f
val conversationName = "selective-audio Demo"
val canHearId = UUID.randomUUID().toString()
val canSpeakId = UUID.randomUUID().toString()
val conversationEventMethod = EventMethod.POST
Expand Down

0 comments on commit 059bfc4

Please sign in to comment.