Search in sources :

Example 6 with LiveVariable

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);
}
Also used : FeatureDecision(com.optimizely.ab.bucketing.FeatureDecision) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) LiveVariable(com.optimizely.ab.config.LiveVariable) Test(org.junit.Test)

Example 7 with LiveVariable

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

Aggregations

LiveVariable (com.optimizely.ab.config.LiveVariable)7 Experiment (com.optimizely.ab.config.Experiment)3 FeatureFlag (com.optimizely.ab.config.FeatureFlag)3 FeatureDecision (com.optimizely.ab.bucketing.FeatureDecision)2 Attribute (com.optimizely.ab.config.Attribute)2 EventType (com.optimizely.ab.config.EventType)2 Group (com.optimizely.ab.config.Group)2 VariableStatus (com.optimizely.ab.config.LiveVariable.VariableStatus)2 VariableType (com.optimizely.ab.config.LiveVariable.VariableType)2 ProjectConfig (com.optimizely.ab.config.ProjectConfig)2 Rollout (com.optimizely.ab.config.Rollout)2 Audience (com.optimizely.ab.config.audience.Audience)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1