Search in sources :

Example 1 with Rollout

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

the class JsonSimpleConfigParser method parseRollouts.

private List<Rollout> parseRollouts(JSONArray rolloutsJson) {
    List<Rollout> rollouts = new ArrayList<Rollout>(rolloutsJson.size());
    for (Object obj : rolloutsJson) {
        JSONObject rolloutObject = (JSONObject) obj;
        String id = (String) rolloutObject.get("id");
        List<Experiment> experiments = parseExperiments((JSONArray) rolloutObject.get("experiments"));
        rollouts.add(new Rollout(id, experiments));
    }
    return rollouts;
}
Also used : JSONObject(org.json.simple.JSONObject) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject) Rollout(com.optimizely.ab.config.Rollout)

Example 2 with Rollout

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

the class JsonSimpleConfigParser method parseProjectConfig.

@Override
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
    try {
        JSONParser parser = new JSONParser();
        JSONObject rootObject = (JSONObject) parser.parse(json);
        String accountId = (String) rootObject.get("accountId");
        String projectId = (String) rootObject.get("projectId");
        String revision = (String) rootObject.get("revision");
        String version = (String) rootObject.get("version");
        int datafileVersion = Integer.parseInt(version);
        List<Experiment> experiments = parseExperiments((JSONArray) rootObject.get("experiments"));
        List<Attribute> attributes;
        attributes = parseAttributes((JSONArray) rootObject.get("attributes"));
        List<EventType> events = parseEvents((JSONArray) rootObject.get("events"));
        List<Audience> audiences = parseAudiences((JSONArray) parser.parse(rootObject.get("audiences").toString()));
        List<Group> groups = parseGroups((JSONArray) rootObject.get("groups"));
        boolean anonymizeIP = false;
        List<LiveVariable> liveVariables = null;
        if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
            liveVariables = parseLiveVariables((JSONArray) rootObject.get("variables"));
            anonymizeIP = (Boolean) rootObject.get("anonymizeIP");
        }
        List<FeatureFlag> featureFlags = null;
        List<Rollout> rollouts = null;
        if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
            featureFlags = parseFeatureFlags((JSONArray) rootObject.get("featureFlags"));
            rollouts = parseRollouts((JSONArray) rootObject.get("rollouts"));
        }
        return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
    } catch (Exception e) {
        throw new ConfigParseException("Unable to parse datafile: " + json, e);
    }
}
Also used : Group(com.optimizely.ab.config.Group) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Rollout(com.optimizely.ab.config.Rollout) Audience(com.optimizely.ab.config.audience.Audience) Experiment(com.optimizely.ab.config.Experiment) JSONArray(org.json.simple.JSONArray) LiveVariable(com.optimizely.ab.config.LiveVariable) ParseException(org.json.simple.parser.ParseException) ProjectConfig(com.optimizely.ab.config.ProjectConfig) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser)

Example 3 with Rollout

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

the class JsonConfigParser method parseProjectConfig.

@Override
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
    try {
        JSONObject rootObject = new JSONObject(json);
        String accountId = rootObject.getString("accountId");
        String projectId = rootObject.getString("projectId");
        String revision = rootObject.getString("revision");
        String version = rootObject.getString("version");
        int datafileVersion = Integer.parseInt(version);
        List<Experiment> experiments = parseExperiments(rootObject.getJSONArray("experiments"));
        List<Attribute> attributes;
        attributes = parseAttributes(rootObject.getJSONArray("attributes"));
        List<EventType> events = parseEvents(rootObject.getJSONArray("events"));
        List<Audience> audiences = parseAudiences(rootObject.getJSONArray("audiences"));
        List<Group> groups = parseGroups(rootObject.getJSONArray("groups"));
        boolean anonymizeIP = false;
        List<LiveVariable> liveVariables = null;
        if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
            liveVariables = parseLiveVariables(rootObject.getJSONArray("variables"));
            anonymizeIP = rootObject.getBoolean("anonymizeIP");
        }
        List<FeatureFlag> featureFlags = null;
        List<Rollout> rollouts = null;
        if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
            featureFlags = parseFeatureFlags(rootObject.getJSONArray("featureFlags"));
            rollouts = parseRollouts(rootObject.getJSONArray("rollouts"));
        }
        return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
    } catch (Exception e) {
        throw new ConfigParseException("Unable to parse datafile: " + json, e);
    }
}
Also used : Group(com.optimizely.ab.config.Group) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) Audience(com.optimizely.ab.config.audience.Audience) Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) LiveVariable(com.optimizely.ab.config.LiveVariable) ProjectConfig(com.optimizely.ab.config.ProjectConfig) JSONObject(org.json.JSONObject) Rollout(com.optimizely.ab.config.Rollout)

Example 4 with Rollout

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

the class ProjectConfigJacksonDeserializer method deserialize.

@Override
public ProjectConfig deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Audience.class, new AudienceJacksonDeserializer());
    module.addDeserializer(Group.class, new GroupJacksonDeserializer());
    mapper.registerModule(module);
    JsonNode node = parser.getCodec().readTree(parser);
    String accountId = node.get("accountId").textValue();
    String projectId = node.get("projectId").textValue();
    String revision = node.get("revision").textValue();
    String version = node.get("version").textValue();
    int datafileVersion = Integer.parseInt(version);
    List<Group> groups = mapper.readValue(node.get("groups").toString(), new TypeReference<List<Group>>() {
    });
    List<Experiment> experiments = mapper.readValue(node.get("experiments").toString(), new TypeReference<List<Experiment>>() {
    });
    List<Attribute> attributes;
    attributes = mapper.readValue(node.get("attributes").toString(), new TypeReference<List<Attribute>>() {
    });
    List<EventType> events = mapper.readValue(node.get("events").toString(), new TypeReference<List<EventType>>() {
    });
    List<Audience> audiences = mapper.readValue(node.get("audiences").toString(), new TypeReference<List<Audience>>() {
    });
    boolean anonymizeIP = false;
    List<LiveVariable> liveVariables = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
        liveVariables = mapper.readValue(node.get("variables").toString(), new TypeReference<List<LiveVariable>>() {
        });
        anonymizeIP = node.get("anonymizeIP").asBoolean();
    }
    List<FeatureFlag> featureFlags = null;
    List<Rollout> rollouts = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        featureFlags = mapper.readValue(node.get("featureFlags").toString(), new TypeReference<List<FeatureFlag>>() {
        });
        rollouts = mapper.readValue(node.get("rollouts").toString(), new TypeReference<List<Rollout>>() {
        });
    }
    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) FeatureFlag(com.optimizely.ab.config.FeatureFlag) JsonNode(com.fasterxml.jackson.databind.JsonNode) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Rollout(com.optimizely.ab.config.Rollout) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 5 with Rollout

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

the class DecisionServiceTest method getVariationForFeatureReturnsVariationFromExperimentBeforeRollout.

/**
 * Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
 * {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
 * check first if the user is bucketed to an {@link Experiment}
 * then check if the user is not bucketed to an experiment,
 * check for a {@link Rollout}.
 */
@Test
public void getVariationForFeatureReturnsVariationFromExperimentBeforeRollout() {
    FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
    Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
    assertNotNull(featureExperiment);
    Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
    Variation experimentVariation = featureExperiment.getVariations().get(0);
    Experiment rolloutExperiment = featureRollout.getExperiments().get(0);
    Variation rolloutVariation = rolloutExperiment.getVariations().get(0);
    DecisionService decisionService = spy(new DecisionService(mock(Bucketer.class), mockErrorHandler, v4ProjectConfig, null));
    // return variation for experiment
    doReturn(experimentVariation).when(decisionService).getVariation(eq(featureExperiment), anyString(), anyMapOf(String.class, String.class));
    // return variation for rollout
    doReturn(new FeatureDecision(rolloutExperiment, rolloutVariation, FeatureDecision.DecisionSource.ROLLOUT)).when(decisionService).getVariationForFeatureInRollout(eq(featureFlag), anyString(), anyMapOf(String.class, String.class));
    // make sure we get the right variation back
    FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertEquals(experimentVariation, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.EXPERIMENT, featureDecision.decisionSource);
    // make sure we do not even check for rollout bucketing
    verify(decisionService, never()).getVariationForFeatureInRollout(any(FeatureFlag.class), anyString(), anyMapOf(String.class, String.class));
    // make sure we ask for experiment bucketing once
    verify(decisionService, times(1)).getVariation(any(Experiment.class), anyString(), anyMapOf(String.class, String.class));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Rollout(com.optimizely.ab.config.Rollout) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)13 Rollout (com.optimizely.ab.config.Rollout)13 Variation (com.optimizely.ab.config.Variation)7 FeatureFlag (com.optimizely.ab.config.FeatureFlag)6 Test (org.junit.Test)6 Audience (com.optimizely.ab.config.audience.Audience)5 Attribute (com.optimizely.ab.config.Attribute)4 EventType (com.optimizely.ab.config.EventType)4 Group (com.optimizely.ab.config.Group)4 LiveVariable (com.optimizely.ab.config.LiveVariable)4 ProjectConfig (com.optimizely.ab.config.ProjectConfig)4 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JSONObject (org.json.JSONObject)2 JSONObject (org.json.simple.JSONObject)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