Search in sources :

Example 86 with Experiment

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);
}
Also used : FeatureDecision(com.optimizely.ab.bucketing.FeatureDecision) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) LiveVariable(com.optimizely.ab.config.LiveVariable) Test(org.junit.Test)

Example 87 with Experiment

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));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 88 with Experiment

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);
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 89 with Experiment

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);
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 90 with Experiment

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()));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) ExperimentUtils.isUserInExperiment(com.optimizely.ab.internal.ExperimentUtils.isUserInExperiment) Test(org.junit.Test)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)125 Test (org.junit.Test)108 Variation (com.optimizely.ab.config.Variation)77 Matchers.anyString (org.mockito.Matchers.anyString)44 LogEvent (com.optimizely.ab.event.LogEvent)42 HashMap (java.util.HashMap)36 EventType (com.optimizely.ab.config.EventType)24 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)23 ProjectConfig (com.optimizely.ab.config.ProjectConfig)19 Map (java.util.Map)14 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 Attribute (com.optimizely.ab.config.Attribute)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9 Rollout (com.optimizely.ab.config.Rollout)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 ArrayList (java.util.ArrayList)8