Search in sources :

Example 1 with DecisionMetadata

use of com.optimizely.ab.event.internal.payload.DecisionMetadata in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decideAll_oneFlag.

// decideAll
@Test
public void decideAll_oneFlag() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey = "feature_2";
    String experimentKey = "exp_no_audience";
    String variationKey = "variation_with_traffic";
    String experimentId = "10420810910";
    String variationId = "10418551353";
    List<String> flagKeys = Arrays.asList(flagKey);
    OptimizelyJSON variablesExpected = optimizely.getAllFeatureVariables(flagKey, userId);
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    Map<String, OptimizelyDecision> decisions = user.decideForKeys(flagKeys);
    assertTrue(decisions.size() == 1);
    OptimizelyDecision decision = decisions.get(flagKey);
    OptimizelyDecision expDecision = new OptimizelyDecision(variationKey, true, variablesExpected, experimentKey, flagKey, user, Collections.emptyList());
    assertEquals(decision, expDecision);
    DecisionMetadata metadata = new DecisionMetadata.Builder().setFlagKey(flagKey).setRuleKey(experimentKey).setRuleType(FeatureDecision.DecisionSource.FEATURE_TEST.toString()).setVariationKey(variationKey).setEnabled(true).build();
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap(), metadata);
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 2 with DecisionMetadata

use of com.optimizely.ab.event.internal.payload.DecisionMetadata in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decide_rollout.

@Test
public void decide_rollout() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey = "feature_1";
    String experimentKey = "18322080788";
    String variationKey = "18257766532";
    String experimentId = "18322080788";
    String variationId = "18257766532";
    OptimizelyJSON variablesExpected = optimizely.getAllFeatureVariables(flagKey, userId);
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertEquals(decision.getVariationKey(), variationKey);
    assertTrue(decision.getEnabled());
    assertEquals(decision.getVariables().toMap(), variablesExpected.toMap());
    assertEquals(decision.getRuleKey(), experimentKey);
    assertEquals(decision.getFlagKey(), flagKey);
    assertEquals(decision.getUserContext(), user);
    assertTrue(decision.getReasons().isEmpty());
    DecisionMetadata metadata = new DecisionMetadata.Builder().setFlagKey(flagKey).setRuleKey(experimentKey).setRuleType(FeatureDecision.DecisionSource.ROLLOUT.toString()).setVariationKey(variationKey).setEnabled(true).build();
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap(), metadata);
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 3 with DecisionMetadata

use of com.optimizely.ab.event.internal.payload.DecisionMetadata in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decide_featureTest.

// decide
@Test
public void decide_featureTest() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey = "feature_2";
    String experimentKey = "exp_no_audience";
    String variationKey = "variation_with_traffic";
    String experimentId = "10420810910";
    String variationId = "10418551353";
    OptimizelyJSON variablesExpected = optimizely.getAllFeatureVariables(flagKey, userId);
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertEquals(decision.getVariationKey(), variationKey);
    assertTrue(decision.getEnabled());
    assertEquals(decision.getVariables().toMap(), variablesExpected.toMap());
    assertEquals(decision.getRuleKey(), experimentKey);
    assertEquals(decision.getFlagKey(), flagKey);
    assertEquals(decision.getUserContext(), user);
    assertTrue(decision.getReasons().isEmpty());
    DecisionMetadata metadata = new DecisionMetadata.Builder().setFlagKey(flagKey).setRuleKey(experimentKey).setRuleType(FeatureDecision.DecisionSource.FEATURE_TEST.toString()).setVariationKey(variationKey).setEnabled(true).build();
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap(), metadata);
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 4 with DecisionMetadata

use of com.optimizely.ab.event.internal.payload.DecisionMetadata in project java-sdk by optimizely.

the class EventFactoryTest 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
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    String userId = "userId";
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    DecisionMetadata decisionMetadata = new DecisionMetadata.Builder().setFlagKey(activatedExperiment.getKey()).setRuleType("experiment").setVariationKey(bucketedVariation.getKey()).build();
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setMetadata(decisionMetadata).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute botFilteringFeature = getBotFilteringAttribute();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()))
        expectedUserFeatures = Arrays.asList(feature, botFilteringFeature);
    else
        expectedUserFeatures = Arrays.asList(feature);
    LogEvent impressionEvent = createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.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(validProjectConfig.getAnonymizeIP()));
    assertTrue(eventBatch.getEnrichDecisions());
    assertThat(eventBatch.getProjectId(), is(validProjectConfig.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(validProjectConfig.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 : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) GsonBuilder(com.google.gson.GsonBuilder) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 5 with DecisionMetadata

use of com.optimizely.ab.event.internal.payload.DecisionMetadata in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEventPassingUserAgentAttribute.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createImpressionEventPassingUserAgentAttribute() throws Exception {
    // use the "valid" project config and its associated experiment, variation, and attributes
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    String userId = "userId";
    String ruleType = "experiment";
    Map<String, String> attributeMap = new HashMap<String, String>();
    attributeMap.put(attribute.getKey(), "value");
    attributeMap.put(ControlAttribute.USER_AGENT_ATTRIBUTE.toString(), "Chrome");
    DecisionMetadata metadata = new DecisionMetadata(activatedExperiment.getKey(), activatedExperiment.getKey(), ruleType, "variationKey", true);
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setMetadata(metadata).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute userAgentFeature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(ControlAttribute.USER_AGENT_ATTRIBUTE.toString()).setKey(ControlAttribute.USER_AGENT_ATTRIBUTE.toString()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("Chrome").build();
    com.optimizely.ab.event.internal.payload.Attribute botFilteringFeature = getBotFilteringAttribute();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()))
        expectedUserFeatures = Arrays.asList(userAgentFeature, feature, botFilteringFeature);
    else
        expectedUserFeatures = Arrays.asList(userAgentFeature, feature);
    LogEvent impressionEvent = createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.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(validProjectConfig.getAnonymizeIP()));
    assertTrue(eventBatch.getEnrichDecisions());
    assertThat(eventBatch.getProjectId(), is(validProjectConfig.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(validProjectConfig.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 : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) GsonBuilder(com.google.gson.GsonBuilder) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Aggregations

DecisionMetadata (com.optimizely.ab.event.internal.payload.DecisionMetadata)9 Test (org.junit.Test)7 ForwardingEventProcessor (com.optimizely.ab.event.ForwardingEventProcessor)5 FlagDecisionNotificationBuilder (com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder)5 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)5 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)4 GsonBuilder (com.google.gson.GsonBuilder)2 LogEvent (com.optimizely.ab.event.LogEvent)2 Decision (com.optimizely.ab.event.internal.payload.Decision)2 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)2 ControlAttribute (com.optimizely.ab.internal.ControlAttribute)2 Experiment (com.optimizely.ab.config.Experiment)1 Variation (com.optimizely.ab.config.Variation)1 Before (org.junit.Before)1