Skip to content

Commit

Permalink
chore: change entitytype enum to uppercase (#23)
Browse files Browse the repository at this point in the history
change entitytype and device enum to uppercase

PR: #23
  • Loading branch information
anonvt authored Aug 19, 2024
1 parent 350fc66 commit 891e6c6
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun getClickOrganic() : Click {
return Click.Factory.buildOrganic(
placement = getTestPlacement(),
entity = Entity(
type = EntityType.Product,
type = EntityType.PRODUCT,
id = randomId("product_"),
),
occurredAt = eventNow(),
Expand All @@ -50,7 +50,7 @@ fun getImpressionOrganic() : Impression {
return Impression.Factory.buildOrganic (
placement = getTestPlacement(),
entity = Entity(
type = EntityType.Product,
type = EntityType.PRODUCT,
id = randomId("product_"),
),
occurredAt = eventNow(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sealed class BannerConfig private constructor() {
data class LandingPage(
val slotId: String,
val ids: List<String>,
val device: Device = Device.mobile,
val device: Device = Device.MOBILE,
val geoTargeting: String? = null
) : BannerConfig()

Expand All @@ -33,7 +33,7 @@ sealed class BannerConfig private constructor() {
data class CategorySingle(
val slotId: String,
val category: String,
val device: Device = Device.mobile,
val device: Device = Device.MOBILE,
val geoTargeting: String? = null
) : BannerConfig()

Expand All @@ -48,7 +48,7 @@ sealed class BannerConfig private constructor() {
data class CategoryMultiple(
val slotId: String,
val categories: List<String>,
val device: Device = Device.mobile,
val device: Device = Device.MOBILE,
val geoTargeting: String? = null,
) : BannerConfig()

Expand All @@ -63,7 +63,7 @@ sealed class BannerConfig private constructor() {
data class CategoryDisjunctions(
val slotId: String,
val disjunctions: List<List<String>>,
val device: Device = Device.mobile,
val device: Device = Device.MOBILE,
val geoTargeting: String? = null,
) : BannerConfig()

Expand All @@ -78,7 +78,7 @@ sealed class BannerConfig private constructor() {
data class Keyword(
val slotId: String,
val keyword: String,
val device: Device = Device.mobile,
val device: Device = Device.MOBILE,
val geoTargeting: String? = null,
) : BannerConfig()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import org.json.JSONObject

enum class EntityType {

Product,
Vendor,
PRODUCT,
VENDOR,
}

data class Entity(
Expand All @@ -23,14 +23,14 @@ data class Entity(
fun toJsonObject(): JSONObject {
return JSONObject()
.put("id", id)
.put("type", type.name)
.put("type", type.name.lowercase())
}

companion object{
fun fromJsonObject(json: JSONObject): Entity {
return Entity(
id = json.getString("id"),
type = EntityType.valueOf(json.getString("type"))
type = EntityType.valueOf(json.getString("type").uppercase())
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ data class ImpressionEvent (
val impressions: List<Impression>,
) {
fun toJsonObject(): JSONObject {
return JSONObject().put("impressions", impressions)
val array = JSONArray()
impressions.indices.map {
array.put(it, impressions[it].toJsonObject())
}
return JSONObject().put("impressions", array)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ data class PurchaseEvent(
val purchases: List<Purchase>
) {
fun toJsonObject(): JSONObject {
return JSONObject().put("purchases", purchases)
val array = JSONArray()
purchases.indices.map {
array.put(it, purchases[it].toJsonObject())
}
return JSONObject().put("purchases", array)
}

companion object{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.topsort.analytics.model.auctions

import org.json.JSONObject

data class Auction private constructor(
val type: String,
val slots: Int,
Expand All @@ -11,6 +13,36 @@ data class Auction private constructor(
val device: Device? = null,
) {

fun toJsonObject(): JSONObject {
val builder = JSONObject()

with(builder) {
put("type", type)
put("slots", slots)
if (products != null) {
put("products", JSONObject.wrap(products))
}
if (category != null) {
put("category", JSONObject.wrap(category))
}
if (searchQuery != null) {
put("searchQuery", searchQuery)
}
if (geoTargeting != null) {
put("geoTargeting", JSONObject.wrap(geoTargeting))
}
if (slotId != null) {
put("slotId", slotId)
}
if (device != null) {
put("device", device.name.lowercase())
}
}

return builder

}

object Factory {

@JvmOverloads
Expand Down Expand Up @@ -88,7 +120,7 @@ data class Auction private constructor(
slots: Int,
slotId: String,
ids: List<String>,
device: Device = Device.mobile,
device: Device = Device.MOBILE,
geoTargeting: String? = null,
): Auction {
return Auction(
Expand All @@ -106,7 +138,7 @@ data class Auction private constructor(
slots: Int,
slotId: String,
category: String,
device: Device = Device.mobile,
device: Device = Device.MOBILE,
geoTargeting: String? = null,
): Auction {
return Auction(
Expand All @@ -124,7 +156,7 @@ data class Auction private constructor(
slots: Int,
slotId: String,
categories: List<String>,
device: Device = Device.mobile,
device: Device = Device.MOBILE,
geoTargeting: String? = null,
): Auction {
return Auction(
Expand All @@ -142,7 +174,7 @@ data class Auction private constructor(
slots: Int,
slotId: String,
disjunctions: List<List<String>>,
device: Device = Device.mobile,
device: Device = Device.MOBILE,
geoTargeting: String? = null,
): Auction {
return Auction(
Expand All @@ -160,7 +192,7 @@ data class Auction private constructor(
slots: Int,
slotId: String,
keyword: String,
device: Device = Device.mobile,
device: Device = Device.MOBILE,
geoTargeting: String? = null,
): Auction {
return Auction(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.topsort.analytics.model.auctions

import org.json.JSONArray
import org.json.JSONObject

data class AuctionRequest(
val auctions : List<Auction>
)
){
fun toJsonObject(): JSONObject {
val array = JSONArray()
auctions.indices.map {
array.put(it, auctions[it].toJsonObject())
}
return JSONObject().put("auctions", array)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.topsort.analytics.model.auctions

@Suppress("EnumNaming")
enum class Device {
desktop,
mobile
}
DESKTOP,
MOBILE,
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.topsort.analytics.service

import com.topsort.analytics.Cache
import com.topsort.analytics.core.HttpClient
import com.topsort.analytics.core.HttpResponse
import com.topsort.analytics.core.ServiceSettings
import com.topsort.analytics.core.ServiceSettings.baseApiUrl
import com.topsort.analytics.core.ServiceSettings.bearerToken
import com.topsort.analytics.model.auctions.AuctionRequest
import com.topsort.analytics.model.auctions.AuctionResponse
import org.json.JSONObject

private const val AUCTION_ENDPOINT = "/v2/auctions"

Expand All @@ -29,7 +27,7 @@ internal object TopsortAuctionsHttpService {
assert(ServiceSettings.isSetup())
httpClient = HttpClient("${baseApiUrl}${AUCTION_ENDPOINT}")
}
val json = JSONObject.wrap(auctionRequest)!!.toString()
val json = auctionRequest.toJsonObject().toString()
return httpClient.post(json, bearerToken)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun getClickOrganic() : Click {
return Click.Factory.buildOrganic(
placement = getTestPlacement(),
entity = Entity(
type = EntityType.Product,
type = EntityType.PRODUCT,
id = randomId("product_"),
),
occurredAt = eventNow(),
Expand All @@ -50,7 +50,7 @@ fun getImpressionOrganic() : Impression {
return Impression.Factory.buildOrganic (
placement = getTestPlacement(),
entity = Entity(
type = EntityType.Product,
type = EntityType.PRODUCT,
id = randomId("product_"),
),
occurredAt = eventNow(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.topsort.analytics.banners

import org.assertj.core.api.Assertions.assertThat
import org.json.JSONObject
import org.junit.Test

internal class RunTest {
Expand All @@ -11,7 +10,7 @@ internal class RunTest {
val ids = listOf("id1", "id2")
val bannerConfig = BannerConfig.LandingPage(slotId = slot, ids = ids)
val bannerAuction = buildBannerAuction(bannerConfig);
val json = JSONObject.wrap(bannerAuction)!!.toString()
val json = bannerAuction.toJsonObject().toString()
val expectedJson =
"{\"slots\":1,\"slotId\":\"$slot\",\"type\":\"banners\",\"device\":\"mobile\",\"products\":{\"ids\":[\"${ids[0]}\",\"${ids[1]}\"]}}"
assertThat(json).isEqualTo(expectedJson)
Expand All @@ -23,7 +22,7 @@ internal class RunTest {
val category = "category"
val bannerConfig = BannerConfig.CategorySingle(slotId = slot, category = category)
val bannerAuction = buildBannerAuction(bannerConfig);
val json = JSONObject.wrap(bannerAuction)!!.toString()
val json = bannerAuction.toJsonObject().toString()
val expectedJson =
"{\"slots\":1,\"slotId\":\"$slot\",\"type\":\"banners\",\"category\":{\"id\":\"$category\"},\"device\":\"mobile\"}"
assertThat(json).isEqualTo(expectedJson)
Expand All @@ -35,7 +34,7 @@ internal class RunTest {
val categories = listOf("cat1", "cat2")
val bannerConfig = BannerConfig.CategoryMultiple(slotId = slot, categories = categories)
val bannerAuction = buildBannerAuction(bannerConfig);
val json = JSONObject.wrap(bannerAuction)!!.toString()
val json = bannerAuction.toJsonObject().toString()
val expectedJson =
"{\"slots\":1,\"slotId\":\"$slot\",\"type\":\"banners\",\"category\":{\"ids\":[\"${categories[0]}\",\"${categories[1]}\"]},\"device\":\"mobile\"}"
assertThat(json).isEqualTo(expectedJson)
Expand All @@ -48,9 +47,9 @@ internal class RunTest {
val bannerConfig =
BannerConfig.CategoryDisjunctions(slotId = slot, disjunctions = disjunctions)
val bannerAuction = buildBannerAuction(bannerConfig);
val json = JSONObject.wrap(bannerAuction)!!.toString()
val json = bannerAuction.toJsonObject().toString()
val expectedJson =
"{\"slots\":1,\"slotId\":\"$slot\",\"type\":\"banners\",\"category\":{\"disjunctions\":[[\"${disjunctions[0][0]}\",\"${disjunctions[0][1]}\"],[\"${disjunctions[1][0]}\"]]},\"device\":\"mobile\"}"
assertThat(json).isEqualTo(expectedJson)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void reportImpression() {
Analytics
.INSTANCE
.reportImpressionOrganic(
new Entity("p_SA0238", EntityType.Product),
new Entity("p_SA0238", EntityType.PRODUCT),
placement,
null,
null,
Expand All @@ -71,7 +71,7 @@ private void reportClick() {
Analytics
.INSTANCE
.reportClickOrganic(
new Entity("p_SA0238", EntityType.Product),
new Entity("p_SA0238", EntityType.PRODUCT),
placement,
null,
null,
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/topsort/analytics/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SampleActivity : Activity() {

Analytics.reportClickOrganic(
placement = placement,
entity = Entity(id = "p_SA0238", type = EntityType.Product),
entity = Entity(id = "p_SA0238", type = EntityType.PRODUCT),
)
}

Expand All @@ -82,7 +82,7 @@ class SampleActivity : Activity() {
Analytics.reportImpressionOrganic(
id = "p_SA0238",
placement = placement,
entity = Entity(id = "p_SA0238", type = EntityType.Product),
entity = Entity(id = "p_SA0238", type = EntityType.PRODUCT),
)
}

Expand Down

0 comments on commit 891e6c6

Please sign in to comment.