Skip to content

Commit

Permalink
fix: Add enum value to hash event attributes (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle authored Jul 9, 2024
1 parent 664c19d commit 4380ffe
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/mparticle/kits/AppboyKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/com/braze/Braze.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,10 +13,22 @@ class Braze {
return Companion.currentUser
}

fun getCustomAttributeArray(): java.util.HashMap<String, MutableList<String>> {
return Companion.currentUser.getCustomAttribute()
}

fun getCurrentUser(callback: IValueCallback<BrazeUser>) {
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,
Expand Down
15 changes: 11 additions & 4 deletions src/test/kotlin/com/braze/BrazeUser.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -58,4 +57,12 @@ class BrazeUser {
customUserAttributes[key] = value
return true
}

fun getCustomAttribute(): HashMap<String, MutableList<String>> {
return customAttributeArray
}

fun getCustomUserAttribute(): HashMap<String, Any> {
return customUserAttributes
}
}
127 changes: 121 additions & 6 deletions src/test/kotlin/com/mparticle/kits/AppboyKitTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,36 +14,46 @@ 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
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
Expand Down Expand Up @@ -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<String, String> = 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<String, String> = 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<String, String> = 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)
}
}

0 comments on commit 4380ffe

Please sign in to comment.