Search in sources :

Example 71 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class EventBuilderTest method createImpressionEvent.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createImpressionEvent() throws Exception {
    // use the "valid" project config and its associated experiment, variation, and attributes
    ProjectConfig projectConfig = validProjectConfigV2();
    Experiment activatedExperiment = projectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = projectConfig.getAttributes().get(0);
    String userId = "userId";
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    Decision expectedDecision = new Decision(activatedExperiment.getLayerId(), activatedExperiment.getId(), bucketedVariation.getId(), false);
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute(attribute.getId(), attribute.getKey(), com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, "value");
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Collections.singletonList(feature);
    LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
    EventBatch eventBatch = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify payload information
    assertThat(eventBatch.getVisitors().get(0).getVisitorId(), is(userId));
    assertThat((double) eventBatch.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
    assertFalse(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
    assertThat(eventBatch.getAnonymizeIp(), is(projectConfig.getAnonymizeIP()));
    assertThat(eventBatch.getProjectId(), is(projectConfig.getProjectId()));
    assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0), is(expectedDecision));
    assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getCampaignId(), is(activatedExperiment.getLayerId()));
    assertThat(eventBatch.getAccountId(), is(projectConfig.getAccountId()));
    assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
    assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
    assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
    assertNull(eventBatch.getVisitors().get(0).getSessionId());
}
Also used : Attribute(com.optimizely.ab.config.Attribute) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Decision(com.optimizely.ab.event.internal.payload.Decision) ProjectConfig(com.optimizely.ab.config.ProjectConfig) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 72 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class EventBuilderTest method createExperimentVariationMap.

// ========== helper methods =========//
public static Map<Experiment, Variation> createExperimentVariationMap(ProjectConfig projectConfig, DecisionService decisionService, String eventName, String userId, @Nullable Map<String, String> attributes) {
    List<Experiment> eventExperiments = projectConfig.getExperimentsForEventKey(eventName);
    Map<Experiment, Variation> experimentVariationMap = new HashMap<Experiment, Variation>(eventExperiments.size());
    for (Experiment experiment : eventExperiments) {
        if (experiment.isRunning()) {
            Variation variation = decisionService.getVariation(experiment, userId, attributes);
            if (variation != null) {
                experimentVariationMap.put(experiment, variation);
            }
        }
    }
    return experimentVariationMap;
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation)

Example 73 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class EventBuilderTest method createConversionEvent.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createConversionEvent() throws Exception {
    // use the "valid" project config and its associated experiment, variation, and attributes
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    String userId = "userId";
    Bucketer mockBucketAlgorithm = mock(Bucketer.class);
    List<Experiment> allExperiments = validProjectConfig.getExperiments();
    List<Experiment> experimentsForEventKey = validProjectConfig.getExperimentsForEventKey(eventType.getKey());
    // call the bucket function.
    for (Experiment experiment : allExperiments) {
        when(mockBucketAlgorithm.bucket(experiment, userId)).thenReturn(experiment.getVariations().get(0));
    }
    DecisionService decisionService = new DecisionService(mockBucketAlgorithm, mock(ErrorHandler.class), validProjectConfig, mock(UserProfileService.class));
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), AUDIENCE_GRYFFINDOR_VALUE);
    Map<String, Object> eventTagMap = new HashMap<String, Object>();
    eventTagMap.put("boolean_param", false);
    eventTagMap.put("string_param", "123");
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), userId, attributeMap);
    LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, userId, eventType.getId(), eventType.getKey(), attributeMap, eventTagMap);
    List<Decision> expectedDecisions = new ArrayList<Decision>();
    for (Experiment experiment : experimentsForEventKey) {
        if (experiment.isRunning()) {
            Decision layerState = new Decision(experiment.getLayerId(), experiment.getId(), experiment.getVariations().get(0).getId(), false);
            expectedDecisions.add(layerState);
        }
    }
    // verify that the request endpoint is correct
    assertThat(conversionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    // verify payload information
    assertThat(conversion.getVisitors().get(0).getVisitorId(), is(userId));
    assertThat((double) conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 120.0));
    assertThat(conversion.getProjectId(), is(validProjectConfig.getProjectId()));
    assertThat(conversion.getAccountId(), is(validProjectConfig.getAccountId()));
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute(attribute.getId(), attribute.getKey(), com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, AUDIENCE_GRYFFINDOR_VALUE);
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Collections.singletonList(feature);
    assertEquals(conversion.getVisitors().get(0).getAttributes(), expectedUserFeatures);
    assertThat(conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions(), containsInAnyOrder(expectedDecisions.toArray()));
    assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getEntityId(), eventType.getId());
    assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getKey(), eventType.getKey());
    assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getRevenue(), null);
    assertTrue(conversion.getVisitors().get(0).getAttributes().containsAll(expectedUserFeatures));
    assertTrue(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTags().equals(eventTagMap));
    assertFalse(conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
    assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
    assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
    assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
}
Also used : Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) ArrayList(java.util.ArrayList) DecisionService(com.optimizely.ab.bucketing.DecisionService) NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) ErrorHandler(com.optimizely.ab.error.ErrorHandler) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Decision(com.optimizely.ab.event.internal.payload.Decision) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Bucketer(com.optimizely.ab.bucketing.Bucketer) Test(org.junit.Test)

Example 74 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class ExperimentUtilsTest method isUserInExperimentHandlesNullValue.

/**
 * If there are audiences with attributes on the experiment, but one of the attribute values is null,
 * they must explicitly pass in null in order for us to evaluate this. Otherwise we will say they do not match.
 */
@Test
public void isUserInExperimentHandlesNullValue() {
    Experiment experiment = v4ProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_WITH_MALFORMED_AUDIENCE_KEY);
    Map<String, String> satisfiesFirstCondition = Collections.singletonMap(ATTRIBUTE_NATIONALITY_KEY, AUDIENCE_WITH_MISSING_VALUE_VALUE);
    Map<String, String> attributesWithNull = Collections.singletonMap(ATTRIBUTE_NATIONALITY_KEY, null);
    Map<String, String> nonMatchingMap = Collections.singletonMap(ATTRIBUTE_NATIONALITY_KEY, "American");
    assertTrue(isUserInExperiment(v4ProjectConfig, experiment, satisfiesFirstCondition));
    assertTrue(isUserInExperiment(v4ProjectConfig, experiment, attributesWithNull));
    assertFalse(isUserInExperiment(v4ProjectConfig, experiment, nonMatchingMap));
    // It should explicitly be set to null otherwise we will return false on empty maps
    assertFalse(isUserInExperiment(v4ProjectConfig, 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)

Example 75 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class DecisionService method getVariationForFeature.

/**
 * Get the variation the user is bucketed into for the FeatureFlag
 * @param featureFlag The feature flag the user wants to access.
 * @param userId User Identifier
 * @param filteredAttributes A map of filtered attributes.
 * @return {@link FeatureDecision}
 */
@Nonnull
public FeatureDecision getVariationForFeature(@Nonnull FeatureFlag featureFlag, @Nonnull String userId, @Nonnull Map<String, String> filteredAttributes) {
    if (!featureFlag.getExperimentIds().isEmpty()) {
        for (String experimentId : featureFlag.getExperimentIds()) {
            Experiment experiment = projectConfig.getExperimentIdMapping().get(experimentId);
            Variation variation = this.getVariation(experiment, userId, filteredAttributes);
            if (variation != null) {
                return new FeatureDecision(experiment, variation, FeatureDecision.DecisionSource.EXPERIMENT);
            }
        }
    } else {
        logger.info("The feature flag \"{}\" is not used in any experiments.", featureFlag.getKey());
    }
    FeatureDecision featureDecision = getVariationForFeatureInRollout(featureFlag, userId, filteredAttributes);
    if (featureDecision.variation == null) {
        logger.info("The user \"{}\" was not bucketed into a rollout for feature flag \"{}\".", userId, featureFlag.getKey());
    } else {
        logger.info("The user \"{}\" was bucketed into a rollout for feature flag \"{}\".", userId, featureFlag.getKey());
    }
    return featureDecision;
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Nonnull(javax.annotation.Nonnull)

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