Skip to content

Commit

Permalink
Merge pull request Netflix#204 from Netflix/ISSUE-203
Browse files Browse the repository at this point in the history
Issue 203
  • Loading branch information
metacret committed Feb 21, 2015
2 parents 31c2ec9 + 28c43bf commit d7cdf0c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public PathExistsMessageFilter(String path) {
@Override
public boolean apply(Object input) {
JXPathContext jxpath = JXPathContext.newContext(input);
// We should allow non-existing path, and let predicate handle it.
// We should allow non-existing path, and let predicate handle it.
jxpath.setLenient(true);

Pointer pointer = jxpath.getPointer(xpath);

return pointer != null && !(pointer instanceof NullPointer);
return pointer != null && !(pointer instanceof NullPointer) && pointer.getValue() != null;
}

public String getXpath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@
*/
package com.netflix.suro.routing;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.netflix.suro.message.MessageContainer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.netflix.suro.message.DefaultMessageContainer;
import com.netflix.suro.message.Message;
import org.junit.Test;

import java.util.List;
import java.util.Map;

import static com.netflix.suro.routing.FilterTestUtil.makeMessageContainer;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

/**
*
*/
public class XpathFilterTest {
private ObjectMapper jsonMapper = new ObjectMapper();

@Test
public void testXPathFilterWorksWithMap() throws Exception {
String path = "//foo/bar/value";
Expand All @@ -40,4 +39,26 @@ public void testXPathFilterWorksWithMap() throws Exception {

assertTrue(filter.doFilter(makeMessageContainer("key", path, value)));
}

@Test
public void testExistFilter() throws Exception {
XPathFilter filter = new XPathFilter("xpath(\"data.fit.sessionId\") exists", new JsonMapConverter());
assertTrue(filter.doFilter(new DefaultMessageContainer(new Message(
"routingKey",
jsonMapper.writeValueAsBytes(
new ImmutableMap.Builder<String, Object>()
.put("data.fit.sessionId", "abc")
.put("f1", "v1")
.build())),
jsonMapper)));

assertFalse(filter.doFilter(new DefaultMessageContainer(new Message(
"routingKey",
jsonMapper.writeValueAsBytes(
new ImmutableMap.Builder<String, Object>()
.put("data.fit.sessionIdABC", "abc")
.put("f1", "v1")
.build())),
jsonMapper)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.netflix.suro.routing.filter;

import com.google.common.collect.ImmutableMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ExistsMessageFilterTest {
@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testValueComparison() throws Exception {
Object[][] inputs = {
{new ImmutableMap.Builder<String, Object>().put("abc", "v1").put("f1", "v2").build(), "abc", true},
{new ImmutableMap.Builder<String, Object>().put("def", "v1").put("f1", "v2").build(), "abc", false}
};

for(Object[] input : inputs){
String value = input[0].toString();
String field = (String)input[1];
boolean expected = ((Boolean)input[2]).booleanValue();

PathExistsMessageFilter pred = new PathExistsMessageFilter(field);

assertEquals(String.format("Given value = %s, and field = %s", value, field), expected, pred.apply(input));
}

}
}

0 comments on commit d7cdf0c

Please sign in to comment.