Skip to content

Commit

Permalink
Fix Call Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacakakpo1 committed Apr 11, 2024
1 parent 9020d1e commit a0a0958
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
58 changes: 30 additions & 28 deletions telnyx_rtc/src/main/java/com/telnyx/webrtc/sdk/TelnyxClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class TelnyxClient(
internal var socket: TxSocket
private var providedHostAddress: String? = null
private var providedPort: Int? = null
private var providedTurn: String? = null
private var providedStun: String? = null
internal var providedTurn: String? = null
internal var providedStun: String? = null

// MediaPlayer for ringtone / ringbacktone
private var mediaPlayer: MediaPlayer? = null
Expand Down Expand Up @@ -148,7 +148,8 @@ class TelnyxClient(
peerConnection?.getLocalDescription()?.description
if (sessionDescriptionString == null) {
callStateLiveData.postValue(CallState.ERROR)
} else {
}
else {
val answerBodyMessage = SendingMessageBody(
uuid, SocketMethod.ANSWER.methodName,
CallParams(
Expand Down Expand Up @@ -1113,17 +1114,17 @@ class TelnyxClient(
}

override fun onOfferReceived(jsonObject: JsonObject) {
Timber.d("[%s] :: onOfferReceived [%s]", this@TelnyxClient.javaClass.simpleName, jsonObject)
val offerCall = call!!.copy(
context = context,
client = this,
socket = socket,
sessionId = sessid,
audioManager = audioManager!!,
providedTurn = providedTurn!!,
providedStun = providedStun!!
).apply {
if (jsonObject.has("params")) {
if (jsonObject.has("params")) {
Timber.d("[%s] :: onOfferReceived [%s]", this@TelnyxClient.javaClass.simpleName, jsonObject)
val offerCall = call!!.copy(
context = context,
client = this,
socket = socket,
sessionId = sessid,
audioManager = audioManager!!,
providedTurn = providedTurn!!,
providedStun = providedStun!!
).apply {
val params = jsonObject.getAsJsonObject("params")
val offerCallId = UUID.fromString(params.get("callID").asString)
val remoteSdp = params.get("sdp").asString
Expand Down Expand Up @@ -1169,23 +1170,24 @@ class TelnyxClient(
)
this.inviteResponse = inviteResponse

} else {
Timber.d(
"[%s] :: Invalid offer received, missing required parameters [%s]",
this.javaClass.simpleName, jsonObject
)
}
}
addToCalls(offerCall)
offerCall.client.socketResponseLiveData.postValue(
SocketResponse.messageReceived(
ReceivedMessageBody(
SocketMethod.INVITE.methodName,
offerCall.inviteResponse
addToCalls(offerCall)
offerCall.client.socketResponseLiveData.postValue(
SocketResponse.messageReceived(
ReceivedMessageBody(
SocketMethod.INVITE.methodName,
offerCall.inviteResponse
)
)
)
)
offerCall.client.playRingtone()
offerCall.client.playRingtone()
} else {
Timber.d(
"[%s] :: Invalid offer received, missing required parameters [%s]",
this.javaClass.simpleName, jsonObject
)
}

}

override fun onRingingReceived(jsonObject: JsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ object ConnectivityHelper {
try {
val request = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
.build()
val manager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
Expand Down
16 changes: 14 additions & 2 deletions telnyx_rtc/src/test/java/com/telnyx/webrtc/sdk/CallTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.junit.rules.TestRule
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.mockito.Spy
import timber.log.Timber
import java.util.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -174,7 +175,9 @@ class CallTest : BaseTest() {
call = Mockito.spy(
Call(mockContext, client, client.socket, "123", audioManager)
)
call.acceptCall(UUID.randomUUID(), "00000")
call.callId = UUID.randomUUID()
client.addToCalls(call)
call.acceptCall(call.callId, "00000")
assertEquals(call.getCallState().value, CallState.ERROR)
}

Expand Down Expand Up @@ -262,7 +265,16 @@ class CallTest : BaseTest() {
call = Mockito.spy(
Call(mockContext, client, client.socket, "123", audioManager)
)
call.client.onErrorReceived(JsonObject())
val jsonObject = JsonObject().apply {
add("error", JsonObject().apply {
addProperty("message", "Your error message here")
})
}

//write a way to test this
//

call.client.onErrorReceived(jsonObject)
}
}
}
Expand Down
27 changes: 18 additions & 9 deletions telnyx_rtc/src/test/java/com/telnyx/webrtc/sdk/TelnyxClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class TelnyxClientTest : BaseTest() {
every { mockContext.getSystemService(AppCompatActivity.AUDIO_SERVICE) } returns audioManager

client = TelnyxClient(mockContext)
client.connect(txPushMetaData = null)
}

private fun networkCallbackSetup() {
Expand Down Expand Up @@ -479,7 +480,7 @@ class TelnyxClientTest : BaseTest() {
Mockito.`when`(client.call).thenReturn(fakeCall)
val callId = UUID.randomUUID()
client.onByeReceived(callId)
Mockito.verify(client.call, Mockito.atLeast(1))?.client?.onByeReceived(callId)
Mockito.verify(client, Mockito.atLeast(1))?.onByeReceived(callId)
}

@Test
Expand All @@ -499,7 +500,7 @@ class TelnyxClientTest : BaseTest() {
params.addProperty("callID", callID.toString())
callMessage.add("params", params)
client.onAnswerReceived(callMessage)
Mockito.verify(client.call, Mockito.atLeast(1))?.client?.onAnswerReceived(callMessage)
Mockito.verify(client, Mockito.atLeast(1))?.onAnswerReceived(callMessage)
}

@Test
Expand All @@ -516,6 +517,8 @@ class TelnyxClientTest : BaseTest() {
val callMessage = JsonObject()
val params = JsonObject()
val callID = UUID.randomUUID()
fakeCall.callId = callID
client.addToCalls(fakeCall)
params.addProperty("callID", callID.toString())
params.addProperty("sdp","sdp")
val dialogParams = JsonObject()
Expand All @@ -527,7 +530,7 @@ class TelnyxClientTest : BaseTest() {
params.add("dialogParams", dialogParams)
callMessage.add("params", params)
client.onAnswerReceived(callMessage)
Mockito.verify(client.call, Mockito.atLeast(1))?.client?.onAnswerReceived(callMessage)
Mockito.verify(client, Mockito.atLeast(1))?.onAnswerReceived(callMessage)
assert(fakeCall.answerResponse != null)
}

Expand All @@ -550,7 +553,7 @@ class TelnyxClientTest : BaseTest() {
params.addProperty("callID", callID.toString())
callMessage.add("params", params)
client.onMediaReceived(callMessage)
Mockito.verify(client.call, Mockito.atLeast(1))?.client?.onMediaReceived(callMessage)
Mockito.verify(client, Mockito.atLeast(1))?.onMediaReceived(callMessage)
}

@Test
Expand All @@ -567,10 +570,14 @@ class TelnyxClientTest : BaseTest() {
val callMessage = JsonObject()
val params = JsonObject()
val callID = UUID.randomUUID()
fakeCall.callId = callID
params.addProperty("callID", callID.toString())
callMessage.add("incorrect_params", params)
client.providedTurn = "stun:rtc.telnyx.com"
client.providedStun = "turn:rtc.telnyx.com"

client.onOfferReceived(callMessage)
Mockito.verify(client.call, Mockito.atLeast(1))?.client?.onOfferReceived(callMessage)
Mockito.verify(client, Mockito.atLeast(1))?.onOfferReceived(callMessage)
}

@Test
Expand All @@ -592,7 +599,7 @@ class TelnyxClientTest : BaseTest() {
params.addProperty("telnyx_leg_id", callID.toString())
callMessage.add("params", params)
client.onRingingReceived(callMessage)
Mockito.verify(client.call, Mockito.atLeast(1))?.client?.onRingingReceived(callMessage)
Mockito.verify(client, Mockito.atLeast(1))?.onRingingReceived(callMessage)
}

@Test
Expand All @@ -609,14 +616,16 @@ class TelnyxClientTest : BaseTest() {
val callMessage = JsonObject()
val params = JsonObject()
val callID = UUID.randomUUID()
fakeCall.telnyxSessionId = callID
fakeCall.telnyxLegId = callID
params.addProperty("callID", callID.toString())
params.addProperty("telnyx_session_id", callID.toString())
params.addProperty("telnyx_leg_id", callID.toString())
callMessage.add("params", params)
client.onRingingReceived(callMessage)
Mockito.verify(client.call, Mockito.atLeast(1))?.client?.onRingingReceived(callMessage)
assertEquals(client.call?.getTelnyxSessionId(), callID)
assertEquals(client.call?.getTelnyxLegId(), callID)
Mockito.verify(client, Mockito.atLeast(1))?.onRingingReceived(callMessage)
assertEquals(fakeCall.getTelnyxSessionId(), callID)
assertEquals(fakeCall.getTelnyxLegId(), callID)
}

@Test
Expand Down

0 comments on commit a0a0958

Please sign in to comment.