Skip to content

Commit 8de8ff2

Browse files
authored
fix: Invert the substring match type to match the condition value as a substring of the user attribute value (#224)
1 parent 684e47a commit 8de8ff2

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

core-api/src/main/java/com/optimizely/ab/config/audience/match/SubstringMatch.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ protected SubstringMatch(String value) {
2424
this.value = value;
2525
}
2626

27+
/**
28+
* This matches the same substring matching logic in the Web client.
29+
* @param attributeValue
30+
* @return true/false if the user attribute string value contains the condition string value
31+
*/
2732
public @Nullable
2833
Boolean eval(Object attributeValue) {
2934
try {
30-
return value.contains(convert(attributeValue));
35+
return convert(attributeValue).contains(value);
3136
}
3237
catch (Exception e) {
3338
MatchType.logger.error("Substring match failed ", e);

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,17 @@ public void ltMatchConditionEvaluatesNull() throws Exception {
310310
*/
311311
@Test
312312
public void substringMatchConditionEvaluatesTrue() throws Exception {
313-
UserAttribute testInstanceString = new UserAttribute("browser_type", "custom_attribute","substring", "chrome1");
313+
UserAttribute testInstanceString = new UserAttribute("browser_type", "custom_attribute","substring", "chrome");
314+
assertTrue(testInstanceString.evaluate(testUserAttributes));
315+
}
316+
317+
/**
318+
* Verify that UserAttribute.evaluate for SUBSTRING match type returns true if the
319+
* UserAttribute's value is a substring of the condition's value.
320+
*/
321+
@Test
322+
public void substringMatchConditionPartialMatchEvaluatesTrue() throws Exception {
323+
UserAttribute testInstanceString = new UserAttribute("browser_type", "custom_attribute","substring", "chro");
314324
assertTrue(testInstanceString.evaluate(testUserAttributes));
315325
}
316326

@@ -320,7 +330,7 @@ public void substringMatchConditionEvaluatesTrue() throws Exception {
320330
*/
321331
@Test
322332
public void substringMatchConditionEvaluatesFalse() throws Exception {
323-
UserAttribute testInstanceString = new UserAttribute("browser_type", "custom_attribute","substring", "chr");
333+
UserAttribute testInstanceString = new UserAttribute("browser_type", "custom_attribute","substring", "chr0me");
324334
assertFalse(testInstanceString.evaluate(testUserAttributes));
325335
}
326336

0 commit comments

Comments
 (0)