Search in sources :

Example 1 with StreamRuleMock

use of org.graylog2.streams.matchers.StreamRuleMock 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 2 with StreamRuleMock

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

the class StreamRouterEngineTest method testOrTestMatch.

@Test
public void testOrTestMatch() throws Exception {
    final StreamMock stream = getStreamMock("test", Stream.MatchingType.OR);
    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();
    final StreamRouterEngine.StreamTestMatch testMatch1 = engine.testMatch(message1).get(0);
    final Map<StreamRule, Boolean> matches1 = testMatch1.getMatches();
    assertFalse(testMatch1.isMatched());
    assertFalse(matches1.get(rule1));
    assertFalse(matches1.get(rule2));
    // With testfield1 but no-matching testfield2 in the message.
    final Message message2 = getMessage();
    message2.addField("testfield1", "testvalue");
    message2.addField("testfield2", "no-testvalue");
    final StreamRouterEngine.StreamTestMatch testMatch2 = engine.testMatch(message2).get(0);
    final Map<StreamRule, Boolean> matches2 = testMatch2.getMatches();
    assertTrue(testMatch2.isMatched());
    assertTrue(matches2.get(rule1));
    assertFalse(matches2.get(rule2));
    // With testfield1 and matching testfield2 in the message.
    final Message message3 = getMessage();
    message3.addField("testfield1", "testvalue");
    message3.addField("testfield2", "testvalue2");
    final StreamRouterEngine.StreamTestMatch testMatch3 = engine.testMatch(message3).get(0);
    final Map<StreamRule, Boolean> matches3 = testMatch3.getMatches();
    assertTrue(testMatch3.isMatched());
    assertTrue(matches3.get(rule1));
    assertTrue(matches3.get(rule2));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRule(org.graylog2.plugin.streams.StreamRule) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 3 with StreamRuleMock

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

the class StreamRouterEngineTest method testPresenceMatch.

@Test
public void testPresenceMatch() 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.setStreamRules(Lists.newArrayList(rule));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message = getMessage();
    // Without testfield in the message.
    assertTrue(engine.match(message).isEmpty());
    // With field in the message.
    message.addField("testfield", "testvalue");
    assertEquals(engine.match(message), Lists.newArrayList(stream));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 4 with StreamRuleMock

use of org.graylog2.streams.matchers.StreamRuleMock 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)

Example 5 with StreamRuleMock

use of org.graylog2.streams.matchers.StreamRuleMock 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();
    // 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(engine.match(message), Lists.newArrayList(stream));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 ObjectId (org.bson.types.ObjectId)14 Message (org.graylog2.plugin.Message)14 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)14 StreamRule (org.graylog2.plugin.streams.StreamRule)2 Stream (org.graylog2.plugin.streams.Stream)1 DateTime (org.joda.time.DateTime)1