Search in sources :

Example 6 with DecisionMetadata

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

the class UserEventFactoryTest method setUp.

@Before
public void setUp() {
    experiment = new Experiment(EXPERIMENT_ID, EXPERIMENT_KEY, LAYER_ID);
    variation = new Variation(VARIATION_ID, VARIATION_KEY);
    decisionMetadata = new DecisionMetadata("", EXPERIMENT_KEY, "experiment", VARIATION_KEY, true);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Before(org.junit.Before)

Example 7 with DecisionMetadata

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

the class UserEventFactory method createImpressionEvent.

public static ImpressionEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig, @Nullable Experiment activatedExperiment, @Nullable Variation variation, @Nonnull String userId, @Nonnull Map<String, ?> attributes, @Nonnull String flagKey, @Nonnull String ruleType, @Nonnull boolean enabled) {
    if ((FeatureDecision.DecisionSource.ROLLOUT.toString().equals(ruleType) || variation == null) && !projectConfig.getSendFlagDecisions()) {
        return null;
    }
    String variationKey = "";
    String variationID = "";
    String layerID = null;
    String experimentId = null;
    String experimentKey = "";
    if (variation != null) {
        variationKey = variation.getKey();
        variationID = variation.getId();
        layerID = activatedExperiment != null ? activatedExperiment.getLayerId() : "";
        experimentId = activatedExperiment != null ? activatedExperiment.getId() : "";
        experimentKey = activatedExperiment != null ? activatedExperiment.getKey() : "";
    }
    UserContext userContext = new UserContext.Builder().withUserId(userId).withAttributes(attributes).withProjectConfig(projectConfig).build();
    DecisionMetadata metadata = new DecisionMetadata.Builder().setFlagKey(flagKey).setRuleKey(experimentKey).setRuleType(ruleType).setVariationKey(variationKey).setEnabled(enabled).build();
    return new ImpressionEvent.Builder().withUserContext(userContext).withLayerId(layerID).withExperimentId(experimentId).withExperimentKey(experimentKey).withVariationId(variationID).withVariationKey(variationKey).withMetadata(metadata).build();
}
Also used : DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata)

Example 8 with DecisionMetadata

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

the class OptimizelyUserContextTest method setForcedDecisionsAndCallDecideFlagToDecision.

/**
 ****************************************[START DECIDE TESTS WITH FDs]*****************************************
 */
@Test
public void setForcedDecisionsAndCallDecideFlagToDecision() {
    String flagKey = "feature_1";
    String variationKey = "a";
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, null);
    OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
    assertEquals(variationKey, optimizelyUserContext.getForcedDecision(optimizelyDecisionContext).getVariationKey());
    optimizely.addDecisionNotificationHandler(decisionNotification -> {
        Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true);
        isListenerCalled = true;
    });
    isListenerCalled = false;
    // Test to confirm decide uses proper FD
    OptimizelyDecision decision = optimizelyUserContext.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(isListenerCalled);
    String variationId = "10389729780";
    String experimentId = "";
    DecisionMetadata metadata = new DecisionMetadata.Builder().setFlagKey(flagKey).setRuleKey("").setRuleType("feature-test").setVariationKey(variationKey).setEnabled(true).build();
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap(), metadata);
    assertNotNull(decision);
    assertTrue(decision.getReasons().contains(String.format("Variation (%s) is mapped to flag (%s) and user (%s) in the forced decision map.", variationKey, flagKey, userId)));
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) 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 9 with DecisionMetadata

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

the class OptimizelyUserContextTest method decide_nullVariation.

@Test
public void decide_nullVariation() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey = "feature_3";
    OptimizelyJSON variablesExpected = new OptimizelyJSON(Collections.emptyMap());
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertEquals(decision.getVariationKey(), null);
    assertFalse(decision.getEnabled());
    assertEquals(decision.getVariables().toMap(), variablesExpected.toMap());
    assertEquals(decision.getRuleKey(), null);
    assertEquals(decision.getFlagKey(), flagKey);
    assertEquals(decision.getUserContext(), user);
    assertTrue(decision.getReasons().isEmpty());
    DecisionMetadata metadata = new DecisionMetadata.Builder().setFlagKey(flagKey).setRuleKey("").setRuleType(FeatureDecision.DecisionSource.ROLLOUT.toString()).setVariationKey("").setEnabled(false).build();
    eventHandler.expectImpression(null, "", 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)

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