4040import com .optimizely .ab .config .DatafileProjectConfig ;
4141import com .optimizely .ab .config .ProjectConfig ;
4242import com .optimizely .ab .config .Variation ;
43+ import com .optimizely .ab .config .parser .ConfigParseException ;
4344import com .optimizely .ab .event .EventHandler ;
4445import com .optimizely .ab .event .EventProcessor ;
46+ import com .optimizely .ab .optimizelyconfig .OptimizelyConfig ;
4547
4648import org .junit .Before ;
4749import org .junit .Test ;
6062import static junit .framework .Assert .assertTrue ;
6163import static junit .framework .Assert .fail ;
6264import static org .mockito .Matchers .any ;
65+ import static org .mockito .Matchers .anyBoolean ;
66+ import static org .mockito .Matchers .anyInt ;
6367import static org .mockito .Matchers .eq ;
6468import static org .mockito .Mockito .doAnswer ;
6569import static org .mockito .Mockito .mock ;
70+ import static org .mockito .Mockito .spy ;
6671import static org .mockito .Mockito .verify ;
6772import static org .mockito .Mockito .when ;
6873
@@ -78,6 +83,7 @@ public class OptimizelyManagerTest {
7883 private Logger logger ;
7984 private OptimizelyManager optimizelyManager ;
8085 private DefaultDatafileHandler defaultDatafileHandler ;
86+ private String defaultDatafile ;
8187
8288 private String minDatafile = "{\n " +
8389 "experiments: [ ],\n " +
@@ -106,8 +112,8 @@ public void setup() throws Exception {
106112 .withEventHandler (eventHandler )
107113 .withEventProcessor (eventProcessor )
108114 .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 ();
111117
112118 when (defaultDatafileHandler .getConfig ()).thenReturn (config );
113119 }
@@ -213,7 +219,8 @@ public void getDatafile() {
213219 assertNotNull (datafile );
214220 assertNotNull (optimizelyManager .getDatafileHandler ());
215221 }
216- @ Test
222+
223+ @ Test
217224 public void initializeAsyncWithEnvironment () {
218225 Logger logger = mock (Logger .class );
219226 DatafileHandler datafileHandler = mock (DefaultDatafileHandler .class );
@@ -496,4 +503,225 @@ public void injectOptimizelyDoesNotDuplicateCallback() {
496503 verify (logger ).info ("Sending Optimizely instance to listener" );
497504 verify (startListener ).onStart (any (OptimizelyClient .class ));
498505 }
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+
499727}
0 commit comments