use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testWithEmptyResultArray.
@Test
public void testWithEmptyResultArray() throws Exception {
final TestExtractor extractor = new TestExtractor.Builder().callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[0];
}
}).build();
final Message msg = createMessage("the hello");
extractor.runExtractor(msg);
assertThat(msg.hasField("target")).isFalse();
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testRunExtractorCheckSourceValueIsString.
@Test
public void testRunExtractorCheckSourceValueIsString() throws Exception {
final TestExtractor extractor = new TestExtractor.Builder().sourceField("a_field").build();
// Extractor should not run for source field values that are not strings!
final Message msg1 = createMessage("the message");
msg1.addField("a_field", 1);
extractor.runExtractor(msg1);
assertThat(msg1.hasField("target")).isFalse();
// The extractor should run for a source field value of type string.
final Message msg2 = createMessage("the message");
msg2.addField("a_field", "the source");
extractor.runExtractor(msg2);
assertThat(msg2.hasField("target")).isTrue();
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testConvertersWithExceptions.
@Test
public void testConvertersWithExceptions() throws Exception {
final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
throw new NullPointerException("EEK");
}
}).build();
final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return input + "2";
}
}).build();
final Converter converter3 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
throw new NullPointerException("ORKS");
}
}).build();
final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2, converter3)).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);
// The two exceptions should have been recorded.
assertThat(extractor.getConverterExceptionCount()).isEqualTo(2);
assertThat(msg.processingErrors()).hasSize(2);
assertThat(msg.processingErrors().get(0)).satisfies(pe -> {
assertThat(pe.getCause()).isEqualTo(ProcessingFailureCause.ExtractorException);
assertThat(pe.getMessage()).isEqualTo("Could not apply converter [NUMERIC] of extractor <test-title (test-id)>");
assertThat(pe.getDetails()).isEqualTo("EEK.");
});
assertThat(msg.processingErrors().get(1)).satisfies(pe -> {
assertThat(pe.getCause()).isEqualTo(ProcessingFailureCause.ExtractorException);
assertThat(pe.getMessage()).isEqualTo("Could not apply converter [NUMERIC] of extractor <test-title (test-id)>");
assertThat(pe.getDetails()).isEqualTo("ORKS.");
});
// It ignores all converters which throw an exception but executes the ones that don't.
// TODO: Is this really the expected behaviour? The converters are executed in order and basically depend on the output of the previous. This might not work for all converters.
assertThat(msg.getField("target")).isEqualTo("converter2");
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testConvertersWithMultipleFields.
@Test
public void testConvertersWithMultipleFields() throws Exception {
final Converter converter = new TestConverter.Builder().multiple(true).callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return ImmutableMap.builder().put("one", 1).put("two", "2").put("message", // Try to overwrite reserved field.
"message should not be overwritten").build();
}
}).build();
final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter)).callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result("1", -1, -1) };
}
}).build();
final Message msg = createMessage("the message");
extractor.runExtractor(msg);
// With a "multiple fields" converter the target field is not touched, only the additional fields are added.
assertThat(msg.getField("target")).isEqualTo("1");
assertThat(msg.getField("one")).isEqualTo(1);
assertThat(msg.getField("two")).isEqualTo("2");
// Reserved fields are not overwritten!
assertThat(msg.getField("message")).isEqualTo("the message");
// Attempts to overwrite a reserved field are recorded as converter exception.
assertThat(extractor.getConverterExceptionCount()).isEqualTo(1);
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorTest method testWithMultipleTargetValueResults.
@Test
public void testWithMultipleTargetValueResults() throws Exception {
final TestExtractor extractor = new TestExtractor.Builder().callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result(1, "one", -1, -1), new Result("2", "two", -1, -1), new Result(3, "three", -1, -1) };
}
}).build();
final Message msg = createMessage("the hello");
extractor.runExtractor(msg);
assertThat(msg.hasField("target")).isFalse();
assertThat(msg.getField("one")).isEqualTo(1);
assertThat(msg.getField("two")).isEqualTo("2");
assertThat(msg.getField("three")).isEqualTo(3);
}
Aggregations