Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(infinity): introduce a pre-configured Json #253

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
kotlinx-serialization-json-okio = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json-okio", version.ref = "kotlinx-serialization" }
# Dummy library for Dependabot to pick up ktlint version changes
ktlint-cli = { module = "com.pinterest.ktlint:ktlint-cli", version.ref = "ktlint" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package com.pexip.sdk.api.infinity
import com.pexip.sdk.api.Call
import com.pexip.sdk.api.EventSourceFactory
import com.pexip.sdk.api.infinity.internal.InfinityServiceImpl
import com.pexip.sdk.infinity.Infinity
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.plus
import okhttp3.OkHttpClient
import java.net.URL
import java.util.UUID
Expand Down Expand Up @@ -706,10 +708,8 @@ public interface InfinityService {

public companion object {

internal val Json = Json {
ignoreUnknownKeys = true
coerceInputValues = true
serializersModule = SerializersModule {
internal val Json = Json(from = Infinity.Json) {
serializersModule += SerializersModule {
polymorphicDefaultDeserializer(ElementResponse::class) {
ElementResponse.Unknown.serializer()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import assertk.assertions.isEqualTo
import assertk.assertions.isInstanceOf
import com.pexip.sdk.api.coroutines.await
import com.pexip.sdk.api.infinity.internal.addPathSegment
import com.pexip.sdk.infinity.Infinity
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockWebServer
import org.junit.Rule
import java.net.URL
Expand All @@ -43,7 +42,6 @@ internal class CallStepTest {
private lateinit var conferenceAlias: String
private lateinit var participantId: UUID
private lateinit var callId: UUID
private lateinit var json: Json
private lateinit var step: InfinityService.CallStep

@BeforeTest
Expand All @@ -52,8 +50,7 @@ internal class CallStepTest {
conferenceAlias = Random.nextString(8)
participantId = UUID.randomUUID()
callId = UUID.randomUUID()
json = Json { ignoreUnknownKeys = true }
val service = InfinityService.create(OkHttpClient(), json)
val service = InfinityService.create()
step = service.newRequest(node)
.conference(conferenceAlias)
.participant(participantId)
Expand Down Expand Up @@ -85,7 +82,7 @@ internal class CallStepTest {
val message = "Neither conference nor gateway found"
server.enqueue {
setResponseCode(404)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val request = Random.nextNewCandidateRequest()
val token = Random.nextString(8)
Expand All @@ -99,7 +96,7 @@ internal class CallStepTest {
val message = "Invalid token"
server.enqueue {
setResponseCode(403)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val request = Random.nextNewCandidateRequest()
val token = Random.nextString(8)
Expand Down Expand Up @@ -148,7 +145,7 @@ internal class CallStepTest {
val message = "Neither conference nor gateway found"
server.enqueue {
setResponseCode(404)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val token = Random.nextString(8)
val request = Random.maybe { nextAckRequest() }
Expand All @@ -165,7 +162,7 @@ internal class CallStepTest {
val message = "Invalid token"
server.enqueue {
setResponseCode(403)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val token = Random.nextString(8)
val request = Random.maybe { nextAckRequest() }
Expand Down Expand Up @@ -213,7 +210,7 @@ internal class CallStepTest {
val message = "Neither conference nor gateway found"
server.enqueue {
setResponseCode(404)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val token = Random.nextString(8)
val request = Random.nextUpdateRequest()
Expand All @@ -228,7 +225,7 @@ internal class CallStepTest {
val message = "Invalid token"
server.enqueue {
setResponseCode(403)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val token = Random.nextString(8)
val request = Random.nextUpdateRequest()
Expand All @@ -243,7 +240,7 @@ internal class CallStepTest {
val response = UpdateResponse(Random.nextString(8))
server.enqueue {
setResponseCode(200)
setBody(json.encodeToString(Box(response)))
setBody(Infinity.Json.encodeToString(Box(response)))
}
val token = Random.nextString(8)
val request = Random.nextUpdateRequest()
Expand Down Expand Up @@ -274,7 +271,7 @@ internal class CallStepTest {
val message = "Neither conference nor gateway found"
server.enqueue {
setResponseCode(404)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val request = DtmfRequest(Random.nextDigits(8))
val token = Random.nextString(8)
Expand All @@ -288,7 +285,7 @@ internal class CallStepTest {
val message = "Invalid token"
server.enqueue {
setResponseCode(403)
setBody(json.encodeToString(Box(message)))
setBody(Infinity.Json.encodeToString(Box(message)))
}
val request = DtmfRequest(Random.nextDigits(8))
val token = Random.nextString(8)
Expand All @@ -301,7 +298,7 @@ internal class CallStepTest {
val response = Random.nextBoolean()
server.enqueue {
setResponseCode(200)
setBody(json.encodeToString(Box(response)))
setBody(Infinity.Json.encodeToString(Box(response)))
}
val request = DtmfRequest(Random.nextDigits(8))
val token = Random.nextString(8)
Expand All @@ -324,7 +321,7 @@ internal class CallStepTest {
addPathSegment("new_candidate")
}
assertToken(token)
assertPost(json, request)
assertPost(Infinity.Json, request)
}

private fun MockWebServer.verifyAck(request: AckRequest?, token: String) = takeRequest {
Expand All @@ -341,7 +338,7 @@ internal class CallStepTest {
assertToken(token)
when (request) {
null -> assertPostEmptyBody()
else -> assertPost(json, request)
else -> assertPost(Infinity.Json, request)
}
}

Expand All @@ -357,7 +354,7 @@ internal class CallStepTest {
addPathSegment("update")
}
assertToken(token)
assertPost(json, request)
assertPost(Infinity.Json, request)
}

private fun MockWebServer.verifyDtmf(request: DtmfRequest, token: String) = takeRequest {
Expand All @@ -372,7 +369,7 @@ internal class CallStepTest {
addPathSegment("dtmf")
}
assertToken(token)
assertPost(json, request)
assertPost(Infinity.Json, request)
}

private fun Random.nextNewCandidateRequest() = NewCandidateRequest(
Expand Down
Loading