Search in sources :

Example 11 with StreamRouterEngine

use of org.graylog2.streams.StreamRouterEngine in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testInvertedRulesMatch.

@Test
public void testInvertedRulesMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule1 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield1", "value", "1", "type", StreamRuleType.PRESENCE.toInteger(), "stream_id", stream.getId()));
    final StreamRuleMock rule2 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield2", "inverted", true, "type", StreamRuleType.PRESENCE.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule1, rule2));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    // Without testfield1 and testfield2 in the message.
    final Message message1 = getMessage();
    assertTrue(engine.match(message1).isEmpty());
    // With testfield1 and testfield2 in the message.
    final Message message2 = getMessage();
    message2.addField("testfield1", "testvalue");
    message2.addField("testfield2", "testvalue");
    assertTrue(engine.match(message2).isEmpty());
    // With testfield1 and not testfield2 in the message.
    final Message message3 = getMessage();
    message3.addField("testfield1", "testvalue");
    assertEquals(Lists.newArrayList(stream), engine.match(message3));
    // With testfield2 in the message.
    final Message message4 = getMessage();
    message4.addField("testfield2", "testvalue");
    assertTrue(engine.match(message4).isEmpty());
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 12 with StreamRouterEngine

use of org.graylog2.streams.StreamRouterEngine in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testMultipleStreamsMatch.

@Test
public void testMultipleStreamsMatch() throws Exception {
    final StreamMock stream1 = getStreamMock("test1");
    final StreamMock stream2 = getStreamMock("test2");
    final StreamRuleMock rule1 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield1", "type", StreamRuleType.PRESENCE.toInteger(), "stream_id", stream1.getId()));
    final StreamRuleMock rule2 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield2", "value", "^test", "type", StreamRuleType.REGEX.toInteger(), "stream_id", stream1.getId()));
    final StreamRuleMock rule3 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield3", "value", "testvalue3", "type", StreamRuleType.EXACT.toInteger(), "stream_id", stream2.getId()));
    stream1.setStreamRules(Lists.newArrayList(rule1, rule2));
    stream2.setStreamRules(Lists.newArrayList(rule3));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream1, stream2));
    // Without testfield1 and testfield2 in the message.
    final Message message1 = getMessage();
    assertTrue(engine.match(message1).isEmpty());
    // With testfield1 and matching testfield2 in the message.
    final Message message2 = getMessage();
    message2.addField("testfield1", "testvalue");
    message2.addField("testfield2", "testvalue2");
    assertEquals(Lists.newArrayList(stream1), engine.match(message2));
    // With testfield1, matching testfield2 and matching testfield3 in the message.
    final Message message3 = getMessage();
    message3.addField("testfield1", "testvalue");
    message3.addField("testfield2", "testvalue2");
    message3.addField("testfield3", "testvalue3");
    final List<Stream> match = engine.match(message3);
    assertTrue(match.contains(stream1));
    assertTrue(match.contains(stream2));
    assertEquals(2, match.size());
    // With matching testfield3 in the message.
    final Message message4 = getMessage();
    message4.addField("testfield3", "testvalue3");
    assertEquals(Lists.newArrayList(stream2), engine.match(message4));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) Stream(org.graylog2.plugin.streams.Stream) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 13 with StreamRouterEngine

use of org.graylog2.streams.StreamRouterEngine in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testExactMatch.

@Test
public void testExactMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "value", "testvalue", "type", StreamRuleType.EXACT.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message = getMessage();
    // With wrong value for field.
    message.addField("testfield", "no-testvalue");
    assertTrue(engine.match(message).isEmpty());
    // With matching value for field.
    message.addField("testfield", "testvalue");
    assertEquals(Lists.newArrayList(stream), engine.match(message));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 14 with StreamRouterEngine

use of org.graylog2.streams.StreamRouterEngine in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testMultipleRulesMatch.

@Test
public void testMultipleRulesMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule1 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield1", "type", StreamRuleType.PRESENCE.toInteger(), "stream_id", stream.getId()));
    final StreamRuleMock rule2 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield2", "value", "^test", "type", StreamRuleType.REGEX.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule1, rule2));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    // Without testfield1 and testfield2 in the message.
    final Message message1 = getMessage();
    assertTrue(engine.match(message1).isEmpty());
    // With testfield1 but no-matching testfield2 in the message.
    final Message message2 = getMessage();
    message2.addField("testfield1", "testvalue");
    message2.addField("testfield2", "no-testvalue");
    assertTrue(engine.match(message2).isEmpty());
    // With testfield1 and matching testfield2 in the message.
    final Message message3 = getMessage();
    message3.addField("testfield1", "testvalue");
    message3.addField("testfield2", "testvalue2");
    assertEquals(Lists.newArrayList(stream), engine.match(message3));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 15 with StreamRouterEngine

use of org.graylog2.streams.StreamRouterEngine in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testGreaterMatch.

@Test
public void testGreaterMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "value", "1", "type", StreamRuleType.GREATER.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message = getMessage();
    // With smaller value.
    message.addField("testfield", "1");
    assertTrue(engine.match(message).isEmpty());
    // With greater value.
    message.addField("testfield", "2");
    assertEquals(Lists.newArrayList(stream), engine.match(message));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Aggregations

Message (org.graylog2.plugin.Message)20 Test (org.junit.Test)20 ObjectId (org.bson.types.ObjectId)15 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)15 Stream (org.graylog2.plugin.streams.Stream)7 StreamRule (org.graylog2.plugin.streams.StreamRule)7 Timed (com.codahale.metrics.annotation.Timed)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)1 StreamRouterEngine (org.graylog2.streams.StreamRouterEngine)1