use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getFeatureVariableValueReturnsVariationValueWhenUserGetsBucketedToVariation.
/**
* Verify {@link Optimizely#getFeatureVariableValueForType(String, String, String, Map, LiveVariable.VariableType)}
* returns the variable value of the variation the user is bucketed into
* if the variation is not null and the variable has a usage within the variation.
* @throws ConfigParseException
*/
@Test
public void getFeatureVariableValueReturnsVariationValueWhenUserGetsBucketedToVariation() throws ConfigParseException {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
String validVariableKey = VARIABLE_FIRST_LETTER_KEY;
LiveVariable variable = FEATURE_FLAG_MULTI_VARIATE_FEATURE.getVariableKeyToLiveVariableMap().get(validVariableKey);
String expectedValue = VARIATION_MULTIVARIATE_EXPERIMENT_GRED.getVariableIdToLiveVariableUsageInstanceMap().get(variable.getId()).getValue();
Experiment multivariateExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withDecisionService(mockDecisionService).build();
FeatureDecision featureDecision = new FeatureDecision(multivariateExperiment, VARIATION_MULTIVARIATE_EXPERIMENT_GRED, FeatureDecision.DecisionSource.EXPERIMENT);
doReturn(featureDecision).when(mockDecisionService).getVariationForFeature(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE));
String value = optimizely.getFeatureVariableValueForType(validFeatureKey, validVariableKey, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE), LiveVariable.VariableType.STRING);
assertEquals(expectedValue, value);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getVariationNoAudiences.
/**
* Verify that {@link Optimizely#getVariation(String, String)} returns a variation when given an experiment
* with no audiences and no user attributes.
*/
@Test
public void getVariationNoAudiences() throws Exception {
Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
Variation bucketedVariation = experiment.getVariations().get(0);
when(mockBucketer.bucket(experiment, testUserId)).thenReturn(bucketedVariation);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketer).withErrorHandler(mockErrorHandler).build();
Variation actualVariation = optimizely.getVariation(experiment.getKey(), testUserId);
verify(mockBucketer).bucket(experiment, testUserId);
assertThat(actualVariation, is(bucketedVariation));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateUserInAudience.
/**
* Verify that a user who falls in an experiment's audience is assigned a variation.
*/
@Test
public void activateUserInAudience() throws Exception {
Experiment experimentToCheck = validProjectConfig.getExperiments().get(0);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testUserAttributes = new HashMap<String, String>();
testUserAttributes.put("browser_type", "chrome");
Variation actualVariation = optimizely.activate(experimentToCheck.getKey(), testUserId, testUserAttributes);
assertNotNull(actualVariation);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithNullEventTags.
/**
* Verify that {@link Optimizely#track(String, String, Map, Map)} called with null event tags will default to
* an empty map when calling {@link EventBuilder#createConversionEvent(ProjectConfig, Map, String, String, String, Map, Map)}
*/
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithNullEventTags() 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(mockErrorHandler).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()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, String>emptyMap()))).thenReturn(logEventToDispatch);
logbackVerifier.expectMessage(Level.INFO, "Tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
logbackVerifier.expectMessage(Level.DEBUG, "Dispatching conversion event to URL test_url with params " + testParams + " and payload \"\"");
// call track
optimizely.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap(), null);
// 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()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, String>emptyMap()));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class ExperimentUtilsTest method isUserInExperimentReturnsTrueIfExperimentHasNoAudiences.
/**
* If the {@link Experiment} does not have any {@link Audience}s,
* then {@link ExperimentUtils#isUserInExperiment(ProjectConfig, Experiment, Map)} should return true;
*/
@Test
public void isUserInExperimentReturnsTrueIfExperimentHasNoAudiences() {
Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
assertTrue(isUserInExperiment(noAudienceProjectConfig, experiment, Collections.<String, String>emptyMap()));
}
Aggregations