1919import com .optimizely .ab .Optimizely ;
2020import com .optimizely .ab .config .Experiment ;
2121import 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 ;
2225import com .optimizely .ab .notification .NotificationListener ;
26+ import com .optimizely .ab .notification .TrackNotificationListener ;
2327
28+ import org .junit .Before ;
2429import org .junit .Test ;
2530import org .junit .runner .RunWith ;
2631import org .mockito .Mock ;
2732import org .mockito .runners .MockitoJUnitRunner ;
2833import org .mockito .exceptions .verification .junit .ArgumentsAreDifferent ;
2934import org .slf4j .Logger ;
3035
36+ import java .lang .reflect .Field ;
37+ import java .lang .reflect .Modifier ;
3138import java .util .Collections ;
3239import java .util .HashMap ;
3340import java .util .Map ;
3441
3542import static junit .framework .Assert .assertTrue ;
3643import static junit .framework .Assert .assertFalse ;
3744import 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