Search in sources :

Example 6 with Experiment

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

the class JsonConfigParser method parseGroups.

private List<Group> parseGroups(JSONArray groupJson) {
    List<Group> groups = new ArrayList<Group>(groupJson.length());
    for (Object obj : groupJson) {
        JSONObject groupObject = (JSONObject) obj;
        String id = groupObject.getString("id");
        String policy = groupObject.getString("policy");
        List<Experiment> experiments = parseExperiments(groupObject.getJSONArray("experiments"), id);
        List<TrafficAllocation> trafficAllocations = parseTrafficAllocation(groupObject.getJSONArray("trafficAllocation"));
        groups.add(new Group(id, policy, experiments, trafficAllocations));
    }
    return groups;
}
Also used : Group(com.optimizely.ab.config.Group) JSONObject(org.json.JSONObject) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject)

Example 7 with Experiment

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

the class OptimizelyTest method trackEventEndToEndForced.

// ======== track tests ========//
/**
 * Verify that the {@link Optimizely#track(String, String)} call correctly builds a V2 event and passes it
 * through {@link EventHandler#dispatchEvent(LogEvent)}.
 */
@Test
public void trackEventEndToEndForced() throws Exception {
    EventType eventType;
    String datafile;
    ProjectConfig config;
    if (datafileVersion >= 4) {
        config = spy(validProjectConfig);
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
        datafile = validDatafile;
    } else {
        config = spy(noAudienceProjectConfig);
        eventType = noAudienceProjectConfig.getEventTypes().get(0);
        datafile = noAudienceDatafile;
    }
    List<Experiment> allExperiments = new ArrayList<Experiment>();
    allExperiments.add(config.getExperiments().get(0));
    EventBuilder eventBuilder = new EventBuilder();
    DecisionService spyDecisionService = spy(new DecisionService(mockBucketer, mockErrorHandler, config, null));
    Optimizely optimizely = Optimizely.builder(datafile, mockEventHandler).withDecisionService(spyDecisionService).withEventBuilder(eventBuilder).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
    // call the bucket function.
    for (Experiment experiment : allExperiments) {
        when(mockBucketer.bucket(experiment, testUserId)).thenReturn(null);
    }
    // call get forced.
    for (Experiment experiment : allExperiments) {
        optimizely.projectConfig.setForcedVariation(experiment.getKey(), testUserId, experiment.getVariations().get(0).getKey());
    }
    // call track
    optimizely.track(eventType.getKey(), testUserId);
    // verify that the bucketing algorithm was called only on experiments corresponding to the specified goal.
    List<Experiment> experimentsForEvent = config.getExperimentsForEventKey(eventType.getKey());
    for (Experiment experiment : allExperiments) {
        if (experiment.isRunning() && experimentsForEvent.contains(experiment)) {
            verify(spyDecisionService).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
            verify(config).getForcedVariation(experiment.getKey(), testUserId);
        } else {
            verify(spyDecisionService, never()).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
        }
    }
    // verify that dispatchEvent was called
    verify(mockEventHandler).dispatchEvent(any(LogEvent.class));
    for (Experiment experiment : allExperiments) {
        assertEquals(optimizely.projectConfig.getForcedVariation(experiment.getKey(), testUserId), experiment.getVariations().get(0));
        optimizely.projectConfig.setForcedVariation(experiment.getKey(), testUserId, null);
        assertNull(optimizely.projectConfig.getForcedVariation(experiment.getKey(), testUserId));
    }
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) DecisionService(com.optimizely.ab.bucketing.DecisionService) Test(org.junit.Test)

Example 8 with Experiment

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

the class OptimizelyTest method trackEventWithUnknownAttribute.

/**
 * Verify that {@link Optimizely#track(String, String)} handles the case where an unknown attribute
 * (i.e., not in the config) is passed through.
 *
 * In this case, the track event call should remove the unknown attribute from the given map.
 */
@Test
@SuppressWarnings("unchecked")
public void trackEventWithUnknownAttribute() throws Exception {
    EventType eventType;
    if (datafileVersion >= 4) {
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
    } else {
        eventType = validProjectConfig.getEventTypes().get(0);
    }
    // setup a mock event builder to return expected conversion params
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
    Map<String, String> testParams = new HashMap<String, String>();
    testParams.put("test", "params");
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
    LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
    when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), anyMapOf(String.class, String.class), eq(Collections.<String, Object>emptyMap()))).thenReturn(logEventToDispatch);
    logbackVerifier.expectMessage(Level.INFO, "Tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
    logbackVerifier.expectMessage(Level.WARN, "Attribute(s) [unknownAttribute] not in the datafile.");
    logbackVerifier.expectMessage(Level.DEBUG, "Dispatching conversion event to URL test_url with params " + testParams + " and payload \"\"");
    // call track
    optimizely.track(eventType.getKey(), genericUserId, ImmutableMap.of("unknownAttribute", "attributeValue"));
    // setup the attribute map captor (so we can verify its content)
    ArgumentCaptor<Map> attributeCaptor = ArgumentCaptor.forClass(Map.class);
    // verify that the event builder was called with the expected attributes
    verify(mockEventBuilder).createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), attributeCaptor.capture(), eq(Collections.<String, Object>emptyMap()));
    Map<String, String> actualValue = attributeCaptor.getValue();
    assertThat(actualValue, not(hasKey("unknownAttribute")));
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) Variation(com.optimizely.ab.config.Variation) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) EventBuilderTest.createExperimentVariationMap(com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap) Test(org.junit.Test)

Example 9 with Experiment

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

the class DecisionServiceTest method getVariationWhitelistedPrecedesAudienceEval.

// ========= getVariation tests =========/
/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives precedence to forced variation bucketing over audience evaluation.
 */
@Test
public void getVariationWhitelistedPrecedesAudienceEval() 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(0);
    // user excluded without audiences and whitelisting
    assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
    logbackVerifier.expectMessage(Level.INFO, "User \"" + whitelistedUserId + "\" is forced in variation \"vtag1\".");
    // no attributes provided for a experiment that has an audience
    assertThat(decisionService.getVariation(experiment, whitelistedUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
    verify(decisionService).getWhitelistedVariation(experiment, whitelistedUserId);
    verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
}
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 10 with Experiment

use of com.optimizely.ab.config.Experiment 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)137 Test (org.junit.Test)113 Variation (com.optimizely.ab.config.Variation)91 Matchers.anyString (org.mockito.Matchers.anyString)58 LogEvent (com.optimizely.ab.event.LogEvent)47 HashMap (java.util.HashMap)42 EventType (com.optimizely.ab.config.EventType)28 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)27 ProjectConfig (com.optimizely.ab.config.ProjectConfig)22 Map (java.util.Map)18 Attribute (com.optimizely.ab.config.Attribute)17 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 ArrayList (java.util.ArrayList)14 Rollout (com.optimizely.ab.config.Rollout)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)11 ExperimentUtils.isUserInExperiment (com.optimizely.ab.internal.ExperimentUtils.isUserInExperiment)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9