Search in sources :

Example 16 with Extractor

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

the class ExtractorTest method testCursorStrategyCutIfTargetFieldEqualsSourceField.

@Test
public void testCursorStrategyCutIfTargetFieldEqualsSourceField() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().cursorStrategy(CUT).sourceField("msg").targetField("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);
    // If source and target fields are the same, the field is not touched because it already got set to a new value.
    assertThat(msg.getField("msg")).isEqualTo("the");
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 17 with Extractor

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

the class ExtractorTest method testWithStringCondition.

@Test
public void testWithStringCondition() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().conditionType(STRING).conditionValue("hello").build();
    // Extractor runs if the message contains the condition value "hello".
    final Message msg1 = createMessage("hello world");
    extractor.runExtractor(msg1);
    assertThat(msg1.hasField("target")).isTrue();
    // Extractor does not run if the message does not contain the condition value.
    final Message msg2 = createMessage("the message");
    extractor.runExtractor(msg2);
    assertThat(msg2.hasField("target")).isFalse();
}
Also used : Message(org.graylog2.plugin.Message) Test(org.junit.Test)

Example 18 with Extractor

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

the class ExtractorTest method testConvertersAreExecutedInOrder.

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

        @Nullable
        @Override
        public Object apply(Object input) {
            return ((String) input) + "1";
        }
    }).build();
    final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return ((String) input) + "2";
        }
    }).build();
    final Converter converter3 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return ((String) input) + "3";
        }
    }).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);
    assertThat(msg.getField("target")).isEqualTo("converter123");
}
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 19 with Extractor

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

the class ExtractorTest method testWithOneTargetValueResult.

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

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("hello", "world", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("the hello");
    extractor.runExtractor(msg);
    assertThat(msg.hasField("target")).isFalse();
    assertThat(msg.getField("world")).isEqualTo("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 20 with Extractor

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

the class ExtractorTest method testCursorStrategyCutIfSourceFieldIsReservedField.

@Test
public void testCursorStrategyCutIfSourceFieldIsReservedField() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().cursorStrategy(CUT).sourceField("message").callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("the", 0, 3) };
        }
    }).build();
    final Message msg = createMessage("the hello");
    extractor.runExtractor(msg);
    // The source value is not modified if it is a reserved field.
    assertThat(msg.getField("message")).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