Search in sources :

Example 1 with VisibleForTesting

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;
}
Also used : FeatureDecision(com.optimizely.ab.bucketing.FeatureDecision) LiveVariableUsageInstance(com.optimizely.ab.config.LiveVariableUsageInstance) FeatureFlag(com.optimizely.ab.config.FeatureFlag) LiveVariable(com.optimizely.ab.config.LiveVariable) VisibleForTesting(com.optimizely.ab.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.optimizely.ab.annotations.VisibleForTesting)1 FeatureDecision (com.optimizely.ab.bucketing.FeatureDecision)1 FeatureFlag (com.optimizely.ab.config.FeatureFlag)1 LiveVariable (com.optimizely.ab.config.LiveVariable)1 LiveVariableUsageInstance (com.optimizely.ab.config.LiveVariableUsageInstance)1