Search in sources :

Example 6 with OptimizelyJSON

use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.

the class OptimizelyTest method getAllFeatureVariablesWithInvalidParameters.

/**
 * Verify {@link Optimizely#getAllFeatureVariables(String,String, Map)} with invalid parameters
 */
@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
@Test
public void getAllFeatureVariablesWithInvalidParameters() throws Exception {
    Optimizely optimizely = optimizelyBuilder.build();
    OptimizelyJSON value;
    value = optimizely.getAllFeatureVariables(null, testUserId);
    assertNull(value);
    value = optimizely.getAllFeatureVariables(FEATURE_MULTI_VARIATE_FEATURE_KEY, null);
    assertNull(value);
    value = optimizely.getAllFeatureVariables("invalid-feature-flag", testUserId);
    assertNull(value);
    Optimizely optimizelyInvalid = Optimizely.builder(invalidProjectConfigV5(), mockEventHandler).build();
    value = optimizelyInvalid.getAllFeatureVariables(FEATURE_MULTI_VARIATE_FEATURE_KEY, testUserId);
    assertNull(value);
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 7 with OptimizelyJSON

use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decideAll_allFlags.

@Test
public void decideAll_allFlags() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey1 = "feature_1";
    String flagKey2 = "feature_2";
    String flagKey3 = "feature_3";
    Map<String, Object> attributes = Collections.singletonMap("gender", "f");
    OptimizelyJSON variablesExpected1 = optimizely.getAllFeatureVariables(flagKey1, userId);
    OptimizelyJSON variablesExpected2 = optimizely.getAllFeatureVariables(flagKey2, userId);
    OptimizelyJSON variablesExpected3 = new OptimizelyJSON(Collections.emptyMap());
    OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
    Map<String, OptimizelyDecision> decisions = user.decideAll();
    assertTrue(decisions.size() == 3);
    assertEquals(decisions.get(flagKey1), new OptimizelyDecision("a", true, variablesExpected1, "exp_with_audience", flagKey1, user, Collections.emptyList()));
    assertEquals(decisions.get(flagKey2), new OptimizelyDecision("variation_with_traffic", true, variablesExpected2, "exp_no_audience", flagKey2, user, Collections.emptyList()));
    assertEquals(decisions.get(flagKey3), new OptimizelyDecision(null, false, variablesExpected3, null, flagKey3, user, Collections.emptyList()));
    eventHandler.expectImpression("10390977673", "10389729780", userId, attributes);
    eventHandler.expectImpression("10420810910", "10418551353", userId, attributes);
    eventHandler.expectImpression(null, "", userId, attributes);
}
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) Test(org.junit.Test)

Example 8 with OptimizelyJSON

use of com.optimizely.ab.optimizelyjson.OptimizelyJSON 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 9 with OptimizelyJSON

use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decideAll_errorDecisionIncluded.

@Test
public void decideAll_errorDecisionIncluded() {
    String flagKey1 = "feature_2";
    String flagKey2 = "invalid_key";
    List<String> flagKeys = Arrays.asList(flagKey1, flagKey2);
    OptimizelyJSON variablesExpected1 = optimizely.getAllFeatureVariables(flagKey1, userId);
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    Map<String, OptimizelyDecision> decisions = user.decideForKeys(flagKeys);
    assertEquals(decisions.size(), 2);
    assertEquals(decisions.get(flagKey1), new OptimizelyDecision("variation_with_traffic", true, variablesExpected1, "exp_no_audience", flagKey1, user, Collections.emptyList()));
    assertEquals(decisions.get(flagKey2), OptimizelyDecision.newErrorDecision(flagKey2, user, DecisionMessage.FLAG_KEY_INVALID.reason(flagKey2)));
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 10 with OptimizelyJSON

use of com.optimizely.ab.optimizelyjson.OptimizelyJSON 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)

Aggregations

OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)21 Test (org.junit.Test)17 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)9 ForwardingEventProcessor (com.optimizely.ab.event.ForwardingEventProcessor)5 FlagDecisionNotificationBuilder (com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder)5 DecisionMetadata (com.optimizely.ab.event.internal.payload.DecisionMetadata)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)4 FeatureDecision (com.optimizely.ab.bucketing.FeatureDecision)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)2 VisibleForTesting (com.optimizely.ab.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1