From 4380ffe177cd3bd26d2b5147767936d53feb516c Mon Sep 17 00:00:00 2001 From: Mansi-mParticle <159845845+Mansi-mParticle@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:23:49 -0400 Subject: [PATCH] fix: Add enum value to hash event attributes (#167) --- .../kotlin/com/mparticle/kits/AppboyKit.kt | 2 +- src/test/kotlin/com/braze/Braze.kt | 13 ++ src/test/kotlin/com/braze/BrazeUser.kt | 15 ++- .../com/mparticle/kits/AppboyKitTest.kt | 127 +++++++++++++++++- 4 files changed, 146 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/mparticle/kits/AppboyKit.kt b/src/main/kotlin/com/mparticle/kits/AppboyKit.kt index 77c53e5..4616e27 100644 --- a/src/main/kotlin/com/mparticle/kits/AppboyKit.kt +++ b/src/main/kotlin/com/mparticle/kits/AppboyKit.kt @@ -152,7 +152,7 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener, event.customAttributeStrings?.let { it -> for ((key, attributeValue) in it) { val hashedKey = - KitUtils.hashForFiltering(event.eventType.toString() + event.eventName + key) + KitUtils.hashForFiltering(event.eventType.value.toString() + event.eventName + key) configuration.eventAttributesAddToUser?.get(hashedKey)?.let { value.addToCustomAttributeArray(it, attributeValue) diff --git a/src/test/kotlin/com/braze/Braze.kt b/src/test/kotlin/com/braze/Braze.kt index 04aa4d7..e6fc4ed 100644 --- a/src/test/kotlin/com/braze/Braze.kt +++ b/src/test/kotlin/com/braze/Braze.kt @@ -2,6 +2,7 @@ package com.braze import android.content.Context import com.braze.configuration.BrazeConfig +import com.braze.events.IValueCallback import com.braze.models.outgoing.BrazeProperties import com.mparticle.kits.BrazePurchase import java.math.BigDecimal @@ -12,10 +13,22 @@ class Braze { return Companion.currentUser } + fun getCustomAttributeArray(): java.util.HashMap> { + return Companion.currentUser.getCustomAttribute() + } + + fun getCurrentUser(callback: IValueCallback) { + callback.onSuccess(currentUser) + } + fun logCustomEvent(key: String, brazeProperties: BrazeProperties) { events[key] = brazeProperties } + fun logCustomEvent(key: String) { + events[key] = BrazeProperties() + } + fun logPurchase( sku: String, currency: String, diff --git a/src/test/kotlin/com/braze/BrazeUser.kt b/src/test/kotlin/com/braze/BrazeUser.kt index f932238..71fdd40 100644 --- a/src/test/kotlin/com/braze/BrazeUser.kt +++ b/src/test/kotlin/com/braze/BrazeUser.kt @@ -1,9 +1,6 @@ package com.braze import com.braze.enums.Month -import java.util.HashMap -import java.util.ArrayList -import java.lang.NullPointerException class BrazeUser { var dobYear = -1 @@ -32,7 +29,9 @@ class BrazeUser { fun removeFromCustomAttributeArray(key: String, value: String): Boolean { return try { - customAttributeArray[key]?.remove(value) + if (customAttributeArray.containsKey(key)) { + customAttributeArray.remove(key) + } true } catch (npe: NullPointerException) { false @@ -58,4 +57,12 @@ class BrazeUser { customUserAttributes[key] = value return true } + + fun getCustomAttribute(): HashMap> { + return customAttributeArray + } + + fun getCustomUserAttribute(): HashMap { + return customUserAttributes + } } diff --git a/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt b/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt index 64ca1fb..1ac3cea 100644 --- a/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt +++ b/src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt @@ -1,12 +1,11 @@ package com.mparticle.kits -import com.braze.enums.Month +import android.util.SparseBooleanArray import com.braze.Braze import com.braze.models.outgoing.BrazeProperties import com.mparticle.MPEvent import com.mparticle.MParticle import com.mparticle.MParticle.IdentityType -import com.mparticle.MParticle.LogLevel import com.mparticle.MParticleOptions import com.mparticle.commerce.CommerceEvent import com.mparticle.commerce.Impression @@ -15,16 +14,17 @@ import com.mparticle.commerce.Promotion import com.mparticle.commerce.TransactionAttributes import com.mparticle.identity.IdentityApi import com.mparticle.identity.MParticleUser -import com.mparticle.internal.Logger -import com.mparticle.internal.Logger.DefaultLogHandler import com.mparticle.kits.mocks.MockAppboyKit import com.mparticle.kits.mocks.MockContextApplication import com.mparticle.kits.mocks.MockKitConfiguration import com.mparticle.kits.mocks.MockUser +import org.json.JSONObject import org.junit.Assert import org.junit.Before import org.junit.Test +import org.mockito.Mock import org.mockito.Mockito +import org.mockito.MockitoAnnotations import java.math.BigDecimal import java.util.Calendar import java.util.Locale @@ -32,19 +32,28 @@ import java.util.Random class AppboyKitTests { private var random = Random() + + private lateinit var braze: Braze.Companion + + @Mock + private val mTypeFilters: SparseBooleanArray? = null + private val kit: AppboyKit get() = AppboyKit() @Before fun setup() { + MockitoAnnotations.initMocks(this) + Braze.clearPurchases() + Braze.clearEvents() MParticle.setInstance(Mockito.mock(MParticle::class.java)) Mockito.`when`(MParticle.getInstance()!!.Identity()).thenReturn( Mockito.mock( IdentityApi::class.java ) ) - Braze.clearPurchases() - Braze.clearEvents() + braze = Braze + braze.currentUser.getCustomAttribute().clear() } @Test @@ -927,4 +936,110 @@ class AppboyKitTests { // Assert.assertEquals(properties.remove("fuzz?"), "foobar") // Assert.assertEquals(0, properties.size.toLong()) // } + + @Test + fun testCustomAttributes_log_add_attribute_event() { + val kit = MockAppboyKit() + val currentUser = braze.currentUser + + kit.configuration = MockKitConfiguration() + + val jsonObject = JSONObject() + val mapValue = JSONObject() + //this is hash for event attribute i.e combination of eventType + eventName + attribute key + mapValue.put("888169310", "testEvent") + val eaaObject = JSONObject() + eaaObject.put("eaa", mapValue) + jsonObject.put("hs", eaaObject) + + Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) + + var kitConfiguration = MockKitConfiguration.createKitConfiguration(jsonObject) + kit.configuration = kitConfiguration + val customAttributes: MutableMap = HashMap() + customAttributes["destination"] = "Shop" + val event = MPEvent.Builder("AndroidTEST", MParticle.EventType.Navigation) + .customAttributes(customAttributes) + .build() + val instance = MParticle.getInstance() + instance?.logEvent(event) + kit.logEvent(event) + Assert.assertEquals(1, braze.events.size.toLong()) + Assert.assertEquals(1, currentUser.getCustomAttribute().size.toLong()) + var outputKey = "" + for (keys in currentUser.getCustomAttribute().keys) { + outputKey = keys + break + } + Assert.assertEquals("testEvent", outputKey) + } + + @Test + fun testCustomAttributes_log_remove_attribute_event() { + val kit = MockAppboyKit() + val currentUser = braze.currentUser + + kit.configuration = MockKitConfiguration() + + val jsonObject = JSONObject() + val mapValue = JSONObject() + //this is hash for event attribute i.e combination of eventType + eventName + attribute key + mapValue.put("888169310", "testEvent") + val eaaObject = JSONObject() + eaaObject.put("eaa", mapValue) + eaaObject.put("ear", mapValue) + jsonObject.put("hs", eaaObject) + + Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) + + var kitConfiguration = MockKitConfiguration.createKitConfiguration(jsonObject) + kit.configuration = kitConfiguration + val customAttributes: MutableMap = HashMap() + customAttributes["destination"] = "Shop" + val event = MPEvent.Builder("AndroidTEST", MParticle.EventType.Navigation) + .customAttributes(customAttributes) + .build() + val instance = MParticle.getInstance() + instance?.logEvent(event) + kit.logEvent(event) + Assert.assertEquals(1, braze.events.size.toLong()) + Assert.assertEquals(0, currentUser.getCustomAttribute().size.toLong()) + } + + @Test + fun testCustomAttributes_log_add_customUserAttribute_event() { + val kit = MockAppboyKit() + val currentUser = braze.currentUser + + kit.configuration = MockKitConfiguration() + + val jsonObject = JSONObject() + val mapValue = JSONObject() + //this is hash for event attribute i.e combination of eventType + eventName + attribute key + mapValue.put("888169310", "testEvent") + val eaaObject = JSONObject() + eaaObject.put("eas", mapValue) + jsonObject.put("hs", eaaObject) + + Mockito.`when`(mTypeFilters!!.size()).thenReturn(0) + + var kitConfiguration = MockKitConfiguration.createKitConfiguration(jsonObject) + kit.configuration = kitConfiguration + val customAttributes: MutableMap = HashMap() + customAttributes["destination"] = "Shop" + val event = MPEvent.Builder("AndroidTEST", MParticle.EventType.Navigation) + .customAttributes(customAttributes) + .build() + val instance = MParticle.getInstance() + instance?.logEvent(event) + kit.logEvent(event) + Assert.assertEquals(1, braze.events.size.toLong()) + Assert.assertEquals(1, currentUser.getCustomUserAttribute().size.toLong()) + var outputKey = "" + for (keys in currentUser.getCustomUserAttribute().keys) { + outputKey = keys + break + } + Assert.assertEquals("testEvent", outputKey) + } }