Skip to content

Add live variables #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Optimizely Android X SDK Changelog
### 0.3.0
December 8, 2016

*New Features*

- Add support for live variables

### 0.2.2
November 30, 2016
*Bug Fixes*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.slf4j.Logger;

import java.util.Collections;
import java.util.Map;

/**
Expand Down Expand Up @@ -165,6 +166,150 @@ public void track(@NonNull String eventName,
}
}

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

/**
* Get the value of a String live variable
* @param variableKey the String key for the variable
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @param userId the user ID
* @param attributes a map of attributes about the user
* @return String value of the live variable
*/
public @Nullable String getVariableString(@NonNull String variableKey,
boolean activateExperiment,
@NonNull String userId,
@NonNull Map<String, String> attributes) {
if (optimizely != null) {
return optimizely.getVariableString(variableKey, activateExperiment, userId,
attributes);
} else {
logger.warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", variableKey, userId);
return null;
}
}

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

/**
* Get the value of a Boolean live variable
* @param variableKey the String key for the variable
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @param userId the user ID
* @param attributes a map of attributes about the user
* @return Boolean value of the live variable
*/
public @Nullable Boolean getVariableBoolean(@NonNull String variableKey,
boolean activateExperiment,
@NonNull String userId,
@NonNull Map<String, String> attributes) {
if (optimizely != null) {
return optimizely.getVariableBoolean(variableKey, activateExperiment, userId,
attributes);
} else {
logger.warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", variableKey, userId);
return null;
}
}

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

/**
* Get the value of a Integer live variable
* @param variableKey the String key for the variable
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @param userId the user ID
* @param attributes a map of attributes about the user
* @return Integer value of the live variable
*/
public @Nullable Integer getVariableInteger(@NonNull String variableKey,
boolean activateExperiment,
@NonNull String userId,
@NonNull Map<String, String> attributes) {
if (optimizely != null) {
return optimizely.getVariableInteger(variableKey, activateExperiment, userId,
attributes);
} else {
logger.warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", variableKey, userId);
return null;
}
}

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

/**
* Get the value of a Float live variable
* @param variableKey the String key for the variable
* @param activateExperiment the flag denoting whether to activate an experiment or not
* @param userId the user ID
* @param attributes a map of attributes about the user
* @return Float value of the live variable
*/
public @Nullable Float getVariableFloat(@NonNull String variableKey,
boolean activateExperiment,
@NonNull String userId,
@NonNull Map<String, String> attributes) {
if (optimizely != null) {
return optimizely.getVariableFloat(variableKey, activateExperiment, userId,
attributes);
} else {
logger.warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", variableKey, userId);
return null;
}
}

/**
* Get the variation the user is bucketed into
* @see Optimizely#getVariation(Experiment, String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;

import java.util.Collections;
import java.util.HashMap;

import static junit.framework.Assert.assertTrue;
Expand Down Expand Up @@ -177,4 +178,76 @@ public void testIsInvalid() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
assertFalse(optimizelyClient.isValid());
}

@Test
public void testGoodGetVariableString() {
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
optimizelyClient.getVariableString("test_key", true, "userId",
Collections.<String, String>emptyMap());
verify(optimizely).getVariableString("test_key", true, "userId",
Collections.<String, String>emptyMap());
}

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

@Test
public void testGoodGetVariableBoolean() {
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
optimizelyClient.getVariableBoolean("test_key", true, "userId",
Collections.<String, String>emptyMap());
verify(optimizely).getVariableBoolean("test_key", true, "userId",
Collections.<String, String>emptyMap());
}

@Test
public void testBadGetVariableBoolean() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableBoolean("test_key", true, "userId",
Collections.<String, String>emptyMap());
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);
optimizelyClient.getVariableInteger("test_key", true, "userId",
Collections.<String, String>emptyMap());
verify(optimizely).getVariableInteger("test_key", true, "userId",
Collections.<String, String>emptyMap());
}

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

@Test
public void testGoodGetVariableFloat() {
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
optimizelyClient.getVariableFloat("test_key", true, "userId",
Collections.<String, String>emptyMap());
verify(optimizely).getVariableFloat("test_key", true, "userId",
Collections.<String, String>emptyMap());
}

@Test
public void testBadGetVariableFloat() {
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariableFloat("test_key", true, "userId",
Collections.<String, String>emptyMap());
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
"for user {}", "test_key", "userId");
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ext {
min_sdk_version = 10
target_sdk_version = 24

java_core_ver = "1.0.3"
java_core_ver = "1.1.0"
android_logger_ver = "1.3.1"
support_annotations_ver = "24.2.1"
junit_ver = "4.12"
Expand Down