Skip to content

Commit 847dbb6

Browse files
committed
fix wrong log message for missing attr with number-exact
1 parent 2c223ac commit 847dbb6

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
class ExactMatch implements Match {
2929
@Nullable
3030
public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
31+
if (attributeValue == null) return null;
32+
3133
if (isValidNumber(attributeValue)) {
3234
if (isValidNumber(conditionValue)) {
3335
return NumberComparator.compareUnsafe(attributeValue, conditionValue) == 0;
@@ -39,7 +41,7 @@ public Boolean eval(Object conditionValue, Object attributeValue) throws Unexpec
3941
throw new UnexpectedValueTypeException();
4042
}
4143

42-
if (attributeValue == null || attributeValue.getClass() != conditionValue.getClass()) {
44+
if (attributeValue.getClass() != conditionValue.getClass()) {
4345
return null;
4446
}
4547

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.optimizely.ab.config.audience;
1818

1919
import ch.qos.logback.classic.Level;
20+
import com.fasterxml.jackson.databind.deser.std.MapEntryDeserializer;
2021
import com.optimizely.ab.internal.LogbackVerifier;
2122
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2223
import org.junit.Before;
@@ -191,24 +192,35 @@ public void unexpectedAttributeTypeNull() throws Exception {
191192
"Audience condition \"{name='browser_type', type='custom_attribute', match='gt', value=20}\" evaluated to UNKNOWN because a null value was passed for user attribute \"browser_type\"");
192193
}
193194

194-
195195
/**
196-
* Verify that UserAttribute.evaluate returns null on missing attribute value.
196+
* Verify that UserAttribute.evaluate returns null (and log debug message) on missing attribute value.
197197
*/
198198
@Test
199-
public void missingAttribute() throws Exception {
200-
UserAttribute testInstance = new UserAttribute("browser_type", "custom_attribute", "gt", 20);
201-
assertNull(testInstance.evaluate(null, Collections.EMPTY_MAP));
202-
logbackVerifier.expectMessage(Level.DEBUG,
203-
"Audience condition \"{name='browser_type', type='custom_attribute', match='gt', value=20}\" evaluated to UNKNOWN because no value was passed for user attribute \"browser_type\"");
204-
}
205-
206-
@Test
207-
public void missingAttribute_semVersion() throws Exception {
208-
UserAttribute testInstance = new UserAttribute("app_version", "custom_attribute", "semver_eq", "1.0.2");
209-
assertNull(testInstance.evaluate(null, Collections.EMPTY_MAP));
210-
logbackVerifier.expectMessage(Level.DEBUG,
211-
"Audience condition \"{name='app_version', type='custom_attribute', match='semver_eq', value='1.0.2'}\" evaluated to UNKNOWN because no value was passed for user attribute \"app_version\"");
199+
public void missingAttribute_returnsNullAndLogDebugMessage() throws Exception {
200+
// check with all valid value types for each match
201+
202+
Map<String, Object[]> items = new HashMap<>();
203+
items.put("exact", new Object[]{"string", 123, true});
204+
items.put("substring", new Object[]{"string"});
205+
items.put("gt", new Object[]{123, 5.3});
206+
items.put("ge", new Object[]{123, 5.3});
207+
items.put("lt", new Object[]{123, 5.3});
208+
items.put("le", new Object[]{123, 5.3});
209+
items.put("semver_eq", new Object[]{"1.2.3"});
210+
items.put("semver_ge", new Object[]{"1.2.3"});
211+
items.put("semver_gt", new Object[]{"1.2.3"});
212+
items.put("semver_le", new Object[]{"1.2.3"});
213+
items.put("semver_lt", new Object[]{"1.2.3"});
214+
215+
for (Map.Entry<String, Object[]> entry : items.entrySet()) {
216+
for (Object value : entry.getValue()) {
217+
UserAttribute testInstance = new UserAttribute("n", "custom_attribute", entry.getKey(), value);
218+
assertNull(testInstance.evaluate(null, Collections.EMPTY_MAP));
219+
String valueStr = (value instanceof String) ? ("'" + value + "'") : value.toString();
220+
logbackVerifier.expectMessage(Level.DEBUG,
221+
"Audience condition \"{name='n', type='custom_attribute', match='" + entry.getKey() + "', value=" + valueStr + "}\" evaluated to UNKNOWN because no value was passed for user attribute \"n\"");
222+
}
223+
}
212224
}
213225

214226
/**

0 commit comments

Comments
 (0)