use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testCursorStrategyCutWithMultipleResults.
@Test
public void testCursorStrategyCutWithMultipleResults() throws Exception {
final TestExtractor extractor = new TestExtractor.Builder().cursorStrategy(CUT).sourceField("msg").callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result("the", "one", 0, 3), new Result("hello", "two", 10, 15) };
}
}).build();
final Message msg = createMessage("message");
msg.addField("msg", "the great hello");
extractor.runExtractor(msg);
// With the cut strategy the matched data will be removed from the message.
assertThat(msg.getField("msg")).isEqualTo("great");
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testWithRegexpCondition.
@Test
public void testWithRegexpCondition() throws Exception {
final TestExtractor extractor = new TestExtractor.Builder().conditionType(REGEX).conditionValue("^hello").build();
// Extractor runs if the message matches the condition regexp.
final Message msg1 = createMessage("hello world");
extractor.runExtractor(msg1);
assertThat(msg1.hasField("target")).isTrue();
// Extractor does not run if the message does not match the condition regexp.
final Message msg2 = createMessage("the hello");
extractor.runExtractor(msg2);
assertThat(msg2.hasField("target")).isFalse();
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testMultipleConvertersWithFirstReturningNullValue.
@Test
public void testMultipleConvertersWithFirstReturningNullValue() throws Exception {
final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return null;
}
}).build();
final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return input + "2";
}
}).build();
final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2)).callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result("converter", -1, -1) };
}
}).build();
final Message msg = createMessage("message");
extractor.runExtractor(msg);
// If the first converter returns null, the second will not be executed because the value is not a string anymore.
assertThat(msg.getField("target")).isNull();
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testCursorStrategyCopyWithMultipleResults.
@Test
public void testCursorStrategyCopyWithMultipleResults() throws Exception {
final TestExtractor extractor = new TestExtractor.Builder().cursorStrategy(COPY).sourceField("msg").callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result("the", "one", 0, 3), new Result("hello", "two", 10, 15) };
}
}).build();
final Message msg = createMessage("message");
msg.addField("msg", "the great hello");
extractor.runExtractor(msg);
// With the copy strategy, the source field will not be modified.
assertThat(msg.getField("msg")).isEqualTo("the great hello");
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testCursorStrategyCutIfEndIndexIsDisabled.
@Test(expected = StringIndexOutOfBoundsException.class)
public void testCursorStrategyCutIfEndIndexIsDisabled() throws Exception {
final TestExtractor extractor = new TestExtractor.Builder().cursorStrategy(CUT).sourceField("msg").callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result("the", 0, -1) };
}
}).build();
final Message msg = createMessage("message");
msg.addField("msg", "the hello");
extractor.runExtractor(msg);
// If the end index is -1, the source field should not be modified.
// TODO: The current implementation only checks if begin index is -1. Needs to be fixed.
assertThat(msg.getField("msg")).isEqualTo("the hello");
}
Aggregations