Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check & unit tests in case the feature value is a collection impl #46

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/FeatureEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ public <ValueType> FeatureResult<ValueType> evaluateFeature(
if (featureUsageCallback != null) {
featureUsageCallback.onFeatureUsage(key, forcedRuleFeatureValue);
}

goOutFromCircularLoop();
return forcedRuleFeatureValue;
} else {

Expand Down Expand Up @@ -419,4 +421,9 @@ public <ValueType> FeatureResult<ValueType> evaluateFeature(
return null;
}
}

private void goOutFromCircularLoop() {
featureEvalContext.setId(null);
featureEvalContext.getEvaluatedFeatures().clear();
}
}
6 changes: 6 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/FeatureResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.Builder;
import lombok.Data;
import javax.annotation.Nullable;
import java.lang.reflect.Type;
import java.util.Collection;

/**
* Results for a {@link FeatureEvaluator#evaluateFeature(String, GBContext, Class, JsonObject)}
Expand Down Expand Up @@ -82,6 +84,10 @@ public Boolean isOn() {
return (Double) value != 0;
}

if (value instanceof Collection<?>) {
return !((Collection<?>) value).isEmpty();
}

return false;
}

Expand Down
49 changes: 48 additions & 1 deletion lib/src/test/java/growthbook/sdk/java/FeatureResultTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package growthbook.sdk.java;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

class FeatureResultTest {
@Test
void canBeConstructed() {
Expand Down Expand Up @@ -169,5 +174,47 @@ void featureResult_isOn_withEmptyValue_returnsFalse_forStrings() {
assertTrue(subject.isOff());
}

@Test
void featureResult_isOn_withPerceivedNotEmptyCollection() {
FeatureResult<Object> subject = FeatureResult
.builder()
.value(Collections.singleton("acme"))
.build();

assertTrue(subject.isOn());
assertFalse(subject.isOff());
}

@Test
void featureResult_isOff_withPerceivedEmptyCollection() {
FeatureResult<Object> subject = FeatureResult
.builder()
.value(Collections.emptyList())
.build();

assertFalse(subject.isOn());
assertTrue(subject.isOff());
}

@Test
void proofOfConceptCollectionTest() {
GBContext ctx = GBContext.builder()
.featuresJson(
"{\"test\":{\"defaultValue\":[],\"rules\":[{\"force\":[\"line1\",\"line2\"]}]}}")
.build();

GrowthBook growthBook = new GrowthBook(ctx);

String featureName = "test";

assertTrue(growthBook.isOn(featureName));
assertFalse(growthBook.isOff(featureName));

Object value = growthBook.getFeatureValue(featureName, new ArrayList<>());

assertInstanceOf(Collection.class, value);
assertFalse(((Collection<?>) value).isEmpty());
}

// endregion isOn() isOff()
}