@@ -651,17 +651,19 @@ public void TestGetVariationForFeatureRolloutWhenNoRuleInRollouts()
651
651
var rollout = projectConfig . GetRolloutFromId ( featureFlag . RolloutId ) ;
652
652
653
653
Assert . AreEqual ( rollout . Experiments . Count , 0 ) ;
654
-
654
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
655
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , "userId1" , null , ErrorHandlerMock . Object , LoggerMock . Object ) ;
655
656
var decisionService = new DecisionService ( new Bucketer ( new NoOpLogger ( ) ) , new NoOpErrorHandler ( ) , null , new NoOpLogger ( ) ) ;
656
657
657
- var variation = decisionService . GetVariationForFeatureRollout ( featureFlag , "userId1" , null , projectConfig ) ;
658
+ var variation = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , projectConfig ) ;
658
659
659
660
Assert . IsNull ( variation . ResultObject ) ;
660
661
}
661
662
662
663
[ Test ]
663
664
public void TestGetVariationForFeatureRolloutWhenRolloutIsNotInDataFile ( )
664
665
{
666
+
665
667
var featureFlag = ProjectConfig . GetFeatureFlagFromKey ( "boolean_feature" ) ;
666
668
var invalidRolloutFeature = new FeatureFlag
667
669
{
@@ -673,8 +675,9 @@ public void TestGetVariationForFeatureRolloutWhenRolloutIsNotInDataFile()
673
675
} ;
674
676
675
677
DecisionServiceMock . Setup ( ds => ds . GetVariationForFeatureExperiment ( It . IsAny < FeatureFlag > ( ) , It . IsAny < OptimizelyUserContext > ( ) , It . IsAny < UserAttributes > ( ) , ProjectConfig , new OptimizelyDecideOption [ ] { } ) ) . Returns < Variation > ( null ) ;
676
-
677
- var actualDecision = DecisionServiceMock . Object . GetVariationForFeatureRollout ( featureFlag , "user1" , new UserAttributes ( ) , ProjectConfig ) ;
678
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
679
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , "user1" , new UserAttributes ( ) , ErrorHandlerMock . Object , LoggerMock . Object ) ;
680
+ var actualDecision = DecisionServiceMock . Object . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , ProjectConfig ) ;
678
681
Assert . IsNull ( actualDecision . ResultObject ) ;
679
682
680
683
LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , "The feature flag \" boolean_feature\" is not used in a rollout." ) ) ;
@@ -699,7 +702,10 @@ public void TestGetVariationForFeatureRolloutWhenUserIsBucketedInTheTargetingRul
699
702
It . IsAny < string > ( ) ) ) . Returns ( variation ) ;
700
703
var decisionService = new DecisionService ( BucketerMock . Object , ErrorHandlerMock . Object , null , LoggerMock . Object ) ;
701
704
702
- var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , "user_1" , userAttributes , ProjectConfig ) ;
705
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
706
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , "user_1" , userAttributes , ErrorHandlerMock . Object , LoggerMock . Object ) ;
707
+
708
+ var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , ProjectConfig ) ;
703
709
Assert . IsTrue ( TestData . CompareObjects ( expectedDecision , actualDecision . ResultObject ) ) ;
704
710
}
705
711
@@ -724,7 +730,9 @@ public void TestGetVariationForFeatureRolloutWhenUserIsNotBucketedInTheTargeting
724
730
BucketerMock . Setup ( bm => bm . Bucket ( It . IsAny < ProjectConfig > ( ) , everyoneElseRule , It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( variation ) ;
725
731
var decisionService = new DecisionService ( BucketerMock . Object , ErrorHandlerMock . Object , null , LoggerMock . Object ) ;
726
732
727
- var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , "user_1" , userAttributes , ProjectConfig ) ;
733
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
734
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , "user_1" , userAttributes , ErrorHandlerMock . Object , LoggerMock . Object ) ;
735
+ var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , ProjectConfig ) ;
728
736
Assert . IsTrue ( TestData . CompareObjects ( expectedDecision , actualDecision . ResultObject ) ) ;
729
737
}
730
738
@@ -743,7 +751,9 @@ public void TestGetVariationForFeatureRolloutWhenUserIsNeitherBucketedInTheTarge
743
751
BucketerMock . Setup ( bm => bm . Bucket ( It . IsAny < ProjectConfig > ( ) , It . IsAny < Experiment > ( ) , It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( Result < Variation > . NullResult ( null ) ) ;
744
752
var decisionService = new DecisionService ( BucketerMock . Object , ErrorHandlerMock . Object , null , LoggerMock . Object ) ;
745
753
746
- var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , "user_1" , userAttributes , ProjectConfig ) ;
754
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
755
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , "user_1" , userAttributes , ErrorHandlerMock . Object , LoggerMock . Object ) ;
756
+ var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , ProjectConfig ) ;
747
757
Assert . IsNull ( actualDecision . ResultObject ) ;
748
758
}
749
759
@@ -766,7 +776,9 @@ public void TestGetVariationForFeatureRolloutWhenUserDoesNotQualifyForAnyTargeti
766
776
var decisionService = new DecisionService ( BucketerMock . Object , ErrorHandlerMock . Object , null , LoggerMock . Object ) ;
767
777
768
778
// Provide null attributes so that user does not qualify for audience.
769
- var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , "user_1" , null , ProjectConfig ) ;
779
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
780
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , "user_1" , null , ErrorHandlerMock . Object , LoggerMock . Object ) ;
781
+ var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , ProjectConfig ) ;
770
782
771
783
Assert . IsTrue ( TestData . CompareObjects ( expectedDecision , actualDecision . ResultObject ) ) ;
772
784
@@ -792,40 +804,45 @@ public void TestGetVariationForFeatureRolloutAudienceAndTrafficeAllocationCheck(
792
804
var decisionService = new DecisionService ( mockBucketer . Object , ErrorHandlerMock . Object , null , LoggerMock . Object ) ;
793
805
794
806
// Calling with audience iPhone users in San Francisco.
795
- var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , GenericUserId , new UserAttributes
807
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
808
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , GenericUserId , new UserAttributes
796
809
{
797
810
{ "device_type" , "iPhone" } ,
798
811
{ "location" , "San Francisco" }
799
- } , ProjectConfig ) ;
812
+ } , ErrorHandlerMock . Object , LoggerMock . Object ) ;
813
+ var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , ProjectConfig ) ;
800
814
801
815
// Returned variation id should be '177773' because of audience 'iPhone users in San Francisco'.
802
816
var expectedDecision = new FeatureDecision ( expWithAudienceiPhoneUsers , varWithAudienceiPhoneUsers , FeatureDecision . DECISION_SOURCE_ROLLOUT ) ;
803
817
Assert . IsTrue ( TestData . CompareObjects ( expectedDecision , actualDecision . ResultObject ) ) ;
804
818
805
819
// Calling with audience Chrome users.
806
- actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , GenericUserId , new UserAttributes
820
+ var optimizelyUserContext2 = new OptimizelyUserContext ( optlyObject , GenericUserId , new UserAttributes
807
821
{
808
822
{ "browser_type" , "chrome" }
809
- } , ProjectConfig ) ;
823
+ } , ErrorHandlerMock . Object , LoggerMock . Object ) ;
824
+ actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext2 , ProjectConfig ) ;
810
825
811
826
// Returned variation id should be '177771' because of audience 'Chrome users'.
812
827
expectedDecision = new FeatureDecision ( expWithAudienceChromeUsers , varWithAudienceChromeUsers , FeatureDecision . DECISION_SOURCE_ROLLOUT ) ;
813
828
Assert . IsTrue ( TestData . CompareObjects ( expectedDecision , actualDecision . ResultObject ) ) ;
814
829
815
830
// Calling with no audience.
816
831
mockBucketer . Setup ( bm => bm . GenerateBucketValue ( It . IsAny < string > ( ) ) ) . Returns ( 8000 ) ;
817
- actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , GenericUserId , new UserAttributes ( ) , ProjectConfig ) ;
832
+ var optimizelyUserContext3 = new OptimizelyUserContext ( optlyObject , GenericUserId , new UserAttributes ( ) , ErrorHandlerMock . Object , LoggerMock . Object ) ;
833
+ actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext3 , ProjectConfig ) ;
818
834
819
835
// Returned variation id should be of everyone else rule because of no audience.
820
836
expectedDecision = new FeatureDecision ( expWithNoAudience , varWithNoAudience , FeatureDecision . DECISION_SOURCE_ROLLOUT ) ;
821
837
Assert . IsTrue ( TestData . CompareObjects ( expectedDecision , actualDecision . ResultObject ) ) ;
822
838
823
839
// Calling with audience 'Chrome users' and traffice allocation '9500'.
824
840
mockBucketer . Setup ( bm => bm . GenerateBucketValue ( It . IsAny < string > ( ) ) ) . Returns ( 9500 ) ;
825
- actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , GenericUserId , new UserAttributes
841
+ var optimizelyUserContext4 = new OptimizelyUserContext ( optlyObject , GenericUserId , new UserAttributes
826
842
{
827
843
{ "browser_type" , "chrome" }
828
- } , ProjectConfig ) ;
844
+ } , ErrorHandlerMock . Object , LoggerMock . Object ) ;
845
+ actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext4 , ProjectConfig ) ;
829
846
830
847
// Returned decision entity should be null because bucket value exceeds traffic allocation of everyone else rule.
831
848
Assert . Null ( actualDecision . ResultObject ) ;
@@ -845,20 +862,23 @@ public void TestGetVariationForFeatureRolloutCheckAudienceInEveryoneElseRule()
845
862
BucketerMock . Setup ( bm => bm . Bucket ( It . IsAny < ProjectConfig > ( ) , everyoneElseRule , It . IsAny < string > ( ) , GenericUserId ) ) . Returns ( Result < Variation > . NullResult ( DecisionReasons ) ) ;
846
863
847
864
var decisionService = new DecisionService ( BucketerMock . Object , ErrorHandlerMock . Object , null , LoggerMock . Object ) ;
865
+ var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
848
866
849
867
// Returned variation id should be of everyone else rule as it passes audience Id checking.
850
- var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , WhitelistedUserId , null , ProjectConfig ) ;
868
+ var optimizelyUserContext = new OptimizelyUserContext ( optlyObject , WhitelistedUserId , null , ErrorHandlerMock . Object , LoggerMock . Object ) ;
869
+ var actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext , ProjectConfig ) ;
851
870
Assert . True ( TestData . CompareObjects ( expectedDecision , actualDecision . ResultObject ) ) ;
852
871
853
872
// Returned variation id should be null.
854
- actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , GenericUserId , null , ProjectConfig ) ;
873
+ var optimizelyUserContext2 = new OptimizelyUserContext ( optlyObject , GenericUserId , null , ErrorHandlerMock . Object , LoggerMock . Object ) ;
874
+ actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext2 , ProjectConfig ) ;
855
875
Assert . Null ( actualDecision . ResultObject ) ;
856
876
857
877
// Returned variation id should be null as it fails audience Id checking.
858
878
everyoneElseRule . AudienceIds = new string [ ] { ProjectConfig . Audiences [ 0 ] . Id } ;
859
879
860
880
BucketerMock . Setup ( bm => bm . Bucket ( It . IsAny < ProjectConfig > ( ) , It . IsAny < Experiment > ( ) , It . IsAny < string > ( ) , GenericUserId ) ) . Returns ( Result < Variation > . NullResult ( DecisionReasons ) ) ;
861
- actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , GenericUserId , null , ProjectConfig ) ?? Result < FeatureDecision > . NullResult ( DecisionReasons ) ;
881
+ actualDecision = decisionService . GetVariationForFeatureRollout ( featureFlag , optimizelyUserContext2 , ProjectConfig ) ?? Result < FeatureDecision > . NullResult ( DecisionReasons ) ;
862
882
Assert . Null ( actualDecision . ResultObject ) ;
863
883
864
884
LoggerMock . Verify ( l => l . Log ( LogLevel . DEBUG , "User \" testUser1\" does not meet the conditions for targeting rule \" 1\" ." ) , Times . Once ) ;
@@ -904,8 +924,8 @@ public void TestGetVariationForFeatureWhenTheUserIsNotBucketedIntoFeatureExperim
904
924
905
925
DecisionServiceMock . Setup ( ds => ds . GetVariationForFeatureExperiment ( It . IsAny < FeatureFlag > ( ) , It . IsAny < OptimizelyUserContext > ( ) ,
906
926
It . IsAny < UserAttributes > ( ) , ProjectConfig , It . IsAny < OptimizelyDecideOption [ ] > ( ) ) ) . Returns ( Result < FeatureDecision > . NullResult ( null ) ) ;
907
- DecisionServiceMock . Setup ( ds => ds . GetVariationForFeatureRollout ( It . IsAny < FeatureFlag > ( ) , It . IsAny < string > ( ) ,
908
- It . IsAny < UserAttributes > ( ) , ProjectConfig ) ) . Returns ( expectedDecision ) ;
927
+ DecisionServiceMock . Setup ( ds => ds . GetVariationForFeatureRollout ( It . IsAny < FeatureFlag > ( ) , It . IsAny < OptimizelyUserContext > ( ) ,
928
+ ProjectConfig ) ) . Returns ( expectedDecision ) ;
909
929
910
930
var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
911
931
OptimizelyUserContextMock = new Mock < OptimizelyUserContext > ( optlyObject , WhitelistedUserId , new UserAttributes ( ) , ErrorHandlerMock . Object , LoggerMock . Object ) ;
@@ -925,7 +945,7 @@ public void TestGetVariationForFeatureWhenTheUserIsNeitherBucketedIntoFeatureExp
925
945
var featureFlag = ProjectConfig . GetFeatureFlagFromKey ( "string_single_variable_feature" ) ;
926
946
927
947
DecisionServiceMock . Setup ( ds => ds . GetVariationForFeatureExperiment ( It . IsAny < FeatureFlag > ( ) , It . IsAny < OptimizelyUserContext > ( ) , It . IsAny < UserAttributes > ( ) , ProjectConfig , new OptimizelyDecideOption [ ] { } ) ) . Returns ( Result < FeatureDecision > . NullResult ( null ) ) ;
928
- DecisionServiceMock . Setup ( ds => ds . GetVariationForFeatureRollout ( It . IsAny < FeatureFlag > ( ) , It . IsAny < string > ( ) , It . IsAny < UserAttributes > ( ) , ProjectConfig ) ) . Returns ( Result < FeatureDecision > . NullResult ( null ) ) ;
948
+ DecisionServiceMock . Setup ( ds => ds . GetVariationForFeatureRollout ( It . IsAny < FeatureFlag > ( ) , It . IsAny < OptimizelyUserContext > ( ) , ProjectConfig ) ) . Returns ( Result < FeatureDecision > . NullResult ( null ) ) ;
929
949
930
950
var optlyObject = new Optimizely ( TestData . Datafile , new ValidEventDispatcher ( ) , LoggerMock . Object ) ;
931
951
OptimizelyUserContextMock = new Mock < OptimizelyUserContext > ( optlyObject , WhitelistedUserId , new UserAttributes ( ) , ErrorHandlerMock . Object , LoggerMock . Object ) ;
@@ -967,7 +987,7 @@ public void TestGetVariationForFeatureWhenTheUserIsBuckedtedInBothExperimentAndR
967
987
var expectedRolloutDecision = new FeatureDecision ( rolloutExperiment , rolloutVariation . ResultObject , FeatureDecision . DECISION_SOURCE_ROLLOUT ) ;
968
988
969
989
BucketerMock . Setup ( bm => bm . Bucket ( ProjectConfig , rolloutExperiment , It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( rolloutVariation ) ;
970
- var actualRolloutDecision = DecisionServiceMock . Object . GetVariationForFeatureRollout ( featureFlag , "user1" , userAttributes , ProjectConfig ) ;
990
+ var actualRolloutDecision = DecisionServiceMock . Object . GetVariationForFeatureRollout ( featureFlag , OptimizelyUserContextMock . Object , ProjectConfig ) ;
971
991
972
992
// The user is bucketed into feature rollout's variation.
973
993
0 commit comments