Skip to content

Commit d7cdf0c

Browse files
committed
Merge pull request #204 from Netflix/ISSUE-203
Issue 203
2 parents 31c2ec9 + 28c43bf commit d7cdf0c

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

suro-core/src/main/java/com/netflix/suro/routing/filter/PathExistsMessageFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public PathExistsMessageFilter(String path) {
1616
@Override
1717
public boolean apply(Object input) {
1818
JXPathContext jxpath = JXPathContext.newContext(input);
19-
// We should allow non-existing path, and let predicate handle it.
19+
// We should allow non-existing path, and let predicate handle it.
2020
jxpath.setLenient(true);
21-
21+
2222
Pointer pointer = jxpath.getPointer(xpath);
2323

24-
return pointer != null && !(pointer instanceof NullPointer);
24+
return pointer != null && !(pointer instanceof NullPointer) && pointer.getValue() != null;
2525
}
2626

2727
public String getXpath() {

suro-core/src/test/java/com/netflix/suro/routing/XpathFilterTest.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@
1515
*/
1616
package com.netflix.suro.routing;
1717

18-
import com.google.common.base.Splitter;
19-
import com.google.common.collect.Lists;
20-
import com.google.common.collect.Maps;
21-
import com.netflix.suro.message.MessageContainer;
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.google.common.collect.ImmutableMap;
20+
import com.netflix.suro.message.DefaultMessageContainer;
21+
import com.netflix.suro.message.Message;
2222
import org.junit.Test;
2323

24-
import java.util.List;
25-
import java.util.Map;
26-
2724
import static com.netflix.suro.routing.FilterTestUtil.makeMessageContainer;
25+
import static org.junit.Assert.assertFalse;
2826
import static org.junit.Assert.assertTrue;
29-
import static org.mockito.Mockito.*;
3027

3128
/**
3229
*
3330
*/
3431
public class XpathFilterTest {
32+
private ObjectMapper jsonMapper = new ObjectMapper();
33+
3534
@Test
3635
public void testXPathFilterWorksWithMap() throws Exception {
3736
String path = "//foo/bar/value";
@@ -40,4 +39,26 @@ public void testXPathFilterWorksWithMap() throws Exception {
4039

4140
assertTrue(filter.doFilter(makeMessageContainer("key", path, value)));
4241
}
42+
43+
@Test
44+
public void testExistFilter() throws Exception {
45+
XPathFilter filter = new XPathFilter("xpath(\"data.fit.sessionId\") exists", new JsonMapConverter());
46+
assertTrue(filter.doFilter(new DefaultMessageContainer(new Message(
47+
"routingKey",
48+
jsonMapper.writeValueAsBytes(
49+
new ImmutableMap.Builder<String, Object>()
50+
.put("data.fit.sessionId", "abc")
51+
.put("f1", "v1")
52+
.build())),
53+
jsonMapper)));
54+
55+
assertFalse(filter.doFilter(new DefaultMessageContainer(new Message(
56+
"routingKey",
57+
jsonMapper.writeValueAsBytes(
58+
new ImmutableMap.Builder<String, Object>()
59+
.put("data.fit.sessionIdABC", "abc")
60+
.put("f1", "v1")
61+
.build())),
62+
jsonMapper)));
63+
}
4364
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.netflix.suro.routing.filter;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class ExistsMessageFilterTest {
11+
@Before
12+
public void setUp() throws Exception {
13+
}
14+
15+
@After
16+
public void tearDown() throws Exception {
17+
}
18+
19+
@Test
20+
public void testValueComparison() throws Exception {
21+
Object[][] inputs = {
22+
{new ImmutableMap.Builder<String, Object>().put("abc", "v1").put("f1", "v2").build(), "abc", true},
23+
{new ImmutableMap.Builder<String, Object>().put("def", "v1").put("f1", "v2").build(), "abc", false}
24+
};
25+
26+
for(Object[] input : inputs){
27+
String value = input[0].toString();
28+
String field = (String)input[1];
29+
boolean expected = ((Boolean)input[2]).booleanValue();
30+
31+
PathExistsMessageFilter pred = new PathExistsMessageFilter(field);
32+
33+
assertEquals(String.format("Given value = %s, and field = %s", value, field), expected, pred.apply(input));
34+
}
35+
36+
}
37+
}

0 commit comments

Comments
 (0)