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.");
}
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);
}
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();
}
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();
}
Aggregations