Search in sources :

Example 6 with Attribute

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

the class EventBuilderTest method createImpressionEventWithBucketingId.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createImpressionEventWithBucketingId() 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 = new HashMap<String, String>();
    attributeMap.put(attribute.getKey(), "value");
    attributeMap.put(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, "variation");
    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");
    com.optimizely.ab.event.internal.payload.Attribute feature1 = new com.optimizely.ab.event.internal.payload.Attribute(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, EventBuilder.ATTRIBUTE_KEY_FOR_BUCKETING_ATTRIBUTE, com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, "variation");
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Arrays.asList(feature, feature1);
    LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify payload information
    assertThat(impression.getVisitors().get(0).getVisitorId(), is(userId));
    assertThat((double) impression.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
    assertFalse(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
    assertThat(impression.getAnonymizeIp(), is(projectConfig.getAnonymizeIP()));
    assertThat(impression.getProjectId(), is(projectConfig.getProjectId()));
    assertThat(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0), is(expectedDecision));
    assertThat(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getCampaignId(), is(activatedExperiment.getLayerId()));
    assertThat(impression.getAccountId(), is(projectConfig.getAccountId()));
    assertThat(impression.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
    assertThat(impression.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
    assertThat(impression.getClientVersion(), is(BuildVersionInfo.VERSION));
    assertNull(impression.getVisitors().get(0).getSessionId());
}
Also used : Attribute(com.optimizely.ab.config.Attribute) HashMap(java.util.HashMap) 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 7 with Attribute

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

the class EventBuilderTest method createImpressionEventAndroidTVClientEngineClientVersion.

/**
 * Verify that supplying {@link EventBuilder} with a custom Android TV client engine and client version
 * results in impression events being sent with the overriden values.
 */
@Test
public void createImpressionEventAndroidTVClientEngineClientVersion() throws Exception {
    String clientVersion = "0.0.0";
    EventBuilder builder = new EventBuilder(EventBatch.ClientEngine.ANDROID_TV_SDK, clientVersion);
    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");
    LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    assertThat(impression.getClientName(), is(EventBatch.ClientEngine.ANDROID_TV_SDK.getClientEngineValue()));
    assertThat(impression.getClientVersion(), is(clientVersion));
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) Attribute(com.optimizely.ab.config.Attribute) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 8 with Attribute

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

the class OptimizelyTest method activateWithAttributes.

/**
 * Verify that {@link Optimizely#activate(String, String, Map)} passes through attributes.
 */
@Test
@SuppressWarnings("unchecked")
public void activateWithAttributes() throws Exception {
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Variation userIdBucketedVariation = activatedExperiment.getVariations().get(1);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    // setup a mock event builder to return expected impression 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");
    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(userIdBucketedVariation);
    when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
    Map<String, String> attr = new HashMap<String, String>();
    attr.put(attribute.getKey(), "attributeValue");
    attr.put(testBucketingIdKey, testBucketingId);
    // activate the experiment
    Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, attr);
    // verify that the bucketing algorithm was called correctly
    verify(mockBucketer).bucket(activatedExperiment, testBucketingId);
    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(validProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), attributeCaptor.capture());
    Map<String, String> actualValue = attributeCaptor.getValue();
    assertThat(actualValue, hasEntry(attribute.getKey(), "attributeValue"));
    // verify that dispatchEvent was called with the correct LogEvent object
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) Attribute(com.optimizely.ab.config.Attribute) 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) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) EventBuilderTest.createExperimentVariationMap(com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap) Test(org.junit.Test)

Example 9 with Attribute

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

the class OptimizelyTest method trackDoesNotSendEventWhenUserDoesNotSatisfyAudiences.

/**
 * Verify that an event is not dispatched if a user doesn't satisfy audience conditions for an experiment.
 */
@Test
public void trackDoesNotSendEventWhenUserDoesNotSatisfyAudiences() throws Exception {
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    EventType eventType = validProjectConfig.getEventTypes().get(2);
    // the audience for the experiments is "NOT firefox" so this user shouldn't satisfy audience conditions
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "firefox");
    Optimizely client = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
    logbackVerifier.expectMessage(Level.INFO, "There are no valid experiments for event \"" + eventType.getKey() + "\" to track.");
    client.track(eventType.getKey(), genericUserId, attributeMap);
    verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Also used : Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 10 with Attribute

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

the class OptimizelyTest method activateWithNullAttributeValues.

/**
 * Verify that {@link Optimizely#activate(String, String, Map)} gracefully handles null attribute values.
 */
@Test
public void activateWithNullAttributeValues() throws Exception {
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    // setup a mock event builder to return expected impression 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");
    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);
    // activate the experiment
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put(attribute.getKey(), null);
    Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, attributes);
    // 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(validProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), attributeCaptor.capture());
    Map<String, String> actualValue = attributeCaptor.getValue();
    assertThat(actualValue, hasEntry(attribute.getKey(), null));
    // verify that dispatchEvent was called with the correct LogEvent object
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) Attribute(com.optimizely.ab.config.Attribute) 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) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) EventBuilderTest.createExperimentVariationMap(com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap) Test(org.junit.Test)

Aggregations

Attribute (com.optimizely.ab.config.Attribute)20 Experiment (com.optimizely.ab.config.Experiment)17 LogEvent (com.optimizely.ab.event.LogEvent)14 Test (org.junit.Test)14 Variation (com.optimizely.ab.config.Variation)13 EventType (com.optimizely.ab.config.EventType)12 ProjectConfig (com.optimizely.ab.config.ProjectConfig)9 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)9 HashMap (java.util.HashMap)9 Bucketer (com.optimizely.ab.bucketing.Bucketer)5 Matchers.anyString (org.mockito.Matchers.anyString)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 DecisionService (com.optimizely.ab.bucketing.DecisionService)4 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)4 FeatureFlag (com.optimizely.ab.config.FeatureFlag)4 Group (com.optimizely.ab.config.Group)4 LiveVariable (com.optimizely.ab.config.LiveVariable)4 Rollout (com.optimizely.ab.config.Rollout)4 Audience (com.optimizely.ab.config.audience.Audience)4 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)4