|
17 | 17 | package com.optimizely.ab.android.sdk; |
18 | 18 |
|
19 | 19 | import com.optimizely.ab.Optimizely; |
| 20 | +import com.optimizely.ab.OptimizelyUserContext; |
20 | 21 | import com.optimizely.ab.config.Experiment; |
21 | 22 | import com.optimizely.ab.config.Variation; |
22 | 23 | import com.optimizely.ab.internal.ReservedEventKey; |
|
39 | 40 |
|
40 | 41 | import static junit.framework.Assert.assertTrue; |
41 | 42 | import static junit.framework.Assert.assertFalse; |
| 43 | +import static junit.framework.Assert.assertEquals; |
42 | 44 | import static org.mockito.Mockito.verify; |
43 | 45 | import static org.mockito.Mockito.when; |
44 | 46 | import org.powermock.reflect.Whitebox; |
@@ -277,4 +279,56 @@ public void testBadClearNotificationCenterListeners() { |
277 | 279 | notificationCenter.clearAllNotificationListeners(); |
278 | 280 | verify(logger).warn("Optimizely is not initialized, could not get the notification listener"); |
279 | 281 | } |
| 282 | + |
| 283 | + @Test |
| 284 | + public void testCreateUserContext_withUserIdAndAttributes() { |
| 285 | + OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger); |
| 286 | + String userId = "testUser123"; |
| 287 | + Map<String, Object> attributes = new HashMap<>(); |
| 288 | + attributes.put("isLoggedIn", true); |
| 289 | + attributes.put("userType", "premium"); |
| 290 | + |
| 291 | + OptimizelyUserContext userContext = optimizelyClient.createUserContext(userId, attributes); |
| 292 | + |
| 293 | + assertTrue(userContext instanceof OptimizelyUserContextAndroid); |
| 294 | + assertEquals(userId, userContext.getUserId()); |
| 295 | + assertEquals(attributes, userContext.getAttributes()); |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + public void testCreateUserContext_withUserIdOnly() { |
| 300 | + OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger); |
| 301 | + String userId = "testUser123"; |
| 302 | + |
| 303 | + OptimizelyUserContext userContext = optimizelyClient.createUserContext(userId); |
| 304 | + |
| 305 | + assertTrue(userContext instanceof OptimizelyUserContextAndroid); |
| 306 | + assertEquals(userId, userContext.getUserId()); |
| 307 | + assertEquals(Collections.emptyMap(), userContext.getAttributes()); |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + public void testCreateUserContext_withNullOptimizely() { |
| 312 | + OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger); |
| 313 | + String userId = "testUser123"; |
| 314 | + Map<String, Object> attributes = new HashMap<>(); |
| 315 | + |
| 316 | + OptimizelyUserContext userContext = optimizelyClient.createUserContext(userId, attributes); |
| 317 | + |
| 318 | + assertEquals(null, userContext); |
| 319 | + verify(logger).warn("Optimizely is not initialized, could not create a user context"); |
| 320 | + } |
| 321 | + |
| 322 | + @Test |
| 323 | + public void testCreateUserContext_withEmptyAttributes() { |
| 324 | + OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger); |
| 325 | + String userId = "testUser123"; |
| 326 | + Map<String, Object> emptyAttributes = Collections.emptyMap(); |
| 327 | + |
| 328 | + OptimizelyUserContext userContext = optimizelyClient.createUserContext(userId, emptyAttributes); |
| 329 | + |
| 330 | + assertTrue(userContext instanceof OptimizelyUserContextAndroid); |
| 331 | + assertEquals(userId, userContext.getUserId()); |
| 332 | + assertEquals(emptyAttributes, userContext.getAttributes()); |
| 333 | + } |
280 | 334 | } |
0 commit comments