@@ -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,108 @@ 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)
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" ))
187
150
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 testAddIdentityAttributes_bothNull () {
202
155
val method: Method = RoktKit ::class .java.getDeclaredMethod(
203
- " filterAttributes " ,
156
+ " addIdentityAttributes " ,
204
157
Map ::class .java,
205
- KitConfiguration ::class .java
158
+ FilteredMParticleUser ::class .java
206
159
)
207
160
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())
230
163
}
231
164
232
165
@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 ,
" [email protected] " )
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
" [email protected] " ), result)
180
+ }
234
181
235
- // Create test attributes
182
+ @Test
183
+ fun testAddIdentityAttributes_nullUser_returnsSameAttributes () {
236
184
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 "
240
188
)
241
-
242
- // Get the private filterAttributes method using reflection
243
189
val method: Method = RoktKit ::class .java.getDeclaredMethod(
244
- " filterAttributes " ,
190
+ " addIdentityAttributes " ,
245
191
Map ::class .java,
246
- KitConfiguration ::class .java
192
+ FilteredMParticleUser ::class .java
247
193
)
248
194
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 >
271
196
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" ))
279
200
}
280
201
202
+
203
+
281
204
internal inner class TestCoreCallbacks : CoreCallbacks {
282
205
override fun isBackgrounded (): Boolean = false
283
206
override fun getUserBucket (): Int = 0
0 commit comments