diff --git a/src/main/kotlin/com/mparticle/kits/RoktKit.kt b/src/main/kotlin/com/mparticle/kits/RoktKit.kt index 26c78a2..60c8cec 100644 --- a/src/main/kotlin/com/mparticle/kits/RoktKit.kt +++ b/src/main/kotlin/com/mparticle/kits/RoktKit.kt @@ -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 @@ -231,6 +232,8 @@ class RoktKit : addIdentityAttributes(finalAttributes, filterUser) verifyHashedEmail(finalAttributes) + + logSelectPlacementEvent(finalAttributes) return finalAttributes } @@ -381,6 +384,13 @@ class RoktKit : } } + private fun logSelectPlacementEvent(attributes: Map) { + 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" @@ -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." } diff --git a/src/test/kotlin/com/mparticle/kits/RoktKitTests.kt b/src/test/kotlin/com/mparticle/kits/RoktKitTests.kt index 5dd583e..fdba649 100644 --- a/src/test/kotlin/com/mparticle/kits/RoktKitTests.kt +++ b/src/test/kotlin/com/mparticle/kits/RoktKitTests.kt @@ -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 @@ -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 @@ -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("user_key" to "user_val") + Mockito.`when`(mockFilterUser.userAttributes).thenReturn(userAttributes) + + roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs", JSONObject())) + val attributes: Map = 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(), + any>(), + any(), + 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()) + + 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(), + any>(), + any(), + any(), + any(), + any(), + ) + } + + unmockkObject(Rokt) + } private var emptyCoreCallbacks: CoreCallbacks = object : CoreCallbacks { var activity = Activity()