Skip to content

Commit

Permalink
make BidResponse.ext a class, updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renam committed Aug 14, 2024
1 parent 9b713cb commit 279879e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import com.adsbynimbus.openrtb.request.BidRequest
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.booleanOrNull
import kotlin.jvm.JvmField
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic

/**
* A winning bid response from Nimbus
Expand Down Expand Up @@ -57,7 +55,7 @@ public class BidResponse(
@JvmField @SerialName("duration") public val duration: Int = 0,
@JvmField @SerialName("exp") public val exp: Int = -1,
@JvmField @SerialName("external_notifications") public val external_notifications: Map<String, String> = emptyMap(),
@JvmField @SerialName("ext") public val ext: JsonElement = JsonNull,
@JvmField @SerialName("ext") public val ext: Extension = Extension(),
) {
/** Urls to fire a request to when an impression is registered */
public val impression_trackers: Array<String>? get() = trackers["impression_trackers"]
Expand All @@ -68,9 +66,6 @@ public class BidResponse(
/** Url to fire a request to when this bid loses an auction */
public val loss_response: String? get() = external_notifications["loss_response"]

public val useNewRenderer: Boolean
get() = ((ext as? JsonObject)?.get("use_new_renderer") as? JsonPrimitive)?.booleanOrNull ?: false

public companion object {
/** Decodes a BidResponse from a Json string using the built in serializer */
@JvmStatic @JvmOverloads
Expand All @@ -86,4 +81,9 @@ public class BidResponse(
jsonSerializer: Json = BidRequest.lenientSerializer,
): String = jsonSerializer.encodeToString(serializer(), response)
}

@Serializable
public class Extension(
@JvmField @SerialName("use_new_renderer") public val useNewRenderer: Boolean = false,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldStartWith

const val testJson = """
private fun testJson(ext: String = "") = """
{
"type": "native",
"auction_id": "d07668d6-35ba-4870-a3cd-02b18fec1a12",
Expand All @@ -32,16 +32,14 @@ const val testJson = """
"external_notifications": {
"win_response": "https://test.adsbynimbus.com/win_response/",
"loss_response": "https://test.adsbynimbus.com/loss_response/auctionPrice=[AUCTION_PRICE]&auctionMinToWin=[AUCTION_MIN_TO_WIN]&winningSource=[WINNING_SOURCE]"
},
"ext": {
"use_new_renderer": true
}
$ext
}
"""

class DeserializationTest : StringSpec({

val response = BidResponse.fromJson(testJson)
val response = BidResponse.fromJson(testJson())

"BidResponse fromJson deserializes the type field" {
response.type shouldBe "native"
Expand Down Expand Up @@ -108,7 +106,17 @@ class DeserializationTest : StringSpec({
}

"BidResponse fromJson deserializes use_new_renderer" {
response.useNewRenderer shouldBe true
response.ext.useNewRenderer shouldBe false
BidResponse.fromJson(testJson("""
,"ext": {
"use_new_renderer": true
}
""".trimIndent())).ext.useNewRenderer shouldBe true
BidResponse.fromJson(testJson("""
,"ext": {
"use_new_renderer": false
}
""".trimIndent())).ext.useNewRenderer shouldBe false
}

"BidResponse fromJson deserializes loss urls" {
Expand Down

0 comments on commit 279879e

Please sign in to comment.