Search in sources :

Example 41 with Extractor

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

the class ExtractorTest method testCursorStrategyCutWithMultipleResults.

@Test
public void testCursorStrategyCutWithMultipleResults() 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", "one", 0, 3), new Result("hello", "two", 10, 15) };
        }
    }).build();
    final Message msg = createMessage("message");
    msg.addField("msg", "the great hello");
    extractor.runExtractor(msg);
    // With the cut strategy the matched data will be removed from the message.
    assertThat(msg.getField("msg")).isEqualTo("great");
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 42 with Extractor

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

the class ExtractorTest method testWithRegexpCondition.

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

Example 43 with Extractor

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

the class ExtractorTest method testMultipleConvertersWithFirstReturningNullValue.

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

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

        @Nullable
        @Override
        public Object apply(Object input) {
            return input + "2";
        }
    }).build();
    final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2)).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);
    // If the first converter returns null, the second will not be executed because the value is not a string anymore.
    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 44 with Extractor

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

the class ExtractorTest method testCursorStrategyCopyWithMultipleResults.

@Test
public void testCursorStrategyCopyWithMultipleResults() 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", "one", 0, 3), new Result("hello", "two", 10, 15) };
        }
    }).build();
    final Message msg = createMessage("message");
    msg.addField("msg", "the great hello");
    extractor.runExtractor(msg);
    // With the copy strategy, the source field will not be modified.
    assertThat(msg.getField("msg")).isEqualTo("the great 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 45 with Extractor

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

the class ExtractorTest method testCursorStrategyCutIfEndIndexIsDisabled.

@Test(expected = StringIndexOutOfBoundsException.class)
public void testCursorStrategyCutIfEndIndexIsDisabled() 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", 0, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    msg.addField("msg", "the hello");
    extractor.runExtractor(msg);
    // If the end index is -1, the source field should not be modified.
    // TODO: The current implementation only checks if begin index is -1. Needs to be fixed.
    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