Skip to content

Commit 5bc87aa

Browse files
committed
add json-api tests
1 parent f6e5b0f commit 5bc87aa

File tree

2 files changed

+234
-6
lines changed

2 files changed

+234
-6
lines changed

core-api/src/main/java/com/optimizely/ab/notification/DecisionNotification.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.optimizely.ab.OptimizelyRuntimeException;
2121
import com.optimizely.ab.bucketing.FeatureDecision;
22-
import com.optimizely.ab.config.FeatureVariable;
2322
import com.optimizely.ab.config.Variation;
2423

2524
import javax.annotation.Nonnull;
@@ -310,14 +309,17 @@ public DecisionNotification build() {
310309
throw new OptimizelyRuntimeException("featureEnabled not set");
311310
}
312311

313-
notificationType = (variableValues != null) ? NotificationCenter.DecisionNotificationType.ALL_FEATURE_VARIABLES :
314-
NotificationCenter.DecisionNotificationType.FEATURE_VARIABLE;
315312

316313
decisionInfo = new HashMap<>();
317314
decisionInfo.put(FEATURE_KEY, featureKey);
318315
decisionInfo.put(FEATURE_ENABLED, featureEnabled);
319316

320-
if (notificationType == NotificationCenter.DecisionNotificationType.FEATURE_VARIABLE) {
317+
if (variableValues != null) {
318+
notificationType = NotificationCenter.DecisionNotificationType.ALL_FEATURE_VARIABLES;
319+
decisionInfo.put(VARIABLE_VALUES, variableValues);
320+
} else {
321+
notificationType = NotificationCenter.DecisionNotificationType.FEATURE_VARIABLE;
322+
321323
if (variableKey == null) {
322324
throw new OptimizelyRuntimeException("variableKey not set");
323325
}
@@ -329,8 +331,6 @@ public DecisionNotification build() {
329331
decisionInfo.put(VARIABLE_KEY, variableKey);
330332
decisionInfo.put(VARIABLE_TYPE, variableType.toString());
331333
decisionInfo.put(VARIABLE_VALUE, variableValue);
332-
} else if (notificationType == NotificationCenter.DecisionNotificationType.ALL_FEATURE_VARIABLES) {
333-
decisionInfo.put(VARIABLE_VALUES, variableValues);
334334
}
335335

336336
SourceInfo sourceInfo = new RolloutSourceInfo();

core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,96 @@ public void getFeatureVariableDoubleWithListenerUserInExperimentFeatureOn() thro
22152215
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
22162216
}
22172217

2218+
/**
2219+
* Verify that the {@link Optimizely#getAllFeatureVariables(String, String, Map)}
2220+
* notification listener of getAllFeatureVariables is called when feature is in experiment and feature is true
2221+
*/
2222+
@Test
2223+
public void getAllFeatureVariablesWithListenerUserInExperimentFeatureOn() throws Exception {
2224+
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
2225+
isListenerCalled = false;
2226+
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
2227+
String expectedString = "{\"first_letter\":\"F\",\"rest_of_name\":\"red\",\"json_patched\":{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}}";
2228+
2229+
Optimizely optimizely = optimizelyBuilder.build();
2230+
2231+
final Map<String, String> testUserAttributes = new HashMap<>();
2232+
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
2233+
2234+
final Map<String, String> testSourceInfo = new HashMap<>();
2235+
testSourceInfo.put(EXPERIMENT_KEY, "multivariate_experiment");
2236+
testSourceInfo.put(VARIATION_KEY, "Fred");
2237+
2238+
final Map<String, Object> testDecisionInfoMap = new HashMap<>();
2239+
testDecisionInfoMap.put(FEATURE_KEY, validFeatureKey);
2240+
testDecisionInfoMap.put(FEATURE_ENABLED, true);
2241+
testDecisionInfoMap.put(VARIABLE_VALUES, expectedString);
2242+
testDecisionInfoMap.put(SOURCE, FeatureDecision.DecisionSource.FEATURE_TEST.toString());
2243+
testDecisionInfoMap.put(SOURCE_INFO, testSourceInfo);
2244+
2245+
int notificationId = optimizely.addDecisionNotificationHandler(
2246+
getDecisionListener(NotificationCenter.DecisionNotificationType.FEATURE_VARIABLES.toString(),
2247+
testUserId,
2248+
testUserAttributes,
2249+
testDecisionInfoMap));
2250+
2251+
assertEquals(optimizely.getAllFeatureVariables(
2252+
validFeatureKey,
2253+
testUserId,
2254+
Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE)).toString(),
2255+
expectedString);
2256+
2257+
// Verify that listener being called
2258+
assertTrue(isListenerCalled);
2259+
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
2260+
}
2261+
2262+
/**
2263+
* Verify that the {@link Optimizely#getAllFeatureVariables(String, String, Map)}
2264+
* notification listener of getAllFeatureVariables is called when feature is in experiment and feature enabled is false
2265+
* than default value will get returned and passing null attribute will send empty map instead of null
2266+
*/
2267+
@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
2268+
@Test
2269+
public void getAllFeatureVariablesWithListenerUserInExperimentFeatureOff() {
2270+
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
2271+
isListenerCalled = false;
2272+
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
2273+
String expectedString = "{\"first_letter\":\"H\",\"rest_of_name\":\"arry\",\"json_patched\":{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}}";
2274+
String userID = "Gred";
2275+
2276+
Optimizely optimizely = optimizelyBuilder.build();
2277+
2278+
final Map<String, String> testUserAttributes = new HashMap<>();
2279+
2280+
final Map<String, String> testSourceInfo = new HashMap<>();
2281+
testSourceInfo.put(EXPERIMENT_KEY, "multivariate_experiment");
2282+
testSourceInfo.put(VARIATION_KEY, "Gred");
2283+
2284+
final Map<String, Object> testDecisionInfoMap = new HashMap<>();
2285+
testDecisionInfoMap.put(FEATURE_KEY, validFeatureKey);
2286+
testDecisionInfoMap.put(FEATURE_ENABLED, false);
2287+
testDecisionInfoMap.put(VARIABLE_VALUES, expectedString);
2288+
testDecisionInfoMap.put(SOURCE, FeatureDecision.DecisionSource.FEATURE_TEST.toString());
2289+
testDecisionInfoMap.put(SOURCE_INFO, testSourceInfo);
2290+
2291+
int notificationId = optimizely.addDecisionNotificationHandler(
2292+
getDecisionListener(NotificationCenter.DecisionNotificationType.FEATURE_VARIABLES.toString(),
2293+
userID,
2294+
testUserAttributes,
2295+
testDecisionInfoMap));
2296+
2297+
assertEquals(optimizely.getAllFeatureVariables(
2298+
validFeatureKey,
2299+
userID,
2300+
null).toString(),
2301+
expectedString);
2302+
2303+
// Verify that listener being called
2304+
assertTrue(isListenerCalled);
2305+
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
2306+
}
2307+
22182308
/**
22192309
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
22202310
* correctly builds an endpoint url and request params
@@ -4046,6 +4136,144 @@ public void getFeatureVariableIntegerCatchesExceptionFromParsing() throws Except
40464136
assertNull(spyOptimizely.getFeatureVariableInteger(featureKey, variableKey, genericUserId));
40474137
}
40484138

4139+
/**
4140+
* Verify that the {@link Optimizely#getFeatureVariableJSON(String, String, String, Map)}
4141+
* is called when feature is in experiment and feature enabled is true
4142+
* returns variable value
4143+
*/
4144+
@Test
4145+
public void getFeatureVariableJSONUserInExperimentFeatureOn() throws Exception {
4146+
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
4147+
4148+
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
4149+
String validVariableKey = VARIABLE_JSON_PATCHED_TYPE_KEY;
4150+
String expectedString = "{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}";
4151+
4152+
Optimizely optimizely = optimizelyBuilder.build();
4153+
4154+
OptimizelyJSON json = optimizely.getFeatureVariableJSON(
4155+
validFeatureKey,
4156+
validVariableKey,
4157+
testUserId,
4158+
Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE));
4159+
4160+
assertEquals(json.toString(), expectedString);
4161+
4162+
assertEquals(json.toMap().get("k1"), "v1");
4163+
assertEquals(json.toMap().get("k2"), 3.5);
4164+
assertEquals(json.toMap().get("k3"), true);
4165+
assertEquals(json.toMap().get("k4").get("kk1"), "vv11");
4166+
assertEquals(json.toMap().get("k4").get("kk2"), false);
4167+
4168+
assertEquals(json.getValue("k1", String.class), "v1");
4169+
assertEquals(json.getValue("k1.k4", Boolean.class), "v1");
4170+
}
4171+
4172+
/**
4173+
* Verify that the {@link Optimizely#getFeatureVariableJSON(String, String, String, Map)}
4174+
* is called when feature is in experiment and feature enabled is false
4175+
* than default value will gets returned
4176+
*/
4177+
@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
4178+
@Test
4179+
public void getFeatureVariableJSONUserInExperimentFeatureOff() {
4180+
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
4181+
4182+
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
4183+
String validVariableKey = VARIABLE_JSON_PATCHED_TYPE_KEY;
4184+
String expectedString = "{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}";
4185+
String userID = "Gred";
4186+
4187+
Optimizely optimizely = optimizelyBuilder.build();
4188+
4189+
OptimizelyJSON json = optimizely.getFeatureVariableJSON(
4190+
validFeatureKey,
4191+
validVariableKey,
4192+
userID,
4193+
null);
4194+
4195+
assertEquals(json.toString(), expectedString);
4196+
4197+
assertEquals(json.toMap().get("k1"), "v1");
4198+
assertEquals(json.toMap().get("k2"), 3.5);
4199+
assertEquals(json.toMap().get("k3"), true);
4200+
assertEquals(json.toMap().get("k4").get("kk1"), "vv11");
4201+
assertEquals(json.toMap().get("k4").get("kk2"), false);
4202+
4203+
assertEquals(json.getValue("k1", String.class), "v1");
4204+
assertEquals(json.getValue("k1.k4", Boolean.class), "v1");
4205+
}
4206+
4207+
/**
4208+
* Verify that the {@link Optimizely#getFeatureVariableJSON(String, String, String, Map)}
4209+
* is called when feature is in experiment and feature enabled is true
4210+
* returns variable value
4211+
*/
4212+
@Test
4213+
public void getAllFeatureVariablesUserInExperimentFeatureOn() throws Exception {
4214+
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
4215+
4216+
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
4217+
String expectedString = "{\"first_letter\":\"F\",\"rest_of_name\":\"red\",\"json_patched\":{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}}";
4218+
4219+
Optimizely optimizely = optimizelyBuilder.build();
4220+
4221+
OptimizelyJSON json = optimizely.getAllFeatureVariables(
4222+
validFeatureKey,
4223+
testUserId,
4224+
Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE));
4225+
4226+
assertEquals(json.toString(), expectedString);
4227+
4228+
assertEquals(json.toMap().get("first_letter"), "F");
4229+
assertEquals(json.toMap().get("rest_of_name"), "red");
4230+
assertEquals(json.toMap().get("json_patched").get("k1"), "v1");
4231+
assertEquals(json.toMap().get("json_patched").get("k2"), 3.5);
4232+
assertEquals(json.toMap().get("json_patched").get("k3"), true);
4233+
assertEquals(json.toMap().get("json_patched").get("k4").get("kk1"), "vv11");
4234+
assertEquals(json.toMap().get("json_patched").get("k4").get("kk2"), false);
4235+
4236+
assertEquals(json.getValue("first_letter", String.class), "F");
4237+
assertEquals(json.getValue("json_patched.k1", String.class), "v1");
4238+
assertEquals(json.getValue("json_patched.k1.k4", Boolean.class), "v1");
4239+
}
4240+
4241+
/**
4242+
* Verify that the {@link Optimizely#getFeatureVariableJSON(String, String, String, Map)}
4243+
* is called when feature is in experiment and feature enabled is false
4244+
* than default value will gets returned
4245+
*/
4246+
@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
4247+
@Test
4248+
public void getAllFeatureVariablesUserInExperimentFeatureOff() {
4249+
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
4250+
4251+
final String validFeatureKey = FEATURE_MULTI_VARIATE_FEATURE_KEY;
4252+
String expectedString = "{\"first_letter\":\"H\",\"rest_of_name\":\"arry\",\"json_patched\":{\"k1\":\"v1\",\"k2\":3.5,\"k3\":true,\"k4\":{\"kk1\":\"vv1\",\"kk2\":false}}}";
4253+
String userID = "Gred";
4254+
4255+
Optimizely optimizely = optimizelyBuilder.build();
4256+
4257+
OptimizelyJSON json = optimizely.getAllFeatureVariables(
4258+
validFeatureKey,
4259+
userID,
4260+
null);
4261+
4262+
assertEquals(json.toString(), expectedString);
4263+
4264+
assertEquals(json.toMap().get("first_letter"), "H");
4265+
assertEquals(json.toMap().get("rest_of_name"), "arry");
4266+
assertEquals(json.toMap().get("json_patched").get("k1"), "v1");
4267+
assertEquals(json.toMap().get("json_patched").get("k2"), 3.5);
4268+
assertEquals(json.toMap().get("json_patched").get("k3"), true);
4269+
assertEquals(json.toMap().get("json_patched").get("k4").get("kk1"), "vv11");
4270+
assertEquals(json.toMap().get("json_patched").get("k4").get("kk2"), false);
4271+
4272+
assertEquals(json.getValue("first_letter", String.class), "H");
4273+
assertEquals(json.getValue("json_patched.k1", String.class), "v1");
4274+
assertEquals(json.getValue("json_patched.k1.k4", Boolean.class), "v1");
4275+
}
4276+
40494277
/**
40504278
* Verify that {@link Optimizely#getVariation(String, String)} returns a variation when given an experiment
40514279
* with no audiences and no user attributes and verify that listener is getting called.

0 commit comments

Comments
 (0)