Search in sources :

Example 21 with Extractor

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

the class ExtractorTest method testCursorStrategyCutIfBeginAndEndIndexAreDisabled.

@Test
public void testCursorStrategyCutIfBeginAndEndIndexAreDisabled() 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", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    msg.addField("msg", "the hello");
    extractor.runExtractor(msg);
    // If the begin and end index is -1, 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)

Example 22 with Extractor

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

the class ExtractorTest method testCursorStrategyCutWithAllTextCut.

@Test
public void testCursorStrategyCutWithAllTextCut() 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 hello", 0, 9) };
        }
    }).build();
    final Message msg = createMessage("message");
    msg.addField("msg", "the hello");
    extractor.runExtractor(msg);
    // If all data is cut from the source field, the "fullyCutByExtractor" string gets inserted.
    assertThat(msg.getField("msg")).isEqualTo("fullyCutByExtractor");
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 23 with Extractor

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

the class ExtractorTest method testConvertersThatReturnNullValue.

@Test
public void testConvertersThatReturnNullValue() throws Exception {
    final Converter converter = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return null;
        }
    }).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("message");
    extractor.runExtractor(msg);
    assertThat(msg.getField("target")).isNull();
}
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 24 with Extractor

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

the class ExtractorTest method testConvertersWithMultipleFieldsAndNull.

@Test
public void testConvertersWithMultipleFieldsAndNull() throws Exception {
    final Converter converter = new TestConverter.Builder().multiple(true).callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return null;
        }
    }).build();
    final TestExtractor extractor = new TestExtractor.Builder().converters(Collections.singletonList(converter)).callback(() -> new Result[] { new Result("1", -1, -1) }).build();
    final Message msg = createMessage("the message");
    extractor.runExtractor(msg);
    assertThat(msg.getField("message")).isEqualTo("the message");
    assertThat(extractor.getConverterExceptionCount()).isEqualTo(0L);
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) DateConverter(org.graylog2.inputs.converters.DateConverter) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 25 with Extractor

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

the class ExtractorTest method testConvertersWithNonStringFieldValue.

@Test
public void testConvertersWithNonStringFieldValue() throws Exception {
    final Converter converter = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return "converted";
        }
    }).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(123, "target", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    extractor.runExtractor(msg);
    // Only string values will be converted.
    assertThat(msg.getField("target")).isEqualTo(123);
}
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)

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