Skip to content

Commit e4c140a

Browse files
bring back getVariable(String,Integer,Double,Boolean) (#200)
* bring back getVariable(String,Integer,Double,Boolean) * remove throws * revert * check for valid input to getVariable calls * log error on invalid user id or variable key for getVariable
1 parent 925e570 commit e4c140a

File tree

3 files changed

+348
-0
lines changed

3 files changed

+348
-0
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,4 +1736,76 @@ public void testBadGetFeatureVariableString() {
17361736
GENERIC_USER_ID
17371737
);
17381738
}
1739+
1740+
@Test
1741+
public void testGoodGetVariableString() {
1742+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V3.toString()));
1743+
1744+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
1745+
String v = optimizelyClient.getVariableString("test_variable", "userId",
1746+
Collections.<String, String>emptyMap(), true);
1747+
assertEquals("true", v);
1748+
}
1749+
1750+
@Test
1751+
public void testBadGetVariableString() {
1752+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
1753+
optimizelyClient.getVariableString("test_key", "userId",
1754+
Collections.<String, String>emptyMap(), true);
1755+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
1756+
"for user {}", "test_key", "userId");
1757+
}
1758+
1759+
@Test
1760+
public void testGoodGetVariableBoolean() {
1761+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V3.toString()));
1762+
1763+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
1764+
Boolean b = optimizelyClient.getVariableBoolean("test_variable", "userId",
1765+
Collections.<String, String>emptyMap(), true);
1766+
assertEquals(new Boolean(true),b);
1767+
}
1768+
1769+
@Test
1770+
public void testBadGetVariableBoolean() {
1771+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
1772+
optimizelyClient.getVariableBoolean("test_key", "userId",
1773+
Collections.<String, String>emptyMap(), true);
1774+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
1775+
"for user {}", "test_key", "userId");
1776+
}
1777+
1778+
@Test
1779+
public void testGoodGetVariableInteger() {
1780+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
1781+
Integer i = optimizelyClient.getVariableInteger("test_variable", "userId",
1782+
Collections.<String, String>emptyMap(), true);
1783+
assertNull(i);
1784+
}
1785+
1786+
@Test
1787+
public void testBadGetVariableInteger() {
1788+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
1789+
optimizelyClient.getVariableInteger("test_key", "userId",
1790+
Collections.<String, String>emptyMap(), true);
1791+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
1792+
"for user {}", "test_key", "userId");
1793+
}
1794+
1795+
@Test
1796+
public void testGoodGetVariableDouble() {
1797+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
1798+
Double v = optimizelyClient.getVariableDouble("test_variable", "userId",
1799+
Collections.<String, String>emptyMap(), true);
1800+
assertNull(v);
1801+
}
1802+
1803+
@Test
1804+
public void testBadGetVariableDouble() {
1805+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
1806+
optimizelyClient.getVariableDouble("test_key", "userId",
1807+
Collections.<String, String>emptyMap(), true);
1808+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
1809+
"for user {}", "test_key", "userId");
1810+
}
17391811
}

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyClient.java

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222

2323
import com.optimizely.ab.Optimizely;
2424
import com.optimizely.ab.UnknownEventTypeException;
25+
import com.optimizely.ab.UnknownLiveVariableException;
2526
import com.optimizely.ab.config.Experiment;
27+
import com.optimizely.ab.config.LiveVariable;
28+
import com.optimizely.ab.config.LiveVariableUsageInstance;
2629
import com.optimizely.ab.config.ProjectConfig;
2730
import com.optimizely.ab.config.Variation;
31+
import com.optimizely.ab.error.NoOpErrorHandler;
32+
import com.optimizely.ab.error.RaiseExceptionErrorHandler;
2833
import com.optimizely.ab.notification.NotificationCenter;
2934

3035
import org.slf4j.Logger;
3136

37+
import java.util.Collections;
3238
import java.util.HashMap;
3339
import java.util.List;
3440
import java.util.Map;
@@ -551,4 +557,237 @@ public NotificationCenter getNotificationCenter() {
551557

552558
return null;
553559
}
560+
561+
//======== live variable getters ========//
562+
/**
563+
* Helper method to retrieve the {@link LiveVariable} for the given variable key.
564+
* If {@link RaiseExceptionErrorHandler} is provided, either a live variable is returned, or an exception is
565+
* thrown.
566+
* If {@link NoOpErrorHandler} is used, either a live variable or {@code null} is returned.
567+
*
568+
* @param projectConfig the current project config
569+
* @param variableKey the key for the live variable being retrieved from the current project config
570+
* @return the live variable to retrieve for the given variable key
571+
*
572+
*/
573+
private LiveVariable getLiveVariable(ProjectConfig projectConfig, String variableKey) {
574+
575+
LiveVariable liveVariable = projectConfig
576+
.getLiveVariableKeyMapping()
577+
.get(variableKey);
578+
579+
if (liveVariable == null) {
580+
String unknownLiveVariableKeyError =
581+
String.format("Live variable \"%s\" is not in the datafile.", variableKey);
582+
logger.error(unknownLiveVariableKeyError);
583+
return null;
584+
585+
}
586+
587+
return liveVariable;
588+
}
589+
590+
/**
591+
* Get the value of a String live variable
592+
* @param variableKey the String key for the variable
593+
* @param userId the user ID
594+
* @param activateExperiment the flag denoting whether to activate an experiment or not
595+
* @return String value of the live variable
596+
*/
597+
@Deprecated
598+
public @Nullable
599+
String getVariableString(@NonNull String variableKey,
600+
@NonNull String userId,
601+
boolean activateExperiment) {
602+
return getVariableString(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
603+
}
604+
605+
/**
606+
* Get the value of a String live variable
607+
* @param variableKey the String key for the variable
608+
* @param userId the user ID
609+
* @param attributes a map of attributes about the user
610+
* @param activateExperiment the flag denoting whether to activate an experiment or not
611+
* @return String value of the live variable
612+
*/
613+
@Deprecated
614+
public @Nullable
615+
String getVariableString(@NonNull String variableKey,
616+
@NonNull String userId,
617+
@NonNull Map<String, String> attributes,
618+
boolean activateExperiment) {
619+
620+
if (!isValid()) {
621+
logger.warn("Optimizely is not initialized, could not get live variable {} " +
622+
"for user {}", variableKey, userId);
623+
return null;
624+
}
625+
626+
if (variableKey == null || variableKey.isEmpty()) {
627+
logger.error("Invalid live variable key (null or empty) " +
628+
"for user {}", userId);
629+
return null;
630+
}
631+
632+
if (userId == null || userId.isEmpty()) {
633+
logger.error("Invalid userId (null or empty) " +
634+
"for live variable {}", variableKey);
635+
return null;
636+
}
637+
638+
LiveVariable variable = getLiveVariable(optimizely.getProjectConfig(), variableKey);
639+
if (variable == null) {
640+
return null;
641+
}
642+
643+
List<Experiment> experimentsUsingLiveVariable =
644+
optimizely.getProjectConfig().getLiveVariableIdToExperimentsMapping().get(variable.getId());
645+
Map<String, Map<String, LiveVariableUsageInstance>> variationToLiveVariableUsageInstanceMapping =
646+
optimizely.getProjectConfig().getVariationToLiveVariableUsageInstanceMapping();
647+
648+
if (experimentsUsingLiveVariable == null) {
649+
logger.warn("No experiment is using variable \"{}\".", variable.getKey());
650+
return variable.getDefaultValue();
651+
}
652+
653+
for (Experiment experiment : experimentsUsingLiveVariable) {
654+
Variation variation;
655+
if (activateExperiment) {
656+
variation = activate(experiment.getKey(), userId, attributes);
657+
} else {
658+
variation = getVariation(experiment.getKey(), userId, attributes);
659+
}
660+
661+
if (variation != null) {
662+
LiveVariableUsageInstance usageInstance =
663+
variationToLiveVariableUsageInstanceMapping.get(variation.getId()).get(variable.getId());
664+
return usageInstance.getValue();
665+
}
666+
}
667+
668+
return variable.getDefaultValue();
669+
}
670+
671+
/**
672+
* Get the value of a Boolean live variable
673+
* @param variableKey the String key for the variable
674+
* @param userId the user ID
675+
* @param activateExperiment the flag denoting whether to activate an experiment or not
676+
* @return Boolean value of the live variable
677+
*/
678+
@Deprecated
679+
public @Nullable
680+
Boolean getVariableBoolean(@NonNull String variableKey,
681+
@NonNull String userId,
682+
boolean activateExperiment) {
683+
return getVariableBoolean(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
684+
}
685+
686+
/**
687+
* Get the value of a Boolean live variable
688+
* @param variableKey the String key for the variable
689+
* @param userId the user ID
690+
* @param attributes a map of attributes about the user
691+
* @param activateExperiment the flag denoting whether to activate an experiment or not
692+
* @return Boolean value of the live variable
693+
*/
694+
@Deprecated
695+
public @Nullable
696+
Boolean getVariableBoolean(@NonNull String variableKey,
697+
@NonNull String userId,
698+
@NonNull Map<String, String> attributes,
699+
boolean activateExperiment) {
700+
701+
String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment);
702+
if (variableValueString != null) {
703+
return Boolean.parseBoolean(variableValueString);
704+
}
705+
706+
return null;
707+
}
708+
709+
/**
710+
* Get the value of a Integer live variable
711+
* @param variableKey the String key for the variable
712+
* @param userId the user ID
713+
* @param activateExperiment the flag denoting whether to activate an experiment or not
714+
* @return Integer value of the live variable
715+
*/
716+
@Deprecated
717+
public @Nullable
718+
Integer getVariableInteger(@NonNull String variableKey,
719+
@NonNull String userId,
720+
boolean activateExperiment) {
721+
return getVariableInteger(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
722+
}
723+
724+
/**
725+
* Get the value of a Integer live variable
726+
* @param variableKey the String key for the variable
727+
* @param userId the user ID
728+
* @param attributes a map of attributes about the user
729+
* @param activateExperiment the flag denoting whether to activate an experiment or not* @return Integer value of the live variable
730+
*/
731+
@Deprecated
732+
public @Nullable
733+
Integer getVariableInteger(@NonNull String variableKey,
734+
@NonNull String userId,
735+
@NonNull Map<String, String> attributes,
736+
boolean activateExperiment) {
737+
738+
String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment);
739+
if (variableValueString != null) {
740+
try {
741+
return Integer.parseInt(variableValueString);
742+
} catch (NumberFormatException e) {
743+
logger.error("Variable value \"{}\" for live variable \"{}\" is not an integer.", variableValueString,
744+
variableKey);
745+
}
746+
}
747+
748+
return null;
749+
}
750+
751+
/**
752+
* Get the value of a Double live variable
753+
* @param variableKey the String key for the variable
754+
* @param userId the user ID
755+
* @param activateExperiment the flag denoting whether to activate an experiment or not
756+
* @return Double value of the live variable
757+
*/
758+
@Deprecated
759+
public @Nullable
760+
Double getVariableDouble(@NonNull String variableKey,
761+
@NonNull String userId,
762+
boolean activateExperiment) {
763+
return getVariableDouble(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment);
764+
}
765+
766+
/**
767+
* Get the value of a Double live variable
768+
* @param variableKey the String key for the variable
769+
* @param userId the user ID
770+
* @param attributes a map of attributes about the user
771+
* @param activateExperiment the flag denoting whether to activate an experiment or not
772+
* @return Double value of the live variable
773+
*/
774+
@Deprecated
775+
public @Nullable
776+
Double getVariableDouble(@NonNull String variableKey,
777+
@NonNull String userId,
778+
@NonNull Map<String, String> attributes,
779+
boolean activateExperiment) {
780+
781+
String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment);
782+
if (variableValueString != null) {
783+
try {
784+
return Double.parseDouble(variableValueString);
785+
} catch (NumberFormatException e) {
786+
logger.error("Variable value \"{}\" for live variable \"{}\" is not a double.", variableValueString,
787+
variableKey);
788+
}
789+
}
790+
791+
return null;
792+
}
554793
}

android-sdk/src/test/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,41 @@ public void testBadClearNotificationCenterListeners() {
296296
verify(logger).warn("Optimizely is not initialized, could not get the notification listener");
297297
}
298298

299+
@Test
300+
public void testBadGetVariableString() {
301+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
302+
optimizelyClient.getVariableString("test_key", "userId",
303+
Collections.<String, String>emptyMap(), true);
304+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
305+
"for user {}", "test_key", "userId");
306+
}
307+
308+
@Test
309+
public void testBadGetVariableBoolean() {
310+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
311+
optimizelyClient.getVariableBoolean("test_key", "userId",
312+
Collections.<String, String>emptyMap(), true);
313+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
314+
"for user {}", "test_key", "userId");
315+
}
316+
317+
@Test
318+
public void testBadGetVariableInteger() {
319+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
320+
optimizelyClient.getVariableInteger("test_key", "userId",
321+
Collections.<String, String>emptyMap(), true);
322+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
323+
"for user {}", "test_key", "userId");
324+
}
325+
326+
@Test
327+
public void testBadGetVariableDouble() {
328+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
329+
optimizelyClient.getVariableDouble("test_key", "userId",
330+
Collections.<String, String>emptyMap(), true);
331+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
332+
"for user {}", "test_key", "userId");
333+
}
334+
335+
299336
}

0 commit comments

Comments
 (0)