use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getVariation.
// ======== getVariation tests ========//
/**
* Verify that {@link Optimizely#getVariation(Experiment, String)} correctly makes the
* {@link Bucketer#bucket(Experiment, String)} call and does NOT dispatch an event.
*/
@Test
public void getVariation() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
Map<String, String> testUserAttributes = new HashMap<String, String>();
testUserAttributes.put("browser_type", "chrome");
// activate the experiment
Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testUserId);
assertThat(actualVariation, is(bucketedVariation));
// verify that we didn't attempt to dispatch an event
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateExperimentStatusPrecedesForcedVariation.
/**
* Verify that {@link Optimizely#activate(String, String)} gives precedence to experiment status over forced
* variation bucketing.
*/
@Test
public void activateExperimentStatusPrecedesForcedVariation() throws Exception {
Experiment experiment;
String whitelistedUserId;
if (datafileVersion == 4) {
experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_PAUSED_EXPERIMENT_KEY);
whitelistedUserId = PAUSED_EXPERIMENT_FORCED_VARIATION_USER_ID_CONTROL;
} else {
experiment = validProjectConfig.getExperiments().get(1);
whitelistedUserId = "testUser3";
}
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
logbackVerifier.expectMessage(Level.INFO, "Experiment \"" + experiment.getKey() + "\" is not running.");
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"" + whitelistedUserId + "\" for experiment \"" + experiment.getKey() + "\".");
// testUser3 has a corresponding forced variation, but experiment status should be checked first
assertTrue(experiment.getUserIdToVariationKeyMap().containsKey(whitelistedUserId));
assertNull(optimizely.activate(experiment.getKey(), whitelistedUserId));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateWithUnknownAttribute.
/**
* Verify that {@link Optimizely#activate(String, String, Map<String, String>)} handles the case
* where an unknown attribute (i.e., not in the config) is passed through.
*
* In this case, the activate call should remove the unknown attribute from the given map.
*/
@Test
@SuppressWarnings("unchecked")
public void activateWithUnknownAttribute() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
// setup a mock event builder to return mock params and endpoint
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
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("unknownAttribute", "dimValue");
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), anyMapOf(String.class, String.class))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" + activatedExperiment.getKey() + "\".");
logbackVerifier.expectMessage(Level.WARN, "Attribute(s) [unknownAttribute] not in the datafile.");
logbackVerifier.expectMessage(Level.DEBUG, "Dispatching impression event to URL test_url with params " + testParams + " and payload \"\"");
// Use an immutable map to also check that we're not attempting to change the provided attribute map
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertThat(actualVariation, is(bucketedVariation));
// 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).createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), attributeCaptor.capture());
Map<String, String> actualValue = attributeCaptor.getValue();
assertThat(actualValue, not(hasKey("unknownAttribute")));
// 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 getVariationWithUnknownExperimentKeyAndNoOpErrorHandler.
/**
* Verify that {@link Optimizely#getVariation(String, String)} handles the case where an unknown experiment
* (i.e., not in the config) is passed through and a {@link NoOpErrorHandler} is used by default.
*/
@Test
public void getVariationWithUnknownExperimentKeyAndNoOpErrorHandler() throws Exception {
Experiment unknownExperiment = createUnknownExperiment();
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withErrorHandler(new NoOpErrorHandler()).build();
logbackVerifier.expectMessage(Level.ERROR, "Experiment \"unknown_experiment\" is not in the datafile");
// since we use a NoOpErrorHandler, we should fail and return null
Variation actualVariation = optimizely.getVariation(unknownExperiment.getKey(), testUserId);
// verify that null is returned, as no project config was available
assertNull(actualVariation);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateWithUnknownExperimentKeyAndRaiseExceptionErrorHandler.
/**
* Verify that {@link Optimizely#activate(String, String)} handles the case where an unknown experiment
* (i.e., not in the config) is passed through and a {@link RaiseExceptionErrorHandler} is provided.
*/
@Test
public void activateWithUnknownExperimentKeyAndRaiseExceptionErrorHandler() throws Exception {
thrown.expect(UnknownExperimentException.class);
Experiment unknownExperiment = createUnknownExperiment();
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
// since we use a RaiseExceptionErrorHandler, we should throw an error
optimizely.activate(unknownExperiment.getKey(), testUserId);
}
Aggregations