Search in sources :

Example 46 with Extractor

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();
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 47 with Extractor

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();
}
Also used : Message(org.graylog2.plugin.Message) Test(org.junit.Test)

Example 48 with Extractor

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");
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) DateConverter(org.graylog2.inputs.converters.DateConverter) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 49 with Extractor

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);
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) DateConverter(org.graylog2.inputs.converters.DateConverter) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 50 with Extractor

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);
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)40 Message (org.graylog2.plugin.Message)39 Result (org.graylog2.plugin.inputs.Extractor.Result)29 Callable (java.util.concurrent.Callable)27 Extractor (org.graylog2.plugin.inputs.Extractor)18 Input (org.graylog2.inputs.Input)10 DateConverter (org.graylog2.inputs.converters.DateConverter)9 MessageInput (org.graylog2.plugin.inputs.MessageInput)9 Function (com.google.common.base.Function)8 NotFoundException (org.graylog2.database.NotFoundException)7 ValidationException (org.graylog2.plugin.database.ValidationException)7 Timed (com.codahale.metrics.annotation.Timed)6 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 Produces (javax.ws.rs.Produces)5 BadRequestException (javax.ws.rs.BadRequestException)4 Path (javax.ws.rs.Path)4 LookupTableExtractor (org.graylog2.inputs.extractors.LookupTableExtractor)4 Converter (org.graylog2.plugin.inputs.Converter)4 ImmutableList (com.google.common.collect.ImmutableList)3