Search in sources :

Example 6 with EventWithContext

use of org.graylog.events.event.EventWithContext in project graylog2-server by Graylog2.

the class TemplateFieldValueProviderTest method templateBooleanFormatting.

@Test
public void templateBooleanFormatting() {
    final TestEvent event = new TestEvent();
    final EventWithContext eventWithContext = EventWithContext.create(event, newMessage(ImmutableMap.of("success", true)));
    final FieldValue fieldValue = newTemplate("success: ${source.success}").doGet("test", eventWithContext);
    assertThat(fieldValue.value()).isEqualTo("success: true");
}
Also used : TestEvent(org.graylog.events.event.TestEvent) EventWithContext(org.graylog.events.event.EventWithContext) FieldValue(org.graylog.events.fields.FieldValue) Test(org.junit.Test)

Example 7 with EventWithContext

use of org.graylog.events.event.EventWithContext in project graylog2-server by Graylog2.

the class TemplateFieldValueProviderTest method templateCalculation.

@Test
@Ignore("template engine doesn't support expressions")
public void templateCalculation() {
    final TestEvent event = new TestEvent();
    final EventWithContext eventWithContext = EventWithContext.create(event, newMessage(ImmutableMap.of("bytes", 1024)));
    final FieldValue fieldValue = newTemplate("${source.bytes / 1024}").doGet("test", eventWithContext);
    assertThat(fieldValue.value()).isEqualTo("1");
}
Also used : TestEvent(org.graylog.events.event.TestEvent) EventWithContext(org.graylog.events.event.EventWithContext) FieldValue(org.graylog.events.fields.FieldValue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with EventWithContext

use of org.graylog.events.event.EventWithContext in project graylog2-server by Graylog2.

the class LookupTableFieldValueProviderTest method testWithMissingLookupTable.

@Test
public void testWithMissingLookupTable() {
    final TestEvent event = new TestEvent();
    final EventWithContext eventWithContext = EventWithContext.create(event, newMessage(ImmutableMap.of("hello", "world")));
    final LookupTableFieldValueProvider.Config config = newConfig("test-doesntexist", "hello");
    setupMocks("test");
    when(lookupTableFunction.lookup("world")).thenReturn(LookupResult.single("lookup-world"));
    assertThatThrownBy(() -> newProvider(config).doGet("test", eventWithContext)).hasMessageContaining("test-doesntexist").isInstanceOf(IllegalArgumentException.class);
}
Also used : TestEvent(org.graylog.events.event.TestEvent) EventWithContext(org.graylog.events.event.EventWithContext) Test(org.junit.Test)

Example 9 with EventWithContext

use of org.graylog.events.event.EventWithContext in project graylog2-server by Graylog2.

the class LookupTableFieldValueProviderTest method testWithEventContext.

@Test
public void testWithEventContext() {
    final String fieldValueString = "event";
    final String expectedLookupValue = "lookup-event";
    final TestEvent event = new TestEvent();
    final TestEvent eventContext = new TestEvent();
    eventContext.setField("hello", FieldValue.string(fieldValueString));
    final EventWithContext eventWithContext = EventWithContext.create(event, eventContext);
    final LookupTableFieldValueProvider.Config config = newConfig("test", "hello");
    setupMocks("test");
    when(lookupTableFunction.lookup(fieldValueString)).thenReturn(LookupResult.single("lookup-" + eventContext.getField("hello").value()));
    final FieldValue fieldValue = newProvider(config).doGet("test", eventWithContext);
    assertThat(fieldValue.value()).isEqualTo(expectedLookupValue);
}
Also used : TestEvent(org.graylog.events.event.TestEvent) EventWithContext(org.graylog.events.event.EventWithContext) FieldValue(org.graylog.events.fields.FieldValue) Test(org.junit.Test)

Example 10 with EventWithContext

use of org.graylog.events.event.EventWithContext in project graylog2-server by Graylog2.

the class TemplateFieldValueProviderTest method templateNumberFormatting.

@Test
public void templateNumberFormatting() {
    final TestEvent event = new TestEvent();
    final EventWithContext eventWithContext = EventWithContext.create(event, newMessage(ImmutableMap.of("count", 10241234, "avg", 1024.42)));
    final FieldValue fieldValue = newTemplate("count: ${source.count} avg: ${source.avg}").doGet("test", eventWithContext);
    assertThat(fieldValue.value()).isEqualTo("count: 10241234 avg: 1024.42");
}
Also used : TestEvent(org.graylog.events.event.TestEvent) EventWithContext(org.graylog.events.event.EventWithContext) FieldValue(org.graylog.events.fields.FieldValue) Test(org.junit.Test)

Aggregations

EventWithContext (org.graylog.events.event.EventWithContext)19 TestEvent (org.graylog.events.event.TestEvent)15 Test (org.junit.Test)15 FieldValue (org.graylog.events.fields.FieldValue)11 Event (org.graylog.events.event.Event)7 Message (org.graylog2.plugin.Message)6 DateTime (org.joda.time.DateTime)5 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)4 AbsoluteRange (org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange)4 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableList (com.google.common.collect.ImmutableList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Objects (java.util.Objects)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Provider (javax.inject.Provider)1