Skip to content
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
54 changes: 44 additions & 10 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.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.commerce.CommerceEvent
import com.mparticle.identity.MParticleUser
import com.mparticle.internal.Logger
Expand Down Expand Up @@ -139,14 +140,13 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
finalAttributes[key] = value.toString()
}
}
filterAttributes(finalAttributes, configuration).let {
finalAttributes.putAll(it)
}

filterUser?.id?.let { mpid ->
finalAttributes.put(MPID, mpid.toString())
} ?: run {
Logger.warning("RoktKit: No user ID available for placement")
}
addIdentityAttributes(finalAttributes, filterUser)

Rokt.execute(
viewName,
Expand All @@ -158,15 +158,49 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
)
}

private fun filterAttributes(attributes: Map<String, String>, kitConfiguration: KitConfiguration): Map<String, String> {
val userAttributes: MutableMap<String, String> = HashMap<String, String>()
for ((key, value) in attributes) {
val hashKey = KitUtils.hashForFiltering(key)
if (!kitConfiguration.mAttributeFilters.get(hashKey)) {
userAttributes[key] = value

private fun addIdentityAttributes(attributes: MutableMap<String, String>?, filterUser: FilteredMParticleUser?): MutableMap<String, String> {
val identityAttributes = mutableMapOf<String, String>()
if (filterUser != null) {
for ((identityNumberKey, identityValue) in filterUser.userIdentities) {
val identityType = getStringForIdentity(identityNumberKey)
identityAttributes[identityType] = identityValue
}
}
return userAttributes
if (attributes != null) {
attributes.putAll(identityAttributes)
return attributes
} else {
return identityAttributes
}
}

private fun getStringForIdentity(identityType: IdentityType): String {
return when (identityType) {
IdentityType.Other -> "other"
IdentityType.CustomerId -> "customerid"
IdentityType.Facebook -> "facebook"
IdentityType.Twitter -> "twitter"
IdentityType.Google -> "google"
IdentityType.Microsoft -> "microsoft"
IdentityType.Yahoo -> "yahoo"
IdentityType.Email -> "email"
IdentityType.Alias -> "alias"
IdentityType.FacebookCustomAudienceId -> "facebookcustomaudienceid"
IdentityType.Other2 -> "other2"
IdentityType.Other3 -> "other3"
IdentityType.Other4 -> "other4"
IdentityType.Other5 -> "other5"
IdentityType.Other6 -> "other6"
IdentityType.Other7 -> "other7"
IdentityType.Other8 -> "other8"
IdentityType.Other9 -> "other9"
IdentityType.Other10 -> "other10"
IdentityType.MobileNumber -> "mobilenumber"
IdentityType.PhoneNumber2 -> "phonenumber2"
IdentityType.PhoneNumber3 -> "phonenumber3"
else -> ""
}
}

companion object {
Expand Down
239 changes: 99 additions & 140 deletions src/test/kotlin/com/mparticle/kits/RoktKitTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Build.VERSION
import com.mparticle.AttributionError
import com.mparticle.AttributionResult
import com.mparticle.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.MParticleOptions
import com.mparticle.MParticleOptions.DataplanOptions
import com.mparticle.identity.IdentityApi
Expand Down Expand Up @@ -98,186 +99,144 @@ class RoktKitTests {
}

@Test
fun testFilterAttributes() {

// Create test attributes
fun test_addIdentityAttributes_When_userIdentities_IS_Null(){
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
val userIdentities = HashMap<IdentityType, String>()
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
val attributes: Map<String, String> = mapOf(
"ShouldFilter" to "shoudl_filter_value",
"ShouldFilter_key_2" to "ShouldFilter_value",
"allowed_key" to "allowed_value"
"key1" to "value1",
"key2" to "value2",
"key3" to "value3"
)

// Get the private filterAttributes method using reflection
val method: Method = RoktKit::class.java.getDeclaredMethod(
"filterAttributes",
"addIdentityAttributes",
Map::class.java,
KitConfiguration::class.java
FilteredMParticleUser::class.java
)
method.isAccessible = true
val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String>
assertEquals(3, result.size)

// Set up the configuration with our test filters
val jsonObject = JSONObject()
try {
val filteredKey:String =KitUtils.hashForFiltering("ShouldFilter").toString()
val allowedKey:String = KitUtils.hashForFiltering("ShouldFilter_key_2").toString()
jsonObject.put(filteredKey, 0)
jsonObject.put(allowedKey, 1)
} catch (e: Exception) {
println("Exception occurred: ${e.message}")
}

val json = JSONObject()
json.put("ea", jsonObject)


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

// Invoke the method and get the result
val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map<String, String>

// Verify the results
assertEquals(1, result.size)
assertTrue(result.containsKey("key1"))
assertTrue(result.containsKey("key2"))
assertTrue(result.containsKey("key3"))
Comment on lines +120 to +122
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also have a test that checks the value as well?


assertFalse(result.containsKey("ShouldFilter"))
assertFalse(result.containsKey("ShouldFilter_key_2"))
assertTrue(result.containsKey("allowed_key"))
assertEquals("allowed_value", result["allowed_key"])
}

@Test
fun testFilterAttributes_When_kitConfig_Attributes_IS_NULL() {

// Create test attributes
fun test_addIdentityAttributes_When_userIdentities_Contain_value(){
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
val userIdentities = HashMap<IdentityType, String>()
userIdentities.put(IdentityType.Email,"TestEmail@gamil.com")
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
val attributes: Map<String, String> = mapOf(
"filtered_key" to "filtered_value",
"allowed_key" to "allowed_value",
"another_allowed_key" to "another_allowed_value"
"key1" to "value1",
"key2" to "value2",
"key3" to "value3"
)

// Get the private filterAttributes method using reflection
val method: Method = RoktKit::class.java.getDeclaredMethod(
"filterAttributes",
"addIdentityAttributes",
Map::class.java,
KitConfiguration::class.java
FilteredMParticleUser::class.java
)
method.isAccessible = true
val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String>
assertEquals(4, result.size)

// Set up the configuration with our test filters
val jsonObject = JSONObject()
try {
val filteredKey:String =KitUtils.hashForFiltering("filtered_key").toString()
val allowedKey:String = KitUtils.hashForFiltering("allowed_key").toString()
jsonObject.put(filteredKey, 0)
jsonObject.put(allowedKey, 1)
} catch (e: Exception) {
println("Exception occurred: ${e.message}")
}

val json = JSONObject()
//here is invalid json key for filtering
json.put("aaa", jsonObject)
assertTrue(result.containsKey("key1"))
assertTrue(result.containsKey("key2"))
assertTrue(result.containsKey("key3"))
assertTrue(result.containsKey("email"))
Comment on lines +146 to +149
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also have a test that checks the value as well?



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

// Invoke the method and get the result
val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map<String, String>

assertEquals(3, result.size)

assertTrue(result.containsKey("allowed_key"))
assertTrue(result.containsKey("filtered_key"))
assertTrue(result.containsKey("another_allowed_key"))
assertEquals("another_allowed_value", result["another_allowed_key"])
}

@Test
fun testFilterAttributes_When_Attributes_IS_Empty() {

// Create test attributes
val emptyAttributes: Map<String, String> = emptyMap()


// Get the private filterAttributes method using reflection
fun test_addIdentityAttributes_When_userIdentities_And_attributes_contains_same_key(){
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
val userIdentities = HashMap<IdentityType, String>()
userIdentities.put(IdentityType.Email,"TestEmail@gamil.com")
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
val attributes: Map<String, String> = mapOf(
"key1" to "value1",
"key2" to "value2",
"key3" to "value3",
"email" to "abc@gmail.com"
)
val method: Method = RoktKit::class.java.getDeclaredMethod(
"filterAttributes",
"addIdentityAttributes",
Map::class.java,
KitConfiguration::class.java
FilteredMParticleUser::class.java
)
method.isAccessible = true
val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String>
assertEquals(4, result.size)

assertTrue(result.containsKey("key1"))
assertTrue(result.containsKey("key2"))
assertTrue(result.containsKey("key3"))
assertTrue(result.containsKey("email"))
assertEquals(
mapOf(
"key1" to "value1",
"key2" to "value2",
"key3" to "value3",
"email" to "TestEmail@gamil.com"
),
result
)
}

// Set up the configuration with our test filters
val jsonObject = JSONObject()
try {
val filteredKey:String =KitUtils.hashForFiltering("filtered_key").toString()
val allowedKey:String = KitUtils.hashForFiltering("allowed_key").toString()
jsonObject.put(filteredKey, 0)
jsonObject.put(allowedKey, 1)
} catch (e: Exception) {
println("Exception occurred: ${e.message}")
}

val json = JSONObject()
json.put("aaa", jsonObject)


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

// Invoke the method and get the result
val result = method.invoke(roktKit, emptyAttributes, roktKit.configuration) as Map<String, String>

assertEquals(0, result.size)
@Test
fun testAddIdentityAttributes_bothNull() {
val method: Method = RoktKit::class.java.getDeclaredMethod(
"addIdentityAttributes",
Map::class.java,
FilteredMParticleUser::class.java
)
method.isAccessible = true
val result = method.invoke(roktKit, null, null) as Map<String, String>
assertTrue(result.isEmpty())
}

@Test
fun testFilterAttributes_When_attribute_different_value() {
fun testAddIdentityAttributes_nullAttributes_validUser() {
val mockFilterUser = Mockito.mock(FilteredMParticleUser::class.java)
val userIdentities = HashMap<IdentityType, String>()
userIdentities.put(IdentityType.Email,"TestEmail@gamil.com")
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
val method: Method = RoktKit::class.java.getDeclaredMethod(
"addIdentityAttributes",
Map::class.java,
FilteredMParticleUser::class.java
)
method.isAccessible = true
val result = method.invoke(roktKit, null, mockFilterUser) as Map<String, String>
assertEquals(1, result.size)
assertEquals(mapOf("email" to "TestEmail@gamil.com"), result)
}

// Create test attributes
@Test
fun testAddIdentityAttributes_nullUser_returnsSameAttributes() {
val attributes: Map<String, String> = mapOf(
"filtered_key" to "filtered_value",
"allowed_key" to "allowed_value",
"another_allowed_key" to "another_allowed_value"
"key1" to "value1",
"key2" to "value2",
"key3" to "value3"
)

// Get the private filterAttributes method using reflection
val method: Method = RoktKit::class.java.getDeclaredMethod(
"filterAttributes",
"addIdentityAttributes",
Map::class.java,
KitConfiguration::class.java
FilteredMParticleUser::class.java
)
method.isAccessible = true

// Set up the configuration with our test filters
val jsonObject = JSONObject()
try {
val filteredKey:String =KitUtils.hashForFiltering("Test1").toString()
val allowedKey:String = KitUtils.hashForFiltering("Test2").toString()
jsonObject.put(filteredKey, 0)
jsonObject.put(allowedKey, 1)
} catch (e: Exception) {
println("Exception occurred: ${e.message}")
}

val json = JSONObject()
json.put("ea", jsonObject)


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

// Invoke the method and get the result
val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map<String, String>

// Verify the results
val result = method.invoke(roktKit, attributes, null) as Map<String, String>
assertEquals(3, result.size)

assertTrue(result.containsKey("filtered_key"))
assertTrue(result.containsKey("allowed_key"))
assertTrue(result.containsKey("another_allowed_key"))
assertEquals("another_allowed_value", result["another_allowed_key"])
assertEquals("filtered_value", result["filtered_key"])
assertEquals("allowed_value", result["allowed_key"])
assertTrue(result.containsKey("key1"))
assertTrue(result.containsKey("key2"))
assertTrue(result.containsKey("key3"))
}



internal inner class TestCoreCallbacks : CoreCallbacks {
override fun isBackgrounded(): Boolean = false
override fun getUserBucket(): Int = 0
Expand Down
Loading