@@ -7,6 +7,7 @@ import android.os.Build.VERSION
7
7
import com.mparticle.AttributionError
8
8
import com.mparticle.AttributionResult
9
9
import com.mparticle.MParticle
10
+ import com.mparticle.MParticle.IdentityType
10
11
import com.mparticle.MParticleOptions
11
12
import com.mparticle.MParticleOptions.DataplanOptions
12
13
import com.mparticle.identity.IdentityApi
@@ -98,186 +99,144 @@ class RoktKitTests {
98
99
}
99
100
100
101
@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)
104
106
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 "
108
110
)
109
-
110
- // Get the private filterAttributes method using reflection
111
111
val method: Method = RoktKit ::class .java.getDeclaredMethod(
112
- " filterAttributes " ,
112
+ " addIdentityAttributes " ,
113
113
Map ::class .java,
114
- KitConfiguration ::class .java
114
+ FilteredMParticleUser ::class .java
115
115
)
116
116
method.isAccessible = true
117
+ val result = method.invoke(roktKit, attributes, mockFilterUser) as Map <String , String >
118
+ assertEquals(3 , result.size)
117
119
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" ))
140
123
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" ])
145
124
}
146
125
147
126
@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 ,
" [email protected] " )
131
+ Mockito .`when `(mockFilterUser.userIdentities).thenReturn(userIdentities)
151
132
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 "
155
136
)
156
-
157
- // Get the private filterAttributes method using reflection
158
137
val method: Method = RoktKit ::class .java.getDeclaredMethod(
159
- " filterAttributes " ,
138
+ " addIdentityAttributes " ,
160
139
Map ::class .java,
161
- KitConfiguration ::class .java
140
+ FilteredMParticleUser ::class .java
162
141
)
163
142
method.isAccessible = true
143
+ val result = method.invoke(roktKit, attributes, mockFilterUser) as Map <String , String >
144
+ assertEquals(4 , result.size)
164
145
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)
146
+ assertTrue(result.containsKey(" key1" ))
147
+ assertTrue(result.containsKey(" key2" ))
148
+ assertTrue(result.containsKey(" key3" ))
149
+ assertTrue(result.containsKey(" email" ))
179
150
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)
187
-
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" ])
192
151
}
193
152
194
153
@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 test_addIdentityAttributes_When_userIdentities_And_attributes_contains_same_key (){
155
+ val mockFilterUser = Mockito .mock(FilteredMParticleUser ::class .java)
156
+ val userIdentities = HashMap <IdentityType , String >()
157
+ userIdentities.put(
IdentityType .
Email ,
" [email protected] " )
158
+ Mockito .`when `(mockFilterUser.userIdentities).thenReturn(userIdentities)
159
+ val attributes: Map <String , String > = mapOf (
160
+ " key1" to " value1" ,
161
+ " key2" to " value2" ,
162
+ " key3" to " value3" ,
163
+
164
+ )
202
165
val method: Method = RoktKit ::class .java.getDeclaredMethod(
203
- " filterAttributes " ,
166
+ " addIdentityAttributes " ,
204
167
Map ::class .java,
205
- KitConfiguration ::class .java
168
+ FilteredMParticleUser ::class .java
206
169
)
207
170
method.isAccessible = true
171
+ val result = method.invoke(roktKit, attributes, mockFilterUser) as Map <String , String >
172
+ assertEquals(4 , result.size)
173
+
174
+ assertTrue(result.containsKey(" key1" ))
175
+ assertTrue(result.containsKey(" key2" ))
176
+ assertTrue(result.containsKey(" key3" ))
177
+ assertTrue(result.containsKey(" email" ))
178
+ assertEquals(
179
+ mapOf (
180
+ " key1" to " value1" ,
181
+ " key2" to " value2" ,
182
+ " key3" to " value3" ,
183
+
184
+ ),
185
+ result
186
+ )
187
+ }
208
188
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)
189
+ @Test
190
+ fun testAddIdentityAttributes_bothNull () {
191
+ val method: Method = RoktKit ::class .java.getDeclaredMethod(
192
+ " addIdentityAttributes" ,
193
+ Map ::class .java,
194
+ FilteredMParticleUser ::class .java
195
+ )
196
+ method.isAccessible = true
197
+ val result = method.invoke(roktKit, null , null ) as Map <String , String >
198
+ assertTrue(result.isEmpty())
230
199
}
231
200
232
201
@Test
233
- fun testFilterAttributes_When_attribute_different_value () {
202
+ fun testAddIdentityAttributes_nullAttributes_validUser () {
203
+ val mockFilterUser = Mockito .mock(FilteredMParticleUser ::class .java)
204
+ val userIdentities = HashMap <IdentityType , String >()
205
+ userIdentities.put(
IdentityType .
Email ,
" [email protected] " )
206
+ Mockito .`when `(mockFilterUser.userIdentities).thenReturn(userIdentities)
207
+ val method: Method = RoktKit ::class .java.getDeclaredMethod(
208
+ " addIdentityAttributes" ,
209
+ Map ::class .java,
210
+ FilteredMParticleUser ::class .java
211
+ )
212
+ method.isAccessible = true
213
+ val result = method.invoke(roktKit, null , mockFilterUser) as Map <String , String >
214
+ assertEquals(1 , result.size)
215
+ assertEquals(
mapOf (
" email" to
" [email protected] " ), result)
216
+ }
234
217
235
- // Create test attributes
218
+ @Test
219
+ fun testAddIdentityAttributes_nullUser_returnsSameAttributes () {
236
220
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 "
221
+ " key1 " to " value1 " ,
222
+ " key2 " to " value2 " ,
223
+ " key3 " to " value3 "
240
224
)
241
-
242
- // Get the private filterAttributes method using reflection
243
225
val method: Method = RoktKit ::class .java.getDeclaredMethod(
244
- " filterAttributes " ,
226
+ " addIdentityAttributes " ,
245
227
Map ::class .java,
246
- KitConfiguration ::class .java
228
+ FilteredMParticleUser ::class .java
247
229
)
248
230
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
231
+ val result = method.invoke(roktKit, attributes, null ) as Map <String , String >
271
232
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" ])
233
+ assertTrue(result.containsKey(" key1" ))
234
+ assertTrue(result.containsKey(" key2" ))
235
+ assertTrue(result.containsKey(" key3" ))
279
236
}
280
237
238
+
239
+
281
240
internal inner class TestCoreCallbacks : CoreCallbacks {
282
241
override fun isBackgrounded (): Boolean = false
283
242
override fun getUserBucket (): Int = 0
0 commit comments