From fbe62a044795a755d00891ccb2bc43f089be0d4f Mon Sep 17 00:00:00 2001 From: Miranda Wilson Date: Mon, 29 Jan 2024 08:48:13 +0000 Subject: [PATCH] Tidy AbstractEvent internal properties (close #666) * Match AbstractEvent entities API with iOS tracker * Restore original behaviour --- .../snowplow/event/AbstractEventTest.kt | 23 ++++++--- .../snowplow/event/AbstractEvent.kt | 49 +++++++++---------- .../snowplow/event/ConsentGranted.kt | 2 +- .../snowplow/event/ConsentWithdrawn.kt | 2 +- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/event/AbstractEventTest.kt b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/event/AbstractEventTest.kt index c38f94f9e..e9c59eea8 100644 --- a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/event/AbstractEventTest.kt +++ b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/event/AbstractEventTest.kt @@ -24,14 +24,21 @@ class AbstractEventTest { @Test fun testAddsEntitiesUsingAllAPIs() { val event = ScreenView("screen") + val entity1 = SelfDescribingJson("schema1", "data1") + val entity2 = SelfDescribingJson("schema2", "data2") + val entity3 = SelfDescribingJson("schema3", "data3") - event.entities.add(SelfDescribingJson("schema1", "data1")) - event.customContexts.add(SelfDescribingJson("schema2", "data2")) - event.entities(listOf(SelfDescribingJson("schema3", "data3"))) - event.contexts(listOf(SelfDescribingJson("schema4", "data4"))) - - Assert.assertEquals(4, event.entities.count()) - Assert.assertEquals(4, event.contexts.count()) - Assert.assertEquals(4, event.customContexts.count()) + event.entities.add(entity1) + Assert.assertEquals(1, event.entities.count()) + + event.entities(listOf(entity2)) + Assert.assertEquals(2, event.entities.count()) + + event.contexts(listOf(entity3)) + Assert.assertEquals(3, event.entities.count()) + Assert.assertEquals(3, event.contexts.count()) + Assert.assertTrue(event.entities.contains(entity1)) + Assert.assertTrue(event.entities.contains(entity2)) + Assert.assertTrue(event.entities.contains(entity3)) } } diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/AbstractEvent.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/AbstractEvent.kt index 2939a575f..4022bfd22 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/AbstractEvent.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/AbstractEvent.kt @@ -22,10 +22,27 @@ import java.util.* * - "True" timestamp: user-defined custom event timestamp */ abstract class AbstractEvent : Event { - /** List of custom contexts associated with the event. */ - @Deprecated("Old nomenclature.", ReplaceWith("entities")) - @JvmField - val customContexts: MutableList = LinkedList() + private var _entities = mutableListOf() + /** + * @return the custom context entities associated with the event. + */ + override var entities: MutableList + get() { + if (isProcessing) { + entitiesForProcessing?.let { + return (_entities + it).toMutableList() + } + } + return _entities + } + set(value) { + _entities = value + } + + @Deprecated("Old nomenclature", ReplaceWith("entities")) + override var contexts: List + get() = entities + set(value) { entities = value.toMutableList()} /** * @return the optional "true" (custom) event timestamp @@ -39,14 +56,14 @@ abstract class AbstractEvent : Event { // Builder methods /** Adds a list of context entities to the existing ones. */ - fun entities(entities: List?): AbstractEvent { - entities?.let { customContexts.addAll(entities) } + fun entities(entities: List): AbstractEvent { + this.entities.addAll(entities) return this } /** Adds a list of context entities to the existing ones. */ @Deprecated("Old nomenclature.", ReplaceWith("entities()")) - fun contexts(contexts: List?): AbstractEvent { + fun contexts(contexts: List): AbstractEvent { return entities(contexts) } @@ -56,24 +73,6 @@ abstract class AbstractEvent : Event { return this } - // Public methods - - /** - * @return the event custom context entities - */ - override val entities: MutableList - get() { - if (isProcessing) { - entitiesForProcessing?.let { - return (customContexts + it).toMutableList() - } - } - return customContexts - } - - @Deprecated("Old nomenclature", ReplaceWith("entities")) - override val contexts: List - get() = entities private var isProcessing = false override fun beginProcessing(tracker: Tracker) { diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentGranted.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentGranted.kt index d95a2accf..9966292f4 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentGranted.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentGranted.kt @@ -108,7 +108,7 @@ class ConsentGranted(expiry: String, documentId: String, documentVersion: String override fun beginProcessing(tracker: Tracker) { for (document in documents) { val context = SelfDescribingJson(document.schema, document.dataPayload) - customContexts.add(context) // TODO: Only the user should modify the public customContexts property + entities.add(context) } } } diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentWithdrawn.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentWithdrawn.kt index 2fb3059a2..9dc793a45 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentWithdrawn.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/event/ConsentWithdrawn.kt @@ -104,7 +104,7 @@ class ConsentWithdrawn(all: Boolean, documentId: String, documentVersion: String override fun beginProcessing(tracker: Tracker) { for (document in documents) { val context = SelfDescribingJson(document.schema, document.dataPayload) - customContexts.add(context) // TODO: Only the user should modify the public customContexts property + entities.add(context) } } }