Skip to content

Commit

Permalink
feat: Add modify call methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 26, 2024
1 parent 77c022c commit e9753f1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 21 deletions.
31 changes: 28 additions & 3 deletions src/main/kotlin/com/vonage/client/kt/Voice.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
package com.vonage.client.kt

import com.vonage.client.voice.CallInfo
import com.vonage.client.voice.CallInfoPage
import com.vonage.client.voice.CallsFilter
import com.vonage.client.voice.VoiceClient
import com.vonage.client.voice.ncco.Ncco
import java.net.URI
import java.util.*

class Voice(private val voiceClient: VoiceClient) {

fun getCall(callId: UUID): CallInfo = getCall(callId.toString())
fun call(callId: String): Call = Call(callId)

fun getCall(callId: String): CallInfo = voiceClient.getCallDetails(callId)
fun call(callId: UUID): Call = call(callId.toString())


inner class Call(val callId: String) {

fun get(): CallInfo = voiceClient.getCallDetails(callId)

fun hangup() = voiceClient.terminateCall(callId)

fun mute() = voiceClient.muteCall(callId)

fun unmute() = voiceClient.unmuteCall(callId)

fun earmuff() = voiceClient.earmuffCall(callId)

fun unearmuff() = voiceClient.unearmuffCall(callId)

fun transfer(ncco: Ncco) = voiceClient.transferCall(callId, ncco)

fun transfer(nccoUrl: String) = voiceClient.transferCall(callId, nccoUrl)

fun transfer(nccoUrl: URI) = transfer(nccoUrl.toString())
}

fun listCalls(filter: CallsFilter? = null): CallInfoPage = voiceClient.listCalls(filter)
}
36 changes: 28 additions & 8 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,38 @@ abstract class AbstractTest {
else -> throw IllegalArgumentException("Unhandled HTTP method: $httpMethod")
})

protected fun mockJsonJwtPost(expectedUrl: String,
expectedRequestParams: Map<String, Any>? = null,
status: Int = 200,
expectedResponseParams: Map<String, Any>? = null) =
private fun mockP(requestMethod: HttpMethod, expectedUrl: String,
expectedRequestParams: Map<String, Any>? = null,
status: Int = 200, authType: AuthType? = AuthType.JWT,
expectedResponseParams: Map<String, Any>? = null) =

mockRequest(HttpMethod.POST, expectedUrl,
mockRequest(requestMethod, expectedUrl,
contentType = if (expectedRequestParams != null) ContentType.APPLICATION_JSON else null,
accept = if (expectedResponseParams != null && status < 400) ContentType.APPLICATION_JSON else null,
AuthType.JWT, expectedRequestParams
authType = authType, expectedRequestParams
).mockReturn(status, expectedResponseParams)

protected fun mockPost(expectedUrl: String,
expectedRequestParams: Map<String, Any>? = null,
status: Int = 200,
authType: AuthType? = AuthType.JWT,
expectedResponseParams: Map<String, Any>? = null) =
mockP(HttpMethod.POST, expectedUrl, expectedRequestParams, status, authType, expectedResponseParams)

protected fun mockPut(expectedUrl: String,
expectedRequestParams: Map<String, Any>? = null,
status: Int = 200,
authType: AuthType? = AuthType.JWT,
expectedResponseParams: Map<String, Any>? = null) =
mockP(HttpMethod.PUT, expectedUrl, expectedRequestParams, status, authType, expectedResponseParams)

protected fun mockPatch(expectedUrl: String,
expectedRequestParams: Map<String, Any>? = null,
status: Int = 200,
authType: AuthType? = AuthType.JWT,
expectedResponseParams: Map<String, Any>? = null) =
mockP(HttpMethod.PUT, expectedUrl, expectedRequestParams, status, authType, expectedResponseParams)

protected fun mockDelete(expectedUrl: String, authType: AuthType? = null) =
mockRequest(HttpMethod.DELETE, expectedUrl, authType = authType).mockReturn(204)

Expand All @@ -134,8 +155,7 @@ abstract class AbstractTest {

mockRequest(HttpMethod.GET, expectedUrl,
contentType = if (expectedQueryParams != null) ContentType.FORM_URLENCODED else null,
accept = ContentType.APPLICATION_JSON,
authType = authType, expectedQueryParams
accept = ContentType.APPLICATION_JSON, authType, expectedQueryParams
).mockReturn(status, expectedResponseParams)


Expand Down
5 changes: 4 additions & 1 deletion src/test/kotlin/com/vonage/client/kt/MessagesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class MessagesTest : AbstractTest() {
val status = 202
val expectedResponseParams = mapOf("message_uuid" to messageUuid)

mockJsonJwtPost(sendUrl, expectedBodyParams, status, expectedResponseParams)
mockPost(
expectedUrl = sendUrl, expectedRequestParams = expectedBodyParams,
status = status, expectedResponseParams = expectedResponseParams
)
assertEquals(messageUuid, messagesClient.send(req))

// TODO fix mocking full url
Expand Down
12 changes: 6 additions & 6 deletions src/test/kotlin/com/vonage/client/kt/VerifyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class VerifyTest : AbstractTest() {
@Test
fun `send verification single workflow required parameters`() {
for (channel in Channel.entries) {
mockJsonJwtPost(
mockPost(
baseUrl, status = 202, expectedRequestParams = mapOf(
"brand" to brand, "workflow" to listOf(
mapOf(
Expand Down Expand Up @@ -92,7 +92,7 @@ class VerifyTest : AbstractTest() {

@Test
fun `send verification all workflows and parameters`() {
mockJsonJwtPost(baseUrl,
mockPost(baseUrl,
expectedRequestParams = mapOf(
"brand" to brand,
"client_ref" to clientRef,
Expand Down Expand Up @@ -170,7 +170,7 @@ class VerifyTest : AbstractTest() {
@Test
fun `next workflow`() {
val expectedUrl = "$requestIdUrl/next-workflow"
mockJsonJwtPost(expectedUrl)
mockPost(expectedUrl)
verifyClient.nextWorkflow(requestIdStr)
verifyClient.nextWorkflow(requestId)
assertVerifyResponseException(expectedUrl, HttpMethod.POST) {
Expand All @@ -185,7 +185,7 @@ class VerifyTest : AbstractTest() {
fun `check valid verification code`() {
val call: () -> Boolean = {verifyClient.isValidVerificationCode(requestIdStr, code)}

mockJsonJwtPost(requestIdUrl, checkCodeRequestParams, 200)
mockPost(requestIdUrl, checkCodeRequestParams, 200)
assertTrue(call.invoke())
verifyClient.checkVerificationCode(requestIdStr, code)
verifyClient.checkVerificationCode(requestId, code)
Expand All @@ -194,14 +194,14 @@ class VerifyTest : AbstractTest() {

val title = "Invalid Code"

mockJsonJwtPost(requestIdUrl, checkCodeRequestParams, 400, mapOf(
mockPost(requestIdUrl, checkCodeRequestParams, 400, expectedResponseParams = mapOf(
"title" to title,
"type" to "https://www.developer.vonage.com/api-errors/verify#invalid-code",
"detail" to "The code you provided does not match the expected value."
))
assertFalse(call.invoke())

mockJsonJwtPost(requestIdUrl, checkCodeRequestParams, 410, mapOf(
mockPost(requestIdUrl, checkCodeRequestParams, 410, expectedResponseParams = mapOf(
"title" to title,
"type" to "https://www.developer.vonage.com/api-errors/verify#expired",
"detail" to "An incorrect code has been provided too many times. Workflow terminated."
Expand Down
38 changes: 35 additions & 3 deletions src/test/kotlin/com/vonage/client/kt/VoiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,50 @@ import kotlin.test.assertNotNull

class VoiceTest : AbstractTest() {
private val voiceClient = vonage.voice
private val callsUrl = "/v1/calls"
private val callsBaseUrl = "/v1/calls"
private val callIdStr = "63f61863-4a51-4f6b-86e1-46edebcf9356"
private val callUrl = "$callsBaseUrl/$callIdStr"
private val callObj = voiceClient.call(UUID.fromString(callIdStr))
private val conversationId = "CON-f972836a-550f-45fa-956c-12a2ab5b7d22"

private fun testModifyCall(actionName: String, invocation: () -> Unit) {
mockPut(expectedUrl = callUrl, expectedRequestParams = mapOf("action" to actionName), status = 204)
invocation.invoke()
}

@Test
fun `terminate call`() {
testModifyCall("hangup", callObj::hangup)
}

@Test
fun `mute call`() {
testModifyCall("mute", callObj::mute)
}

@Test
fun `umute call`() {
testModifyCall("unmute", callObj::unmute)
}

@Test
fun `earmuff call`() {
testModifyCall("earmuff", callObj::earmuff)
}

@Test
fun `umearmuff call`() {
testModifyCall("unearmuff", callObj::unearmuff)
}

@Test
fun `get call`() {
val price = "23.40"
val duration = 60
val rate = "0.39"
val phoneType = "phone"

mockGet(expectedUrl = "$callsUrl/$callIdStr", expectedResponseParams = mapOf(
mockGet(expectedUrl = callUrl, expectedResponseParams = mapOf(
"_links" to mapOf(
"self" to mapOf(
"href" to "/calls/$callIdStr"
Expand All @@ -48,7 +80,7 @@ class VoiceTest : AbstractTest() {
"network" to networkCode
))

val response = voiceClient.getCall(UUID.fromString(callIdStr))
val response = callObj.get()
assertNotNull(response)
assertEquals(callIdStr, response.uuid)
assertEquals(conversationId, response.conversationUuid)
Expand Down

0 comments on commit e9753f1

Please sign in to comment.