Skip to content

Commit

Permalink
fix: Default values for streamAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 28, 2024
1 parent b0e5243 commit c350dad
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/main/kotlin/com/vonage/client/kt/Voice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ class Voice(private val voiceClient: VoiceClient) {

fun sendDtmf(digits: String): DtmfResponse = voiceClient.sendDtmf(callId, digits)

fun streamAudio(streamUrl: String, loop: Int? = null, level: Double? = null): StreamResponse =
if (loop != null && level != null) voiceClient.startStream(callId, streamUrl, loop, level)
else if (loop != null) voiceClient.startStream(callId, streamUrl, loop)
else voiceClient.startStream(callId, streamUrl)
fun streamAudio(streamUrl: String, loop: Int = 1, level: Double = 0.0): StreamResponse =
voiceClient.startStream(callId, streamUrl, loop, level)

fun stopStream(): StreamResponse = voiceClient.stopStream(callId)

Expand Down
15 changes: 9 additions & 6 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,20 @@ abstract class AbstractTest {

protected inline fun <reified E: VonageApiResponseException> assertApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Any, status: Int,
errorType: String, title: String, detail: String, instance: String): E {
errorType: String? = null, title: String? = null,
detail: String? = null, instance: String? = null): E {

mockRequest(requestMethod, url).mockReturn(status, mapOf(
"type" to errorType, "title" to title,
"detail" to detail, "instance" to instance
))
val responseParams = mutableMapOf<String, Any>()
if (errorType != null) responseParams["type"] = errorType
if (title != null) responseParams["title"] = title
if (detail != null) responseParams["detail"] = detail
if (instance != null) responseParams["instance"] = instance

mockRequest(requestMethod, url).mockReturn(status, responseParams)
val exception = assertThrows<E> { actualCall.invoke() }

assertEquals(status, exception.statusCode)
assertEquals(URI.create(errorType), exception.type)
assertEquals(if (errorType != null) URI.create(errorType) else null, exception.type)
assertEquals(title, exception.title)
assertEquals(instance, exception.instance)
assertEquals(detail, exception.detail)
Expand Down
23 changes: 19 additions & 4 deletions src/test/kotlin/com/vonage/client/kt/VoiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ class VoiceTest : AbstractTest() {
assertEqualsSampleCall(infos[1])
}

private fun assertExistingCall404(url: String, requestMethod: HttpMethod, invocation: () -> Any) {
assertApiResponseException<VoiceResponseException>(url, requestMethod, invocation, 404,
title = "Not Found", detail = "Call $callIdStr doesn't exist."
)
}

private fun testModifyCall(actionName: String = "transfer", invocation: () -> Unit,
nccoAction: Map<String, Any>? = null, nccoUrl: String? = null) {
mockPut(expectedUrl = callUrl,
Expand All @@ -112,21 +118,22 @@ class VoiceTest : AbstractTest() {
status = 204
)
invocation.invoke()
assertExistingCall404(callUrl, HttpMethod.PUT, invocation)
}

private fun testStream(loop: Int = 1, level: Double = 0.0, invocation: (() -> StreamResponse)? = null) {

val message = "Stream ${if (invocation == null) "stopped" else "started"}"
val expectedResponseParams = mapOf("message" to message, "uuid" to callIdStr)
val expectedUrl = "$callUrl/stream"
val streamUrl = "$callUrl/stream"
val response = if (invocation == null) {
mockDelete(expectedUrl, expectedResponseParams = expectedResponseParams)
mockDelete(streamUrl, expectedResponseParams = expectedResponseParams)
callObj.stopStream()
}
else {
mockPut(expectedUrl, status = 200,
mockPut(streamUrl, status = 200,
expectedRequestParams = mapOf(
"stream_url" to listOf(streamUrl),
"stream_url" to listOf(this.streamUrl),
"loop" to loop, "level" to level
),
expectedResponseParams = expectedResponseParams
Expand All @@ -136,6 +143,9 @@ class VoiceTest : AbstractTest() {
assertNotNull(response)
assertEquals(message, response.message)
assertEquals(callIdStr, response.uuid)

if (invocation != null) assertExistingCall404(streamUrl, HttpMethod.PUT, invocation)
else assertExistingCall404(streamUrl, HttpMethod.DELETE, callObj::stopStream)
}

private fun testTextToSpeech(expectedRequestParams: Map<String, Any>? = null, invocation: () -> TalkResponse) {
Expand All @@ -152,6 +162,11 @@ class VoiceTest : AbstractTest() {
assertNotNull(response)
assertEquals(message, response.message)
assertEquals(callIdStr, response.uuid)

assertExistingCall404(talkUrl,
if (expectedRequestParams != null) HttpMethod.PUT else HttpMethod.DELETE,
invocation
)
}

@Test
Expand Down

0 comments on commit c350dad

Please sign in to comment.