generated from segment-integrations/analytics-kotlin-destination-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from RawatRaveena/testing/unit-cases
testing/unit-cases to main
- Loading branch information
Showing
2 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
238 changes: 238 additions & 0 deletions
238
...rc/test/kotlin/com/segment/analytics/kotlin/destinations/adjust/AdjustDestinationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,242 @@ | ||
package com.segment.analytics.kotlin.destinations.adjust | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.content.Context | ||
import android.os.Build | ||
import com.adjust.sdk.Adjust | ||
import com.adjust.sdk.AdjustAttribution | ||
import com.adjust.sdk.AdjustInstance | ||
import com.segment.analytics.kotlin.core.* | ||
import com.segment.analytics.kotlin.core.platform.Plugin | ||
import com.segment.analytics.kotlin.core.utilities.LenientJson | ||
import com.segment.analytics.kotlin.core.utilities.getString | ||
import io.mockk.* | ||
import io.mockk.impl.annotations.MockK | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
@Config(sdk = [Build.VERSION_CODES.O_MR1]) | ||
class AdjustDestinationTests { | ||
@MockK | ||
lateinit var mockApplication: Application | ||
|
||
@MockK | ||
lateinit var mockedContext: Context | ||
|
||
@MockK(relaxUnitFun = true) | ||
lateinit var mockedAnalytics: Analytics | ||
|
||
@MockK(relaxUnitFun = true) | ||
lateinit var mockedAdjustInstance: AdjustInstance | ||
|
||
private lateinit var adjustDestination: AdjustDestination | ||
|
||
private val sampleAdjustSettings: Settings = LenientJson.decodeFromString( | ||
""" | ||
{ | ||
"integrations": { | ||
"Adjust": { | ||
"appToken": "xyz1234", | ||
"setEnvironmentProduction": true, | ||
"setEventBufferingEnabled": true, | ||
"trackAttributionData": true, | ||
"customEvents": { | ||
"foo": "bar" | ||
} | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
init { | ||
MockKAnnotations.init(this) | ||
} | ||
|
||
@Before | ||
fun setUp() { | ||
mockkStatic(Adjust::class) | ||
every { Adjust.getDefaultInstance() } answers { mockedAdjustInstance } | ||
adjustDestination = AdjustDestination() | ||
every { mockedAnalytics.configuration.application } returns mockApplication | ||
every { mockApplication.applicationContext } returns mockedContext | ||
mockedAnalytics.configuration.application = mockedContext | ||
|
||
adjustDestination.analytics = mockedAnalytics | ||
|
||
mockkStatic(Adjust::class) | ||
} | ||
|
||
|
||
@Test | ||
fun `settings are updated correctly`() { | ||
// An adjust example settings | ||
val adjustSettings: Settings = sampleAdjustSettings | ||
|
||
adjustDestination.update(adjustSettings, Plugin.UpdateType.Initial) | ||
|
||
/* assertions Adjust config */ | ||
assertNotNull(adjustDestination.settings) | ||
with(adjustDestination.settings!!) { | ||
assertTrue(adjustDestination.settings!!.setEnvironmentProduction) | ||
assertTrue(adjustDestination.settings!!.setEventBufferingEnabled) | ||
assertTrue(adjustDestination.settings!!.trackAttributionData) | ||
assertEquals(adjustDestination.settings!!.appToken, "xyz1234") | ||
assertNotNull(adjustDestination.settings!!.customEvents) | ||
} | ||
} | ||
|
||
@Test | ||
fun `identify is handled correctly`() { | ||
val sampleIdentifyEvent = IdentifyEvent( | ||
userId = "adjust-UserID-123", | ||
traits = buildJsonObject { | ||
put("email", "adjustUserID@abc.com") | ||
put("firstName", "adjust") | ||
put("lastName", "user") | ||
} | ||
).apply { | ||
messageId = "abc-message-1234" | ||
anonymousId = "adjust-anonId-123" | ||
integrations = emptyJsonObject | ||
context = emptyJsonObject | ||
timestamp = "2022-11-22T11:16:09" | ||
} | ||
|
||
val identifyEvent = adjustDestination.identify(sampleIdentifyEvent) | ||
assertNotNull(identifyEvent) | ||
|
||
with(identifyEvent as IdentifyEvent) { | ||
assertEquals("adjust-UserID-123", userId) | ||
with(traits) { | ||
assertEquals("adjust", getString("firstName")) | ||
assertEquals("user", getString("lastName")) | ||
assertEquals("adjustUserID@abc.com", getString("email")) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `identify is handled correctly with userId`() { | ||
val sampleIdentifyEvent = IdentifyEvent( | ||
userId = "adjust-UserID-123", | ||
traits = buildJsonObject { | ||
put("email", "adjustUserID@abc.com") | ||
put("firstName", "adjust") | ||
put("lastName", "user") | ||
} | ||
).apply { | ||
messageId = "abc-message-1234" | ||
anonymousId = "adjust-anonId-123" | ||
integrations = emptyJsonObject | ||
context = emptyJsonObject | ||
timestamp = "2022-11-22T11:16:09" | ||
} | ||
val identifyEvent = adjustDestination.identify(sampleIdentifyEvent) | ||
assertNotNull(identifyEvent) | ||
verify { mockedAdjustInstance.addSessionPartnerParameter("userId", "adjust-UserID-123") } | ||
} | ||
|
||
@Test | ||
fun `identify is handled correctly with anonymousId`() { | ||
val sampleIdentifyEvent = IdentifyEvent( | ||
userId = "adjust-UserID-123", | ||
traits = buildJsonObject { | ||
put("email", "adjustUserID@abc.com") | ||
put("firstName", "adjust") | ||
put("lastName", "user") | ||
} | ||
).apply { | ||
messageId = "abc-message-1234" | ||
anonymousId = "adjust-anonId-123" | ||
integrations = emptyJsonObject | ||
context = emptyJsonObject | ||
timestamp = "2022-11-22T11:16:09" | ||
} | ||
|
||
val identifyEvent = adjustDestination.identify(sampleIdentifyEvent) | ||
assertNotNull(identifyEvent) | ||
verify { | ||
mockedAdjustInstance.addSessionPartnerParameter( | ||
"anonymousId", | ||
"adjust-anonId-123" | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `reset is handled correctly`() { | ||
adjustDestination.reset() | ||
verify { mockedAdjustInstance.resetSessionPartnerParameters() } | ||
} | ||
|
||
@Test | ||
fun `track is handled correctly`() { | ||
adjustDestination.update(sampleAdjustSettings, Plugin.UpdateType.Initial) | ||
|
||
val sampleTrackEvent = TrackEvent( | ||
event = "foo", | ||
properties = buildJsonObject { | ||
put("revenue", 200.0f) | ||
put("currency", "USD") | ||
} | ||
).apply { | ||
messageId = "abc-message-1234" | ||
anonymousId = "adjust-anonId-123" | ||
integrations = emptyJsonObject | ||
context = emptyJsonObject | ||
timestamp = "2022-11-22T11:16:09" | ||
} | ||
val trackEvent = adjustDestination.track(sampleTrackEvent) | ||
assertNotNull(trackEvent) | ||
// verify { mockedAdjustInstance.trackEvent(AdjustEvent("foo")) } | ||
} | ||
|
||
@Test | ||
fun `trackAttribution data sent correctly to analytics`() { | ||
val segmentAttributionChangedListener = AdjustDestination.AdjustSegmentAttributionChangedListener(mockedAnalytics) | ||
val attributionData = AdjustAttribution().apply { | ||
network = "Adjust Network" | ||
campaign = "Adjust Campaign Name" | ||
clickLabel = "Adjust Click Label" | ||
creative = "Adjust creative" | ||
adgroup = "Adjust Ad group" | ||
trackerToken = "foo" | ||
trackerName = "bar" | ||
} | ||
segmentAttributionChangedListener.onAttributionChanged(attributionData) | ||
verify { mockedAnalytics.track("Install Attributed", buildJsonObject { | ||
put("provider", "Adjust") | ||
put("trackerToken", "foo") | ||
put("trackerName", "bar") | ||
put("campaign", buildJsonObject { | ||
put("source", "Adjust Network") | ||
put("name", "Adjust Campaign Name") | ||
put("content", "Adjust Click Label") | ||
put("adCreative", "Adjust creative") | ||
put("adGroup", "Adjust Ad group") | ||
}) | ||
}) } | ||
} | ||
|
||
@Test | ||
fun `onActivityResumed() handled correctly`() { | ||
adjustDestination.onActivityResumed(mockkClass(Activity::class)) | ||
verify { mockedAdjustInstance.onResume() } | ||
} | ||
|
||
@Test | ||
fun `onActivityPaused() handled correctly`() { | ||
adjustDestination.onActivityPaused(mockkClass(Activity::class)) | ||
verify { mockedAdjustInstance.onPause() } | ||
} | ||
} |