adds tests and guards against null values in some mutate methods
This commit is contained in:
parent
082686d9c5
commit
de33f5a911
|
@ -201,7 +201,9 @@ public final class MutateProcessor implements Processor {
|
||||||
private void doSplit(Data data) {
|
private void doSplit(Data data) {
|
||||||
for(Map.Entry<String, String> entry : split.entrySet()) {
|
for(Map.Entry<String, String> entry : split.entrySet()) {
|
||||||
Object oldVal = data.getProperty(entry.getKey());
|
Object oldVal = data.getProperty(entry.getKey());
|
||||||
if (oldVal instanceof String) {
|
if (oldVal == null) {
|
||||||
|
throw new IllegalArgumentException("Cannot split field. [" + entry.getKey() + "] is null.");
|
||||||
|
} else if (oldVal instanceof String) {
|
||||||
data.addField(entry.getKey(), Arrays.asList(((String) oldVal).split(entry.getValue())));
|
data.addField(entry.getKey(), Arrays.asList(((String) oldVal).split(entry.getValue())));
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Cannot split a field that is not a String type");
|
throw new IllegalArgumentException("Cannot split a field that is not a String type");
|
||||||
|
@ -247,7 +249,9 @@ public final class MutateProcessor implements Processor {
|
||||||
private void doTrim(Data data) {
|
private void doTrim(Data data) {
|
||||||
for(String field : trim) {
|
for(String field : trim) {
|
||||||
Object val = data.getProperty(field);
|
Object val = data.getProperty(field);
|
||||||
if (val instanceof String) {
|
if (val == null) {
|
||||||
|
throw new IllegalArgumentException("Cannot trim field. [" + field + "] is null.");
|
||||||
|
} else if (val instanceof String) {
|
||||||
data.addField(field, ((String) val).trim());
|
data.addField(field, ((String) val).trim());
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Cannot trim field:" + field + " with type: " + val.getClass());
|
throw new IllegalArgumentException("Cannot trim field:" + field + " with type: " + val.getClass());
|
||||||
|
@ -258,7 +262,9 @@ public final class MutateProcessor implements Processor {
|
||||||
private void doUppercase(Data data) {
|
private void doUppercase(Data data) {
|
||||||
for(String field : uppercase) {
|
for(String field : uppercase) {
|
||||||
Object val = data.getProperty(field);
|
Object val = data.getProperty(field);
|
||||||
if (val instanceof String) {
|
if (val == null) {
|
||||||
|
throw new IllegalArgumentException("Cannot uppercase field. [" + field + "] is null.");
|
||||||
|
} else if (val instanceof String) {
|
||||||
data.addField(field, ((String) val).toUpperCase(Locale.ROOT));
|
data.addField(field, ((String) val).toUpperCase(Locale.ROOT));
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Cannot uppercase field:" + field + " with type: " + val.getClass());
|
throw new IllegalArgumentException("Cannot uppercase field:" + field + " with type: " + val.getClass());
|
||||||
|
@ -269,7 +275,9 @@ public final class MutateProcessor implements Processor {
|
||||||
private void doLowercase(Data data) {
|
private void doLowercase(Data data) {
|
||||||
for(String field : lowercase) {
|
for(String field : lowercase) {
|
||||||
Object val = data.getProperty(field);
|
Object val = data.getProperty(field);
|
||||||
if (val instanceof String) {
|
if (val == null) {
|
||||||
|
throw new IllegalArgumentException("Cannot lowercase field. [" + field + "] is null.");
|
||||||
|
} else if (val instanceof String) {
|
||||||
data.addField(field, ((String) val).toLowerCase(Locale.ROOT));
|
data.addField(field, ((String) val).toLowerCase(Locale.ROOT));
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Cannot lowercase field:" + field + " with type: " + val.getClass());
|
throw new IllegalArgumentException("Cannot lowercase field:" + field + " with type: " + val.getClass());
|
||||||
|
|
|
@ -28,6 +28,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
|
||||||
public class DataTests extends ESTestCase {
|
public class DataTests extends ESTestCase {
|
||||||
|
|
||||||
|
@ -51,6 +52,11 @@ public class DataTests extends ESTestCase {
|
||||||
assertThat(data.getProperty("fizz.buzz"), equalTo("hello world"));
|
assertThat(data.getProperty("fizz.buzz"), equalTo("hello world"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testGetPropertyNotFound() {
|
||||||
|
data.getProperty("not.here");
|
||||||
|
assertThat(data.getProperty("not.here"), nullValue());
|
||||||
|
}
|
||||||
|
|
||||||
public void testContainsProperty() {
|
public void testContainsProperty() {
|
||||||
assertTrue(data.containsProperty("fizz"));
|
assertTrue(data.containsProperty("fizz"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,6 +110,18 @@ public class MutateProcessorTests extends ESTestCase {
|
||||||
assertThat(data.getProperty("ip"), equalTo(Arrays.asList("127", "0", "0", "1")));
|
assertThat(data.getProperty("ip"), equalTo(Arrays.asList("127", "0", "0", "1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSplitNullValue() throws IOException {
|
||||||
|
Map<String, String> split = new HashMap<>();
|
||||||
|
split.put("not.found", "\\.");
|
||||||
|
Processor processor = new MutateProcessor(null, null, null, split, null, null, null, null, null, null);
|
||||||
|
try {
|
||||||
|
processor.execute(data);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat(e.getMessage(), equalTo("Cannot split field. [not.found] is null."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void testGsub() throws IOException {
|
public void testGsub() throws IOException {
|
||||||
List<GsubExpression> gsubExpressions = Collections.singletonList(new GsubExpression("ip", Pattern.compile("\\."), "-"));
|
List<GsubExpression> gsubExpressions = Collections.singletonList(new GsubExpression("ip", Pattern.compile("\\."), "-"));
|
||||||
Processor processor = new MutateProcessor(null, null, null, null, gsubExpressions, null, null, null, null, null);
|
Processor processor = new MutateProcessor(null, null, null, null, gsubExpressions, null, null, null, null, null);
|
||||||
|
@ -156,6 +168,17 @@ public class MutateProcessorTests extends ESTestCase {
|
||||||
assertThat(data.getProperty("to_strip"), equalTo("clean"));
|
assertThat(data.getProperty("to_strip"), equalTo("clean"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testTrimNullValue() throws IOException {
|
||||||
|
List<String> trim = Collections.singletonList("not.found");
|
||||||
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, trim, null, null);
|
||||||
|
try {
|
||||||
|
processor.execute(data);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat(e.getMessage(), equalTo("Cannot trim field. [not.found] is null."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void testUppercase() throws IOException {
|
public void testUppercase() throws IOException {
|
||||||
List<String> uppercase = Collections.singletonList("foo");
|
List<String> uppercase = Collections.singletonList("foo");
|
||||||
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, uppercase, null);
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, uppercase, null);
|
||||||
|
@ -164,6 +187,17 @@ public class MutateProcessorTests extends ESTestCase {
|
||||||
assertThat(data.getProperty("foo"), equalTo("BAR"));
|
assertThat(data.getProperty("foo"), equalTo("BAR"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testUppercaseNullValue() throws IOException {
|
||||||
|
List<String> uppercase = Collections.singletonList("not.found");
|
||||||
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, uppercase, null);
|
||||||
|
try {
|
||||||
|
processor.execute(data);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat(e.getMessage(), equalTo("Cannot uppercase field. [not.found] is null."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void testLowercase() throws IOException {
|
public void testLowercase() throws IOException {
|
||||||
List<String> lowercase = Collections.singletonList("alpha");
|
List<String> lowercase = Collections.singletonList("alpha");
|
||||||
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, null, lowercase);
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, null, lowercase);
|
||||||
|
@ -171,4 +205,15 @@ public class MutateProcessorTests extends ESTestCase {
|
||||||
assertThat(data.getDocument().size(), equalTo(7));
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("alpha"), equalTo("abcd"));
|
assertThat(data.getProperty("alpha"), equalTo("abcd"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testLowercaseNullValue() throws IOException {
|
||||||
|
List<String> lowercase = Collections.singletonList("not.found");
|
||||||
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, null, lowercase);
|
||||||
|
try {
|
||||||
|
processor.execute(data);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat(e.getMessage(), equalTo("Cannot lowercase field. [not.found] is null."));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -207,7 +207,7 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
- length: { docs: 2 }
|
- length: { docs: 2 }
|
||||||
- match: { docs.0.error.type: "null_pointer_exception" }
|
- match: { docs.0.error.type: "illegal_argument_exception" }
|
||||||
- is_true: docs.1.doc.modified
|
- is_true: docs.1.doc.modified
|
||||||
- match: { docs.1.doc._source.foo: "BAR" }
|
- match: { docs.1.doc._source.foo: "BAR" }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue