use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.
the class OptimizelyTest method getEnabledFeatureCheckingListIsSorted.
/**
* Verify {@link Optimizely#getEnabledFeatures(String, Map)} calls into
* {@link Optimizely#isFeatureEnabled(String, String, Map)} for each featureFlag
* return sorted List of FeatureFlags
* Also checks that the orignal list directly from projectConfig is unsorted
*/
@Test
public void getEnabledFeatureCheckingListIsSorted() throws ConfigParseException {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
Optimizely spyOptimizely = spy(Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build());
ArrayList<String> featureFlagsSortedList = (ArrayList<String>) spyOptimizely.getEnabledFeatures(genericUserId, new HashMap<String, String>());
assertFalse(featureFlagsSortedList.isEmpty());
// To get Unsorted list directly from project config
List<String> unSortedFeaturesListFromProjectConfig = new ArrayList<String>();
for (FeatureFlag featureFlag : spyOptimizely.projectConfig.getFeatureFlags()) {
String featureKey = featureFlag.getKey();
unSortedFeaturesListFromProjectConfig.add(featureKey);
}
// unSortedFeaturesListFromProjectConfig will retain only the elements which are also contained in featureFlagsSortedList.
unSortedFeaturesListFromProjectConfig.retainAll(featureFlagsSortedList);
assertFalse(Ordering.natural().isOrdered(unSortedFeaturesListFromProjectConfig));
assertTrue(Ordering.natural().isOrdered(featureFlagsSortedList));
}
use of com.optimizely.ab.config.FeatureFlag 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;
}
use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.
the class GsonHelpers method parseFeatureFlag.
static FeatureFlag parseFeatureFlag(JsonObject featureFlagJson, JsonDeserializationContext context) {
String id = featureFlagJson.get("id").getAsString();
String key = featureFlagJson.get("key").getAsString();
String layerId = featureFlagJson.get("rolloutId").getAsString();
JsonArray experimentIdsJson = featureFlagJson.getAsJsonArray("experimentIds");
List<String> experimentIds = new ArrayList<String>();
for (JsonElement experimentIdObj : experimentIdsJson) {
experimentIds.add(experimentIdObj.getAsString());
}
List<LiveVariable> liveVariables = new ArrayList<LiveVariable>();
try {
Type liveVariableType = new TypeToken<List<LiveVariable>>() {
}.getType();
liveVariables = context.deserialize(featureFlagJson.getAsJsonArray("variables"), liveVariableType);
} catch (JsonParseException exception) {
logger.warn("Unable to parse variables for feature \"" + key + "\". JsonParseException: " + exception);
}
return new FeatureFlag(id, key, layerId, experimentIds, liveVariables);
}
use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.
the class Optimizely method getEnabledFeatures.
/**
* Get the list of features that are enabled for the user.
* @param userId The ID of the user.
* @param attributes The user's attributes.
* @return List of the feature keys that are enabled for the user if the userId is empty it will
* return Empty List.
*/
public List<String> getEnabledFeatures(@Nonnull String userId, @Nonnull Map<String, String> attributes) {
List<String> enabledFeaturesList = new ArrayList<String>();
if (!validateUserId(userId)) {
return enabledFeaturesList;
}
for (FeatureFlag featureFlag : projectConfig.getFeatureFlags()) {
String featureKey = featureFlag.getKey();
if (isFeatureEnabled(featureKey, userId, attributes))
enabledFeaturesList.add(featureKey);
}
Collections.sort(enabledFeaturesList);
return enabledFeaturesList;
}
use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.
the class Optimizely method isFeatureEnabled.
/**
* Determine whether a boolean feature is enabled.
* Send an impression event if the user is bucketed into an experiment using the feature.
*
* @param featureKey The unique key of the feature.
* @param userId The ID of the user.
* @param attributes The user's attributes.
* @return True if the feature is enabled.
* False if the feature is disabled.
* False if the feature is not found.
*/
@Nonnull
public Boolean isFeatureEnabled(@Nonnull String featureKey, @Nonnull String userId, @Nonnull Map<String, String> attributes) {
if (featureKey == null) {
logger.warn("The featureKey parameter must be nonnull.");
return false;
} else if (userId == null) {
logger.warn("The userId parameter must be nonnull.");
return false;
}
FeatureFlag featureFlag = projectConfig.getFeatureKeyMapping().get(featureKey);
if (featureFlag == null) {
logger.info("No feature flag was found for key \"{}\".", featureKey);
return false;
}
Map<String, String> filteredAttributes = filterAttributes(projectConfig, attributes);
FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, filteredAttributes);
if (featureDecision.variation == null || !featureDecision.variation.getFeatureEnabled()) {
logger.info("Feature \"{}\" is not enabled for user \"{}\".", featureKey, userId);
return false;
} else {
if (featureDecision.decisionSource.equals(FeatureDecision.DecisionSource.EXPERIMENT)) {
sendImpression(projectConfig, featureDecision.experiment, userId, filteredAttributes, featureDecision.variation);
} else {
logger.info("The user \"{}\" is not included in an experiment for feature \"{}\".", userId, featureKey);
}
logger.info("Feature \"{}\" is enabled for user \"{}\".", featureKey, userId);
return true;
}
}
Aggregations