Skip to content

Commit

Permalink
Add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Nov 26, 2024
1 parent 5bb8661 commit 0229c27
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.snowplowanalytics.snowplow.tracker.MockNetworkConnection
import com.snowplowanalytics.snowplow.util.TimeTraveler
import junit.framework.TestCase
import org.json.JSONException
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
Expand Down Expand Up @@ -181,6 +182,29 @@ class TrackerWebViewInterfaceV2Test {
assertEquals(1, trackedEvents2.size)
}

@Test
@Throws(JSONException::class, InterruptedException::class)
fun tracksEventWithEntity() {
val entities = "[{\"schema\":\"http://schema.com\",\"data\":{\"key\":\"val\"}},{\"schema\":\"http://example.com\",\"data\":{\"anotherKey\":\"anotherValue\"}}]"
webInterface!!.trackWebViewEvent(
eventName = "pp",
trackerVersion = "webview",
useragent = "Chrome",
pageUrl = "http://snowplow.com",
entities = entities
)
Thread.sleep(200)

assertEquals(1, trackedEvents.size)
val entity1 = firstEvent.entities[0]
val entity2 = firstEvent.entities[1]

assertEquals("http://schema.com", entity1.map["schema"] as? String)
assertEquals("val", (entity1.map["data"] as? Map<*, *>)?.get("key"))
assertEquals("http://example.com", entity2.map["schema"] as? String)
assertEquals("anotherValue", (entity2.map["data"] as? Map<*, *>)?.get("anotherKey"))
}


// --- PRIVATE
private val context: Context
Expand All @@ -192,6 +216,8 @@ class TrackerWebViewInterfaceV2Test {
val trackerConfig = TrackerConfiguration("appId")
.installAutotracking(false)
.lifecycleAutotracking(false)
.platformContext(false)
.base64encoding(false)

val plugin = PluginConfiguration("plugin")
plugin.afterTrack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.snowplowanalytics.snowplow.event.*
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
Expand Down Expand Up @@ -71,11 +70,11 @@ class TrackerWebViewInterfaceV2 {
}

@Throws(JSONException::class)
private fun trackEvent(event: AbstractEvent, context: String?, trackers: Array<String>?) {
if (context != null) {
val contextEntities = parseContext(context)
if (contextEntities.isNotEmpty()) {
event.entities(contextEntities)
private fun trackEvent(event: AbstractEvent, contextEntities: String?, trackers: Array<String>?) {
if (contextEntities != null) {
val entities = parseEntities(contextEntities)
if (entities.isNotEmpty()) {
event.entities(entities)
}
}
if (trackers.isNullOrEmpty()) {
Expand All @@ -98,9 +97,9 @@ class TrackerWebViewInterfaceV2 {
}

@Throws(JSONException::class)
private fun parseContext(context: String): List<SelfDescribingJson> {
private fun parseEntities(serialisedEntities: String): List<SelfDescribingJson> {
val entities: MutableList<SelfDescribingJson> = ArrayList()
val contextJson = JSONArray(context)
val contextJson = JSONArray(serialisedEntities)
for (i in 0 until contextJson.length()) {
val itemJson = contextJson.getJSONObject(i)
val item = jsonToMap(itemJson)
Expand All @@ -114,6 +113,6 @@ class TrackerWebViewInterfaceV2 {
}

companion object {
const val TAG = "SnowplowWebInterface"
const val TAG = "SnowplowWebInterfaceV2"
}
}

0 comments on commit 0229c27

Please sign in to comment.