use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.
the class OptimizelyTest method getFeatureVariableJSONUserInExperimentFeatureOff.
/**
* Verify that the {@link Optimizely#getFeatureVariableJSON(String, String, String, Map)}
* is called when feature is in experiment and feature enabled is false
* than default value will gets returned
*/
@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
@Test
public void getFeatureVariableJSONUserInExperimentFeatureOff() throws Exception {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
String validVariableKey = VARIABLE_JSON_PATCHED_TYPE_KEY;
String expectedString = "{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}";
String userID = "Gred";
Optimizely optimizely = optimizelyBuilder.build();
OptimizelyJSON json = optimizely.getFeatureVariableJSON(validFeatureKey, validVariableKey, userID, null);
assertTrue(compareJsonStrings(json.toString(), expectedString));
assertEquals(json.toMap().get("k1"), "v1");
assertEquals(json.toMap().get("k2"), 3.5);
assertEquals(json.toMap().get("k3"), true);
assertEquals(((Map) json.toMap().get("k4")).get("kk1"), "vv1");
assertEquals(((Map) json.toMap().get("k4")).get("kk2"), false);
assertEquals(json.getValue("k1", String.class), "v1");
assertEquals(json.getValue("k4.kk2", Boolean.class), false);
}
use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.
the class OptimizelyTest method getFeatureVariableJSONWithListenerUserInExperimentFeatureOff.
/**
* Verify that the {@link Optimizely#getFeatureVariableJSON(String, String, String, Map)}
* notification listener of getFeatureVariableString is called when feature is in experiment and feature enabled is false
* than default value will get returned and passing null attribute will send empty map instead of null
*/
@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
@Test
public void getFeatureVariableJSONWithListenerUserInExperimentFeatureOff() {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
isListenerCalled = false;
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
String validVariableKey = VARIABLE_JSON_PATCHED_TYPE_KEY;
String expectedString = "{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}";
String userID = "Gred";
Optimizely optimizely = optimizelyBuilder.build();
final Map<String, String> testUserAttributes = new HashMap<>();
final Map<String, String> testSourceInfo = new HashMap<>();
testSourceInfo.put(EXPERIMENT_KEY, "multivariate_experiment");
testSourceInfo.put(VARIATION_KEY, "Gred");
final Map<String, Object> testDecisionInfoMap = new HashMap<>();
testDecisionInfoMap.put(FEATURE_KEY, validFeatureKey);
testDecisionInfoMap.put(FEATURE_ENABLED, false);
testDecisionInfoMap.put(VARIABLE_KEY, validVariableKey);
testDecisionInfoMap.put(VARIABLE_TYPE, FeatureVariable.JSON_TYPE);
testDecisionInfoMap.put(VARIABLE_VALUE, parseJsonString(expectedString));
testDecisionInfoMap.put(SOURCE, FeatureDecision.DecisionSource.FEATURE_TEST.toString());
testDecisionInfoMap.put(SOURCE_INFO, testSourceInfo);
int notificationId = optimizely.addDecisionNotificationHandler(getDecisionListener(NotificationCenter.DecisionNotificationType.FEATURE_VARIABLE.toString(), userID, testUserAttributes, testDecisionInfoMap));
OptimizelyJSON json = optimizely.getFeatureVariableJSON(validFeatureKey, validVariableKey, userID, null);
assertTrue(compareJsonStrings(json.toString(), expectedString));
// Verify that listener being called
assertTrue(isListenerCalled);
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
}
use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.
the class OptimizelyUserContextTest method decideAll_allFlags_enabledFlagsOnly.
@Test
public void decideAll_allFlags_enabledFlagsOnly() {
String flagKey1 = "feature_1";
OptimizelyJSON variablesExpected1 = optimizely.getAllFeatureVariables(flagKey1, userId);
OptimizelyUserContext user = optimizely.createUserContext(userId, Collections.singletonMap("gender", "f"));
Map<String, OptimizelyDecision> decisions = user.decideAll(Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY));
assertTrue(decisions.size() == 2);
assertEquals(decisions.get(flagKey1), new OptimizelyDecision("a", true, variablesExpected1, "exp_with_audience", flagKey1, user, Collections.emptyList()));
}
use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.
the class OptimizelyUserContextTest method decisionNotification.
// notifications
@Test
public void decisionNotification() {
String flagKey = "feature_2";
String variationKey = "variation_with_traffic";
boolean enabled = true;
OptimizelyJSON variables = optimizely.getAllFeatureVariables(flagKey, userId);
String ruleKey = "exp_no_audience";
List<String> reasons = Collections.emptyList();
final Map<String, Object> testDecisionInfoMap = new HashMap<>();
testDecisionInfoMap.put(FLAG_KEY, flagKey);
testDecisionInfoMap.put(VARIATION_KEY, variationKey);
testDecisionInfoMap.put(ENABLED, enabled);
testDecisionInfoMap.put(VARIABLES, variables.toMap());
testDecisionInfoMap.put(RULE_KEY, ruleKey);
testDecisionInfoMap.put(REASONS, reasons);
Map<String, Object> attributes = Collections.singletonMap("gender", "f");
OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
optimizely.addDecisionNotificationHandler(decisionNotification -> {
Assert.assertEquals(decisionNotification.getType(), NotificationCenter.DecisionNotificationType.FLAG.toString());
Assert.assertEquals(decisionNotification.getUserId(), userId);
Assert.assertEquals(decisionNotification.getAttributes(), attributes);
Assert.assertEquals(decisionNotification.getDecisionInfo(), testDecisionInfoMap);
isListenerCalled = true;
});
isListenerCalled = false;
testDecisionInfoMap.put(DECISION_EVENT_DISPATCHED, true);
user.decide(flagKey);
assertTrue(isListenerCalled);
isListenerCalled = false;
testDecisionInfoMap.put(DECISION_EVENT_DISPATCHED, false);
user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT));
assertTrue(isListenerCalled);
}
use of com.optimizely.ab.optimizelyjson.OptimizelyJSON in project java-sdk by optimizely.
the class OptimizelyUserContextTest method decideAll_twoFlags.
@Test
public void decideAll_twoFlags() {
String flagKey1 = "feature_1";
String flagKey2 = "feature_2";
List<String> flagKeys = Arrays.asList(flagKey1, flagKey2);
OptimizelyJSON variablesExpected1 = optimizely.getAllFeatureVariables(flagKey1, userId);
OptimizelyJSON variablesExpected2 = optimizely.getAllFeatureVariables(flagKey2, userId);
OptimizelyUserContext user = optimizely.createUserContext(userId, Collections.singletonMap("gender", "f"));
Map<String, OptimizelyDecision> decisions = user.decideForKeys(flagKeys);
assertTrue(decisions.size() == 2);
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()));
}
Aggregations