Search in sources :

Example 26 with Extractor

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

the class ExtractorTest method testConverters.

@Test
public void testConverters() 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("1", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    extractor.runExtractor(msg);
    assertThat(msg.getField("target")).isEqualTo("converted");
}
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 27 with Extractor

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

the class ExtractorFilterTest method buildExceptionalExtractor.

private Extractor buildExceptionalExtractor() {
    final Extractor extractor = mock(Extractor.class);
    lenient().when(extractor.getOrder()).thenReturn(1L);
    lenient().doThrow(new RuntimeException("EIEIO!")).when(extractor).runExtractor(any());
    return extractor;
}
Also used : Extractor(org.graylog2.plugin.inputs.Extractor)

Example 28 with Extractor

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

the class ExtractorFilterTest method testFailureHandling.

@Test
void testFailureHandling() throws NotFoundException {
    final Input input = mock(Input.class);
    when(input.getId()).thenReturn("123");
    when(inputService.all()).thenReturn(ImmutableList.of(input));
    final Extractor extractor = buildExceptionalExtractor();
    when(extractor.getTitle()).thenReturn("failing extractor");
    when(extractor.getId()).thenReturn("888");
    when(inputService.getExtractors(any())).thenReturn(ImmutableList.of(extractor));
    // extractors are initialized within constructor
    dut = new ExtractorFilter(inputService, eventBus, executorService);
    final Message message = new Message("message", "source", new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC));
    message.setSourceInputId("123");
    dut.filter(message);
    assertThat(message.processingErrors()).hasSize(1);
    assertThat(message.processingErrors().get(0)).satisfies(pe -> {
        assertThat(pe.getCause()).isEqualTo(ProcessingFailureCause.ExtractorException);
        assertThat(pe.getMessage()).isEqualTo("Could not apply extractor <failing extractor(888)>");
        assertThat(pe.getDetails()).isEqualTo("EIEIO!");
    });
}
Also used : Input(org.graylog2.inputs.Input) Message(org.graylog2.plugin.Message) Extractor(org.graylog2.plugin.inputs.Extractor) DateTime(org.joda.time.DateTime) Test(org.junit.jupiter.api.Test)

Example 29 with Extractor

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

the class RegexReplaceExtractorTest method testReplacementWithOnePlaceholder.

@Test
public void testReplacementWithOnePlaceholder() throws Exception {
    final Message message = new Message("Test Foobar", "source", Tools.nowUTC());
    final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "Test (\\w+)"), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
    extractor.runExtractor(message);
    assertThat(message.getMessage()).isEqualTo("Foobar");
}
Also used : Message(org.graylog2.plugin.Message) Test(org.junit.Test)

Example 30 with Extractor

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

the class RegexReplaceExtractorTest method testReplacementWithNoMatchAndDefaultReplacement.

@Test
public void testReplacementWithNoMatchAndDefaultReplacement() throws Exception {
    final Message message = new Message("Test", "source", Tools.nowUTC());
    final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "NO-MATCH"), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
    extractor.runExtractor(message);
    assertThat(message.getMessage()).isEqualTo("Test");
}
Also used : Message(org.graylog2.plugin.Message) 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