Search in sources :

Example 11 with FeatureFlag

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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 12 with FeatureFlag

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;
}
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)

Example 13 with FeatureFlag

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);
}
Also used : JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) FeatureFlag(com.optimizely.ab.config.FeatureFlag) ArrayList(java.util.ArrayList) List(java.util.List) LiveVariable(com.optimizely.ab.config.LiveVariable) JsonParseException(com.google.gson.JsonParseException)

Example 14 with FeatureFlag

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;
}
Also used : ArrayList(java.util.ArrayList) FeatureFlag(com.optimizely.ab.config.FeatureFlag)

Example 15 with FeatureFlag

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;
    }
}
Also used : FeatureDecision(com.optimizely.ab.bucketing.FeatureDecision) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Nonnull(javax.annotation.Nonnull)

Aggregations

FeatureFlag (com.optimizely.ab.config.FeatureFlag)19 Experiment (com.optimizely.ab.config.Experiment)9 Test (org.junit.Test)9 Matchers.anyString (org.mockito.Matchers.anyString)9 LiveVariable (com.optimizely.ab.config.LiveVariable)8 Rollout (com.optimizely.ab.config.Rollout)6 ArrayList (java.util.ArrayList)5 Attribute (com.optimizely.ab.config.Attribute)4 EventType (com.optimizely.ab.config.EventType)4 Group (com.optimizely.ab.config.Group)4 ProjectConfig (com.optimizely.ab.config.ProjectConfig)4 Audience (com.optimizely.ab.config.audience.Audience)4 Variation (com.optimizely.ab.config.Variation)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 List (java.util.List)3 FeatureDecision (com.optimizely.ab.bucketing.FeatureDecision)2 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)2 Type (java.lang.reflect.Type)2 HashMap (java.util.HashMap)2 JSONObject (org.json.JSONObject)2