Search in sources :

Example 91 with Experiment

use of com.optimizely.ab.config.Experiment 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 92 with Experiment

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

the class EventBuilder method createConversionEvent.

public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig, @Nonnull Map<Experiment, Variation> experimentVariationMap, @Nonnull String userId, @Nonnull String eventId, @Nonnull String eventName, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags) {
    if (experimentVariationMap.isEmpty()) {
        return null;
    }
    ArrayList<Decision> decisions = new ArrayList<Decision>();
    for (Map.Entry<Experiment, Variation> entry : experimentVariationMap.entrySet()) {
        Decision decision = new Decision(entry.getKey().getLayerId(), entry.getKey().getId(), entry.getValue().getId(), false);
        decisions.add(decision);
    }
    EventType eventType = projectConfig.getEventNameMapping().get(eventName);
    Event conversionEvent = new Event(System.currentTimeMillis(), UUID.randomUUID().toString(), eventType.getId(), eventType.getKey(), null, EventTagUtils.getRevenueValue(eventTags), eventTags, eventType.getKey(), EventTagUtils.getNumericValue(eventTags));
    Snapshot snapshot = new Snapshot(decisions, Arrays.asList(conversionEvent));
    Visitor visitor = new Visitor(userId, null, buildAttributeList(projectConfig, attributes), Arrays.asList(snapshot));
    List<Visitor> visitors = Arrays.asList(visitor);
    EventBatch eventBatch = new EventBatch(clientEngine.getClientEngineValue(), clientVersion, projectConfig.getAccountId(), visitors, projectConfig.getAnonymizeIP(), projectConfig.getProjectId(), projectConfig.getRevision());
    String payload = this.serializer.serialize(eventBatch);
    return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
}
Also used : Visitor(com.optimizely.ab.event.internal.payload.Visitor) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) Decision(com.optimizely.ab.event.internal.payload.Decision) Snapshot(com.optimizely.ab.event.internal.payload.Snapshot) Event(com.optimizely.ab.event.internal.payload.Event) LogEvent(com.optimizely.ab.event.LogEvent) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Map(java.util.Map)

Example 93 with Experiment

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

the class OptimizelyTest method getVariationWithExperimentKeyForced.

/**
 * Verify that the {@link Optimizely#getVariation(String, String, Map<String, String>)} call
 * uses forced variation to force the user into the second variation.  The mock bucket returns
 * the first variation. Then remove the forced variation and confirm that the forced variation is null.
 */
@Test
public void getVariationWithExperimentKeyForced() throws Exception {
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation forcedVariation = activatedExperiment.getVariations().get(1);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
    optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, forcedVariation.getKey());
    Map<String, String> testUserAttributes = new HashMap<String, String>();
    if (datafileVersion >= 4) {
        testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
    } else {
        testUserAttributes.put("browser_type", "chrome");
    }
    testUserAttributes.put(testBucketingIdKey, testBucketingId);
    Map<String, String> testParams = new HashMap<String, String>();
    testParams.put("test", "params");
    LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
    when(mockEventBuilder.createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(forcedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
    when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
    // activate the experiment
    Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
    assertThat(actualVariation, is(forcedVariation));
    optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, null);
    assertEquals(optimizely.getForcedVariation(activatedExperiment.getKey(), testUserId), null);
    actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
    assertThat(actualVariation, is(bucketedVariation));
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 94 with Experiment

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

the class DecisionServiceTest method getVariationForcedPrecedesAudienceEval.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives precedence to forced variation bucketing over audience evaluation.
 */
@Test
public void getVariationForcedPrecedesAudienceEval() throws Exception {
    Bucketer bucketer = spy(new Bucketer(validProjectConfig));
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
    Experiment experiment = validProjectConfig.getExperiments().get(0);
    Variation expectedVariation = experiment.getVariations().get(1);
    // user excluded without audiences and whitelisting
    assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
    logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"etag1\".");
    // set the runtimeForcedVariation
    validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, expectedVariation.getKey());
    // no attributes provided for a experiment that has an audience
    assertThat(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
    verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
    assertEquals(validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, null), true);
    assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), genericUserId));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 95 with Experiment

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

the class DecisionServiceTest method getVariationOnNonRunningExperimentWithForcedVariation.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives a null variation on a Experiment that is not running. Set the forced variation.
 * And, test to make sure that after setting forced variation, the getVariation still returns
 * null.
 */
@Test
public void getVariationOnNonRunningExperimentWithForcedVariation() {
    Experiment experiment = validProjectConfig.getExperiments().get(1);
    assertFalse(experiment.isRunning());
    Variation variation = experiment.getVariations().get(0);
    Bucketer bucketer = new Bucketer(validProjectConfig);
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
    // ensure that the not running variation returns null with no forced variation set.
    assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
    // we call getVariation 3 times on an experiment that is not running.
    logbackVerifier.expectMessage(Level.INFO, "Experiment \"etag2\" is not running.", times(3));
    // set a forced variation on the user that got back null
    assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), "userId", variation.getKey()));
    // ensure that a user with a forced variation set
    // still gets back a null variation if the variation is not running.
    assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
    // set the forced variation back to null
    assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), "userId", null));
    // test one more time that the getVariation returns null for the experiment that is not running.
    assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)125 Test (org.junit.Test)108 Variation (com.optimizely.ab.config.Variation)77 Matchers.anyString (org.mockito.Matchers.anyString)44 LogEvent (com.optimizely.ab.event.LogEvent)42 HashMap (java.util.HashMap)36 EventType (com.optimizely.ab.config.EventType)24 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)23 ProjectConfig (com.optimizely.ab.config.ProjectConfig)19 Map (java.util.Map)14 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 Attribute (com.optimizely.ab.config.Attribute)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9 Rollout (com.optimizely.ab.config.Rollout)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 ArrayList (java.util.ArrayList)8