Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raju-opti committed Oct 19, 2024
1 parent 3087bad commit fb5744b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.optimizely.ab.bucketing.FeatureDecision;
import com.optimizely.ab.bucketing.UserProfile;
import com.optimizely.ab.bucketing.UserProfileService;
import com.optimizely.ab.bucketing.UserProfileUtils;
import com.optimizely.ab.config.*;
import com.optimizely.ab.config.parser.ConfigParseException;
import com.optimizely.ab.event.EventHandler;
Expand Down Expand Up @@ -369,7 +371,7 @@ public void decideAll_allFlags() {

OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
Map<String, OptimizelyDecision> decisions = user.decideAll();
assertTrue(decisions.size() == 3);
assertEquals(decisions.size(), 3);

assertEquals(
decisions.get(flagKey1),
Expand Down Expand Up @@ -421,6 +423,39 @@ public void decideAll_allFlags() {
assertEquals(sentEvents.get(2).getUserContext().getUserId(), userId);
}

@Test
public void decideForKeys_ups_batching() throws Exception {
UserProfileService ups = mock(UserProfileService.class);

optimizely = new Optimizely.Builder()
.withDatafile(datafile)
.withUserProfileService(ups)
.build();

String flagKey1 = "feature_1";
String flagKey2 = "feature_2";
String flagKey3 = "feature_3";
Map<String, Object> attributes = Collections.singletonMap("gender", "f");

OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
Map<String, OptimizelyDecision> decisions = user.decideForKeys(Arrays.asList(
flagKey1, flagKey2, flagKey3
));

assertEquals(decisions.size(), 3);

ArgumentCaptor<Map> argumentCaptor = ArgumentCaptor.forClass(Map.class);


verify(ups, times(1)).lookup(userId);
verify(ups, times(1)).save(argumentCaptor.capture());

Map<String, Object> savedUps = argumentCaptor.getValue();
UserProfile savedProfile = UserProfileUtils.convertMapToUserProfile(savedUps);

assertEquals(savedProfile.userId, userId);
}

@Test
public void decideAll_allFlags_enabledFlagsOnly() {
String flagKey1 = "feature_1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,30 @@ public void getVariationForFeatureReturnsVariationFromRolloutWhenExperimentFails

//========== getVariationForFeatureList tests ==========//

@Test
public void getVariationsForFeatureListBatchesUpsLoadAndSave() throws Exception {
Bucketer bucketer = new Bucketer();
ErrorHandler mockErrorHandler = mock(ErrorHandler.class);
UserProfileService mockUserProfileService = mock(UserProfileService.class);

DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, mockUserProfileService);

FeatureFlag featureFlag1 = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
FeatureFlag featureFlag2 = FEATURE_FLAG_MULTI_VARIATE_FUTURE_FEATURE;
FeatureFlag featureFlag3 = FEATURE_FLAG_MUTEX_GROUP_FEATURE;

List<DecisionResponse<FeatureDecision>> decisions = decisionService.getVariationsForFeatureList(
Arrays.asList(featureFlag1, featureFlag2, featureFlag3),
optimizely.createUserContext(genericUserId),
v4ProjectConfig,
new ArrayList<>()
);

assertEquals(decisions.size(), 3);
verify(mockUserProfileService, times(1)).lookup(genericUserId);
verify(mockUserProfileService, times(1)).save(anyObject());
}


//========== getVariationForFeatureInRollout tests ==========//

Expand Down

0 comments on commit fb5744b

Please sign in to comment.