Search in sources :

Example 11 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorTest method testWithOneValueOnlyResult.

@Test
public void testWithOneValueOnlyResult() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("1", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("the hello");
    extractor.runExtractor(msg);
    assertThat(msg.getField("target")).isEqualTo("1");
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 12 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorTest method testWithOneValueOnlyResultsAndValueIsNull.

@Test
public void testWithOneValueOnlyResultsAndValueIsNull() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result(null, -1, -1) };
        }
    }).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 13 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorTest method testExtractorsWithExceptions.

@Test
public void testExtractorsWithExceptions() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            throw new ExtractorException(new IOException("BARF"));
        }
    }).build();
    final Message msg = createMessage("message");
    extractor.runExtractor(msg);
    assertThat(msg.processingErrors()).hasSize(1);
    assertThat(msg.processingErrors().get(0)).satisfies(pe -> {
        assertThat(pe.getCause()).isEqualTo(ProcessingFailureCause.ExtractorException);
        assertThat(pe.getMessage()).isEqualTo("Could not apply extractor <test-title (test-id)>");
        assertThat(pe.getDetails()).isEqualTo("BARF.");
    });
}
Also used : Message(org.graylog2.plugin.Message) ExtractorException(org.graylog2.inputs.extractors.ExtractorException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 14 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorTest method testConvertersWithTimestamp.

@Test
public // The converter however, will parse that string and everything is fine.
void testConvertersWithTimestamp() throws Exception {
    final Converter converter = new DateConverter(ImmutableMap.of("date_format", "yyyy-MM-dd HH:mm:ss,SSS"));
    final TestExtractor extractor = new TestExtractor.Builder().targetField("timestamp").converters(Collections.singletonList(converter)).callback(() -> new Result[] { new Result("2021-10-20 09:05:39,892", -1, -1) }).build();
    final Message msg = createMessage("the message");
    extractor.runExtractor(msg);
    assertThat(msg.getTimestamp()).isEqualTo(new DateTime(2021, 10, 20, 9, 5, 39, 892, UTC));
}
Also used : DateConverter(org.graylog2.inputs.converters.DateConverter) Message(org.graylog2.plugin.Message) DateConverter(org.graylog2.inputs.converters.DateConverter) DateTime(org.joda.time.DateTime) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 15 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorTest method testCursorStrategyCopy.

@Test
public void testCursorStrategyCopy() 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", 0, 3) };
        }
    }).build();
    final Message msg = createMessage("message");
    msg.addField("msg", "the hello");
    extractor.runExtractor(msg);
    // With the copy strategy, the source field should not be modified.
    assertThat(msg.getField("msg")).isEqualTo("the hello");
}
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