Search in sources :

Example 6 with StreamRouterEngine

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

the class StreamRouterEngineTest method testSmallerMatch.

@Test
public void testSmallerMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "value", "5", "type", StreamRuleType.SMALLER.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message = getMessage();
    // With bigger value.
    message.addField("testfield", "5");
    assertTrue(engine.match(message).isEmpty());
    // With smaller 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)

Example 7 with StreamRouterEngine

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

the class StreamRouterEngineTest method testOrMatchingShouldNotMatch.

@Test
public void testOrMatchingShouldNotMatch() {
    final String dummyField = "dummyField";
    final String dummyValue = "dummyValue";
    final Stream stream = mock(Stream.class);
    when(stream.getMatchingType()).thenReturn(Stream.MatchingType.OR);
    final StreamRule streamRule1 = getStreamRuleMock("StreamRule1Id", StreamRuleType.EXACT, dummyField, "not" + dummyValue);
    final StreamRule streamRule2 = getStreamRuleMock("StreamRule2Id", StreamRuleType.EXACT, dummyField, "alsoNot" + dummyValue);
    when(stream.getStreamRules()).thenReturn(Lists.newArrayList(streamRule1, streamRule2));
    final Message message = mock(Message.class);
    when(message.getField(eq(dummyField))).thenReturn(dummyValue);
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final List<Stream> result = engine.match(message);
    assertThat(result).isEmpty();
}
Also used : Message(org.graylog2.plugin.Message) StreamRule(org.graylog2.plugin.streams.StreamRule) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 8 with StreamRouterEngine

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

the class StreamRouterEngineTest method testContainsMatch.

@Test
public void testContainsMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "value", "testvalue", "type", StreamRuleType.CONTAINS.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message = getMessage();
    // Without the field
    assertTrue(engine.match(message).isEmpty());
    // With wrong value for field.
    message.addField("testfield", "no-foobar");
    assertTrue(engine.match(message).isEmpty());
    // With matching value for field.
    message.addField("testfield", "hello 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 9 with StreamRouterEngine

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

the class StreamRouterEngineTest method testGetFingerprint.

@Test
public void testGetFingerprint() {
    final StreamMock stream1 = getStreamMock("test");
    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()));
    stream1.setStreamRules(Lists.newArrayList(rule1, rule2));
    final StreamMock stream2 = getStreamMock("test");
    final StreamRuleMock rule3 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "value", "^test", "type", StreamRuleType.REGEX.toInteger(), "stream_id", stream2.getId()));
    stream2.setStreamRules(Lists.newArrayList(rule3));
    final StreamRouterEngine engine1 = newEngine(Lists.newArrayList(stream1));
    final StreamRouterEngine engine2 = newEngine(Lists.newArrayList(stream1));
    final StreamRouterEngine engine3 = newEngine(Lists.newArrayList(stream2));
    assertEquals(engine1.getFingerprint(), engine2.getFingerprint());
    assertNotEquals(engine1.getFingerprint(), engine3.getFingerprint());
}
Also used : ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 10 with StreamRouterEngine

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

the class StreamRouterEngineTest method testRemoveFromAllMessages.

@Test
public void testRemoveFromAllMessages() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "type", StreamRuleType.PRESENCE.toInteger(), "stream_id", stream.getId()));
    stream.setRemoveMatchesFromDefaultStream(true);
    stream.setStreamRules(Collections.singletonList(rule));
    final StreamRouterEngine engine = newEngine(Collections.singletonList(stream));
    final Message message = getMessage();
    message.addStream(defaultStream);
    assertThat(message.getStreams()).containsExactly(defaultStream);
    // Without testfield in the message.
    assertThat(engine.match(message)).isEmpty();
    // With field in the message.
    message.addField("testfield", "testvalue");
    assertThat(engine.match(message)).containsExactly(stream);
    assertThat(message.getStreams()).doesNotContain(defaultStream);
}
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