@@ -5,6 +5,7 @@ import com.segment.analytics.kotlin.core.utils.mockAnalytics
5
5
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
6
6
import com.segment.analytics.kotlin.core.platform.Plugin
7
7
import com.segment.analytics.kotlin.core.platform.Timeline
8
+ import com.segment.analytics.kotlin.core.utilities.putInContext
8
9
import io.mockk.spyk
9
10
import io.mockk.verify
10
11
import kotlinx.serialization.json.buildJsonObject
@@ -14,6 +15,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
14
15
import org.junit.jupiter.api.Test
15
16
import org.junit.jupiter.api.TestInstance
16
17
import java.util.*
18
+ import kotlin.math.exp
17
19
18
20
@TestInstance(TestInstance .Lifecycle .PER_CLASS )
19
21
class DestinationPluginTests {
@@ -81,38 +83,67 @@ class DestinationPluginTests {
81
83
fun `execute runs the destination timeline` () {
82
84
val destinationPlugin = spyk(object : DestinationPlugin () {
83
85
override val key: String = " TestDestination"
86
+ override fun track (payload : TrackEvent ): BaseEvent ? {
87
+ return payload.putInContext(" processedDestination" , true )
88
+ }
84
89
})
85
90
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
+
96
92
val beforePlugin = spyk(object : Plugin {
97
93
override lateinit var analytics: Analytics
98
94
override val type: Plugin .Type = Plugin .Type .Before
95
+ override fun execute (event : BaseEvent ): BaseEvent ? {
96
+ return event.putInContext(" processedBefore" , true )
97
+ }
99
98
})
100
99
val enrichmentPlugin = spyk(object : Plugin {
101
100
override lateinit var analytics: Analytics
102
101
override val type: Plugin .Type = Plugin .Type .Enrichment
102
+ override fun execute (event : BaseEvent ): BaseEvent ? {
103
+ return event.putInContext(" processedEnrichment" , true )
104
+ }
103
105
})
104
106
val afterPlugin = spyk(object : Plugin {
105
107
override lateinit var analytics: Analytics
106
108
override val type: Plugin .Type = Plugin .Type .After
109
+ override fun execute (event : BaseEvent ): BaseEvent ? {
110
+ return event.putInContext(" processedAfter" , true )
111
+ }
107
112
})
108
113
destinationPlugin.add(beforePlugin)
109
114
destinationPlugin.add(enrichmentPlugin)
110
115
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
+
111
144
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)
116
147
}
117
148
118
149
@Test
0 commit comments