Search in sources :

Example 1 with Variation

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

the class JsonSimpleConfigParser method parseVariations.

private List<Variation> parseVariations(JSONArray variationJson) {
    List<Variation> variations = new ArrayList<Variation>(variationJson.size());
    for (Object obj : variationJson) {
        JSONObject variationObject = (JSONObject) obj;
        String id = (String) variationObject.get("id");
        String key = (String) variationObject.get("key");
        Boolean featureEnabled = false;
        if (variationObject.containsKey("featureEnabled"))
            featureEnabled = (Boolean) variationObject.get("featureEnabled");
        List<LiveVariableUsageInstance> liveVariableUsageInstances = null;
        if (variationObject.containsKey("variables")) {
            liveVariableUsageInstances = parseLiveVariableInstances((JSONArray) variationObject.get("variables"));
        }
        variations.add(new Variation(id, key, featureEnabled, liveVariableUsageInstances));
    }
    return variations;
}
Also used : LiveVariableUsageInstance(com.optimizely.ab.config.LiveVariableUsageInstance) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) Variation(com.optimizely.ab.config.Variation)

Example 2 with Variation

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

the class JsonConfigParser method parseVariations.

private List<Variation> parseVariations(JSONArray variationJson) {
    List<Variation> variations = new ArrayList<Variation>(variationJson.length());
    for (Object obj : variationJson) {
        JSONObject variationObject = (JSONObject) obj;
        String id = variationObject.getString("id");
        String key = variationObject.getString("key");
        Boolean featureEnabled = false;
        if (variationObject.has("featureEnabled"))
            featureEnabled = variationObject.getBoolean("featureEnabled");
        List<LiveVariableUsageInstance> liveVariableUsageInstances = null;
        if (variationObject.has("variables")) {
            liveVariableUsageInstances = parseLiveVariableInstances(variationObject.getJSONArray("variables"));
        }
        variations.add(new Variation(id, key, featureEnabled, liveVariableUsageInstances));
    }
    return variations;
}
Also used : LiveVariableUsageInstance(com.optimizely.ab.config.LiveVariableUsageInstance) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) Variation(com.optimizely.ab.config.Variation)

Example 3 with Variation

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

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

use of com.optimizely.ab.config.Variation 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

Variation (com.optimizely.ab.config.Variation)99 Experiment (com.optimizely.ab.config.Experiment)91 Test (org.junit.Test)81 Matchers.anyString (org.mockito.Matchers.anyString)50 LogEvent (com.optimizely.ab.event.LogEvent)44 HashMap (java.util.HashMap)42 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)25 EventType (com.optimizely.ab.config.EventType)22 Map (java.util.Map)18 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 Attribute (com.optimizely.ab.config.Attribute)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ArrayList (java.util.ArrayList)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ProjectConfig (com.optimizely.ab.config.ProjectConfig)9 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)8 Rollout (com.optimizely.ab.config.Rollout)7 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)7 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)7