use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class ExperimentUtilsTest method doesUserMeetAudienceConditionsReturnsTrueIfUserDoesNotSatisfyAnyAudiences.
/**
* If the attributes satisfies no {@link Condition} of any {@link Audience} of the {@link Experiment},
* then {@link ExperimentUtils#doesUserMeetAudienceConditions(ProjectConfig, Experiment, Map, String, String)} should return false.
*/
@Test
public void doesUserMeetAudienceConditionsReturnsTrueIfUserDoesNotSatisfyAnyAudiences() {
Experiment experiment = projectConfig.getExperiments().get(0);
Map<String, String> attributes = Collections.singletonMap("browser_type", "firefox");
Boolean result = doesUserMeetAudienceConditions(projectConfig, experiment, attributes, EXPERIMENT, experiment.getKey()).getResult();
assertFalse(result);
logbackVerifier.expectMessage(Level.DEBUG, "Evaluating audiences for experiment \"etag1\": [100].");
logbackVerifier.expectMessage(Level.DEBUG, "Starting to evaluate audience \"100\" with conditions: [and, [or, [not, [or, {name='browser_type', type='custom_attribute', match='null', value='firefox'}]]]].");
logbackVerifier.expectMessage(Level.DEBUG, "Audience \"100\" evaluated to false.");
logbackVerifier.expectMessage(Level.INFO, "Audiences for experiment \"etag1\" collectively evaluated to false.");
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class Optimizely method getExperimentOrThrow.
// ======== Helper methods ========//
/**
* Helper method to retrieve the {@link Experiment} for the given experiment key.
* If {@link RaiseExceptionErrorHandler} is provided, either an experiment is returned, or an exception is thrown.
* If {@link NoOpErrorHandler} is used, either an experiment or {@code null} is returned.
*
* @param projectConfig the current project config
* @param experimentKey the experiment to retrieve from the current project config
* @return the experiment for given experiment key
*
* @throws UnknownExperimentException if there are no experiments in the current project config with the given
* experiment key
*/
@CheckForNull
private Experiment getExperimentOrThrow(@Nonnull ProjectConfig projectConfig, @Nonnull String experimentKey) throws UnknownExperimentException {
Experiment experiment = projectConfig.getExperimentKeyMapping().get(experimentKey);
// if the given experiment key isn't present in the config, log and potentially throw an exception
if (experiment == null) {
String unknownExperimentError = String.format("Experiment \"%s\" is not in the datafile.", experimentKey);
logger.error(unknownExperimentError);
errorHandler.handleError(new UnknownExperimentException(unknownExperimentError));
}
return experiment;
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateWithListenerNullAttributes.
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void activateWithListenerNullAttributes() throws Exception {
final Experiment activatedExperiment = noAudienceProjectConfig.getExperiments().get(0);
final Variation bucketedVariation = activatedExperiment.getVariations().get(0);
// setup a mock event builder to return expected impression params
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
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(noAudienceProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), eq(Collections.<String, String>emptyMap()))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
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);
assertTrue(attributes.isEmpty());
assertEquals(event.getRequestMethod(), RequestMethod.GET);
}
};
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
// activate the experiment
Map<String, String> attributes = null;
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, attributes);
optimizely.notificationCenter.removeNotificationListener(notificationId);
logbackVerifier.expectMessage(Level.WARN, "Attributes is null when non-null was expected. Defaulting to an empty attributes map.");
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testUserId);
assertThat(actualVariation, is(bucketedVariation));
// setup the attribute map captor (so we can verify its content)
ArgumentCaptor<Map> attributeCaptor = ArgumentCaptor.forClass(Map.class);
verify(mockEventBuilder).createImpressionEvent(eq(noAudienceProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), attributeCaptor.capture());
Map<String, String> actualValue = attributeCaptor.getValue();
assertThat(actualValue, is(Collections.<String, String>emptyMap()));
// 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 trackEventEndToEnd.
/**
* 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 trackEventEndToEnd() 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 = config.getExperiments();
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(experiment.getVariations().get(0));
}
// 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));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateUserNoAttributesWithAudiences.
/**
* Verify that when an experiment has audiences, but no attributes are provided, the user is not assigned a
* variation.
*/
@Test
public void activateUserNoAttributesWithAudiences() throws Exception {
Experiment experiment;
if (datafileVersion == 4) {
experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
} else {
experiment = validProjectConfig.getExperiments().get(0);
}
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).build();
logbackVerifier.expectMessage(Level.INFO, "User \"userId\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"userId\" for experiment \"" + experiment.getKey() + "\".");
assertNull(optimizely.activate(experiment.getKey(), testUserId));
}
Aggregations