Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing/unit-cases to main #2

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AdjustDestination(
private var adjustInstance: AdjustInstance = Adjust.getDefaultInstance()
) : DestinationPlugin(), AndroidLifecycle {

private var settings: AdjustSettings? = null
internal var settings: AdjustSettings? = null

private val REVENUE_KEY = "revenue"
private val CURRENCY_KEY = "currency"
Expand Down
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() }
}
}