40
40
import com .optimizely .ab .config .DatafileProjectConfig ;
41
41
import com .optimizely .ab .config .ProjectConfig ;
42
42
import com .optimizely .ab .config .Variation ;
43
+ import com .optimizely .ab .config .parser .ConfigParseException ;
43
44
import com .optimizely .ab .event .EventHandler ;
44
45
import com .optimizely .ab .event .EventProcessor ;
46
+ import com .optimizely .ab .optimizelyconfig .OptimizelyConfig ;
45
47
46
48
import org .junit .Before ;
47
49
import org .junit .Test ;
60
62
import static junit .framework .Assert .assertTrue ;
61
63
import static junit .framework .Assert .fail ;
62
64
import static org .mockito .Matchers .any ;
65
+ import static org .mockito .Matchers .anyBoolean ;
66
+ import static org .mockito .Matchers .anyInt ;
63
67
import static org .mockito .Matchers .eq ;
64
68
import static org .mockito .Mockito .doAnswer ;
65
69
import static org .mockito .Mockito .mock ;
70
+ import static org .mockito .Mockito .spy ;
66
71
import static org .mockito .Mockito .verify ;
67
72
import static org .mockito .Mockito .when ;
68
73
@@ -78,6 +83,7 @@ public class OptimizelyManagerTest {
78
83
private Logger logger ;
79
84
private OptimizelyManager optimizelyManager ;
80
85
private DefaultDatafileHandler defaultDatafileHandler ;
86
+ private String defaultDatafile ;
81
87
82
88
private String minDatafile = "{\n " +
83
89
"experiments: [ ],\n " +
@@ -106,8 +112,8 @@ public void setup() throws Exception {
106
112
.withEventHandler (eventHandler )
107
113
.withEventProcessor (eventProcessor )
108
114
.build (InstrumentationRegistry .getTargetContext ());
109
- String datafile = optimizelyManager .getDatafile (InstrumentationRegistry .getTargetContext (), R .raw .datafile );
110
- ProjectConfig config = new DatafileProjectConfig .Builder ().withDatafile (datafile ).build ();
115
+ defaultDatafile = optimizelyManager .getDatafile (InstrumentationRegistry .getTargetContext (), R .raw .datafile );
116
+ ProjectConfig config = new DatafileProjectConfig .Builder ().withDatafile (defaultDatafile ).build ();
111
117
112
118
when (defaultDatafileHandler .getConfig ()).thenReturn (config );
113
119
}
@@ -213,7 +219,8 @@ public void getDatafile() {
213
219
assertNotNull (datafile );
214
220
assertNotNull (optimizelyManager .getDatafileHandler ());
215
221
}
216
- @ Test
222
+
223
+ @ Test
217
224
public void initializeAsyncWithEnvironment () {
218
225
Logger logger = mock (Logger .class );
219
226
DatafileHandler datafileHandler = mock (DefaultDatafileHandler .class );
@@ -496,4 +503,225 @@ public void injectOptimizelyDoesNotDuplicateCallback() {
496
503
verify (logger ).info ("Sending Optimizely instance to listener" );
497
504
verify (startListener ).onStart (any (OptimizelyClient .class ));
498
505
}
506
+
507
+ // Init Sync Flows
508
+
509
+ @ Test
510
+ public void initializeSyncWithUpdateOnNewDatafileDisabled () {
511
+ boolean downloadToCache = true ;
512
+ boolean updateConfigOnNewDatafiel = false ;
513
+ int pollingInterval = 0 ; // disable polling
514
+
515
+ DefaultDatafileHandler datafileHandler = spy (new DefaultDatafileHandler ());
516
+ Logger logger = mock (Logger .class );
517
+ Context context = InstrumentationRegistry .getTargetContext ();
518
+
519
+ OptimizelyManager manager = new OptimizelyManager (testProjectId , testSdkKey , null , logger , pollingInterval , datafileHandler , null , 0 ,
520
+ null , null , null , null );
521
+
522
+ doAnswer (
523
+ new Answer <Object >() {
524
+ public Object answer (InvocationOnMock invocation ) {
525
+ String newDatafile = manager .getDatafile (context , R .raw .datafile_api );
526
+ datafileHandler .saveDatafile (context , manager .getDatafileConfig (), newDatafile );
527
+ return null ;
528
+ }
529
+ }).when (manager .getDatafileHandler ()).downloadDatafile (any (Context .class ), any (DatafileConfig .class ), any (DatafileLoadedListener .class ));
530
+
531
+ OptimizelyClient client = manager .initialize (context , defaultDatafile , downloadToCache , updateConfigOnNewDatafiel );
532
+
533
+ try {
534
+ executor .awaitTermination (1 , TimeUnit .SECONDS );
535
+ } catch (InterruptedException e ) {
536
+ //
537
+ }
538
+
539
+ assertEquals (client .getOptimizelyConfig ().getRevision (), "7" );
540
+ }
541
+
542
+ @ Test
543
+ public void initializeSyncWithUpdateOnNewDatafileEnabled () {
544
+ boolean downloadToCache = true ;
545
+ boolean updateConfigOnNewDatafiel = true ;
546
+ int pollingInterval = 0 ; // disable polling
547
+
548
+ DefaultDatafileHandler datafileHandler = spy (new DefaultDatafileHandler ());
549
+ Logger logger = mock (Logger .class );
550
+ Context context = InstrumentationRegistry .getTargetContext ();
551
+
552
+ OptimizelyManager manager = new OptimizelyManager (testProjectId , testSdkKey , null , logger , pollingInterval , datafileHandler , null , 0 ,
553
+ null , null , null , null );
554
+
555
+ doAnswer (
556
+ new Answer <Object >() {
557
+ public Object answer (InvocationOnMock invocation ) {
558
+ String newDatafile = manager .getDatafile (context , R .raw .datafile_api );
559
+ datafileHandler .saveDatafile (context , manager .getDatafileConfig (), newDatafile );
560
+ return null ;
561
+ }
562
+ }).when (manager .getDatafileHandler ()).downloadDatafile (any (Context .class ), any (DatafileConfig .class ), any (DatafileLoadedListener .class ));
563
+
564
+ OptimizelyClient client = manager .initialize (context , defaultDatafile , downloadToCache , updateConfigOnNewDatafiel );
565
+
566
+ try {
567
+ executor .awaitTermination (1 , TimeUnit .SECONDS );
568
+ } catch (InterruptedException e ) {
569
+ //
570
+ }
571
+
572
+ assertEquals (client .getOptimizelyConfig ().getRevision (), "241" );
573
+ }
574
+
575
+ @ Test
576
+ public void initializeSyncWithDownloadToCacheDisabled () {
577
+ boolean downloadToCache = false ;
578
+ boolean updateConfigOnNewDatafiel = true ;
579
+ int pollingInterval = 0 ; // disable polling
580
+
581
+ DefaultDatafileHandler datafileHandler = spy (new DefaultDatafileHandler ());
582
+ Logger logger = mock (Logger .class );
583
+ Context context = InstrumentationRegistry .getTargetContext ();
584
+
585
+ OptimizelyManager manager = new OptimizelyManager (testProjectId , testSdkKey , null , logger , pollingInterval , datafileHandler , null , 0 ,
586
+ null , null , null , null );
587
+
588
+ doAnswer (
589
+ new Answer <Object >() {
590
+ public Object answer (InvocationOnMock invocation ) {
591
+ String newDatafile = manager .getDatafile (context , R .raw .datafile_api );
592
+ datafileHandler .saveDatafile (context , manager .getDatafileConfig (), newDatafile );
593
+ return null ;
594
+ }
595
+ }).when (manager .getDatafileHandler ()).downloadDatafile (any (Context .class ), any (DatafileConfig .class ), any (DatafileLoadedListener .class ));
596
+
597
+ OptimizelyClient client = manager .initialize (context , defaultDatafile , downloadToCache , updateConfigOnNewDatafiel );
598
+
599
+ try {
600
+ executor .awaitTermination (1 , TimeUnit .SECONDS );
601
+ } catch (InterruptedException e ) {
602
+ //
603
+ }
604
+
605
+ assertEquals (client .getOptimizelyConfig ().getRevision (), "7" );
606
+ }
607
+
608
+ @ Test
609
+ public void initializeSyncWithUpdateOnNewDatafileDisabledWithPeriodicPollingEnabled () {
610
+ boolean downloadToCache = true ;
611
+ boolean updateConfigOnNewDatafiel = false ;
612
+ int pollingInterval = 30 ; // enable polling
613
+
614
+ DefaultDatafileHandler datafileHandler = spy (new DefaultDatafileHandler ());
615
+ Logger logger = mock (Logger .class );
616
+ Context context = InstrumentationRegistry .getTargetContext ();
617
+
618
+ OptimizelyManager manager = new OptimizelyManager (testProjectId , testSdkKey , null , logger , pollingInterval , datafileHandler , null , 0 ,
619
+ null , null , null , null );
620
+
621
+ doAnswer (
622
+ new Answer <Object >() {
623
+ public Object answer (InvocationOnMock invocation ) {
624
+ String newDatafile = manager .getDatafile (context , R .raw .datafile_api );
625
+ datafileHandler .saveDatafile (context , manager .getDatafileConfig (), newDatafile );
626
+ return null ;
627
+ }
628
+ }).when (manager .getDatafileHandler ()).downloadDatafile (any (Context .class ), any (DatafileConfig .class ), any (DatafileLoadedListener .class ));
629
+
630
+ OptimizelyClient client = manager .initialize (context , defaultDatafile , downloadToCache , updateConfigOnNewDatafiel );
631
+
632
+ try {
633
+ executor .awaitTermination (1 , TimeUnit .SECONDS );
634
+ } catch (InterruptedException e ) {
635
+ //
636
+ }
637
+
638
+ // when periodic polling enabled, project config always updated on cache datafile update (regardless of "updateConfigOnNewDatafile" setting)
639
+ assertEquals (client .getOptimizelyConfig ().getRevision (), "241" );
640
+ }
641
+
642
+ @ Test
643
+ public void initializeSyncWithUpdateOnNewDatafileEnabledWithPeriodicPollingEnabled () {
644
+ boolean downloadToCache = true ;
645
+ boolean updateConfigOnNewDatafiel = true ;
646
+ int pollingInterval = 30 ; // enable polling
647
+
648
+ DefaultDatafileHandler datafileHandler = spy (new DefaultDatafileHandler ());
649
+ Logger logger = mock (Logger .class );
650
+ Context context = InstrumentationRegistry .getTargetContext ();
651
+
652
+ OptimizelyManager manager = new OptimizelyManager (testProjectId , testSdkKey , null , logger , pollingInterval , datafileHandler , null , 0 ,
653
+ null , null , null , null );
654
+
655
+ doAnswer (
656
+ new Answer <Object >() {
657
+ public Object answer (InvocationOnMock invocation ) {
658
+ String newDatafile = manager .getDatafile (context , R .raw .datafile_api );
659
+ datafileHandler .saveDatafile (context , manager .getDatafileConfig (), newDatafile );
660
+ return null ;
661
+ }
662
+ }).when (manager .getDatafileHandler ()).downloadDatafile (any (Context .class ), any (DatafileConfig .class ), any (DatafileLoadedListener .class ));
663
+
664
+ OptimizelyClient client = manager .initialize (context , defaultDatafile , downloadToCache , updateConfigOnNewDatafiel );
665
+
666
+ try {
667
+ executor .awaitTermination (1 , TimeUnit .SECONDS );
668
+ } catch (InterruptedException e ) {
669
+ //
670
+ }
671
+
672
+ // when periodic polling enabled, project config always updated on cache datafile update (regardless of "updateConfigOnNewDatafile" setting)
673
+ assertEquals (client .getOptimizelyConfig ().getRevision (), "241" );
674
+ }
675
+
676
+ @ Test
677
+ public void initializeSyncWithResourceDatafileNoCache () {
678
+ boolean downloadToCache = true ;
679
+ boolean updateConfigOnNewDatafiel = true ;
680
+ int pollingInterval = 30 ; // enable polling
681
+
682
+ DefaultDatafileHandler datafileHandler = spy (new DefaultDatafileHandler ());
683
+ Logger logger = mock (Logger .class );
684
+ Context context = InstrumentationRegistry .getTargetContext ();
685
+
686
+ OptimizelyManager manager = spy (new OptimizelyManager (testProjectId , testSdkKey , null , logger , pollingInterval , datafileHandler , null , 0 ,
687
+ null , null , null , null ));
688
+
689
+ datafileHandler .removeSavedDatafile (context , manager .getDatafileConfig ());
690
+ OptimizelyClient client = manager .initialize (context , R .raw .datafile , downloadToCache , updateConfigOnNewDatafiel );
691
+
692
+ verify (manager ).initialize (eq (context ), eq (defaultDatafile ), eq (downloadToCache ), eq (updateConfigOnNewDatafiel ));
693
+ }
694
+
695
+ @ Test
696
+ public void initializeSyncWithResourceDatafileNoCacheWithDefaultParams () {
697
+ boolean downloadToCache = true ;
698
+ boolean updateConfigOnNewDatafiel = true ;
699
+ int pollingInterval = 30 ; // enable polling
700
+
701
+ DefaultDatafileHandler datafileHandler = spy (new DefaultDatafileHandler ());
702
+ Logger logger = mock (Logger .class );
703
+ Context context = InstrumentationRegistry .getTargetContext ();
704
+
705
+ OptimizelyManager manager = spy (new OptimizelyManager (testProjectId , testSdkKey , null , logger , pollingInterval , datafileHandler , null , 0 ,
706
+ null , null , null , null ));
707
+
708
+ datafileHandler .removeSavedDatafile (context , manager .getDatafileConfig ());
709
+ OptimizelyClient client = manager .initialize (context , R .raw .datafile );
710
+
711
+ verify (manager ).initialize (eq (context ), eq (defaultDatafile ), eq (true ), eq (false ));
712
+ }
713
+
714
+
715
+ // Utils
716
+
717
+ void mockProjectConfig (DefaultDatafileHandler datafileHandler , String datafile ) {
718
+ ProjectConfig config = null ;
719
+ try {
720
+ config = new DatafileProjectConfig .Builder ().withDatafile (datafile ).build ();
721
+ when (datafileHandler .getConfig ()).thenReturn (config );
722
+ } catch (ConfigParseException e ) {
723
+ e .printStackTrace ();
724
+ }
725
+ }
726
+
499
727
}
0 commit comments