Search in sources :

Example 16 with FeatureFlag

use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.

the class OptimizelyTest method getFeatureVariableValueReturnsDefaultValueWhenFeatureIsAttachedToOneExperimentButFailsTargeting.

/**
 * Verify {@link Optimizely#getFeatureVariableValueForType(String, String, String, Map, LiveVariable.VariableType)}
 * returns the String default value for a live variable
 * when the feature is attached to an experiment and no rollout, but the user is excluded from the experiment.
 * @throws ConfigParseException
 */
@Test
public void getFeatureVariableValueReturnsDefaultValueWhenFeatureIsAttachedToOneExperimentButFailsTargeting() throws ConfigParseException {
    assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
    String validFeatureKey = FEATURE_SINGLE_VARIABLE_DOUBLE_KEY;
    String validVariableKey = VARIABLE_DOUBLE_VARIABLE_KEY;
    String expectedValue = VARIABLE_DOUBLE_DEFAULT_VALUE;
    FeatureFlag featureFlag = FEATURE_FLAG_SINGLE_VARIABLE_DOUBLE;
    Experiment experiment = validProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
    String valueWithImproperAttributes = optimizely.getFeatureVariableValueForType(validFeatureKey, validVariableKey, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, "Ravenclaw"), LiveVariable.VariableType.DOUBLE);
    assertEquals(expectedValue, valueWithImproperAttributes);
    logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
    logbackVerifier.expectMessage(Level.INFO, "The feature flag \"" + validFeatureKey + "\" is not used in a rollout.");
    logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" was not bucketed into any variation for feature flag \"" + validFeatureKey + "\". The default value \"" + expectedValue + "\" for \"" + validVariableKey + "\" is being returned.");
}
Also used : Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 17 with FeatureFlag

use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.

the class ProjectConfigGsonDeserializer method deserialize.

@Override
public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String accountId = jsonObject.get("accountId").getAsString();
    String projectId = jsonObject.get("projectId").getAsString();
    String revision = jsonObject.get("revision").getAsString();
    String version = jsonObject.get("version").getAsString();
    int datafileVersion = Integer.parseInt(version);
    // generic list type tokens
    Type groupsType = new TypeToken<List<Group>>() {
    }.getType();
    Type experimentsType = new TypeToken<List<Experiment>>() {
    }.getType();
    Type attributesType = new TypeToken<List<Attribute>>() {
    }.getType();
    Type eventsType = new TypeToken<List<EventType>>() {
    }.getType();
    Type audienceType = new TypeToken<List<Audience>>() {
    }.getType();
    List<Group> groups = context.deserialize(jsonObject.get("groups").getAsJsonArray(), groupsType);
    List<Experiment> experiments = context.deserialize(jsonObject.get("experiments").getAsJsonArray(), experimentsType);
    List<Attribute> attributes;
    attributes = context.deserialize(jsonObject.get("attributes"), attributesType);
    List<EventType> events = context.deserialize(jsonObject.get("events").getAsJsonArray(), eventsType);
    List<Audience> audiences = context.deserialize(jsonObject.get("audiences").getAsJsonArray(), audienceType);
    boolean anonymizeIP = false;
    // live variables should be null if using V2
    List<LiveVariable> liveVariables = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
        Type liveVariablesType = new TypeToken<List<LiveVariable>>() {
        }.getType();
        liveVariables = context.deserialize(jsonObject.getAsJsonArray("variables"), liveVariablesType);
        anonymizeIP = jsonObject.get("anonymizeIP").getAsBoolean();
    }
    List<FeatureFlag> featureFlags = null;
    List<Rollout> rollouts = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        Type featureFlagsType = new TypeToken<List<FeatureFlag>>() {
        }.getType();
        featureFlags = context.deserialize(jsonObject.getAsJsonArray("featureFlags"), featureFlagsType);
        Type rolloutsType = new TypeToken<List<Rollout>>() {
        }.getType();
        rollouts = context.deserialize(jsonObject.get("rollouts").getAsJsonArray(), rolloutsType);
    }
    return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
}
Also used : Group(com.optimizely.ab.config.Group) Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) JsonObject(com.google.gson.JsonObject) FeatureFlag(com.optimizely.ab.config.FeatureFlag) List(java.util.List) Rollout(com.optimizely.ab.config.Rollout) Audience(com.optimizely.ab.config.audience.Audience) Experiment(com.optimizely.ab.config.Experiment) LiveVariable(com.optimizely.ab.config.LiveVariable) ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventType(com.optimizely.ab.config.EventType) Type(java.lang.reflect.Type)

Example 18 with FeatureFlag

use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.

the class DecisionServiceTest method getVariationForFeatureReturnsNullWhenFeatureFlagExperimentIdsIsEmpty.

// ========== get Variation for Feature tests ==========//
/**
 * Verify that {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)}
 * returns null when the {@link FeatureFlag} is not used in any experiments or rollouts.
 */
@Test
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
public void getVariationForFeatureReturnsNullWhenFeatureFlagExperimentIdsIsEmpty() {
    FeatureFlag emptyFeatureFlag = mock(FeatureFlag.class);
    when(emptyFeatureFlag.getExperimentIds()).thenReturn(Collections.<String>emptyList());
    String featureKey = "testFeatureFlagKey";
    when(emptyFeatureFlag.getKey()).thenReturn(featureKey);
    when(emptyFeatureFlag.getRolloutId()).thenReturn("");
    DecisionService decisionService = new DecisionService(mock(Bucketer.class), mockErrorHandler, validProjectConfig, null);
    logbackVerifier.expectMessage(Level.INFO, "The feature flag \"" + featureKey + "\" is not used in any experiments.");
    logbackVerifier.expectMessage(Level.INFO, "The feature flag \"" + featureKey + "\" is not used in a rollout.");
    logbackVerifier.expectMessage(Level.INFO, "The user \"" + genericUserId + "\" was not bucketed into a rollout for feature flag \"" + featureKey + "\".");
    FeatureDecision featureDecision = decisionService.getVariationForFeature(emptyFeatureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertNull(featureDecision.variation);
    assertNull(featureDecision.decisionSource);
    verify(emptyFeatureFlag, times(1)).getExperimentIds();
    verify(emptyFeatureFlag, times(1)).getRolloutId();
    verify(emptyFeatureFlag, times(3)).getKey();
}
Also used : FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 19 with FeatureFlag

use of com.optimizely.ab.config.FeatureFlag in project java-sdk by optimizely.

the class DecisionServiceTest method getVariationForFeatureReturnsVariationReturnedFromGetVariation.

/**
 * Verify that {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)}
 * returns the variation of the experiment a user gets bucketed into for an experiment.
 */
@Test
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
public void getVariationForFeatureReturnsVariationReturnedFromGetVariation() {
    FeatureFlag spyFeatureFlag = spy(ValidProjectConfigV4.FEATURE_FLAG_MUTEX_GROUP_FEATURE);
    DecisionService spyDecisionService = spy(new DecisionService(mock(Bucketer.class), mockErrorHandler, validProjectConfigV4(), null));
    doReturn(null).when(spyDecisionService).getVariation(eq(ValidProjectConfigV4.EXPERIMENT_MUTEX_GROUP_EXPERIMENT_1), anyString(), anyMapOf(String.class, String.class));
    doReturn(ValidProjectConfigV4.VARIATION_MUTEX_GROUP_EXP_2_VAR_1).when(spyDecisionService).getVariation(eq(ValidProjectConfigV4.EXPERIMENT_MUTEX_GROUP_EXPERIMENT_2), anyString(), anyMapOf(String.class, String.class));
    FeatureDecision featureDecision = spyDecisionService.getVariationForFeature(spyFeatureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertEquals(ValidProjectConfigV4.VARIATION_MUTEX_GROUP_EXP_2_VAR_1, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.EXPERIMENT, featureDecision.decisionSource);
    verify(spyFeatureFlag, times(2)).getExperimentIds();
    verify(spyFeatureFlag, never()).getKey();
}
Also used : FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

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