Skip to content

Commit d4a4de6

Browse files
authored
fix destination processing logic (#39)
1 parent 6ee0a8c commit d4a4de6

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/platform/Plugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ abstract class DestinationPlugin : EventPlugin {
104104
val beforeResult = timeline.applyPlugins(Plugin.Type.Before, event)
105105
val enrichmentResult = timeline.applyPlugins(Plugin.Type.Enrichment, beforeResult)
106106

107-
enrichmentResult?.let {
107+
val destinationResult = enrichmentResult?.let {
108108
when (it) {
109109
is IdentifyEvent -> {
110110
identify(it)
@@ -124,7 +124,7 @@ abstract class DestinationPlugin : EventPlugin {
124124
}
125125
}
126126

127-
val afterResult = timeline.applyPlugins(Plugin.Type.After, enrichmentResult)
127+
val afterResult = timeline.applyPlugins(Plugin.Type.After, destinationResult)
128128

129129
return afterResult
130130
}

core/src/test/kotlin/com/segment/analytics/kotlin/core/DestinationPluginTests.kt

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.segment.analytics.kotlin.core.utils.mockAnalytics
55
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
66
import com.segment.analytics.kotlin.core.platform.Plugin
77
import com.segment.analytics.kotlin.core.platform.Timeline
8+
import com.segment.analytics.kotlin.core.utilities.putInContext
89
import io.mockk.spyk
910
import io.mockk.verify
1011
import kotlinx.serialization.json.buildJsonObject
@@ -14,6 +15,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
1415
import org.junit.jupiter.api.Test
1516
import org.junit.jupiter.api.TestInstance
1617
import java.util.*
18+
import kotlin.math.exp
1719

1820
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1921
class DestinationPluginTests {
@@ -81,38 +83,67 @@ class DestinationPluginTests {
8183
fun `execute runs the destination timeline`() {
8284
val destinationPlugin = spyk(object: DestinationPlugin() {
8385
override val key: String = "TestDestination"
86+
override fun track(payload: TrackEvent): BaseEvent? {
87+
return payload.putInContext("processedDestination", true)
88+
}
8489
})
8590
timeline.add(destinationPlugin)
86-
val trackEvent = TrackEvent(
87-
event = "clicked",
88-
properties = buildJsonObject { put("behaviour", "good") })
89-
.apply {
90-
messageId = "qwerty-1234"
91-
anonymousId = "anonId"
92-
integrations = emptyJsonObject
93-
context = emptyJsonObject
94-
timestamp = Date(0).toInstant().toString()
95-
}
91+
9692
val beforePlugin = spyk(object: Plugin {
9793
override lateinit var analytics: Analytics
9894
override val type: Plugin.Type = Plugin.Type.Before
95+
override fun execute(event: BaseEvent): BaseEvent? {
96+
return event.putInContext("processedBefore", true)
97+
}
9998
})
10099
val enrichmentPlugin = spyk(object: Plugin {
101100
override lateinit var analytics: Analytics
102101
override val type: Plugin.Type = Plugin.Type.Enrichment
102+
override fun execute(event: BaseEvent): BaseEvent? {
103+
return event.putInContext("processedEnrichment", true)
104+
}
103105
})
104106
val afterPlugin = spyk(object: Plugin {
105107
override lateinit var analytics: Analytics
106108
override val type: Plugin.Type = Plugin.Type.After
109+
override fun execute(event: BaseEvent): BaseEvent? {
110+
return event.putInContext("processedAfter", true)
111+
}
107112
})
108113
destinationPlugin.add(beforePlugin)
109114
destinationPlugin.add(enrichmentPlugin)
110115
destinationPlugin.add(afterPlugin)
116+
117+
val trackEvent = TrackEvent(
118+
event = "clicked",
119+
properties = buildJsonObject { put("behaviour", "good") })
120+
.apply {
121+
messageId = "qwerty-1234"
122+
anonymousId = "anonId"
123+
integrations = emptyJsonObject
124+
context = emptyJsonObject
125+
timestamp = Date(0).toInstant().toString()
126+
}
127+
128+
val expected = TrackEvent(
129+
event = "clicked",
130+
properties = buildJsonObject { put("behaviour", "good") })
131+
.apply {
132+
messageId = "qwerty-1234"
133+
anonymousId = "anonId"
134+
integrations = emptyJsonObject
135+
context = buildJsonObject {
136+
put("processedBefore", true)
137+
put("processedEnrichment", true)
138+
put("processedDestination", true)
139+
put("processedAfter", true)
140+
}
141+
timestamp = Date(0).toInstant().toString()
142+
}
143+
111144
val result = timeline.process(trackEvent)
112-
assertEquals(trackEvent, result)
113-
verify(exactly = 1) { beforePlugin.execute(trackEvent) }
114-
verify(exactly = 1) { enrichmentPlugin.execute(trackEvent) }
115-
verify(exactly = 1) { afterPlugin.execute(trackEvent) }
145+
146+
assertEquals(expected, result)
116147
}
117148

118149
@Test

0 commit comments

Comments
 (0)