Skip to content

Commit 8e3dafc

Browse files
mnoman09mikeproeng37
authored andcommitted
fix: Allowing Empty UserID as valid field (#220)
1 parent 1821874 commit 8e3dafc

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,6 @@ private boolean validateUserId(String userId) {
819819
logger.error("The user ID parameter must be nonnull.");
820820
return false;
821821
}
822-
if (userId.trim().isEmpty()) {
823-
logger.error("Non-empty user ID required");
824-
return false;
825-
}
826822

827823
return true;
828824
}

core-api/src/main/java/com/optimizely/ab/config/ProjectConfig.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,7 @@ public boolean setForcedVariation(@Nonnull String experimentKey,
496496
}
497497

498498
// if the user id is invalid, return false.
499-
if (userId == null || userId.trim().isEmpty()) {
500-
logger.error("User ID is invalid");
499+
if (!validateUserId(userId)) {
501500
return false;
502501
}
503502

@@ -554,8 +553,7 @@ public boolean setForcedVariation(@Nonnull String experimentKey,
554553
@Nonnull String userId) {
555554

556555
// if the user id is invalid, return false.
557-
if (userId == null || userId.trim().isEmpty()) {
558-
logger.error("User ID is invalid");
556+
if (!validateUserId(userId)) {
559557
return null;
560558
}
561559

@@ -587,6 +585,21 @@ public boolean setForcedVariation(@Nonnull String experimentKey,
587585
return null;
588586
}
589587

588+
/**
589+
* Helper function to check that the provided userId is valid
590+
*
591+
* @param userId the userId being validated
592+
* @return whether the user ID is valid
593+
*/
594+
private boolean validateUserId(String userId) {
595+
if (userId == null) {
596+
logger.error("User ID is invalid");
597+
return false;
598+
}
599+
600+
return true;
601+
}
602+
590603
@Override
591604
public String toString() {
592605
return "ProjectConfig{" +

core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ public void activateUserNoAttributesWithAudiences() throws Exception {
13851385
}
13861386

13871387
/**
1388-
* Verify that {@link Optimizely#activate(String, String)} doesn't return a variation when provided an empty string.
1388+
* Verify that {@link Optimizely#activate(String, String)} return a variation when provided an empty string.
13891389
*/
13901390
@Test
13911391
public void activateWithEmptyUserId() throws Exception {
@@ -1397,9 +1397,7 @@ public void activateWithEmptyUserId() throws Exception {
13971397
.withErrorHandler(new RaiseExceptionErrorHandler())
13981398
.build();
13991399

1400-
logbackVerifier.expectMessage(Level.ERROR, "Non-empty user ID required");
1401-
logbackVerifier.expectMessage(Level.INFO, "Not activating user for experiment \"" + experimentKey + "\".");
1402-
assertNull(optimizely.activate(experimentKey, ""));
1400+
assertNotNull(optimizely.activate(experimentKey, ""));
14031401
}
14041402

14051403
/**
@@ -2695,7 +2693,7 @@ public void getVariationWithUnknownExperimentKeyAndRaiseExceptionErrorHandler()
26952693
}
26962694

26972695
/**
2698-
* Verify that {@link Optimizely#getVariation(String, String)} doesn't return a variation when provided an
2696+
* Verify that {@link Optimizely#getVariation(String, String)} return a variation when provided an
26992697
* empty string.
27002698
*/
27012699
@Test
@@ -2707,8 +2705,7 @@ public void getVariationWithEmptyUserId() throws Exception {
27072705
.withErrorHandler(new RaiseExceptionErrorHandler())
27082706
.build();
27092707

2710-
logbackVerifier.expectMessage(Level.ERROR, "Non-empty user ID required");
2711-
assertNull(optimizely.getVariation(experiment.getKey(), ""));
2708+
assertNotNull(optimizely.getVariation(experiment.getKey(), ""));
27122709
}
27132710

27142711
/**
@@ -4049,8 +4046,7 @@ public void getEnabledFeatureWithEmptyUserId() throws ConfigParseException{
40494046
.build());
40504047
ArrayList<String> featureFlags = (ArrayList<String>) spyOptimizely.getEnabledFeatures("",
40514048
new HashMap<String, String>());
4052-
logbackVerifier.expectMessage(Level.ERROR, "Non-empty user ID required");
4053-
assertTrue(featureFlags.isEmpty());
4049+
assertFalse(featureFlags.isEmpty());
40544050

40554051
}
40564052

core-api/src/test/java/com/optimizely/ab/config/ProjectConfigTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@
3131

3232
import static java.util.Arrays.asList;
3333
import static org.hamcrest.CoreMatchers.is;
34-
import static org.junit.Assert.assertFalse;
35-
import static org.junit.Assert.assertNull;
36-
import static org.junit.Assert.assertThat;
37-
import static org.junit.Assert.assertTrue;
38-
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.*;
3935

4036

4137
import com.optimizely.ab.internal.LogbackVerifier;
@@ -244,12 +240,12 @@ public void getForcedVariationNullUserId() {
244240

245241
@Test
246242
public void setForcedVariationEmptyUserId() {
247-
assertFalse(projectConfig.setForcedVariation("etag1", "", "vtag1"));
243+
assertTrue(projectConfig.setForcedVariation("etag1", "", "vtag1"));
248244
}
249245

250246
@Test
251247
public void getForcedVariationEmptyUserId() {
252-
assertNull(projectConfig.getForcedVariation("etag1", ""));
248+
assertNotNull(projectConfig.getForcedVariation("etag1", ""));
253249
}
254250

255251
/* Invalid Experiement */

0 commit comments

Comments
 (0)