Search in sources :

Example 1 with IpAddress

use of org.graylog.plugins.pipelineprocessor.functions.ips.IpAddress in project graylog2-server by Graylog2.

the class FunctionsSnippetsTest method conversions.

@Test
public void conversions() {
    final Rule rule = parser.parseRule(ruleForTest(), false);
    final EvaluationContext context = contextForRuleEval(rule, new Message("test", "test", Tools.nowUTC()));
    assertThat(context.evaluationErrors()).isEmpty();
    final Message message = context.currentMessage();
    assertNotNull(message);
    assertThat(message.getField("string_1")).isEqualTo("1");
    assertThat(message.getField("string_2")).isEqualTo("2");
    // special case, Message doesn't allow adding fields with empty string values
    assertThat(message.hasField("string_3")).isFalse();
    assertThat(message.getField("string_4")).isEqualTo("default");
    assertThat(message.getField("string_5")).isEqualTo("false");
    assertThat(message.getField("string_6")).isEqualTo("42");
    assertThat(message.getField("string_7")).isEqualTo("23.42");
    assertThat(message.getField("long_1")).isEqualTo(1L);
    assertThat(message.getField("long_2")).isEqualTo(2L);
    assertThat(message.getField("long_3")).isEqualTo(0L);
    assertThat(message.getField("long_4")).isEqualTo(1L);
    assertThat(message.getField("long_5")).isEqualTo(23L);
    assertThat(message.getField("long_6")).isEqualTo(23L);
    assertThat(message.getField("long_7")).isEqualTo(1L);
    assertThat(message.getField("long_min1")).isEqualTo(Long.MIN_VALUE);
    assertThat(message.getField("long_min2")).isEqualTo(1L);
    assertThat(message.getField("long_max1")).isEqualTo(Long.MAX_VALUE);
    assertThat(message.getField("long_max2")).isEqualTo(1L);
    assertThat(message.getField("double_1")).isEqualTo(1d);
    assertThat(message.getField("double_2")).isEqualTo(2d);
    assertThat(message.getField("double_3")).isEqualTo(0d);
    assertThat(message.getField("double_4")).isEqualTo(1d);
    assertThat(message.getField("double_5")).isEqualTo(23d);
    assertThat(message.getField("double_6")).isEqualTo(23d);
    assertThat(message.getField("double_7")).isEqualTo(23.42d);
    assertThat(message.getField("double_min1")).isEqualTo(Double.MIN_VALUE);
    assertThat(message.getField("double_min2")).isEqualTo(0d);
    assertThat(message.getField("double_max1")).isEqualTo(Double.MAX_VALUE);
    assertThat(message.getField("double_inf1")).isEqualTo(Double.POSITIVE_INFINITY);
    assertThat(message.getField("double_inf2")).isEqualTo(Double.NEGATIVE_INFINITY);
    assertThat(message.getField("double_inf3")).isEqualTo(Double.POSITIVE_INFINITY);
    assertThat(message.getField("double_inf4")).isEqualTo(Double.NEGATIVE_INFINITY);
    assertThat(message.getField("bool_1")).isEqualTo(true);
    assertThat(message.getField("bool_2")).isEqualTo(false);
    assertThat(message.getField("bool_3")).isEqualTo(false);
    assertThat(message.getField("bool_4")).isEqualTo(true);
    // the is wrapped in our own class for safety in rules
    assertThat(message.getField("ip_1")).isEqualTo(new IpAddress(InetAddresses.forString("127.0.0.1")));
    assertThat(message.getField("ip_2")).isEqualTo(new IpAddress(InetAddresses.forString("127.0.0.1")));
    assertThat(message.getField("ip_3")).isEqualTo(new IpAddress(InetAddresses.forString("0.0.0.0")));
    assertThat(message.getField("ip_4")).isEqualTo(new IpAddress(InetAddresses.forString("::1")));
    assertThat(message.getField("map_1")).isEqualTo(Collections.singletonMap("foo", "bar"));
    assertThat(message.getField("map_2")).isEqualTo(Collections.emptyMap());
    assertThat(message.getField("map_3")).isEqualTo(Collections.emptyMap());
    assertThat(message.getField("map_4")).isEqualTo(Collections.emptyMap());
    assertThat(message.getField("map_5")).isEqualTo(Collections.emptyMap());
    assertThat(message.getField("map_6")).isEqualTo(Collections.emptyMap());
}
Also used : CreateMessage(org.graylog.plugins.pipelineprocessor.functions.messages.CreateMessage) CloneMessage(org.graylog.plugins.pipelineprocessor.functions.messages.CloneMessage) DropMessage(org.graylog.plugins.pipelineprocessor.functions.messages.DropMessage) Message(org.graylog2.plugin.Message) IpAddress(org.graylog.plugins.pipelineprocessor.functions.ips.IpAddress) MockitoRule(org.mockito.junit.MockitoRule) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) EvaluationContext(org.graylog.plugins.pipelineprocessor.EvaluationContext) BaseParserTest(org.graylog.plugins.pipelineprocessor.BaseParserTest) Test(org.junit.Test)

Aggregations

BaseParserTest (org.graylog.plugins.pipelineprocessor.BaseParserTest)1 EvaluationContext (org.graylog.plugins.pipelineprocessor.EvaluationContext)1 Rule (org.graylog.plugins.pipelineprocessor.ast.Rule)1 IpAddress (org.graylog.plugins.pipelineprocessor.functions.ips.IpAddress)1 CloneMessage (org.graylog.plugins.pipelineprocessor.functions.messages.CloneMessage)1 CreateMessage (org.graylog.plugins.pipelineprocessor.functions.messages.CreateMessage)1 DropMessage (org.graylog.plugins.pipelineprocessor.functions.messages.DropMessage)1 Message (org.graylog2.plugin.Message)1 Test (org.junit.Test)1 MockitoRule (org.mockito.junit.MockitoRule)1