use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getFeatureVariableValueReturnsDefaultValueWhenFeatureIsAttachedToOneExperimentButFailsTargeting.
/**
* Verify {@link Optimizely#getFeatureVariableValueForType(String, String, String, Map, LiveVariable.VariableType)}
* returns the String default value for a live variable
* when the feature is attached to an experiment and no rollout, but the user is excluded from the experiment.
* @throws ConfigParseException
*/
@Test
public void getFeatureVariableValueReturnsDefaultValueWhenFeatureIsAttachedToOneExperimentButFailsTargeting() throws ConfigParseException {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
String validFeatureKey = FEATURE_SINGLE_VARIABLE_DOUBLE_KEY;
String validVariableKey = VARIABLE_DOUBLE_VARIABLE_KEY;
String expectedValue = VARIABLE_DOUBLE_DEFAULT_VALUE;
FeatureFlag featureFlag = FEATURE_FLAG_SINGLE_VARIABLE_DOUBLE;
Experiment experiment = validProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
String valueWithImproperAttributes = optimizely.getFeatureVariableValueForType(validFeatureKey, validVariableKey, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, "Ravenclaw"), LiveVariable.VariableType.DOUBLE);
assertEquals(expectedValue, valueWithImproperAttributes);
logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
logbackVerifier.expectMessage(Level.INFO, "The feature flag \"" + validFeatureKey + "\" is not used in a rollout.");
logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" was not bucketed into any variation for feature flag \"" + validFeatureKey + "\". The default value \"" + expectedValue + "\" for \"" + validVariableKey + "\" is being returned.");
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateWithExperimentKeyForced.
/**
* Verify that the {@link Optimizely#activate(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 activateWithExperimentKeyForced() 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.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertThat(actualVariation, is(forcedVariation));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, null);
assertEquals(optimizely.getForcedVariation(activatedExperiment.getKey(), testUserId), null);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateWithListener.
// ======== Notification listeners ========//
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* correctly builds an endpoint url and request params
* and passes them through {@link EventHandler#dispatchEvent(LogEvent)}.
*/
@Test
public void activateWithListener() throws Exception {
final Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
final 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();
final 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);
ActivateNotificationListener activateNotification = new ActivateNotificationListener() {
@Override
public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Variation variation, @Nonnull LogEvent event) {
assertEquals(experiment.getKey(), activatedExperiment.getKey());
assertEquals(bucketedVariation.getKey(), variation.getKey());
assertEquals(userId, testUserId);
for (Map.Entry<String, String> entry : attributes.entrySet()) {
assertEquals(testUserAttributes.get(entry.getKey()), entry.getValue());
}
assertEquals(event.getRequestMethod(), RequestMethod.GET);
}
};
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
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(bucketedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testBucketingId);
assertThat(actualVariation, is(bucketedVariation));
// verify that dispatchEvent was called with the correct LogEvent object
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateUserWithNoAudiences.
/**
* Verify that when no audiences are provided, the user is included in the experiment (i.e., no audiences means
* the experiment is targeted to "everyone").
*/
@Test
public void activateUserWithNoAudiences() throws Exception {
Experiment experimentToCheck = noAudienceProjectConfig.getExperiments().get(0);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withErrorHandler(mockErrorHandler).build();
assertNotNull(optimizely.activate(experimentToCheck.getKey(), testUserId));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getVariationForGroupExperimentWithNonMatchingAttributes.
/**
* Verify that {@link Optimizely#getVariation(String, String, Map)} doesn't return a variation when given
* non-matching user attributes.
*/
@Test
public void getVariationForGroupExperimentWithNonMatchingAttributes() throws Exception {
Experiment experiment = validProjectConfig.getGroups().get(0).getExperiments().get(0);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
assertNull(optimizely.getVariation(experiment.getKey(), "user", Collections.singletonMap("browser_type", "firefox")));
}
Aggregations