10000 move validate forced decision to decision service by msohailhussain · Pull Request #292 · optimizely/csharp-sdk · GitHub
[go: up one dir, main page]

Skip to content

move validate forced decision to decision service #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions OptimizelySDK.Tests/DecisionServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ public void SetUp()
Config = DatafileProjectConfig.Create(TestData.Datafile, LoggerMock.Object, ErrorHandlerMock.Object);
}

[Test]
public void TestFindValidatedForcedDecisionReturnsCorrectDecisionWithNullVariation()
{
DecisionService decisionService = new DecisionService(BucketerMock.Object, ErrorHandlerMock.Object, null, LoggerMock.Object);

var optlyObject = new Optimizely(TestData.Datafile, new ValidEventDispatcher(), LoggerMock.Object);

var decisionReasons = new DecisionReasons();
decisionReasons.AddInfo("{0}", "Invalid variation is mapped to flag: flagKey and rule: rule forced decision map.");
var expectedResult = Result<Variation>.NullResult(decisionReasons);
var user = optlyObject.CreateUserContext(GenericUserId);

var context = new OptimizelyDecisionContext("flagKey", "ruleKey");

var result = decisionService.ValidatedForcedDecision(context, Config, user);

Assertions.AreEqual(expectedResult, result);
}

[Test]
public void TestGetVariationForcedVariationPrecedesAudienceEval()
{
Expand Down
15 changes: 0 additions & 15 deletions OptimizelySDK.Tests/OptimizelyUserContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,6 @@ public void TestGetForcedDecisionReturnsValueWithRuleKey()
Assertions.AreEqual(decision, result);
}

[Test]
public void TestFindValidatedForcedDecisionReturnsCorrectDecisionWithNullVariation()
{
var decisionReasons = new DecisionReasons();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this test will be moved to DecisionService.

decisionReasons.AddInfo("{0}", "Invalid variation is mapped to flag: flagKey and rule: rule forced decision map.");
var expectedResult = Result<Variation>.NullResult(decisionReasons);
var user = Optimizely.CreateUserContext(UserID);

var context = new OptimizelyDecisionContext("flagKey", "ruleKey");

var result = user.FindValidatedForcedDecision(context, null);

Assertions.AreEqual(expectedResult, result);
}

[Test]
public void TestRemoveForcedDecisionReturnsFalseForNullFlagKey()
{
Expand Down
36 changes: 31 additions & 5 deletions OptimizelySDK/Bucketing/DecisionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public virtual Result<FeatureDecision> GetVariationForFeatureRollout(FeatureFlag
//Check forced decision first
var rule = rolloutRules[index];
var decisionContext = new OptimizelyDecisionContext(featureFlag.Key, rule.Key);
var forcedDecisionResponse = user.FindValidatedForcedDecision(decisionContext, config);
var forcedDecisionResponse = ValidatedForcedDecision(decisionContext, config, user);

reasons += forcedDecisionResponse.DecisionReasons;
if (forcedDecisionResponse.ResultObject != null)
Expand Down Expand Up @@ -550,10 +550,9 @@ public virtual Result<FeatureDecision> GetVariationForFeatureExperiment(FeatureF

if (string.IsNullOrEmpty(experiment.Key))
continue;

var forcedDecisionResponse = user.FindValidatedForcedDecision(
new OptimizelyDecisionContext(featureFlag.Key, experiment?.Key),
config);
var decisionContext = new OptimizelyDecisionContext(featureFlag.Key, experiment?.Key);
var forcedDecisionResponse = ValidatedForcedDecision(decisionContext, config, user);

reasons += forcedDecisionResponse.DecisionReasons;

if (forcedDecisionResponse?.ResultObject != null)
Expand Down Expand Up @@ -662,5 +661,32 @@ private Result<string> GetBucketingId(string userId, UserAttributes filteredAttr

return Result<string>.NewResult(bucketingId, reasons);
}

/// <summary>
/// Finds a validated forced decision.
/// </summary>
/// <param name="context">Object containing flag and rule key of which forced decision is set.</param>
/// <param name="config">The Project config.</param>
/// <param name="user">Optimizely user context.</param>
/// <returns>A result with the variation</returns>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to revise parameters.

public Result<Variation> ValidatedForcedDecision(OptimizelyDecisionContext context, ProjectConfig config, OptimizelyUserContext user)
{
DecisionReasons reasons = new DecisionReasons();
var userId = user.GetUserId();
var forcedDecision = user.GetForcedDecision(context);
if (config != null && forcedDecision != null) {
var loggingKey = context.RuleKey != null ? "flag (" + context.FlagKey + "), rule (" + context.RuleKey + ")" : "flag (" + context.FlagKey + ")";
var variationKey = forcedDecision.VariationKey;
var variation = config.GetFlagVariationByKey(context.FlagKey, variationKey);
if (variation != null) {
reasons.AddInfo("Decided by forced decision.");
reasons.AddInfo("Variation ({0}) is mapped to {1} and user ({2}) in the forced decision map.", variationKey, loggingKey, userId);
return Result<Variation>.NewResult(variation, reasons);
} else {
reasons.AddInfo("Invalid variation is mapped to {0} and user ({1}) in the forced decision map.", loggingKey, userId);
}
}
return Result<Variation>.NullResult(reasons);
}
}
}
2 changes: 1 addition & 1 deletion OptimizelySDK/Optimizely.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ internal OptimizelyDecision Decide(OptimizelyUserContext user,
FeatureDecision decision = null;

var decisionContext = new OptimizelyDecisionContext(flag.Key);
var forcedDecisionVariation = user.FindValidatedForcedDecision(decisionContext, config);
var forcedDecisionVariation = DecisionService.ValidatedForcedDecision(decisionContext, config, user);
decisionReasons += forcedDecisionVariation.DecisionReasons;

if (forcedDecisionVariation.ResultObject != null)
Expand Down
30 changes: 0 additions & 30 deletions OptimizelySDK/OptimizelyUserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,35 +298,5 @@ public bool RemoveAllForcedDecisions()
}
return true;
}

/// <summary>
/// Finds a validated forced decision.
/// </summary>
/// <param name="context">Object containing flag and rule key of which forced decision is set.</param>
/// <param name="config">The Project config.</param>
/// <returns>A result with the variation</returns>
public Result<Variation> FindValidatedForcedDecision(OptimizelyDecisionContext context, ProjectConfig config)
{
DecisionReasons reasons = new DecisionReasons();
var forcedDecision = GetForcedDecision(context);

if (config != null && forcedDecision != null)
{
var loggingKey = context.RuleKey != null ? "flag (" + context.FlagKey + "), rule (" + context.RuleKey + ")" : "flag (" + context.FlagKey + ")";
var variationKey = forcedDecision.VariationKey;
var variation = config.GetFlagVariationByKey(context.FlagKey, variationKey);
if (variation != null)
{
reasons.AddInfo("Decided by forced decision.");
reasons.AddInfo("Variation ({0}) is mapped to {1} and user ({2}) in the forced decision map.", variationKey, loggingKey, UserId);
return Result<Variation>.NewResult(variation, reasons);
}
else
{
reasons.AddInfo("Invalid variation is mapped to {0} and user ({1}) in the forced decision map.", loggingKey, UserId);
}
}
return Result<Variation>.NullResult(reasons);
}
}
}
0