@@ -7,6 +7,7 @@ import android.os.Build.VERSION
77import com.mparticle.AttributionError
88import com.mparticle.AttributionResult
99import com.mparticle.MParticle
10+ import com.mparticle.MParticle.IdentityType
1011import com.mparticle.MParticleOptions
1112import com.mparticle.MParticleOptions.DataplanOptions
1213import com.mparticle.identity.IdentityApi
@@ -98,186 +99,108 @@ class RoktKitTests {
9899 }
99100
100101 @Test
101- fun testFilterAttributes () {
102-
103- // Create test attributes
102+ fun test_addIdentityAttributes_When_userIdentities_IS_Null (){
103+ val mockFilterUser = Mockito .mock(FilteredMParticleUser ::class .java)
104+ val userIdentities = HashMap <IdentityType , String >()
105+ Mockito .`when `(mockFilterUser.userIdentities).thenReturn(userIdentities)
104106 val attributes: Map <String , String > = mapOf (
105- " ShouldFilter " to " shoudl_filter_value " ,
106- " ShouldFilter_key_2 " to " ShouldFilter_value " ,
107- " allowed_key " to " allowed_value "
107+ " key1 " to " value1 " ,
108+ " key2 " to " value2 " ,
109+ " key3 " to " value3 "
108110 )
109-
110- // Get the private filterAttributes method using reflection
111111 val method: Method = RoktKit ::class .java.getDeclaredMethod(
112- " filterAttributes " ,
112+ " addIdentityAttributes " ,
113113 Map ::class .java,
114- KitConfiguration ::class .java
114+ FilteredMParticleUser ::class .java
115115 )
116116 method.isAccessible = true
117+ val result = method.invoke(roktKit, attributes, mockFilterUser) as Map <String , String >
118+ assertEquals(3 , result.size)
117119
118- // Set up the configuration with our test filters
119- val jsonObject = JSONObject ()
120- try {
121- val filteredKey: String = KitUtils .hashForFiltering(" ShouldFilter" ).toString()
122- val allowedKey: String = KitUtils .hashForFiltering(" ShouldFilter_key_2" ).toString()
123- jsonObject.put(filteredKey, 0 )
124- jsonObject.put(allowedKey, 1 )
125- } catch (e: Exception ) {
126- println (" Exception occurred: ${e.message} " )
127- }
128-
129- val json = JSONObject ()
130- json.put(" ea" , jsonObject)
131-
132-
133- roktKit.configuration = MockKitConfiguration .createKitConfiguration(JSONObject ().put(" hs" ,json))
134-
135- // Invoke the method and get the result
136- val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map <String , String >
137-
138- // Verify the results
139- assertEquals(1 , result.size)
120+ assertTrue(result.containsKey(" key1" ))
121+ assertTrue(result.containsKey(" key2" ))
122+ assertTrue(result.containsKey(" key3" ))
140123
141- assertFalse(result.containsKey(" ShouldFilter" ))
142- assertFalse(result.containsKey(" ShouldFilter_key_2" ))
143- assertTrue(result.containsKey(" allowed_key" ))
144- assertEquals(" allowed_value" , result[" allowed_key" ])
145124 }
146125
147126 @Test
148- fun testFilterAttributes_When_kitConfig_Attributes_IS_NULL () {
149-
150- // Create test attributes
127+ fun test_addIdentityAttributes_When_userIdentities_Contain_value (){
128+ val mockFilterUser = Mockito .mock(FilteredMParticleUser ::class .java)
129+ val userIdentities = HashMap <IdentityType , String >()
130+ userIdentities.put(IdentityType .Email ," TestEmail@gamil.com" )
131+ Mockito .`when `(mockFilterUser.userIdentities).thenReturn(userIdentities)
151132 val attributes: Map <String , String > = mapOf (
152- " filtered_key " to " filtered_value " ,
153- " allowed_key " to " allowed_value " ,
154- " another_allowed_key " to " another_allowed_value "
133+ " key1 " to " value1 " ,
134+ " key2 " to " value2 " ,
135+ " key3 " to " value3 "
155136 )
156-
157- // Get the private filterAttributes method using reflection
158137 val method: Method = RoktKit ::class .java.getDeclaredMethod(
159- " filterAttributes " ,
138+ " addIdentityAttributes " ,
160139 Map ::class .java,
161- KitConfiguration ::class .java
140+ FilteredMParticleUser ::class .java
162141 )
163142 method.isAccessible = true
143+ val result = method.invoke(roktKit, attributes, mockFilterUser) as Map <String , String >
144+ assertEquals(4 , result.size)
164145
165- // Set up the configuration with our test filters
166- val jsonObject = JSONObject ()
167- try {
168- val filteredKey: String = KitUtils .hashForFiltering(" filtered_key" ).toString()
169- val allowedKey: String = KitUtils .hashForFiltering(" allowed_key" ).toString()
170- jsonObject.put(filteredKey, 0 )
171- jsonObject.put(allowedKey, 1 )
172- } catch (e: Exception ) {
173- println (" Exception occurred: ${e.message} " )
174- }
175-
176- val json = JSONObject ()
177- // here is invalid json key for filtering
178- json.put(" aaa" , jsonObject)
179-
180-
181- roktKit.configuration = MockKitConfiguration .createKitConfiguration(JSONObject ().put(" hs" ,json))
182-
183- // Invoke the method and get the result
184- val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map <String , String >
185-
186- assertEquals(3 , result.size)
146+ assertTrue(result.containsKey(" key1" ))
147+ assertTrue(result.containsKey(" key2" ))
148+ assertTrue(result.containsKey(" key3" ))
149+ assertTrue(result.containsKey(" email" ))
187150
188- assertTrue(result.containsKey(" allowed_key" ))
189- assertTrue(result.containsKey(" filtered_key" ))
190- assertTrue(result.containsKey(" another_allowed_key" ))
191- assertEquals(" another_allowed_value" , result[" another_allowed_key" ])
192151 }
193152
194153 @Test
195- fun testFilterAttributes_When_Attributes_IS_Empty () {
196-
197- // Create test attributes
198- val emptyAttributes: Map <String , String > = emptyMap()
199-
200-
201- // Get the private filterAttributes method using reflection
154+ fun testAddIdentityAttributes_bothNull () {
202155 val method: Method = RoktKit ::class .java.getDeclaredMethod(
203- " filterAttributes " ,
156+ " addIdentityAttributes " ,
204157 Map ::class .java,
205- KitConfiguration ::class .java
158+ FilteredMParticleUser ::class .java
206159 )
207160 method.isAccessible = true
208-
209- // Set up the configuration with our test filters
210- val jsonObject = JSONObject ()
211- try {
212- val filteredKey: String = KitUtils .hashForFiltering(" filtered_key" ).toString()
213- val allowedKey: String = KitUtils .hashForFiltering(" allowed_key" ).toString()
214- jsonObject.put(filteredKey, 0 )
215- jsonObject.put(allowedKey, 1 )
216- } catch (e: Exception ) {
217- println (" Exception occurred: ${e.message} " )
218- }
219-
220- val json = JSONObject ()
221- json.put(" aaa" , jsonObject)
222-
223-
224- roktKit.configuration = MockKitConfiguration .createKitConfiguration(JSONObject ().put(" hs" ,json))
225-
226- // Invoke the method and get the result
227- val result = method.invoke(roktKit, emptyAttributes, roktKit.configuration) as Map <String , String >
228-
229- assertEquals(0 , result.size)
161+ val result = method.invoke(roktKit, null , null ) as Map <String , String >
162+ assertTrue(result.isEmpty())
230163 }
231164
232165 @Test
233- fun testFilterAttributes_When_attribute_different_value () {
166+ fun testAddIdentityAttributes_nullAttributes_validUser () {
167+ val mockFilterUser = Mockito .mock(FilteredMParticleUser ::class .java)
168+ val userIdentities = HashMap <IdentityType , String >()
169+ userIdentities.put(IdentityType .Email ," TestEmail@gamil.com" )
170+ Mockito .`when `(mockFilterUser.userIdentities).thenReturn(userIdentities)
171+ val method: Method = RoktKit ::class .java.getDeclaredMethod(
172+ " addIdentityAttributes" ,
173+ Map ::class .java,
174+ FilteredMParticleUser ::class .java
175+ )
176+ method.isAccessible = true
177+ val result = method.invoke(roktKit, null , mockFilterUser) as Map <String , String >
178+ assertEquals(1 , result.size)
179+ assertEquals(mapOf (" email" to " TestEmail@gamil.com" ), result)
180+ }
234181
235- // Create test attributes
182+ @Test
183+ fun testAddIdentityAttributes_nullUser_returnsSameAttributes () {
236184 val attributes: Map <String , String > = mapOf (
237- " filtered_key " to " filtered_value " ,
238- " allowed_key " to " allowed_value " ,
239- " another_allowed_key " to " another_allowed_value "
185+ " key1 " to " value1 " ,
186+ " key2 " to " value2 " ,
187+ " key3 " to " value3 "
240188 )
241-
242- // Get the private filterAttributes method using reflection
243189 val method: Method = RoktKit ::class .java.getDeclaredMethod(
244- " filterAttributes " ,
190+ " addIdentityAttributes " ,
245191 Map ::class .java,
246- KitConfiguration ::class .java
192+ FilteredMParticleUser ::class .java
247193 )
248194 method.isAccessible = true
249-
250- // Set up the configuration with our test filters
251- val jsonObject = JSONObject ()
252- try {
253- val filteredKey: String = KitUtils .hashForFiltering(" Test1" ).toString()
254- val allowedKey: String = KitUtils .hashForFiltering(" Test2" ).toString()
255- jsonObject.put(filteredKey, 0 )
256- jsonObject.put(allowedKey, 1 )
257- } catch (e: Exception ) {
258- println (" Exception occurred: ${e.message} " )
259- }
260-
261- val json = JSONObject ()
262- json.put(" ea" , jsonObject)
263-
264-
265- roktKit.configuration = MockKitConfiguration .createKitConfiguration(JSONObject ().put(" hs" ,json))
266-
267- // Invoke the method and get the result
268- val result = method.invoke(roktKit, attributes, roktKit.configuration) as Map <String , String >
269-
270- // Verify the results
195+ val result = method.invoke(roktKit, attributes, null ) as Map <String , String >
271196 assertEquals(3 , result.size)
272-
273- assertTrue(result.containsKey(" filtered_key" ))
274- assertTrue(result.containsKey(" allowed_key" ))
275- assertTrue(result.containsKey(" another_allowed_key" ))
276- assertEquals(" another_allowed_value" , result[" another_allowed_key" ])
277- assertEquals(" filtered_value" , result[" filtered_key" ])
278- assertEquals(" allowed_value" , result[" allowed_key" ])
197+ assertTrue(result.containsKey(" key1" ))
198+ assertTrue(result.containsKey(" key2" ))
199+ assertTrue(result.containsKey(" key3" ))
279200 }
280201
202+
203+
281204 internal inner class TestCoreCallbacks : CoreCallbacks {
282205 override fun isBackgrounded (): Boolean = false
283206 override fun getUserBucket (): Int = 0
0 commit comments