use of com.optimizely.ab.config.LiveVariable in project java-sdk by optimizely.
the class OptimizelyTest method getFeatureVariableValueReturnsVariationValueWhenUserGetsBucketedToVariation.
/**
* Verify {@link Optimizely#getFeatureVariableValueForType(String, String, String, Map, LiveVariable.VariableType)}
* returns the variable value of the variation the user is bucketed into
* if the variation is not null and the variable has a usage within the variation.
* @throws ConfigParseException
*/
@Test
public void getFeatureVariableValueReturnsVariationValueWhenUserGetsBucketedToVariation() throws ConfigParseException {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
String validVariableKey = VARIABLE_FIRST_LETTER_KEY;
LiveVariable variable = FEATURE_FLAG_MULTI_VARIATE_FEATURE.getVariableKeyToLiveVariableMap().get(validVariableKey);
String expectedValue = VARIATION_MULTIVARIATE_EXPERIMENT_GRED.getVariableIdToLiveVariableUsageInstanceMap().get(variable.getId()).getValue();
Experiment multivariateExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withDecisionService(mockDecisionService).build();
FeatureDecision featureDecision = new FeatureDecision(multivariateExperiment, VARIATION_MULTIVARIATE_EXPERIMENT_GRED, FeatureDecision.DecisionSource.EXPERIMENT);
doReturn(featureDecision).when(mockDecisionService).getVariationForFeature(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE));
String value = optimizely.getFeatureVariableValueForType(validFeatureKey, validVariableKey, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE), LiveVariable.VariableType.STRING);
assertEquals(expectedValue, value);
}
use of com.optimizely.ab.config.LiveVariable 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);
}
Aggregations