Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/main/kotlin/com/mparticle/kits/RoktKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Build
import com.mparticle.BuildConfig
import com.mparticle.MPEvent
import com.mparticle.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.MpRoktEventCallback
Expand Down Expand Up @@ -231,6 +232,8 @@ class RoktKit :
addIdentityAttributes(finalAttributes, filterUser)

verifyHashedEmail(finalAttributes)

logSelectPlacementEvent(finalAttributes)
return finalAttributes
}

Expand Down Expand Up @@ -381,6 +384,13 @@ class RoktKit :
}
}

private fun logSelectPlacementEvent(attributes: Map<String, String>) {
val event = MPEvent.Builder(EVENT_NAME_SELECT_PLACEMENTS, MParticle.EventType.Other)
.customAttributes(attributes)
.build()
MParticle.getInstance()?.logEvent(event)
}

private fun getStringForIdentity(identityType: IdentityType): String = when (identityType) {
IdentityType.Other -> "other"
IdentityType.CustomerId -> "customerid"
Expand Down Expand Up @@ -420,6 +430,7 @@ class RoktKit :
const val ROKT_ACCOUNT_ID = "accountId"
const val HASHED_EMAIL_USER_IDENTITY_TYPE = "hashedEmailUserIdentityType"
const val MPID = "mpid"
const val EVENT_NAME_SELECT_PLACEMENTS = "selectplacements"
const val NO_ROKT_ACCOUNT_ID = "No Rokt account ID provided, can't initialize kit."
const val NO_APP_VERSION_FOUND = "No App version found, can't initialize kit."
}
Expand Down
89 changes: 89 additions & 0 deletions src/test/kotlin/com/mparticle/kits/RoktKitTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.net.Uri
import android.os.Build.VERSION
import com.mparticle.AttributionError
import com.mparticle.AttributionResult
import com.mparticle.MPEvent
import com.mparticle.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.MParticleOptions
Expand Down Expand Up @@ -39,6 +40,7 @@ import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.Mockito
import org.mockito.Mockito.mock
import java.lang.ref.WeakReference
Expand Down Expand Up @@ -1210,6 +1212,93 @@ class RoktKitTests {
field[null] = newValue
}
}
@Test
fun test_prepareFinalAttributes_logs_attributes_event() {
val mParticleMock = MParticle.getInstance()!!
val method: Method = RoktKit::class.java.getDeclaredMethod(
"prepareFinalAttributes",
FilteredMParticleUser::class.java,
Map::class.java,
)
method.isAccessible = true

val mockFilterUser = mock(FilteredMParticleUser::class.java)
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(HashMap())
Mockito.`when`(mockFilterUser.id).thenReturn(9876L)
val userAttributes = hashMapOf<String, Any?>("user_key" to "user_val")
Mockito.`when`(mockFilterUser.userAttributes).thenReturn(userAttributes)

roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs", JSONObject()))
val attributes: Map<String, String> = mapOf("attr1" to "val1")

method.invoke(roktKit, mockFilterUser, attributes)

val eventCaptor = ArgumentCaptor.forClass(MPEvent::class.java)
Mockito.verify(mParticleMock).logEvent(eventCaptor.capture())

val loggedEvent = eventCaptor.value
assertEquals("selectplacements", loggedEvent.eventName)
assertEquals(MParticle.EventType.Other, loggedEvent.eventType)
assertEquals(
mapOf(
"user_key" to "user_val",
"attr1" to "val1",
"mpid" to "9876",
),
loggedEvent.customAttributes,
)
}

@Test
fun test_execute_logsMPEventWhenMParticleInstanceIsNull() {
// Arrange
mockkObject(Rokt)
every {
Rokt.execute(
any<String>(),
any<Map<String, String>>(),
any<Rokt.RoktCallback>(),
any(),
any(),
any(),
)
} just runs

val mockFilterUser = mock(FilteredMParticleUser::class.java)
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(HashMap())
Mockito.`when`(mockFilterUser.id).thenReturn(12345L)
Mockito.`when`(mockFilterUser.userAttributes).thenReturn(HashMap<String, Any?>())

roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs", JSONObject()))

val testAttributes = mapOf("key1" to "value1")

// Set MParticle instance to null
MParticle.setInstance(null)

roktKit.execute(
viewName = "test_view",
attributes = testAttributes,
mpRoktEventCallback = null,
placeHolders = null,
fontTypefaces = null,
filterUser = mockFilterUser,
mpRoktConfig = null,
)

verify(exactly = 1) {
Rokt.execute(
any<String>(),
any<Map<String, String>>(),
any<Rokt.RoktCallback>(),
any(),
any(),
any(),
)
}

unmockkObject(Rokt)
}

private var emptyCoreCallbacks: CoreCallbacks = object : CoreCallbacks {
var activity = Activity()
Expand Down
Loading