19
19
import com .optimizely .ab .Optimizely ;
20
20
import com .optimizely .ab .config .Experiment ;
21
21
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 ;
22
25
import com .optimizely .ab .notification .NotificationListener ;
26
+ import com .optimizely .ab .notification .TrackNotificationListener ;
23
27
28
+ import org .junit .Before ;
24
29
import org .junit .Test ;
25
30
import org .junit .runner .RunWith ;
26
31
import org .mockito .Mock ;
27
32
import org .mockito .runners .MockitoJUnitRunner ;
28
33
import org .mockito .exceptions .verification .junit .ArgumentsAreDifferent ;
29
34
import org .slf4j .Logger ;
30
35
36
+ import java .lang .reflect .Field ;
37
+ import java .lang .reflect .Modifier ;
31
38
import java .util .Collections ;
32
39
import java .util .HashMap ;
33
40
import java .util .Map ;
34
41
35
42
import static junit .framework .Assert .assertTrue ;
36
43
import static junit .framework .Assert .assertFalse ;
37
44
import static org .mockito .Mockito .verify ;
45
+ import static org .mockito .Mockito .when ;
38
46
39
47
/**
40
48
* Tests for {@link OptimizelyClient}
@@ -44,6 +52,29 @@ public class OptimizelyClientTest {
44
52
45
53
@ Mock Logger logger ;
46
54
@ 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
+ }
47
78
48
79
@ Test (expected =ArgumentsAreDifferent .class )
49
80
public void testGoodActivation1 () {
@@ -76,7 +107,6 @@ public void testBadActivation2() {
76
107
"for user {} with attributes" , "1" , "1" );
77
108
}
78
109
79
-
80
110
@ Test (expected =ArgumentsAreDifferent .class )
81
111
public void testGoodTrack1 () {
82
112
OptimizelyClient optimizelyClient = new OptimizelyClient (optimizely , logger );
@@ -283,6 +313,74 @@ public void testBadGetVariableDouble() {
283
313
284
314
//======== Notification listeners ========//
285
315
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
+
286
384
@ Test
287
385
public void testGoodAddNotificationListener () {
288
386
OptimizelyClient optimizelyClient = new OptimizelyClient (optimizely , logger );
@@ -364,4 +462,5 @@ public void testBadClearNotificationListeners() {
364
462
optimizelyClient .clearNotificationListeners ();
365
463
verify (logger ).warn ("Optimizely is not initialized, could not clear notification listeners" );
366
464
}
465
+
367
466
}
0 commit comments