Search in sources :

Example 6 with ForwardingEventProcessor

use of com.optimizely.ab.event.ForwardingEventProcessor in project java-sdk by optimizely.

the class OptimizelyUserContextTest method setForcedDecisionsAndCallDecideExperimentRuleToDecision.

@Test
public void setForcedDecisionsAndCallDecideExperimentRuleToDecision() {
    String flagKey = "feature_1";
    String ruleKey = "exp_with_audience";
    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, ruleKey);
    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 = "10390977673";
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap());
    assertNotNull(decision);
    assertTrue(decision.getReasons().contains(String.format("Variation (%s) is mapped to flag (%s), rule (%s) and user (%s) in the forced decision map.", variationKey, flagKey, ruleKey, userId)));
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) Test(org.junit.Test)

Example 7 with ForwardingEventProcessor

use of com.optimizely.ab.event.ForwardingEventProcessor in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decide_sendEvent_featureTest_withSendFlagDecisionsOff.

@Test
public void decide_sendEvent_featureTest_withSendFlagDecisionsOff() {
    String datafileWithSendFlagDecisionsOff = datafile.replace("\"sendFlagDecisions\": true", "\"sendFlagDecisions\": false");
    optimizely = new Optimizely.Builder().withDatafile(datafileWithSendFlagDecisionsOff).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    Map<String, Object> attributes = Collections.singletonMap("gender", "f");
    OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
    optimizely.addDecisionNotificationHandler(decisionNotification -> {
        Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true);
        isListenerCalled = true;
    });
    String flagKey = "feature_2";
    String experimentId = "10420810910";
    String variationId = "10418551353";
    isListenerCalled = false;
    user.decide(flagKey);
    assertTrue(isListenerCalled);
    eventHandler.expectImpression(experimentId, variationId, userId, attributes);
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) Test(org.junit.Test)

Example 8 with ForwardingEventProcessor

use of com.optimizely.ab.event.ForwardingEventProcessor in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decide_sendEvent_featureTest_withSendFlagDecisionsOn.

@Test
public void decide_sendEvent_featureTest_withSendFlagDecisionsOn() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    Map<String, Object> attributes = Collections.singletonMap("gender", "f");
    OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
    optimizely.addDecisionNotificationHandler(decisionNotification -> {
        Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true);
        isListenerCalled = true;
    });
    String flagKey = "feature_2";
    String experimentId = "10420810910";
    String variationId = "10418551353";
    isListenerCalled = false;
    user.decide(flagKey);
    assertTrue(isListenerCalled);
    eventHandler.expectImpression(experimentId, variationId, userId, attributes);
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) Test(org.junit.Test)

Example 9 with ForwardingEventProcessor

use of com.optimizely.ab.event.ForwardingEventProcessor in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decide_sendEvent.

// send events
@Test
public void decide_sendEvent() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey = "feature_2";
    String variationKey = "variation_with_traffic";
    String experimentId = "10420810910";
    String variationId = "10418551353";
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertEquals(decision.getVariationKey(), variationKey);
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap());
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) Test(org.junit.Test)

Example 10 with ForwardingEventProcessor

use of com.optimizely.ab.event.ForwardingEventProcessor 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)

Aggregations

ForwardingEventProcessor (com.optimizely.ab.event.ForwardingEventProcessor)17 FlagDecisionNotificationBuilder (com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder)17 Test (org.junit.Test)17 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)10 DecisionMetadata (com.optimizely.ab.event.internal.payload.DecisionMetadata)5 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)5