Skip to content

Commit

Permalink
Check for Null Parameters in the Feature APIs (#161)
Browse files Browse the repository at this point in the history
* check for null parameters in feature APIs
* add null parameter tests for isFeatureEnabled
* add null parameter tests for getFeatureVariableBoolean
* add null parameter tests for getFeatureVariableDouble
* add null parameter tests for getFeatureVariableInteger
* add null parameter tests for getFeatureVariableString
  • Loading branch information
wangjoshuah authored Jan 5, 2018
1 parent 429ad63 commit 7a71f03
Show file tree
Hide file tree
Showing 2 changed files with 512 additions and 15 deletions.
32 changes: 25 additions & 7 deletions core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2017, Optimizely, Inc. and contributors *
* Copyright 2016-2018, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -43,21 +43,19 @@
import com.optimizely.ab.notification.NotificationBroadcaster;
import com.optimizely.ab.notification.NotificationCenter;
import com.optimizely.ab.notification.NotificationListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

/**
* Top-level container class for Optimizely functionality.
* Thread-safe, so can be created as a singleton and safely passed around.
Expand Down Expand Up @@ -333,6 +331,14 @@ public void track(@Nonnull String eventName,
public @Nonnull Boolean isFeatureEnabled(@Nonnull String featureKey,
@Nonnull String userId,
@Nonnull Map<String, String> attributes) {
if (featureKey == null) {
logger.warn("The featureKey parameter must be nonnull.");
return false;
}
else if (userId == null) {
logger.warn("The userId parameter must be nonnull.");
return false;
}
FeatureFlag featureFlag = projectConfig.getFeatureKeyMapping().get(featureKey);
if (featureFlag == null) {
logger.info("No feature flag was found for key \"{}\".", featureKey);
Expand Down Expand Up @@ -533,6 +539,18 @@ String getFeatureVariableValueForType(@Nonnull String featureKey,
@Nonnull String userId,
@Nonnull Map<String, String> attributes,
@Nonnull LiveVariable.VariableType variableType) {
if (featureKey == null) {
logger.warn("The featureKey parameter must be nonnull.");
return null;
}
else if (variableKey == null) {
logger.warn("The variableKey parameter must be nonnull.");
return null;
}
else if (userId == null) {
logger.warn("The userId parameter must be nonnull.");
return null;
}
FeatureFlag featureFlag = projectConfig.getFeatureKeyMapping().get(featureKey);
if (featureFlag == null) {
logger.info("No feature flag was found for key \"{}\".", featureKey);
Expand Down
Loading

0 comments on commit 7a71f03

Please sign in to comment.