Skip to content

Commit e4c0913

Browse files
mnoman09mikeproeng37
authored andcommitted
Added Wrapper for getEnabledFeatures (#184)
Added its unit tests updated java version to 2.0.0-beta6 updated datafile as it was not passing all unittests of due to featureEnabled was not set
1 parent bb25e3f commit e4c0913

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.Collection;
4848
import java.util.Collections;
4949
import java.util.HashMap;
50+
import java.util.List;
5051
import java.util.Map;
5152

5253
import javax.annotation.Nonnull;
@@ -1220,6 +1221,59 @@ public void testBadIsFeatureEnabledWithoutAttribute() {
12201221
);
12211222
}
12221223

1224+
/**
1225+
* Verify {@link Optimizely#getEnabledFeatures(String, Map)} calls into
1226+
* {@link Optimizely#isFeatureEnabled(String, String, Map)} for each featureFlag
1227+
* return List of FeatureFlags that are enabled
1228+
*/
1229+
@Test
1230+
public void testGetEnabledFeaturesWithValidUserID(){
1231+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString()));
1232+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1233+
optimizely,
1234+
logger
1235+
);
1236+
List<String> enabledFeatures = optimizelyClient.getEnabledFeatures(GENERIC_USER_ID,
1237+
Collections.singletonMap("house", "Gryffindor"));
1238+
assertFalse(enabledFeatures.isEmpty());
1239+
}
1240+
1241+
/**
1242+
* Verify {@link Optimizely#getEnabledFeatures(String, Map)} calls into
1243+
* {@link Optimizely#isFeatureEnabled(String, String, Map)} for each featureFlag
1244+
* here user id is not valid because its not bucketed into any variation so it will
1245+
* return empty List of enabledFeatures
1246+
*/
1247+
@Test
1248+
public void testGetEnabledFeaturesWithInValidUserIDandValidAttributes(){
1249+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString()));
1250+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1251+
optimizely,
1252+
logger
1253+
);
1254+
List<String> enabledFeatures = optimizelyClient.getEnabledFeatures("InvalidUserID",
1255+
Collections.singletonMap("house", "Gryffindor"));
1256+
assertTrue(enabledFeatures.isEmpty());
1257+
}
1258+
1259+
1260+
/**
1261+
* Verify {@link Optimizely#getEnabledFeatures(String, Map)} calls into
1262+
* {@link Optimizely#isFeatureEnabled(String, String, Map)} for each featureFlag
1263+
* here Attributes are not valid because its not meeting any audience condition so
1264+
* return empty List of enabledFeatures
1265+
*/
1266+
@Test
1267+
public void testGetEnabledFeaturesWithValidUserIDAndInvalidAttributes(){
1268+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString()));
1269+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1270+
optimizely,
1271+
logger
1272+
);
1273+
List<String> enabledFeatures = optimizelyClient.getEnabledFeatures(GENERIC_USER_ID,
1274+
Collections.singletonMap("invalidKey", "invalidVal"));
1275+
assertTrue(enabledFeatures.isEmpty());
1276+
}
12231277
//=======Feature Variables Testing===========
12241278

12251279
/* FeatureVariableBoolean

android-sdk/src/debug/res/raw/validprojectconfigv4

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"variations": [{
103103
"id": "1880281238",
104104
"key": "Fred",
105+
"featureEnabled": true,
105106
"variables": [{
106107
"id": "675244127",
107108
"value": "F"
@@ -112,6 +113,7 @@
112113
}, {
113114
"id": "3631049532",
114115
"key": "Feorge",
116+
"featureEnabled": true,
115117
"variables": [{
116118
"id": "675244127",
117119
"value": "F"
@@ -138,6 +140,7 @@
138140
}, {
139141
"id": "2099211198",
140142
"key": "George",
143+
"featureEnabled": false,
141144
"variables": [{
142145
"id": "675244127",
143146
"value": "G"
@@ -174,13 +177,15 @@
174177
"variations": [{
175178
"id": "1505457580",
176179
"key": "pi_variation",
180+
"featureEnabled": true,
177181
"variables": [{
178182
"id": "4111654444",
179183
"value": "3.14"
180184
}]
181185
}, {
182186
"id": "119616179",
183187
"key": "euler_variation",
188+
"featureEnabled": true,
184189
"variables": [{
185190
"id": "4111654444",
186191
"value": "2.718"
@@ -320,6 +325,7 @@
320325
"variations": [{
321326
"id": "1394671166",
322327
"key": "mutex_group_2_experiment_1_variation_1",
328+
"featureEnabled": true,
323329
"variables": [{
324330
"id": "2059187672",
325331
"value": "mutex_group_2_experiment_1_variation_1"
@@ -339,6 +345,7 @@
339345
"variations": [{
340346
"id": "1619235542",
341347
"key": "mutex_group_2_experiment_2_variation_2",
348+
"featureEnabled": true,
342349
"variables": [{
343350
"id": "2059187672",
344351
"value": "mutex_group_2_experiment_2_variation_2"
@@ -449,6 +456,7 @@
449456
"variations": [{
450457
"id": "1566407342",
451458
"key": "1566407342",
459+
"featureEnabled": true,
452460
"variables": [{
453461
"id": "2077511132",
454462
"value": "lumos"
@@ -471,6 +479,7 @@
471479
"variations": [{
472480
"id": "521740985",
473481
"key": "521740985",
482+
"featureEnabled": false,
474483
"variables": [{
475484
"id": "675244127",
476485
"value": "G"
@@ -493,6 +502,7 @@
493502
"variations": [{
494503
"id": "180042646",
495504
"key": "180042646",
505+
"featureEnabled": true,
496506
"variables": [{
497507
"id": "675244127",
498508
"value": "S"
@@ -515,6 +525,7 @@
515525
"variations": [{
516526
"id": "2346257680",
517527
"key": "2346257680",
528+
"featureEnabled": true,
518529
"variables": [{
519530
"id": "675244127",
520531
"value": "D"
@@ -537,6 +548,7 @@
537548
"variations": [{
538549
"id": "3137445031",
539550
"key": "3137445031",
551+
"featureEnabled": true,
540552
"variables": [{
541553
"id": "675244127",
542554
"value": "M"
@@ -562,6 +574,7 @@
562574
"variations": [{
563575
"id": "589640735",
564576
"key": "589640735",
577+
"featureEnabled": true,
565578
"variables": []
566579
}],
567580
"trafficAllocation": [{
@@ -571,4 +584,4 @@
571584
}]
572585
}],
573586
"variables": []
574-
}
587+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.util.Collections;
3535
import java.util.HashMap;
36+
import java.util.List;
3637
import java.util.Map;
3738

3839
/**
@@ -382,6 +383,22 @@ public void clearNotificationListeners() {
382383
}
383384
}
384385

386+
/**
387+
* Get the list of features that are enabled for the user.
388+
* @param userId The ID of the user.
389+
* @param attributes The user's attributes.
390+
* @return List of the feature keys that are enabled for the user if the userId is empty it will
391+
* return Empty List.
392+
*/
393+
public List<String> getEnabledFeatures(@NonNull String userId, @NonNull Map<String, String> attributes) {
394+
if (isValid()) {
395+
return optimizely.getEnabledFeatures(userId, attributes);
396+
} else {
397+
logger.warn("Optimizely is not initialized, could not get enabled feature for user {}",
398+
userId);
399+
return null;
400+
}
401+
}
385402
//======== FeatureFlag APIs ========//
386403

387404
/**

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ext {
5353
build_tools_version = "27.0.0"
5454
min_sdk_version = 10
5555
target_sdk_version = 26
56-
java_core_ver = "2.0.0-beta4"
56+
java_core_ver = "2.0.0-beta6"
5757
android_logger_ver = "1.3.6"
5858
support_annotations_ver = "24.2.1"
5959
junit_ver = "4.12"

0 commit comments

Comments
 (0)