Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1736,4 +1736,76 @@ public void testBadGetFeatureVariableString() {
GENERIC_USER_ID
);
}

@Test
public void testGoodGetVariableString() {
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V3.toString()));

OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
String v = optimizelyClient.getVariableString("test_variable", "userId",
Collections.<String, String>emptyMap(), true);
assertEquals("true", v);
}

@Test
public void testBadGetVariableString() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableString("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}

@Test
public void testGoodGetVariableBoolean() {
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V3.toString()));

OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
Boolean b = optimizelyClient.getVariableBoolean("test_variable", "userId",
Collections.<String, String>emptyMap(), true);
assertEquals(new Boolean(true),b);
}

@Test
public void testBadGetVariableBoolean() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableBoolean("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}

@Test
public void testGoodGetVariableInteger() {
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
Integer i = optimizelyClient.getVariableInteger("test_variable", "userId",
Collections.<String, String>emptyMap(), true);
assertNull(i);
}

@Test
public void testBadGetVariableInteger() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableInteger("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}

@Test
public void testGoodGetVariableDouble() {
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
Double v = optimizelyClient.getVariableDouble("test_variable", "userId",
Collections.<String, String>emptyMap(), true);
assertNull(v);
}

@Test
public void testBadGetVariableDouble() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableDouble("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@

import com.optimizely.ab.Optimizely;
import com.optimizely.ab.UnknownEventTypeException;
import com.optimizely.ab.UnknownLiveVariableException;
import com.optimizely.ab.config.Experiment;
import com.optimizely.ab.config.LiveVariable;
import com.optimizely.ab.config.LiveVariableUsageInstance;
import com.optimizely.ab.config.ProjectConfig;
import com.optimizely.ab.config.Variation;
import com.optimizely.ab.error.NoOpErrorHandler;
import com.optimizely.ab.error.RaiseExceptionErrorHandler;
import com.optimizely.ab.notification.NotificationCenter;

import org.slf4j.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -551,4 +557,226 @@ public NotificationCenter getNotificationCenter() {

return null;
}

//======== live variable getters ========//
/**
* Helper method to retrieve the {@link LiveVariable} for the given variable key.
* If {@link RaiseExceptionErrorHandler} is provided, either a live variable is returned, or an exception is
* thrown.
* If {@link NoOpErrorHandler} is used, either a live variable or {@code null} is returned.
*
* @param projectConfig the current project config
* @param variableKey the key for the live variable being retrieved from the current project config
* @return the live variable to retrieve for the given variable key
*
*/
private LiveVariable getLiveVariable(ProjectConfig projectConfig, String variableKey) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should null-check the variableKey since it is a user-provided input or we can do it at a higher level, like the caller of this function


LiveVariable liveVariable = projectConfig
.getLiveVariableKeyMapping()
.get(variableKey);

if (liveVariable == null) {
String unknownLiveVariableKeyError =
String.format("Live variable \"%s\" is not in the datafile.", variableKey);
logger.error(unknownLiveVariableKeyError);
return null;

}

return liveVariable;
}

/**
* Get the value of a String live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @return String value of the live variable
*/
@Deprecated
public @Nullable
String getVariableString(@NonNull String variableKey,
@NonNull String userId,
boolean activateExperiment) {
return getVariableString(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
}

/**
* Get the value of a String live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param attributes a map of attributes about the user
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @return String value of the live variable
*/
@Deprecated
public @Nullable
String getVariableString(@NonNull String variableKey,
@NonNull String userId,
@NonNull Map<String, String> attributes,
boolean activateExperiment) {

if (!isValid()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's null-check the userId and variableKey input params here

logger.warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", variableKey, userId);
return null;
}


LiveVariable variable = getLiveVariable(optimizely.getProjectConfig(), variableKey);
if (variable == null) {
return null;
}

List<Experiment> experimentsUsingLiveVariable =
optimizely.getProjectConfig().getLiveVariableIdToExperimentsMapping().get(variable.getId());
Map<String, Map<String, LiveVariableUsageInstance>> variationToLiveVariableUsageInstanceMapping =
optimizely.getProjectConfig().getVariationToLiveVariableUsageInstanceMapping();

if (experimentsUsingLiveVariable == null) {
logger.warn("No experiment is using variable \"{}\".", variable.getKey());
return variable.getDefaultValue();
}

for (Experiment experiment : experimentsUsingLiveVariable) {
Variation variation;
if (activateExperiment) {
variation = activate(experiment.getKey(), userId, attributes);
} else {
variation = getVariation(experiment.getKey(), userId, attributes);
}

if (variation != null) {
LiveVariableUsageInstance usageInstance =
variationToLiveVariableUsageInstanceMapping.get(variation.getId()).get(variable.getId());
return usageInstance.getValue();
}
}

return variable.getDefaultValue();
}

/**
* Get the value of a Boolean live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @return Boolean value of the live variable
*/
@Deprecated
public @Nullable
Boolean getVariableBoolean(@NonNull String variableKey,
@NonNull String userId,
boolean activateExperiment) {
return getVariableBoolean(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
}

/**
* Get the value of a Boolean live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param attributes a map of attributes about the user
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @return Boolean value of the live variable
*/
@Deprecated
public @Nullable
Boolean getVariableBoolean(@NonNull String variableKey,
@NonNull String userId,
@NonNull Map<String, String> attributes,
boolean activateExperiment) {

String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment);
if (variableValueString != null) {
return Boolean.parseBoolean(variableValueString);
}

return null;
}

/**
* Get the value of a Integer live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @return Integer value of the live variable
*/
@Deprecated
public @Nullable
Integer getVariableInteger(@NonNull String variableKey,
@NonNull String userId,
boolean activateExperiment) {
return getVariableInteger(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
}

/**
* Get the value of a Integer live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param attributes a map of attributes about the user
* @param activateExperiment the flag denoting whether to activate an experiment or not* @return Integer value of the live variable
*/
@Deprecated
public @Nullable
Integer getVariableInteger(@NonNull String variableKey,
@NonNull String userId,
@NonNull Map<String, String> attributes,
boolean activateExperiment) {

String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment);
if (variableValueString != null) {
try {
return Integer.parseInt(variableValueString);
} catch (NumberFormatException e) {
logger.error("Variable value \"{}\" for live variable \"{}\" is not an integer.", variableValueString,
variableKey);
}
}

return null;
}

/**
* Get the value of a Double live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @return Double value of the live variable
*/
@Deprecated
public @Nullable
Double getVariableDouble(@NonNull String variableKey,
@NonNull String userId,
boolean activateExperiment) {
return getVariableDouble(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
}

/**
* Get the value of a Double live variable
* @param variableKey the String key for the variable
* @param userId the user ID
* @param attributes a map of attributes about the user
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @return Double value of the live variable
*/
@Deprecated
public @Nullable
Double getVariableDouble(@NonNull String variableKey,
@NonNull String userId,
@NonNull Map<String, String> attributes,
boolean activateExperiment) {

String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment);
if (variableValueString != null) {
try {
return Double.parseDouble(variableValueString);
} catch (NumberFormatException e) {
logger.error("Variable value \"{}\" for live variable \"{}\" is not a double.", variableValueString,
variableKey);
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,41 @@ public void testBadClearNotificationCenterListeners() {
verify(logger).warn("Optimizely is not initialized, could not get the notification listener");
}

@Test
public void testBadGetVariableString() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableString("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}

@Test
public void testBadGetVariableBoolean() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableBoolean("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}

@Test
public void testBadGetVariableInteger() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableInteger("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}

@Test
public void testBadGetVariableDouble() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableDouble("test_key", "userId",
Collections.<String, String>emptyMap(), true);
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}


}