Skip to content

Commit

Permalink
PathExistsMessageFilter bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
metacret committed Feb 20, 2015
1 parent 31c2ec9 commit 7b2a8d7
Show file tree
Hide file tree
Showing 3 changed files with 73 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,41 @@
package com.netflix.suro.routing.filter.parser;

import com.netflix.suro.routing.filter.PathExistsMessageFilter;
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 = {
{"abc", "abc", true},
{"", "", true},
{"AB", "A", false},
{null, null, true},
{null, "", false},
{"", null, false}
};

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

PathExistsMessageFilter pred = new PathExistsMessageFilter("");

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

}
}

0 comments on commit 7b2a8d7

Please sign in to comment.