use of com.optimizely.ab.annotations.VisibleForTesting in project java-sdk by optimizely.
the class Optimizely method getFeatureVariableValueForType.
@VisibleForTesting
String getFeatureVariableValueForType(@Nonnull String featureKey, @Nonnull String variableKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull LiveVariable.VariableType variableType) {
if (featureKey == null) {
logger.warn("The featureKey parameter must be nonnull.");
return null;
} else if (variableKey == null) {
logger.warn("The variableKey parameter must be nonnull.");
return null;
} else if (userId == null) {
logger.warn("The userId parameter must be nonnull.");
return null;
}
FeatureFlag featureFlag = projectConfig.getFeatureKeyMapping().get(featureKey);
if (featureFlag == null) {
logger.info("No feature flag was found for key \"{}\".", featureKey);
return null;
}
LiveVariable variable = featureFlag.getVariableKeyToLiveVariableMap().get(variableKey);
if (variable == null) {
logger.info("No feature variable was found for key \"{}\" in feature flag \"{}\".", variableKey, featureKey);
return null;
} else if (!variable.getType().equals(variableType)) {
logger.info("The feature variable \"" + variableKey + "\" is actually of type \"" + variable.getType().toString() + "\" type. You tried to access it as type \"" + variableType.toString() + "\". Please use the appropriate feature variable accessor.");
return null;
}
String variableValue = variable.getDefaultValue();
FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, attributes);
if (featureDecision.variation != null) {
LiveVariableUsageInstance liveVariableUsageInstance = featureDecision.variation.getVariableIdToLiveVariableUsageInstanceMap().get(variable.getId());
if (liveVariableUsageInstance != null) {
variableValue = liveVariableUsageInstance.getValue();
} else {
variableValue = variable.getDefaultValue();
}
} else {
logger.info("User \"{}\" was not bucketed into any variation for feature flag \"{}\". " + "The default value \"{}\" for \"{}\" is being returned.", userId, featureKey, variableValue, variableKey);
}
return variableValue;
}
Aggregations