Skip to content

Commit fedf965

Browse files
update with bucketing id and notification listener tests
1 parent 92bff91 commit fedf965

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
2525
import com.optimizely.ab.bucketing.Bucketer;
2626
import com.optimizely.ab.bucketing.DecisionService;
27+
import com.optimizely.ab.config.Attribute;
2728
import com.optimizely.ab.config.Experiment;
2829
import com.optimizely.ab.config.ProjectConfig;
2930
import com.optimizely.ab.config.Variation;
@@ -186,7 +187,19 @@ public void testGoodActivationAttrib() {
186187
}
187188

188189
@Test
189-
public void testBadForcedActivationAttrib() {
190+
public void testGoodActivationBucketingId() {
191+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
192+
final HashMap<String, String> attributes = new HashMap<>();
193+
String bucketingId = "1";
194+
Experiment experiment = optimizelyClient.getProjectConfig().getExperimentKeyMapping().get("android_experiment_key");
195+
attributes.put(DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
196+
Variation v = optimizelyClient.activate("android_experiment_key", "userId", attributes);
197+
verify(bucketer).bucket( experiment, bucketingId);
198+
assertNotNull(v);
199+
}
200+
201+
@Test
202+
public void testBadForcedActivationAttribute() {
190203
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
191204
boolean didSetForced = optimizelyClient.setForcedVariation("android_experiment_key", "1", "var_2");
192205

@@ -261,6 +274,18 @@ public void testGoodTrack() {
261274
verifyZeroInteractions(logger);
262275
}
263276

277+
@Test
278+
public void testGoodTrackBucketing() {
279+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
280+
Map<String,String> attributes = new HashMap<>();
281+
String bucketingId = "1";
282+
Experiment experiment = optimizelyClient.getProjectConfig().getExperimentsForEventKey("test_event").get(0);
283+
attributes.put(DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
284+
optimizelyClient.track("test_event", "userId", attributes);
285+
verify(bucketer).bucket(experiment, bucketingId);
286+
verifyZeroInteractions(logger);
287+
}
288+
264289
@Test
265290
public void testBadTrack() {
266291
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
@@ -601,6 +626,18 @@ public void testGoodGetVariation1() {
601626
assertNotNull(v);
602627
}
603628

629+
@Test
630+
public void testGoodGetVariationBucketingId() {
631+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
632+
Experiment experiment = optimizelyClient.getProjectConfig().getExperimentKeyMapping().get("android_experiment_key");
633+
String bucketingId = "1";
634+
Map<String, String> attributes = new HashMap<>();
635+
attributes.put(DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
636+
Variation v = optimizelyClient.getVariation("android_experiment_key", "userId", attributes);
637+
verify(bucketer).bucket(experiment, bucketingId);
638+
assertNotNull(v);
639+
}
640+
604641
@Test
605642
public void testGoodGetVariation1Forced() {
606643
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyClient.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.optimizely.ab.config.Experiment;
2626
import com.optimizely.ab.config.ProjectConfig;
2727
import com.optimizely.ab.config.Variation;
28+
import com.optimizely.ab.notification.NotificationCenter;
2829
import com.optimizely.ab.notification.NotificationListener;
2930

3031
import org.slf4j.Logger;
@@ -489,6 +490,7 @@ public boolean setForcedVariation(@NonNull String experimentKey,
489490
*
490491
* @param listener listener to add
491492
*/
493+
@Deprecated
492494
public void addNotificationListener(@NonNull NotificationListener listener) {
493495
if (isValid()) {
494496
optimizely.addNotificationListener(listener);
@@ -502,6 +504,7 @@ public void addNotificationListener(@NonNull NotificationListener listener) {
502504
*
503505
* @param listener listener to remove
504506
*/
507+
@Deprecated
505508
public void removeNotificationListener(@NonNull NotificationListener listener) {
506509
if (isValid()) {
507510
optimizely.removeNotificationListener(listener);
@@ -513,11 +516,27 @@ public void removeNotificationListener(@NonNull NotificationListener listener) {
513516
/**
514517
* Remove all {@link NotificationListener} instances.
515518
*/
519+
@Deprecated
516520
public void clearNotificationListeners() {
517521
if (isValid()) {
518522
optimizely.clearNotificationListeners();
519523
} else {
520524
logger.warn("Optimizely is not initialized, could not clear notification listeners");
521525
}
522526
}
527+
528+
/**
529+
* Return the notification center {@link NotificationCenter} used to add notifications for events
530+
* such as Activate and track.
531+
* @return
532+
*/
533+
public NotificationCenter getNotificationCenter() {
534+
if (isValid()) {
535+
return optimizely.notificationCenter;
536+
} else {
537+
logger.warn("Optimizely is not initialized, could not get the notification listener");
538+
}
539+
540+
return null;
541+
}
523542
}

android-sdk/src/test/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,30 @@
1919
import com.optimizely.ab.Optimizely;
2020
import com.optimizely.ab.config.Experiment;
2121
import com.optimizely.ab.config.Variation;
22+
import com.optimizely.ab.event.LogEvent;
23+
import com.optimizely.ab.notification.ActivateNotificationListener;
24+
import com.optimizely.ab.notification.NotificationCenter;
2225
import com.optimizely.ab.notification.NotificationListener;
26+
import com.optimizely.ab.notification.TrackNotificationListener;
2327

28+
import org.junit.Before;
2429
import org.junit.Test;
2530
import org.junit.runner.RunWith;
2631
import org.mockito.Mock;
2732
import org.mockito.runners.MockitoJUnitRunner;
2833
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
2934
import org.slf4j.Logger;
3035

36+
import java.lang.reflect.Field;
37+
import java.lang.reflect.Modifier;
3138
import java.util.Collections;
3239
import java.util.HashMap;
3340
import java.util.Map;
3441

3542
import static junit.framework.Assert.assertTrue;
3643
import static junit.framework.Assert.assertFalse;
3744
import static org.mockito.Mockito.verify;
45+
import static org.mockito.Mockito.when;
3846

3947
/**
4048
* Tests for {@link OptimizelyClient}
@@ -44,6 +52,29 @@ public class OptimizelyClientTest {
4452

4553
@Mock Logger logger;
4654
@Mock Optimizely optimizely;
55+
@Mock NotificationCenter notificationCenter;
56+
57+
@Before
58+
public void setup() {
59+
Field field = null;
60+
try {
61+
field = Optimizely.class.getDeclaredField("notificationCenter");
62+
// Mark the field as public so we can toy with it
63+
field.setAccessible(true);
64+
// Get the Modifiers for the Fields
65+
Field modifiersField = Field.class.getDeclaredField("modifiers");
66+
// Allow us to change the modifiers
67+
modifiersField.setAccessible(true);
68+
// Remove final modifier from field by blanking out the bit that says "FINAL" in the Modifiers
69+
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
70+
// Set new value
71+
field.set(optimizely, notificationCenter);
72+
} catch (NoSuchFieldException e) {
73+
e.printStackTrace();
74+
} catch (IllegalAccessException e) {
75+
e.printStackTrace();
76+
}
77+
}
4778

4879
@Test(expected=ArgumentsAreDifferent.class)
4980
public void testGoodActivation1() {
@@ -76,7 +107,6 @@ public void testBadActivation2() {
76107
"for user {} with attributes", "1", "1");
77108
}
78109

79-
80110
@Test(expected=ArgumentsAreDifferent.class)
81111
public void testGoodTrack1() {
82112
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
@@ -283,6 +313,74 @@ public void testBadGetVariableDouble() {
283313

284314
//======== Notification listeners ========//
285315

316+
@Test
317+
public void testNewGoodAddNotificationCenterListener() {
318+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
319+
320+
ActivateNotificationListener listener = new ActivateNotificationListener() {
321+
@Override
322+
public void onActivate(Experiment experiment,String userId, Map<String, String> attributes, Variation variation, LogEvent event) {
323+
324+
}
325+
};
326+
327+
int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, listener);
328+
verify(optimizely.notificationCenter).addNotification(NotificationCenter.NotificationType.Activate, listener);
329+
}
330+
331+
@Test
332+
public void testBadAddNotificationCenterListener() {
333+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
334+
ActivateNotificationListener listener = new ActivateNotificationListener() {
335+
@Override
336+
public void onActivate(Experiment experiment, String userId, Map<String, String> attributes, Variation variation, LogEvent event) {
337+
338+
}
339+
};
340+
optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, listener);
341+
verify(optimizely.notificationCenter).addNotification(NotificationCenter.NotificationType.Activate, listener);
342+
}
343+
344+
@Test
345+
public void testGoodRemoveNotificationCenterListener() {
346+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
347+
TrackNotificationListener listener = new TrackNotificationListener() {
348+
@Override
349+
public void onTrack( String eventKey, String userId, Map<String, String> attributes, Map<String, ?> eventTags, LogEvent event) {
350+
351+
}
352+
};
353+
int note = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Track, listener);
354+
optimizelyClient.getNotificationCenter().removeNotification(note);
355+
verify(optimizely.notificationCenter).removeNotification(note);
356+
}
357+
358+
@Test
359+
public void testBadRemoveNotificationCenterListener() {
360+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
361+
362+
NotificationCenter notificationCenter = optimizelyClient.getNotificationCenter() != null ?
363+
optimizelyClient.getNotificationCenter() : new NotificationCenter();
364+
notificationCenter.removeNotification(1);
365+
verify(logger).warn("Optimizely is not initialized, could not get the notification listener");
366+
}
367+
368+
@Test
369+
public void testGoodClearNotificationCenterListeners() {
370+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
371+
optimizelyClient.getNotificationCenter().clearAllNotifications();
372+
verify(optimizely.notificationCenter).clearAllNotifications();
373+
}
374+
375+
@Test
376+
public void testBadClearNotificationCenterListeners() {
377+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
378+
NotificationCenter notificationCenter = optimizelyClient.getNotificationCenter() != null ?
379+
optimizelyClient.getNotificationCenter() : new NotificationCenter();
380+
notificationCenter.clearAllNotifications();
381+
verify(logger).warn("Optimizely is not initialized, could not get the notification listener");
382+
}
383+
286384
@Test
287385
public void testGoodAddNotificationListener() {
288386
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
@@ -364,4 +462,5 @@ public void testBadClearNotificationListeners() {
364462
optimizelyClient.clearNotificationListeners();
365463
verify(logger).warn("Optimizely is not initialized, could not clear notification listeners");
366464
}
465+
367466
}

0 commit comments

Comments
 (0)