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