-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathMParticleIdentityClientImplTest.kt
More file actions
568 lines (547 loc) · 23.5 KB
/
MParticleIdentityClientImplTest.kt
File metadata and controls
568 lines (547 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package com.mparticle.identity
import android.os.Handler
import android.os.Looper
import android.util.MutableBoolean
import com.mparticle.MParticle
import com.mparticle.internal.ConfigManager
import com.mparticle.internal.MPUtility
import com.mparticle.networking.MPConnection
import com.mparticle.networking.MPConnectionTestImpl
import com.mparticle.testutils.AndroidUtils
import com.mparticle.testutils.BaseCleanStartedEachTest
import com.mparticle.testutils.MPLatch
import org.json.JSONException
import org.json.JSONObject
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.io.IOException
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.util.concurrent.CountDownLatch
class MParticleIdentityClientImplTest : BaseCleanStartedEachTest() {
private var mConfigManager: ConfigManager? = null
private var mApiClient: MParticleIdentityClientImpl? = null
@Before
@Throws(Exception::class)
fun before() {
mConfigManager = MParticle.getInstance()?.Internal()?.configManager
}
@Test
@Throws(Exception::class)
fun testModifySameData() {
val latch: CountDownLatch = MPLatch(2)
val handler = Handler()
handler.postDelayed({ Assert.fail("modify did not complete") }, (10 * 1000).toLong())
val called = AndroidUtils.Mutable(false)
MParticle.getInstance()?.Identity()?.modify(IdentityApiRequest.withEmptyUser().build())
?.addSuccessListener {
latch.countDown()
MParticle.getInstance()?.Identity()
?.modify(IdentityApiRequest.withEmptyUser().build())
?.addSuccessListener {
val currentModifyRequestCount = mServer.Requests().modify.size
// make sure we made 1 or 0 modify requests. It could go either way for the first modify request,
// it may have changes, it may not depending on state. The second request though, should not have
// changes, and therefore it should not take place, so less than 2 requests is a good condition
Assert.assertTrue(2 > currentModifyRequestCount)
// handler.removeCallbacks(null)
called.value = true
latch.countDown()
}
?.addFailureListener { Assert.fail("task failed") }
}
?.addFailureListener { Assert.fail("task failed") }
latch.await()
Assert.assertTrue(called.value)
}
@Test
@Throws(Exception::class)
fun testLoginWithTwoDifferentUsers() {
// clear existing catch
clearIdentityCache()
val latch: CountDownLatch = MPLatch(2)
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong())
val called = AndroidUtils.Mutable(false)
val identityRequest = IdentityApiRequest.withEmptyUser()
.email("TestEmail@mparticle6.com")
.customerId("TestUser777777")
.build()
// Login with First User
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
latch.countDown()
}
// Login With second User
MParticle.getInstance()?.Identity()?.login(IdentityApiRequest.withEmptyUser().build())?.addSuccessListener {
val currentLoginRequestCount = mServer.Requests().login.size
Assert.assertEquals(2, currentLoginRequestCount)
called.value = true
latch.countDown()
}
latch.await()
Assert.assertTrue(called.value)
}
@Test
@Throws(Exception::class)
fun testLoginWithTwoSameUsers() {
// clear existing catch
clearIdentityCache()
val latch: CountDownLatch = MPLatch(2)
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong())
val called = AndroidUtils.Mutable(false)
val identityRequest = IdentityApiRequest.withEmptyUser()
.email("TestEmail@mparticle6.com")
.customerId("TestUser777777")
.build()
// Login with First User
Thread {
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
latch.countDown()
}
// Login With same User
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
val currentLoginRequestCount = mServer.Requests().login.size
Assert.assertEquals(1, currentLoginRequestCount)
called.value = true
latch.countDown()
}
latch.await()
Assert.assertTrue(called.value)
}.start()
}
@Test
@Throws(Exception::class)
fun testLoginWithTwoSameUsers_withLogout() {
// clear existing catch
clearIdentityCache()
val latch: CountDownLatch = MPLatch(3)
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong())
val called = AndroidUtils.Mutable(false)
val identityRequest = IdentityApiRequest.withEmptyUser()
.email("TestEmail@mparticle6.com")
.customerId("TestUser777777")
.build()
// Login with First User
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
latch.countDown()
}
MParticle.getInstance()?.Identity()?.logout()?.addSuccessListener {
latch.countDown()
}
// Login With same User
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
val currentLoginRequestCount = mServer.Requests().login.size
Assert.assertEquals(2, currentLoginRequestCount)
called.value = true
latch.countDown()
}
latch.await()
Assert.assertTrue(called.value)
}
@Test
@Throws(Exception::class)
fun testLoginAndIdentitySameUser() {
// clear existing catch
clearIdentityCache()
val latch: CountDownLatch = MPLatch(2)
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong())
val called = AndroidUtils.Mutable(false)
val identityRequest = IdentityApiRequest.withEmptyUser()
.email("TestEmail@mparticle6.com")
.customerId("TestUser777777")
.build()
// Login with First User
MParticle.getInstance()?.Identity()?.identify(identityRequest)?.addSuccessListener {
latch.countDown()
}
// Login With same User
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
val currentLoginRequestCount = mServer.Requests().login.size
Assert.assertEquals(1, currentLoginRequestCount)
val currentIdentityRequestCount = mServer.Requests().login.size
Assert.assertEquals(1, currentIdentityRequestCount)
called.value = true
latch.countDown()
}
latch.await()
Assert.assertTrue(called.value)
}
@Test
@Throws(Exception::class)
fun testTwoIdentitySameUser_WithModify() {
// clear existing catch
clearIdentityCache()
val latch: CountDownLatch = MPLatch(3)
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong())
val called = AndroidUtils.Mutable(false)
val identityRequest = IdentityApiRequest.withEmptyUser()
.email("TestEmail@mparticle6.com")
.customerId("TestUser777777")
.build()
val identityRequestModify = IdentityApiRequest.withEmptyUser()
.email("NewTest@mparticle6.com")
.customerId("TestUser777777")
.build()
// Identity with First User
MParticle.getInstance()?.Identity()?.identify(identityRequest)?.addSuccessListener {
latch.countDown()
}
MParticle.getInstance()?.Identity()?.modify(identityRequestModify)?.addSuccessListener {
latch.countDown()
}
// Identity With same User
MParticle.getInstance()?.Identity()?.identify(identityRequest)?.addSuccessListener {
val currentIdentityRequestCount = mServer.Requests().identify.size
Assert.assertEquals(3, currentIdentityRequestCount)
called.value = true
latch.countDown()
}
latch.await()
Assert.assertTrue(called.value)
}
@Test
@Throws(Exception::class)
fun testLoginWithTwoSameUsers_WithTimeout() {
// clear existing catch
val mParticleIdentityClient = MParticleIdentityClientImpl(
mContext,
mConfigManager,
MParticle.OperatingSystem.ANDROID
)
clearIdentityCache()
val latch: CountDownLatch = MPLatch(2)
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong())
val called = AndroidUtils.Mutable(false)
val identityRequest = IdentityApiRequest.withEmptyUser()
.email("TestEmail@mparticle6.com")
.customerId("TestUser777777")
.build()
// Login with First User
Thread {
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
latch.countDown()
}
val field: Field =
MParticleIdentityClientImpl::class.java.getDeclaredField("identityCacheTime")
field.isAccessible = true
field.set(mParticleIdentityClient, 0L)
// Login With same User
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener {
val currentLoginRequestCount = mServer.Requests().login.size
Assert.assertEquals(2, currentLoginRequestCount)
called.value = true
latch.countDown()
}
latch.await()
Assert.assertTrue(called.value)
}.start()
}
@Test
@Throws(Exception::class)
fun testIdentifyMessage() {
val iterations = 5
for (i in 0 until iterations) {
val userIdentities = mRandomUtils.randomUserIdentities
val checked = MutableBoolean(false)
val latch = CountDownLatch(1)
setApiClient(object : MockIdentityApiClient {
@Throws(IOException::class, JSONException::class)
override fun makeUrlRequest(
connection: MPConnection,
payload: String?,
mparticle: Boolean
) {
if (connection.url.toString().contains("/identify")) {
val jsonObject = JSONObject(payload)
val knownIdentities =
jsonObject.getJSONObject(MParticleIdentityClientImpl.KNOWN_IDENTITIES)
Assert.assertNotNull(knownIdentities)
checkStaticsAndRemove(knownIdentities)
if (knownIdentities.length() != userIdentities.size) {
Assert.assertEquals(
knownIdentities.length().toLong(),
userIdentities.size.toLong()
)
}
for ((key, value1) in userIdentities) {
val value = knownIdentities.getString(
MParticleIdentityClientImpl.getStringValue(
key
)
)
Assert.assertEquals(value, value1)
}
checked.value = true
setApiClient(null)
latch.countDown()
}
}
})
mApiClient?.identify(
IdentityApiRequest.withEmptyUser()
.userIdentities(userIdentities)
.build()
)
latch.await()
Assert.assertTrue(checked.value)
}
}
@Test
@Throws(Exception::class)
fun testLoginMessage() {
val iterations = 5
for (i in 0 until iterations) {
val latch: CountDownLatch = MPLatch(1)
val checked = MutableBoolean(false)
val userIdentities = mRandomUtils.randomUserIdentities
setApiClient(object : MockIdentityApiClient {
@Throws(IOException::class, JSONException::class)
override fun makeUrlRequest(
connection: MPConnection,
payload: String?,
mparticle: Boolean
) {
if (connection.url.toString().contains("/login")) {
val jsonObject = JSONObject(payload)
val knownIdentities =
jsonObject.getJSONObject(MParticleIdentityClientImpl.KNOWN_IDENTITIES)
Assert.assertNotNull(knownIdentities)
checkStaticsAndRemove(knownIdentities)
Assert.assertEquals(
knownIdentities.length().toLong(),
userIdentities.size.toLong()
)
for ((key, value1) in userIdentities) {
val value = knownIdentities.getString(
MParticleIdentityClientImpl.getStringValue(
key
)
)
Assert.assertEquals(value, value1)
}
checked.value = true
latch.countDown()
}
}
})
mApiClient?.login(
IdentityApiRequest.withEmptyUser()
.userIdentities(userIdentities)
.build()
)
latch.await()
Assert.assertTrue(checked.value)
}
}
@Test
@Throws(Exception::class)
fun testLogoutMessage() {
val iterations = 5
for (i in 0 until iterations) {
val userIdentities = mRandomUtils.randomUserIdentities
val latch: CountDownLatch = MPLatch(1)
val checked = MutableBoolean(false)
setApiClient(object : MockIdentityApiClient {
@Throws(IOException::class, JSONException::class)
override fun makeUrlRequest(
connection: MPConnection,
payload: String?,
mparticle: Boolean
) {
if (connection.url.toString().contains("/logout")) {
val jsonObject = payload?.let { JSONObject(it) }
val knownIdentities =
jsonObject?.getJSONObject(MParticleIdentityClientImpl.KNOWN_IDENTITIES)
Assert.assertNotNull(knownIdentities)
if (knownIdentities != null) {
checkStaticsAndRemove(knownIdentities)
}
if (knownIdentities != null) {
Assert.assertEquals(
knownIdentities.length().toLong(),
userIdentities.size.toLong()
)
}
for ((key, value1) in userIdentities) {
val value = knownIdentities?.getString(
MParticleIdentityClientImpl.getStringValue(
key
)
)
Assert.assertEquals(value, value1)
}
checked.value = true
latch.countDown()
}
}
})
mApiClient?.logout(
IdentityApiRequest.withEmptyUser()
.userIdentities(userIdentities)
.build()
)
latch.await()
Assert.assertTrue(checked.value)
}
}
@Test
@Throws(Exception::class)
fun testModifyMessage() {
val iterations = 5
for (i in 1..iterations) {
mConfigManager?.setMpid(i.toLong(), ran.nextBoolean())
val oldUserIdentities = mRandomUtils.randomUserIdentities
val newUserIdentities = mRandomUtils.randomUserIdentities
(
MParticle.getInstance()
?.Identity()?.currentUser as MParticleUserImpl?
)?.userIdentities = oldUserIdentities
val latch: CountDownLatch = MPLatch(1)
val checked = MutableBoolean(false)
setApiClient(object : MockIdentityApiClient {
@Throws(IOException::class, JSONException::class)
override fun makeUrlRequest(
connection: MPConnection,
payload: String?,
mparticle: Boolean
) {
if (connection.url.toString()
.contains(MParticleIdentityClientImpl.MODIFY_PATH)
) {
val jsonObject = payload?.let { JSONObject(it) }
val changedIdentities =
jsonObject?.getJSONArray(MParticleIdentityClientImpl.IDENTITY_CHANGES)
if (changedIdentities != null) {
for (i in 0 until changedIdentities.length()) {
val changeJson = changedIdentities.getJSONObject(i)
val newValue: Any =
changeJson.getString(MParticleIdentityClientImpl.NEW_VALUE)
val oldValue: Any =
changeJson.getString(MParticleIdentityClientImpl.OLD_VALUE)
val identityType = MParticleIdentityClientImpl.getIdentityType(
changeJson.getString(MParticleIdentityClientImpl.IDENTITY_TYPE)
)
val nullString = JSONObject.NULL.toString()
if (oldUserIdentities[identityType] == null) {
if (oldValue != JSONObject.NULL.toString()) {
Assert.fail()
}
} else {
Assert.assertEquals(
oldValue,
oldUserIdentities[identityType]
)
}
if (newUserIdentities[identityType] == null) {
if (newValue != nullString) {
Assert.fail()
}
} else {
Assert.assertEquals(
newValue,
newUserIdentities[identityType]
)
}
}
}
setApiClient(null)
checked.value = true
latch.countDown()
}
}
})
mApiClient?.modify(
IdentityApiRequest.withEmptyUser()
.userIdentities(newUserIdentities)
.build()
)
latch.await()
Assert.assertTrue(checked.value)
}
}
private fun setApiClient(identityClient: MockIdentityApiClient?) {
mApiClient = object : MParticleIdentityClientImpl(
mContext,
mConfigManager,
MParticle.OperatingSystem.ANDROID
) {
@Throws(IOException::class)
override fun makeUrlRequest(
endpoint: Endpoint,
connection: MPConnection,
payload: String,
identity: Boolean
): MPConnection {
try {
identityClient?.makeUrlRequest(connection, payload, identity)
} catch (e: JSONException) {
e.printStackTrace()
Assert.fail(e.message)
}
(connection as MPConnectionTestImpl).responseCode = 202
return connection
}
}
MParticle.getInstance()?.Identity()?.apiClient = mApiClient
}
private fun clearIdentityCache() {
val mParticleIdentityClient = MParticleIdentityClientImpl(
mContext,
mConfigManager,
MParticle.OperatingSystem.ANDROID
)
val method: Method = MParticleIdentityClientImpl::class.java.getDeclaredMethod("clearCatch")
method.isAccessible = true
method.invoke(mParticleIdentityClient)
}
@Throws(JSONException::class)
private fun checkStaticsAndRemove(knowIdentites: JSONObject) {
if (knowIdentites.has(MParticleIdentityClientImpl.ANDROID_AAID)) {
Assert.assertEquals(
MPUtility.getAdIdInfo(mContext)?.id,
knowIdentites.getString(MParticleIdentityClientImpl.ANDROID_AAID)
)
knowIdentites.remove(MParticleIdentityClientImpl.ANDROID_AAID)
} else {
Assert.assertTrue(
MPUtility.getAdIdInfo(mContext) == null || MPUtility.isEmpty(
MPUtility.getAdIdInfo(mContext)?.id
)
)
}
if (knowIdentites.has(MParticleIdentityClientImpl.ANDROID_UUID)) {
Assert.assertEquals(
MPUtility.getAndroidID(mContext),
knowIdentites.getString(MParticleIdentityClientImpl.ANDROID_UUID)
)
knowIdentites.remove(MParticleIdentityClientImpl.ANDROID_UUID)
} else {
Assert.assertTrue(MPUtility.isEmpty(MPUtility.getAndroidID(mContext)))
}
if (knowIdentites.has(MParticleIdentityClientImpl.PUSH_TOKEN)) {
Assert.assertEquals(
mConfigManager?.pushInstanceId,
knowIdentites.getString(MParticleIdentityClientImpl.PUSH_TOKEN)
)
knowIdentites.remove(MParticleIdentityClientImpl.PUSH_TOKEN)
} else {
Assert.assertNull(mConfigManager?.pushInstanceId)
}
Assert.assertTrue(knowIdentites.has(MParticleIdentityClientImpl.DEVICE_APPLICATION_STAMP))
Assert.assertEquals(
mConfigManager?.deviceApplicationStamp,
knowIdentites[MParticleIdentityClientImpl.DEVICE_APPLICATION_STAMP]
)
knowIdentites.remove(MParticleIdentityClientImpl.DEVICE_APPLICATION_STAMP)
}
internal interface MockIdentityApiClient {
@Throws(IOException::class, JSONException::class)
fun makeUrlRequest(connection: MPConnection, payload: String?, mparticle: Boolean)
}
}