Skip to content

Commit cf6c9d9

Browse files
fix: Reorder attribute merge to apply user attributes first (#96)
1 parent f7c5c2f commit cf6c9d9

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/main/kotlin/com/mparticle/kits/RoktKit.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class RoktKit :
212212
filterUser: FilteredMParticleUser?,
213213
attributes: Map<String, String>,
214214
): Map<String, String> {
215-
val finalAttributes = filterAttributes(attributes, configuration)
215+
val finalAttributes = mutableMapOf<String, String>()
216216

217217
filterUser?.userAttributes?.let { userAttrs ->
218218
for ((key, value) in userAttrs) {
@@ -222,6 +222,8 @@ class RoktKit :
222222
}
223223
}
224224

225+
finalAttributes.putAll(filterAttributes(attributes, configuration))
226+
225227
filterUser?.id?.toString()?.let { mpid ->
226228
finalAttributes[MPID] = mpid
227229
} ?: Logger.warning("RoktKit: No user ID available for placement")

src/test/kotlin/com/mparticle/kits/RoktKitTests.kt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,82 @@ class RoktKitTests {
326326
unmockkObject(Rokt)
327327
}
328328

329+
330+
@Test
331+
fun test_prepareFinalAttributes_handlesSameKeysInAttributesAndUserAttributes() {
332+
// Arrange
333+
mockkObject(Rokt)
334+
val capturedAttributesSlot = slot<Map<String, String>>()
335+
every {
336+
Rokt.execute(
337+
any<String>(),
338+
capture(capturedAttributesSlot),
339+
any<Rokt.RoktCallback>(),
340+
null,
341+
null,
342+
null,
343+
)
344+
} just runs
345+
346+
val mockFilterUser = mock(FilteredMParticleUser::class.java)
347+
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(HashMap())
348+
Mockito.`when`(mockFilterUser.id).thenReturn(12345L)
349+
350+
val userAttributes = HashMap<String, Any?>()
351+
userAttributes["attr_non_null_string"] = "value"
352+
userAttributes["attr_null"] = null
353+
userAttributes["attr_non_string"] = 123
354+
userAttributes["user_key"] = "1231545"
355+
Mockito.`when`(mockFilterUser.userAttributes).thenReturn(userAttributes)
356+
// Set up the configuration with our test filters
357+
val jsonObject = JSONObject()
358+
try {
359+
val filteredKey: String = KitUtils.hashForFiltering("ShouldFilter").toString()
360+
val filteredKey2: String = KitUtils.hashForFiltering("ShouldFilter_key_2").toString()
361+
jsonObject.put(filteredKey, 0)
362+
jsonObject.put(filteredKey2, 1)
363+
} catch (e: Exception) {
364+
println("Exception occurred: ${e.message}")
365+
}
366+
val json = JSONObject()
367+
json.put("ua", jsonObject)
368+
roktKit.configuration = MockKitConfiguration.createKitConfiguration(JSONObject().put("hs", json))
369+
val inputAttributes: Map<String, String> = mapOf(
370+
"key1" to "value1",
371+
"key2" to "value2",
372+
"key3" to "value3",
373+
"user_key" to "2223333",
374+
"ShouldFilter" to "testData"
375+
)
376+
// Act
377+
roktKit.execute(
378+
viewName = "test",
379+
attributes = inputAttributes,
380+
mpRoktEventCallback = null,
381+
placeHolders = null,
382+
fontTypefaces = null,
383+
filterUser = mockFilterUser,
384+
mpRoktConfig = null,
385+
)
386+
387+
// Assert
388+
val capturedAttributes = capturedAttributesSlot.captured
389+
390+
391+
assertEquals(7, capturedAttributes.size)
392+
assertEquals("value", capturedAttributes["attr_non_null_string"])
393+
assertEquals("123", capturedAttributes["attr_non_string"])
394+
assertEquals("2223333", capturedAttributes["user_key"])
395+
assertEquals("value1", capturedAttributes["key1"])
396+
assertEquals("value2", capturedAttributes["key2"])
397+
assertEquals("value3", capturedAttributes["key3"])
398+
assertEquals("12345", capturedAttributes["mpid"])
399+
400+
assertFalse(capturedAttributes.containsKey("ShouldFilter"))
401+
assertFalse(capturedAttributes.containsKey("ShouldFilter_key_2"))
402+
unmockkObject(Rokt)
403+
}
404+
329405
private inner class TestKitManager :
330406
KitManagerImpl(context, null, TestCoreCallbacks(), mock(MParticleOptions::class.java)) {
331407
var attributes = HashMap<String, String>()

0 commit comments

Comments
 (0)