Skip to content

Commit 1a63d81

Browse files
author
Vignesh Raja
authored
Merge pull request #51 from optimizely/devel
Add live variables
2 parents d399b32 + be4b2b0 commit 1a63d81

File tree

4 files changed

+236
-1
lines changed

4 files changed

+236
-1
lines changed

CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
# Optimizely Android X SDK Changelog
2+
### 0.3.0
3+
December 8, 2016
4+
5+
*New Features*
6+
7+
- Add support for live variables
8+
9+
### 0.2.2
10+
November 30, 2016
11+
*Bug Fixes*
12+
- Update to java-core 1.0.3 which fixes crashes with Jackson annotations on ICS
13+
- Use the old SQLiteDatabse constructor for compatibility with ICS
14+
15+
*Breaking Changes*
16+
- Changed the initialization call from `start` to `initialize`
17+
- `getOptimizely` now only returns the cached version of the client
18+
219
### 0.2.1
320
November 4, 2016
421

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

+145
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.slf4j.Logger;
2929

30+
import java.util.Collections;
3031
import java.util.Map;
3132

3233
/**
@@ -165,6 +166,150 @@ public void track(@NonNull String eventName,
165166
}
166167
}
167168

169+
/**
170+
* Get the value of a String live variable
171+
* @param variableKey the String key for the variable
172+
* @param activateExperiment the flag denoting whether to activate an experiment or not
173+
* @param userId the user ID
174+
* @return String value of the live variable
175+
*/
176+
public @Nullable String getVariableString(@NonNull String variableKey,
177+
boolean activateExperiment,
178+
@NonNull String userId) {
179+
return getVariableString(variableKey, activateExperiment, userId,
180+
Collections.<String, String>emptyMap());
181+
}
182+
183+
/**
184+
* Get the value of a String live variable
185+
* @param variableKey the String key for the variable
186+
* @param activateExperiment the flag denoting whether to activate an experiment or not
187+
* @param userId the user ID
188+
* @param attributes a map of attributes about the user
189+
* @return String value of the live variable
190+
*/
191+
public @Nullable String getVariableString(@NonNull String variableKey,
192+
boolean activateExperiment,
193+
@NonNull String userId,
194+
@NonNull Map<String, String> attributes) {
195+
if (optimizely != null) {
196+
return optimizely.getVariableString(variableKey, activateExperiment, userId,
197+
attributes);
198+
} else {
199+
logger.warn("Optimizely is not initialized, could not get live variable {} " +
200+
"for user {}", variableKey, userId);
201+
return null;
202+
}
203+
}
204+
205+
/**
206+
* Get the value of a Boolean live variable
207+
* @param variableKey the String key for the variable
208+
* @param activateExperiment the flag denoting whether to activate an experiment or not
209+
* @param userId the user ID
210+
* @return Boolean value of the live variable
211+
*/
212+
public @Nullable Boolean getVariableBoolean(@NonNull String variableKey,
213+
boolean activateExperiment,
214+
@NonNull String userId) {
215+
return getVariableBoolean(variableKey, activateExperiment, userId,
216+
Collections.<String, String>emptyMap());
217+
}
218+
219+
/**
220+
* Get the value of a Boolean live variable
221+
* @param variableKey the String key for the variable
222+
* @param activateExperiment the flag denoting whether to activate an experiment or not
223+
* @param userId the user ID
224+
* @param attributes a map of attributes about the user
225+
* @return Boolean value of the live variable
226+
*/
227+
public @Nullable Boolean getVariableBoolean(@NonNull String variableKey,
228+
boolean activateExperiment,
229+
@NonNull String userId,
230+
@NonNull Map<String, String> attributes) {
231+
if (optimizely != null) {
232+
return optimizely.getVariableBoolean(variableKey, activateExperiment, userId,
233+
attributes);
234+
} else {
235+
logger.warn("Optimizely is not initialized, could not get live variable {} " +
236+
"for user {}", variableKey, userId);
237+
return null;
238+
}
239+
}
240+
241+
/**
242+
* Get the value of a Integer live variable
243+
* @param variableKey the String key for the variable
244+
* @param activateExperiment the flag denoting whether to activate an experiment or not
245+
* @param userId the user ID
246+
* @return Integer value of the live variable
247+
*/
248+
public @Nullable Integer getVariableInteger(@NonNull String variableKey,
249+
boolean activateExperiment,
250+
@NonNull String userId) {
251+
return getVariableInteger(variableKey, activateExperiment, userId,
252+
Collections.<String, String>emptyMap());
253+
}
254+
255+
/**
256+
* Get the value of a Integer live variable
257+
* @param variableKey the String key for the variable
258+
* @param activateExperiment the flag denoting whether to activate an experiment or not
259+
* @param userId the user ID
260+
* @param attributes a map of attributes about the user
261+
* @return Integer value of the live variable
262+
*/
263+
public @Nullable Integer getVariableInteger(@NonNull String variableKey,
264+
boolean activateExperiment,
265+
@NonNull String userId,
266+
@NonNull Map<String, String> attributes) {
267+
if (optimizely != null) {
268+
return optimizely.getVariableInteger(variableKey, activateExperiment, userId,
269+
attributes);
270+
} else {
271+
logger.warn("Optimizely is not initialized, could not get live variable {} " +
272+
"for user {}", variableKey, userId);
273+
return null;
274+
}
275+
}
276+
277+
/**
278+
* Get the value of a Float live variable
279+
* @param variableKey the String key for the variable
280+
* @param activateExperiment the flag denoting whether to activate an experiment or not
281+
* @param userId the user ID
282+
* @return Float value of the live variable
283+
*/
284+
public @Nullable Float getVariableFloat(@NonNull String variableKey,
285+
boolean activateExperiment,
286+
@NonNull String userId) {
287+
return getVariableFloat(variableKey, activateExperiment, userId,
288+
Collections.<String, String>emptyMap());
289+
}
290+
291+
/**
292+
* Get the value of a Float live variable
293+
* @param variableKey the String key for the variable
294+
* @param activateExperiment the flag denoting whether to activate an experiment or not
295+
* @param userId the user ID
296+
* @param attributes a map of attributes about the user
297+
* @return Float value of the live variable
298+
*/
299+
public @Nullable Float getVariableFloat(@NonNull String variableKey,
300+
boolean activateExperiment,
301+
@NonNull String userId,
302+
@NonNull Map<String, String> attributes) {
303+
if (optimizely != null) {
304+
return optimizely.getVariableFloat(variableKey, activateExperiment, userId,
305+
attributes);
306+
} else {
307+
logger.warn("Optimizely is not initialized, could not get live variable {} " +
308+
"for user {}", variableKey, userId);
309+
return null;
310+
}
311+
}
312+
168313
/**
169314
* Get the variation the user is bucketed into
170315
* @see Optimizely#getVariation(Experiment, String)

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

+73
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.mockito.runners.MockitoJUnitRunner;
2525
import org.slf4j.Logger;
2626

27+
import java.util.Collections;
2728
import java.util.HashMap;
2829

2930
import static junit.framework.Assert.assertTrue;
@@ -177,4 +178,76 @@ public void testIsInvalid() {
177178
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
178179
assertFalse(optimizelyClient.isValid());
179180
}
181+
182+
@Test
183+
public void testGoodGetVariableString() {
184+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
185+
optimizelyClient.getVariableString("test_key", true, "userId",
186+
Collections.<String, String>emptyMap());
187+
verify(optimizely).getVariableString("test_key", true, "userId",
188+
Collections.<String, String>emptyMap());
189+
}
190+
191+
@Test
192+
public void testBadGetVariableString() {
193+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
194+
optimizelyClient.getVariableString("test_key", true, "userId",
195+
Collections.<String, String>emptyMap());
196+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
197+
"for user {}", "test_key", "userId");
198+
}
199+
200+
@Test
201+
public void testGoodGetVariableBoolean() {
202+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
203+
optimizelyClient.getVariableBoolean("test_key", true, "userId",
204+
Collections.<String, String>emptyMap());
205+
verify(optimizely).getVariableBoolean("test_key", true, "userId",
206+
Collections.<String, String>emptyMap());
207+
}
208+
209+
@Test
210+
public void testBadGetVariableBoolean() {
211+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
212+
optimizelyClient.getVariableBoolean("test_key", true, "userId",
213+
Collections.<String, String>emptyMap());
214+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
215+
"for user {}", "test_key", "userId");
216+
}
217+
218+
@Test
219+
public void testGoodGetVariableInteger() {
220+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
221+
optimizelyClient.getVariableInteger("test_key", true, "userId",
222+
Collections.<String, String>emptyMap());
223+
verify(optimizely).getVariableInteger("test_key", true, "userId",
224+
Collections.<String, String>emptyMap());
225+
}
226+
227+
@Test
228+
public void testBadGetVariableInteger() {
229+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
230+
optimizelyClient.getVariableInteger("test_key", true, "userId",
231+
Collections.<String, String>emptyMap());
232+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
233+
"for user {}", "test_key", "userId");
234+
}
235+
236+
@Test
237+
public void testGoodGetVariableFloat() {
238+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
239+
optimizelyClient.getVariableFloat("test_key", true, "userId",
240+
Collections.<String, String>emptyMap());
241+
verify(optimizely).getVariableFloat("test_key", true, "userId",
242+
Collections.<String, String>emptyMap());
243+
}
244+
245+
@Test
246+
public void testBadGetVariableFloat() {
247+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
248+
optimizelyClient.getVariableFloat("test_key", true, "userId",
249+
Collections.<String, String>emptyMap());
250+
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
251+
"for user {}", "test_key", "userId");
252+
}
180253
}

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ext {
5252
min_sdk_version = 10
5353
target_sdk_version = 24
5454

55-
java_core_ver = "1.0.3"
55+
java_core_ver = "1.1.0"
5656
android_logger_ver = "1.3.1"
5757
support_annotations_ver = "24.2.1"
5858
junit_ver = "4.12"

0 commit comments

Comments
 (0)